KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
l_functors.h
Go to the documentation of this file.
1 /* $Id: l_functors.h 18278 2014-11-25 01:42:10Z ksimek $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Ernesto Brau
18  * =========================================================================== */
19 
20 #ifndef L_FUNCTORS_H_INCLUDED
21 #define L_FUNCTORS_H_INCLUDED
22 
30 #include <algorithm>
31 
32 namespace kjb {
33 
38 template<class T>
39 class Increment
40 {
41 public:
42  typedef T result_type;
43 
44 private:
45  T value;
46 
47 public:
48  Increment(const T& initial_value = T()) : value(initial_value)
49  {}
50 
52  {
53  return value++;
54  }
55 
57  {}
58 };
59 
64 template<class T>
66 {
67 public:
68  typedef T result_type;
69 
70 private:
71  T value;
72  T inc_value;
73 
74 public:
75  Increase_by(const T& initial_value, const T& increase_value) :
76  value(initial_value), inc_value(increase_value)
77  {}
78 
80  {
81  T v = value;
82  value += inc_value;
83  return v;
84  }
85 
87  {}
88 };
89 
93 template<class T>
95 {
96 public:
97  typedef bool result_type;
98 
99 private:
100  int m_index;
101 
102 public:
103  Index_less_than(int index) : m_index(index)
104  {}
105 
106  bool operator()(const T& lhs, const T& rhs) const
107  {
108  return lhs[m_index] < rhs[m_index];
109  }
110 
112  {}
113 };
114 
118 template<class V>
120 {
121 public:
122  typedef double result_type;
123 
124 private:
125  int coord;
126 
127 public:
128  Select_coordinate(int coordinate) : coord(coordinate)
129  {}
130 
131  double operator()(const V& v) const
132  {
133  return v[coord];
134  }
135 
137  {}
138 };
139 
143 template<class T>
144 class Identity
145 {
146 public:
147  typedef T result_type;
148 
149 public:
151 
152  T operator()(const T& t) const
153  {
154  return t;
155  }
156 
158 };
159 
163 template<class T>
165 {
166 public:
167  typedef T argument_type;
168  typedef bool result_type;
169 
170 private:
171  int m_n;
172  mutable int m_i;
173 
174 public:
175  Every_nth_element(int n, int start = 1) : m_n(n), m_i(start)
176  {}
177 
178  bool operator()(const T& /*t*/) const
179  {
180  if(m_i++ == m_n)
181  {
182  m_i = 1;
183  return true;
184  }
185 
186  return false;
187  }
188 
190  {}
191 };
192 
196 template<class T>
198 {
199 public:
200  typedef T argument_type;
201  typedef bool result_type;
202 
203 private:
204  const T* m_Tp;
205 
206 public:
207  Compare_address(const T* Tp) : m_Tp(Tp) {}
208 
209  bool operator()(const T& t) const
210  {
211  return &t == m_Tp;
212  }
213 };
214 
218 template<class T>
220 {
221 public:
222  typedef T argument_type;
223  typedef const T* result_type;
224 
225 public:
226  const T* operator()(const T& t) const
227  {
228  return &t;
229  }
230 };
231 
237 template <class T>
238 class minimum
239 {
240 public:
241  const T operator()(const T& op1, const T& op2) { return std::min(op1, op2); }
242 };
243 
249 template <class T>
250 class maximum
251 {
252 public:
253  const T operator()(const T& op1, const T& op2) { return std::max(op1, op2); }
254 };
255 
256 
257 
258 } //namespace kjb
259 
260 #endif /* L_FUNCTORS_H_INCLUDED */
261 
Identity function.
Definition: l_functors.h:144
T argument_type
Definition: l_functors.h:167
T operator()()
Definition: l_functors.h:51
const T * operator()(const T &t) const
Definition: l_functors.h:226
const T * result_type
Definition: l_functors.h:223
bool operator()(const T &lhs, const T &rhs) const
Definition: l_functors.h:106
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
double result_type
Definition: l_functors.h:122
T argument_type
Definition: l_functors.h:222
Predicate that compares the kth element of a indexable type.
Definition: l_functors.h:94
Generator that increases (using +=) itself by the given value everytime it is called.
Definition: l_functors.h:65
~Identity()
Definition: l_functors.h:157
~Every_nth_element()
Definition: l_functors.h:189
double operator()(const V &v) const
Definition: l_functors.h:131
Generator that increments (++) its state everytime it is called. Useful for creating sequences of con...
Definition: l_functors.h:39
bool operator()(const T &t) const
Definition: l_functors.h:209
T result_type
Definition: l_functors.h:147
Increase_by(const T &initial_value, const T &increase_value)
Definition: l_functors.h:75
~Index_less_than()
Definition: l_functors.h:111
Select_coordinate(int coordinate)
Definition: l_functors.h:128
T operator()()
Definition: l_functors.h:79
bool operator()(const T &) const
Definition: l_functors.h:178
Predicate that returns true if address of element equals given.
Definition: l_functors.h:197
Functor that returns the address of a given object.
Definition: l_functors.h:219
T result_type
Definition: l_functors.h:42
Index_less_than(int index)
Definition: l_functors.h:103
T argument_type
Definition: l_functors.h:200
Selects a coordinate from a vector type.
Definition: l_functors.h:119
Identity()
Definition: l_functors.h:150
bool result_type
Definition: l_functors.h:97
~Increase_by()
Definition: l_functors.h:86
const T operator()(const T &op1, const T &op2)
Definition: l_functors.h:253
Int_matrix::Value_type min(const Int_matrix &mat)
Return the minimum value in this matrix.
Definition: l_int_matrix.h:1385
bool result_type
Definition: l_functors.h:201
~Increment()
Definition: l_functors.h:56
Compare_address(const T *Tp)
Definition: l_functors.h:207
Definition: l_functors.h:250
~Select_coordinate()
Definition: l_functors.h:136
const T operator()(const T &op1, const T &op2)
Definition: l_functors.h:241
T operator()(const T &t) const
Definition: l_functors.h:152
T result_type
Definition: l_functors.h:68
Every_nth_element(int n, int start=1)
Definition: l_functors.h:175
Definition: l_functors.h:238
Increment(const T &initial_value=T())
Definition: l_functors.h:48
bool result_type
Definition: l_functors.h:168
Predicate that returns true every nth call.
Definition: l_functors.h:164