source: trunk/lib/Functor.h @ 791

Last change on this file since 791 was 727, checked in by Jari Häkkinen, 13 years ago

Changes needed to cleanly compile on g++ 4.3 on SuSE 11 x64.

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