source: trunk/lib/Functor.h @ 519

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

trac moved to new location.

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