source: trunk/lib/Functor.h

Last change on this file was 1515, checked in by Peter Johansson, 9 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1#ifndef _theplu_svndigest_functor_
2#define _theplu_svndigest_functor_
3
4// $Id: Functor.h 1515 2012-09-26 00:35:10Z peter $
5
6/*
7  Copyright (C) 2005 Peter Johansson
8  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2010, 2011, 2012 Peter Johansson
10
11  This file is part of svndigest, http://dev.thep.lu.se/svndigest
12
13  svndigest is free software; you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation; either version 3 of the License, or
16  (at your option) any later version.
17
18  svndigest is distributed in the hope that it will be useful, but
19  WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
25*/
26
27#include <algorithm>
28#include <functional>
29#include <string>
30#include <utility>
31#include <vector>
32
33#include <sys/stat.h>
34
35namespace theplu{
36namespace svndigest{
37
38  struct AlNum
39  {
40    inline bool operator()(std::string::const_iterator i) const
41    { return isalnum(*i); }
42  };
43
44
45  struct Digit
46  {
47    inline bool operator()(std::string::const_iterator i) const
48    { return isdigit(*i); }
49  };
50
51
52  class notChar
53  {
54  public:
55    notChar(char);
56    inline bool operator()(std::string::const_iterator i) const
57    { return *i!=char_; }
58  private:
59    char char_;
60  };
61
62
63  class not2Char
64  {
65  public:
66    not2Char(char, char);
67    inline bool operator()(std::string::const_iterator i) const
68    { return *i!=char1_ && *i!=char2_; }
69  private:
70    const char char1_;
71    const char char2_;
72  };
73
74  class not2Str
75  {
76  public:
77    not2Str(std::string, std::string);
78    inline bool operator()(std::string::const_iterator i) const
79    {
80      return !(std::equal(str1_.begin(), str1_.end(), i) ||
81              std::equal(str2_.begin(), str2_.end(), i));
82    }
83
84  private:
85    const std::string str1_;
86    const std::string str2_;
87  };
88
89  ///
90  /// Functor to be used on contaioners and works as standard less,
91  /// but on the reversed container.
92  ///
93  /// Requirements on T is that has rend and rbegin. T::value_type
94  /// must be comparable (i.e. have operator<)
95  ///
96  template <typename T>
97  struct LessReversed
98  {
99    ///
100    /// using std::lexicographical_compare on the reversed container
101    ///
102    inline bool operator()(const T& x, const T& y) const
103    { return std::lexicographical_compare(x.rbegin(),x.rend(),
104                                          y.rbegin(),y.rend()); }
105  };
106
107
108  ///
109  /// @brief Functor comparing pairs using second.
110  ///
111  /// STL provides operator< for the pair.first element, but none for
112  /// pair.second. This template provides this and can be used as the
113  /// comparison object in generic functions such as the STL sort.
114  ///
115  template <typename T1, typename T2>
116  struct pair_value_compare
117  {
118    ///
119    /// @return true if x.second<y.second or (x.second==y.second and
120    /// x.first<y.first)
121    ///
122    inline bool operator()(const std::pair<T1,T2>& x,
123                           const std::pair<T1,T2>& y) {
124      return ((x.second<y.second) ||
125              (!(y.second<x.second) && (x.first<y.first)));
126    }
127  };
128
129
130  ///
131  /// Functor working on pair.second, using a user passed functor.
132  ///
133  template <typename T1, typename T2, typename T3>
134  struct PairSecondCompare
135  {
136
137    ///
138    /// @brief Constructor
139    ///
140    explicit PairSecondCompare(const T3& comp)
141      : compare_(comp) {}
142
143    ///
144    /// @return compare(x.second, y.second) where compare is a
145    /// internal comparison functor.
146    ///
147    inline bool operator()(const std::pair<T1,T2>& x,
148                           const std::pair<T1,T2>& y) const
149    { return compare_(x.second,y.second); }
150
151  private:
152    T3 compare_;
153
154  };
155
156  /**
157     Functor perfoming plus assignment on first argument using second argument
158
159     arg1 += arg2.second
160   */
161  template <typename Key, typename T>
162  struct PairValuePlusAssign :
163    public std::binary_function<T&, const std::pair<const Key, T>&, void>
164  {
165    void operator()(T& x, const std::pair<const Key, T>& p) const
166    {
167      x += p.second;
168    }
169  };
170
171
172  /**
173     T1 must be mutable
174   */
175  template<typename T1, typename T2>
176  struct PlusAssign : public std::binary_function<T1, T2, void>
177  {
178    void operator()(T1 t1, T2 t2) const
179    { t1+=t2; }
180  };
181
182}} // end of namespace svndigest end of namespace theplu
183
184#endif
Note: See TracBrowser for help on using the repository browser.