aimstil  5.0.5
functors.h
Go to the documentation of this file.
1 #ifndef TIL_FUNCTORS_H
2 #define TIL_FUNCTORS_H
3 
11 
12 // includes from STL
13 #include <cmath> // sqrt, exp
14 #include <functional> // unary_function, binary_function
15 
16 // includes from BOOST
17 #include <boost/call_traits.hpp>
18 
19 // includes from TIL
20 #include "til/traits.h"
21 
22 namespace til { namespace functor
23 {
24 
25 
27  template < typename T >
28  struct Abs : public std::unary_function<T,T>
29  {
30  typedef std::unary_function<T,T> Base;
31  typedef typename Base::argument_type argument_type;
32  typedef typename Base::result_type result_type;
33 
34  inline result_type operator()(argument_type x) const
35  {
36  return std::abs(x);//( x>0? x : -x );
37  }
38  };
39 
40 
42  /*
43  template < typename T >
44  struct Square : public std::unary_function<T,T>
45  {
46  typedef std::unary_function<T,T> Base;
47  typedef typename Base::argument_type argument_type;
48  typedef typename Base::result_type result_type;
49 
50  inline result_type operator()(argument_type x) const
51  {
52  return x*x;
53  }
54  };
55  */
56 
57  template < typename T, unsigned int D >
58  struct Pow
59  : public std::unary_function<T,T>
60  {
61  typedef std::unary_function<T,T> Base;
62  typedef typename Base::argument_type argument_type;
63  typedef typename Base::result_type result_type;
64 
65  inline result_type operator()(argument_type x) const
66  {
67  if (D%2)
68  return square(Pow<T,D/2>()(x)) * x;
69  else
70  return square(Pow<T,D/2>()(x));
71  }
72  };
73 
74  template < typename T >
75  struct Pow<T,1>
76  : public std::unary_function<T,T>
77  {
78  typedef std::unary_function<T,T> Base;
79  typedef typename Base::argument_type argument_type;
80  typedef typename Base::result_type result_type;
81 
82  inline result_type operator()(argument_type x) const
83  {
84  return x;
85  }
86  };
87 
88  template < typename T >
89  struct Pow<T,0>
90  : public std::unary_function<T,T>
91  {
92  typedef std::unary_function<T,T> Base;
93  typedef typename Base::argument_type argument_type;
94  typedef typename Base::result_type result_type;
95 
96  class InvalidArgument : public std::exception {};
97 
98  inline result_type operator()(argument_type x) const
99  {
100  if (x == 0) throw InvalidArgument();
101  else return 1;
102  }
103  };
104 
106  template < typename T >
107  struct Sqrt
108  : public std::unary_function<T,T>
109  {
110  typedef std::unary_function<T,T> Base;
111  typedef typename Base::argument_type argument_type;
112  typedef typename Base::result_type result_type;
113 
114  inline result_type operator()(argument_type x) const
115  {
116  return std::sqrt(x);
117  }
118  };
119 
121  template < typename T >
122  struct Exp
123  : public std::unary_function<T,T>
124  {
125  typedef std::unary_function<T,T> Base;
126  typedef typename Base::argument_type argument_type;
127  typedef typename Base::result_type result_type;
128  result_type operator()(argument_type x) const
129  {
130  return std::exp(x);
131  }
132  };
133 
135  template < typename TTo, typename TFrom >
136  struct CastTo
137  : public std::binary_function<TTo &, typename boost::call_traits<TFrom>::param_type, void>
138  {
139  typedef std::binary_function<TTo &, typename boost::call_traits<TFrom>::param_type, void> Base;
140  typedef typename Base::first_argument_type first_argument_type;
141  typedef typename Base::second_argument_type second_argument_type;
142  typedef typename Base::result_type result_type;
143  void operator()(first_argument_type x, second_argument_type y) const { x = y; }
144  };
145 
147  template < typename TFrom >
148  struct Convertor
149  {
150  Convertor(typename boost::call_traits<TFrom>::param_type from) : m_from(from) {}
151 
152  template < typename TTo >
153  void into(TTo & to)
154  {
155  CastTo<TTo, TFrom>()(to, m_from);
156  }
157 
158  typename boost::call_traits<TFrom>::param_type m_from;
159  };
160 
162  template < typename TTo, typename TFrom >
163  struct Cast
164  : public std::unary_function<typename boost::call_traits<TFrom>::param_type, TTo>
165  {
166  typedef std::unary_function<typename boost::call_traits<TFrom>::param_type,TTo> Base;
167  typedef typename Base::argument_type argument_type;
168  typedef typename Base::result_type result_type;
169  result_type operator()(argument_type x) const
170  {
171  // Actually, what is the real difference between static casting and
172  // constructing.
173  // return static_cast<TTo>(x);
174  // return TTo(x);
175  // Do not call castTo because they should rely on different mechanisms:
176  // Cast rely on constructors
177  // castTo implements operator=
178  TTo y;
179  CastTo<TTo,TFrom>()(y,x);
180  return y;
181  }
182  };
183 
185  template < typename T >
186  struct Deref
187  : public std::unary_function<T, typename deref<T>::type>
188  {
189  typedef std::unary_function<T, typename deref<T>::type> Base;
190  typedef typename Base::argument_type argument_type;
191  typedef typename Base::result_type result_type;
192  result_type operator()(argument_type x) const
193  {
194  return *x;
195  }
196  };
197 
199  template < typename T1, typename T2 >
200  struct AddTo
201  : public std::binary_function<typename boost::add_reference<T1>::type,typename boost::call_traits<T2>::param_type,void>
202  {
203  typedef std::binary_function<typename boost::add_reference<T1>::type,typename boost::call_traits<T2>::param_type,void> Base;
204  typedef typename Base::first_argument_type first_argument_type;
205  typedef typename Base::second_argument_type second_argument_type;
206  typedef typename Base::result_type result_type;
207  result_type operator()(first_argument_type x, second_argument_type y) const
208  {
209  x += y;
210  }
211  };
212 
214  template < typename T1, typename T2 >
215  struct SubTo
216  : public std::binary_function<typename boost::add_reference<T1>::type,typename boost::call_traits<T2>::param_type,void>
217  {
218  typedef std::binary_function<typename boost::add_reference<T1>::type,typename boost::call_traits<T2>::param_type,void> Base;
219  typedef typename Base::first_argument_type first_argument_type;
220  typedef typename Base::second_argument_type second_argument_type;
221  typedef typename Base::result_type result_type;
222  result_type operator()(first_argument_type x, second_argument_type y) const
223  {
224  x -= y;
225  }
226  };
227 
229  template < typename T1, typename T2 >
230  struct MulTo
231  : public std::binary_function<typename boost::add_reference<T1>::type,typename boost::call_traits<T2>::param_type,void>
232  {
233  typedef std::binary_function<typename boost::add_reference<T1>::type,typename boost::call_traits<T2>::param_type,void> Base;
234  typedef typename Base::first_argument_type first_argument_type;
235  typedef typename Base::second_argument_type second_argument_type;
236  typedef typename Base::result_type result_type;
237  result_type operator()(first_argument_type x, second_argument_type y) const
238  {
239  x *= y;
240  }
241  };
242 
244  template < typename T1, typename T2 >
245  struct DivTo
246  : public std::binary_function<typename boost::add_reference<T1>::type,typename boost::call_traits<T2>::param_type,void>
247  {
248  typedef std::binary_function<typename boost::add_reference<T1>::type,typename boost::call_traits<T2>::param_type,void> Base;
249  typedef typename Base::first_argument_type first_argument_type;
250  typedef typename Base::second_argument_type second_argument_type;
251  typedef typename Base::result_type result_type;
252  result_type operator()(first_argument_type x, second_argument_type y) const
253  {
254  x /= y;
255  }
256  };
257 
258 
260  // TODO: this stupid combine doesn't mean anything. Or what I mean is that it is relevant to numerical types
261  // only. Probably we should have a default_add_trait struct somewhere, that itself could default to combine.
262  template < typename T1, typename T2, typename TRes = typename combine<T1,T2>::type >
263  struct Add
264  : public std::binary_function<typename boost::call_traits<T1>::param_type, typename boost::call_traits<T2>::param_type, TRes>
265  {
266  typedef std::binary_function<typename boost::call_traits<T1>::param_type, typename boost::call_traits<T2>::param_type, TRes> Base;
267  typedef typename Base::first_argument_type first_argument_type;
268  typedef typename Base::second_argument_type second_argument_type;
269  typedef typename Base::result_type result_type;
270  result_type operator()(first_argument_type x, second_argument_type y) const
271  {
272  return x + y;
273  }
274  };
275 
277  template < typename T1, typename T2, typename TRes = typename combine<T1,T2>::type >
278  struct Sub
279  : public std::binary_function<typename boost::call_traits<T1>::param_type, typename boost::call_traits<T2>::param_type, TRes>
280  {
281  typedef std::binary_function<typename boost::call_traits<T1>::param_type, typename boost::call_traits<T2>::param_type, TRes> Base;
282  typedef typename Base::first_argument_type first_argument_type;
283  typedef typename Base::second_argument_type second_argument_type;
284  typedef typename Base::result_type result_type;
285  result_type operator()(first_argument_type x, second_argument_type y) const
286  {
287  return x - y;
288  }
289  };
290 
292  template < typename T1, typename T2, typename TRes = typename combine<T1,T2>::type >
293  struct Mul
294  : public std::binary_function<typename boost::call_traits<T1>::param_type, typename boost::call_traits<T2>::param_type, TRes>
295  {
296  typedef std::binary_function<typename boost::call_traits<T1>::param_type, typename boost::call_traits<T2>::param_type, TRes> Base;
297  typedef typename Base::first_argument_type first_argument_type;
298  typedef typename Base::second_argument_type second_argument_type;
299  typedef typename Base::result_type result_type;
300  result_type operator()(first_argument_type x, second_argument_type y) const
301  {
302  return x * y;
303  }
304  };
305 
307  template < typename T1, typename T2, typename TRes = typename combine<T1,T2>::type >
308  struct Div
309  : public std::binary_function<typename boost::call_traits<T1>::param_type, typename boost::call_traits<T2>::param_type, TRes>
310  {
311  typedef std::binary_function<typename boost::call_traits<T1>::param_type, typename boost::call_traits<T2>::param_type, TRes> Base;
312  typedef typename Base::first_argument_type first_argument_type;
313  typedef typename Base::second_argument_type second_argument_type;
314  typedef typename Base::result_type result_type;
315  result_type operator()(first_argument_type x, second_argument_type y) const
316  {
317  // TODO: should we use til::Fraction instead of raw '/' here?
318  return x / y;
319  }
320  };
321 
323  template < typename TFunctor, typename T >
324  struct Call
325  : public std::binary_function<TFunctor, T, typename TFunctor::result_type>
326  {
327  typedef std::binary_function<TFunctor, T, typename TFunctor::result_type> Base;
328  typedef typename Base::first_argument_type first_argument_type;
329  typedef typename Base::second_argument_type second_argument_type;
330  typedef typename Base::result_type result_type;
331  result_type operator()(first_argument_type x, second_argument_type y) const
332  {
333  return x(y);
334  }
335  };
336 
338  template < typename T >
339  struct Assign
340  : public std::binary_function<typename boost::add_reference<T>::type,typename boost::call_traits<T>::param_type,void>
341  {
342  typedef std::binary_function<typename boost::add_reference<T>::type,typename boost::call_traits<T>::param_type,void> Base;
343  typedef typename Base::first_argument_type first_argument_type;
344  typedef typename Base::second_argument_type second_argument_type;
345  typedef typename Base::result_type result_type;
346  result_type operator()(first_argument_type x, second_argument_type y)
347  {
348  x = y;
349  }
350  };
351 
352  /*
355  // NB: no default is given for TAccumulation, because this is an important choice
356  // that depends on the context and should not be ignored.
357  template < typename T, typename TAccumulation, typename TBinaryOperator = Add<TAccumulation,T> >
358  class Accumulate : public std::unary_function<T,void>
359  {
360  public: // typedefs
361  typedef std::unary_function<T,void> Base;
362  typedef typename Base::argument_type argument_type;
363  typedef typename Base::result_type result_type;
364 
365  public: // constructors & destructor
366  Accumulate() : Base(), m_accumulation() {}
367 
368  public: // functions
369  TAccumulation get() const { return m_accumulation; }
370 
371  public: // operators
372  result_type operator()(argument_type x)
373  {
374  m_accumulation = TBinaryOperator()(m_accumulation, x);
375  }
376 
377  private: // data
378  TAccumulation m_accumulation;
379  };
380  */
381 
382 }} // namespace til::functor
383 
384 
385  //-------------------------------------------------------------------------------------------------
386 
387  //--------------------//
388  // helper functions //
389  //--------------------//
390 
391 namespace til
392 {
393  template < typename TRes, typename T1, typename T2 >
394  inline TRes add(T1 x, T2 y)
395  { return functor::Add<T1,T2,TRes>()(x,y); }
396 
397  template < typename TRes, typename T1, typename T2 >
398  inline TRes sub(T1 x, T2 y)
399  { return functor::Sub<T1,T2,TRes>()(x,y); }
400 
401  template < typename TRes, typename T1, typename T2 >
402  inline TRes mul(T1 x, T2 y)
403  { return functor::Mul<T1,T2,TRes>()(x,y); }
404 
405  template < typename TRes, typename T1, typename T2 >
406  inline TRes div(T1 x, T2 y)
407  { return functor::Div<T1,T2,TRes>()(x,y); }
408 
409  template < typename TTo, typename TFrom >
410  inline void convert(TTo & x, const TFrom & y)
411  { functor::CastTo<TTo,TFrom>()(x,y); }
412 
413  template < typename TTo, typename TFrom >
414  inline TTo convert(const TFrom & y)
415  { return functor::Cast<TTo, TFrom>()(y); }
416 
417  template < typename T >
418  inline functor::Convertor<T>
419  convert2(const T & from)
420  { return functor::Convertor<T>(from); }
421 }
422 
423 #endif
424 
425 
result_type operator()(first_argument_type x, second_argument_type y) const
Definition: functors.h:237
Base::second_argument_type second_argument_type
Definition: functors.h:250
std::unary_function< T, T > Base
Definition: functors.h:92
Base::result_type result_type
Definition: functors.h:251
Base::result_type result_type
Definition: functors.h:221
void convert(TTo &x, const TFrom &y)
Definition: functors.h:410
Base::first_argument_type first_argument_type
Definition: functors.h:234
Square root.
Definition: functors.h:107
Absolute value.
Definition: functors.h:28
void sqrt(const TImage &in, TImage &out)
Definition: imageArith.h:326
Base::result_type result_type
Definition: functors.h:127
Base::result_type result_type
Definition: functors.h:80
result_type operator()(argument_type x) const
Definition: functors.h:114
Base::result_type result_type
Definition: functors.h:330
Base::result_type result_type
Definition: functors.h:345
result_type operator()(first_argument_type x, second_argument_type y) const
Definition: functors.h:331
std::binary_function< typename boost::add_reference< T >::type, typename boost::call_traits< T >::param_type, void > Base
Definition: functors.h:342
Base::first_argument_type first_argument_type
Definition: functors.h:328
std::binary_function< typename boost::call_traits< T1 >::param_type, typename boost::call_traits< T2 >::param_type, TRes > Base
Definition: functors.h:266
Base::result_type result_type
Definition: functors.h:284
Base::second_argument_type second_argument_type
Definition: functors.h:220
std::binary_function< TTo &, typename boost::call_traits< TFrom >::param_type, void > Base
Definition: functors.h:139
Base::second_argument_type second_argument_type
Definition: functors.h:205
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
Base::first_argument_type first_argument_type
Definition: functors.h:204
Base::argument_type argument_type
Definition: functors.h:126
In-place addition functor.
Definition: functors.h:200
std::unary_function< T, T > Base
Definition: functors.h:30
Base::result_type result_type
Definition: functors.h:112
Base::second_argument_type second_argument_type
Definition: functors.h:344
result_type operator()(first_argument_type x, second_argument_type y) const
Definition: functors.h:207
Function call functor.
Definition: functors.h:324
boost::enable_if_c< std::numeric_limits< T1 >::is_specialized &&std::numeric_limits< T2 >::is_specialized >::type sub(T1 x, T2 y, T3 &z)
Definition: basicFunctors.h:32
functor::Convertor< T > convert2(const T &from)
Definition: functors.h:419
result_type operator()(first_argument_type x, second_argument_type y) const
Definition: functors.h:315
Dereferenciation functor.
Definition: functors.h:186
Base::result_type result_type
Definition: functors.h:191
numeric_array< T, D > abs(const numeric_array< T, D > &a)
Absolute value, element-wise.
Assignment functor.
Definition: functors.h:339
Base::second_argument_type second_argument_type
Definition: functors.h:298
Base::argument_type argument_type
Definition: functors.h:111
Two-step static class functor.
Definition: functors.h:148
Base::first_argument_type first_argument_type
Definition: functors.h:312
result_type operator()(argument_type x) const
Definition: functors.h:169
Base::first_argument_type first_argument_type
Definition: functors.h:267
std::binary_function< typename boost::call_traits< T1 >::param_type, typename boost::call_traits< T2 >::param_type, TRes > Base
Definition: functors.h:296
Base::first_argument_type first_argument_type
Definition: functors.h:249
Base::second_argument_type second_argument_type
Definition: functors.h:283
In-place multiplication functor.
Definition: functors.h:230
Base::argument_type argument_type
Definition: functors.h:79
Exponential.
Definition: functors.h:122
result_type operator()(argument_type x) const
Definition: functors.h:34
Base::first_argument_type first_argument_type
Definition: functors.h:343
Base::first_argument_type first_argument_type
Definition: functors.h:219
std::binary_function< TFunctor, T, typename TFunctor::result_type > Base
Definition: functors.h:327
std::unary_function< T, T > Base
Definition: functors.h:125
Base::argument_type argument_type
Definition: functors.h:62
Static cast functor.
Definition: functors.h:136
result_type operator()(first_argument_type x, second_argument_type y) const
Definition: functors.h:285
result_type operator()(argument_type x) const
Definition: functors.h:82
Base::first_argument_type first_argument_type
Definition: functors.h:282
boost::call_traits< TFrom >::param_type m_from
Definition: functors.h:158
Base::argument_type argument_type
Definition: functors.h:93
Convertor(typename boost::call_traits< TFrom >::param_type from)
Definition: functors.h:150
Square.
Definition: functors.h:58
Base::argument_type argument_type
Definition: functors.h:167
Base::second_argument_type second_argument_type
Definition: functors.h:329
result_type operator()(first_argument_type x, second_argument_type y) const
Definition: functors.h:252
Base::second_argument_type second_argument_type
Definition: functors.h:235
Base::second_argument_type second_argument_type
Definition: functors.h:313
result_type operator()(first_argument_type x, second_argument_type y) const
Definition: functors.h:300
In-place subtraction functor.
Definition: functors.h:215
result_type operator()(argument_type x) const
Definition: functors.h:65
std::binary_function< typename boost::add_reference< T1 >::type, typename boost::call_traits< T2 >::param_type, void > Base
Definition: functors.h:218
Base::argument_type argument_type
Definition: functors.h:31
Base::first_argument_type first_argument_type
Definition: functors.h:297
boost::enable_if_c< std::numeric_limits< T1 >::is_specialized &&std::numeric_limits< T2 >::is_specialized >::type add(T1 x, T2 y, T3 &z)
The following functions are necessary for the coming functors.
Definition: basicFunctors.h:21
Base::first_argument_type first_argument_type
Definition: functors.h:140
Base::result_type result_type
Definition: functors.h:314
result_type operator()(first_argument_type x, second_argument_type y)
Definition: functors.h:346
Base::result_type result_type
Definition: functors.h:299
std::unary_function< T, T > Base
Definition: functors.h:61
std::binary_function< typename boost::add_reference< T1 >::type, typename boost::call_traits< T2 >::param_type, void > Base
Definition: functors.h:248
Base::second_argument_type second_argument_type
Definition: functors.h:268
std::unary_function< T, T > Base
Definition: functors.h:110
Multiplication functor.
Definition: functors.h:293
result_type operator()(argument_type x) const
Definition: functors.h:98
result_type operator()(argument_type x) const
Definition: functors.h:192
Base::second_argument_type second_argument_type
Definition: functors.h:141
Base::result_type result_type
Definition: functors.h:206
std::binary_function< typename boost::add_reference< T1 >::type, typename boost::call_traits< T2 >::param_type, void > Base
Definition: functors.h:203
std::unary_function< T, T > Base
Definition: functors.h:78
Base::argument_type argument_type
Definition: functors.h:190
Base::result_type result_type
Definition: functors.h:168
Subtraction functor.
Definition: functors.h:278
void square(const TImage &in, TImage &out)
Definition: imageArith.h:310
Base::result_type result_type
Definition: functors.h:63
Base::result_type result_type
Definition: functors.h:236
Addition functor.
Definition: functors.h:263
Base::result_type result_type
Definition: functors.h:32
Base::result_type result_type
Definition: functors.h:142
std::unary_function< typename boost::call_traits< TFrom >::param_type, TTo > Base
Definition: functors.h:166
result_type operator()(argument_type x) const
Definition: functors.h:128
boost::enable_if_c< std::numeric_limits< T1 >::is_specialized &&std::numeric_limits< T2 >::is_specialized >::type div(T1 x, T2 y, T3 &z)
Definition: basicFunctors.h:54
void operator()(first_argument_type x, second_argument_type y) const
Definition: functors.h:143
In-place division functor.
Definition: functors.h:245
Base::result_type result_type
Definition: functors.h:94
std::binary_function< typename boost::add_reference< T1 >::type, typename boost::call_traits< T2 >::param_type, void > Base
Definition: functors.h:233
void into(TTo &to)
Definition: functors.h:153
std::binary_function< typename boost::call_traits< T1 >::param_type, typename boost::call_traits< T2 >::param_type, TRes > Base
Definition: functors.h:311
boost::enable_if_c< std::numeric_limits< T1 >::is_specialized &&std::numeric_limits< T2 >::is_specialized >::type mul(T1 x, T2 y, T3 &z)
Definition: basicFunctors.h:43
Static cast functor.
Definition: functors.h:163
std::binary_function< typename boost::call_traits< T1 >::param_type, typename boost::call_traits< T2 >::param_type, TRes > Base
Definition: functors.h:281
Division functor.
Definition: functors.h:308
result_type operator()(first_argument_type x, second_argument_type y) const
Definition: functors.h:270
Base::result_type result_type
Definition: functors.h:269
std::unary_function< T, typename deref< T >::type > Base
Definition: functors.h:189
result_type operator()(first_argument_type x, second_argument_type y) const
Definition: functors.h:222