DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
BasicFunctors.h
1 
17 #pragma once
18 
36 #if defined(BasicFunctors_RECURSES)
37 #error Recursive header files inclusion detected in BasicFunctors.h
38 #else // defined(BasicFunctors_RECURSES)
39 
40 #define BasicFunctors_RECURSES
41 
42 #if !defined BasicFunctors_h
43 
44 #define BasicFunctors_h
45 
47 // Inclusions
48 #include <algorithm>
49 #include <functional>
50 #include <cmath>
51 
52 #include "BasicBoolFunctions.h"
54 
55 namespace DGtal
56 {
57 
61  template<typename T>
62  struct MinFunctor
63  {
64  inline
65  T operator() (const T&a, const T&b) const
66  { return /*std::*/min(a,b); }
67  };
68 
69  template<typename T>
70  struct MaxFunctor
71  {
72  inline
73  T operator() (const T&a, const T&b) const
74  { return /*std::*/max(a,b); }
75  };
76 
80  template <class T>
81  struct MinusFunctor : binary_function <T,T,T>
82  {
83  T operator() (const T& x, const T& y) const
84  {return x-y;}
85  };
86 
90  template <class T>
91  struct AbsFunctor : unary_function<T,T>
92  {
93  inline
94  T operator() (const T &x) const
95  {
96  if (x < 0)
97  return -x;
98  else
99  return x;
100  }
101  };
102 
104 // Some basic unary functors that may be useful
106 
113  {
119  template <typename T >
120  inline
121  T operator()(const T& aT) const
122  {
123  return aT;
124  }
125  };
126 
134  template <typename TValue>
136  {
137  public:
138  typedef TValue Value;
139 
144  ConstValueFunctor(const Value& aValue = 0)
145  :myValue(aValue) {};
146 
155  template <typename TInput>
156  inline
157  Value operator()(const TInput& /*aInput*/) const
158  {
159  return myValue;
160  }
161 
162  private:
167 
168  };
169 
170 
177  template <typename TOutput >
178  struct CastFunctor
179  {
185  template<typename TInput>
186  inline
187  TOutput operator()(const TInput& aInput) const
188  {
189  return static_cast<TOutput>(aInput);
190  }
191  };
192 
193 
202  template <typename TFunctor1, typename TFunctor2, typename ReturnType >
203  class Composer
204  {
205  public:
209  Composer(): myF1(NULL), myF2(NULL) {}
215  Composer(const TFunctor1& aF1, const TFunctor2& aF2): myF1(&aF1), myF2(&aF2) {}
220  Composer(const Composer& other): myF1(other.myF1), myF2(other.myF2) {}
221 
226  Composer& operator=(const Composer& other)
227  {
228  if (this != &other)
229  {
230  myF1 = other.myF1;
231  myF2 = other.myF2;
232  }
233  return *this;
234  }
235 
236 
249  template<typename TInput>
250  inline
251  ReturnType operator()(const TInput& aInput) const
252  {
253  ASSERT( myF1 );
254  ASSERT( myF2 );
255  return myF2->operator()( myF1->operator()( aInput ) );
256  }
257 
258  private:
262  const TFunctor1* myF1;
266  const TFunctor2* myF2;
267  };
268 
282 template <typename T, bool isLower = true, bool isEqual = true >
283 class Thresholder {
284  public:
285  BOOST_CONCEPT_ASSERT(( boost::EqualityComparable<T> ));
286  BOOST_CONCEPT_ASSERT(( boost::LessThanComparable<T> ));
287 
288  typedef T Input;
289 
294  Thresholder(const Input& aT = 0):myT(aT) {};
300  bool operator()(const Input& aI) const {
301  std::less_equal<Input> c;
302  return c(aI,myT);
303  }
304  private:
309 };
310 
311 //specializations
312 template <typename T>
314 
315  public:
316  BOOST_CONCEPT_ASSERT(( boost::EqualityComparable<T> ));
317  BOOST_CONCEPT_ASSERT(( boost::LessThanComparable<T> ));
318 
319  typedef T Input;
320 
321  Thresholder(const Input& aT = 0):myT(aT) {};
322 
323  bool operator()(const Input& aI) const {
324  std::greater<Input> c;
325  return c(aI,myT);
326  }
327 
328  private:
330 };
331 template <typename T>
332 struct Thresholder<T,false,true> {
333  public:
334  BOOST_CONCEPT_ASSERT(( boost::EqualityComparable<T> ));
335  BOOST_CONCEPT_ASSERT(( boost::LessThanComparable<T> ));
336 
337  typedef T Input;
338 
339  Thresholder(const Input& aT = 0):myT(aT) {};
340  bool operator()(const Input& aI) const {
341  std::greater_equal<Input> c;
342  return c(aI,myT);
343  }
344 
345  private:
347 };
348 
349 template <typename T>
350 struct Thresholder<T,true,false> {
351  public:
352  BOOST_CONCEPT_ASSERT(( boost::EqualityComparable<T> ));
353  BOOST_CONCEPT_ASSERT(( boost::LessThanComparable<T> ));
354 
355  typedef T Input;
356 
357  Thresholder(const Input& aT = 0):myT(aT) {};
358 
359  bool operator()(const Input& aI) const {
360  std::less<Input> c;
361  return c(aI,myT);
362  }
363 
364  private:
366 };
367 
368 template <typename T>
369 struct Thresholder<T,true,true> {
370  public:
371  BOOST_CONCEPT_ASSERT(( boost::EqualityComparable<T> ));
372  BOOST_CONCEPT_ASSERT(( boost::LessThanComparable<T> ));
373 
374  typedef T Input;
375 
376  Thresholder(const Input& aT = 0):myT(aT) {};
377 
378  bool operator()(const Input& aI) const {
379  std::less_equal<Input> c;
380  return c(aI,myT);
381  }
382 
383  private:
385 };
386 
388  // template class PredicateCombiner
398  template <typename TPredicate1, typename TPredicate2,
399  typename TBinaryFunctor = BoolFunction2 >
401  {
402  typedef TPredicate1 Predicate1;
403  typedef TPredicate2 Predicate2;
404 
413  const Predicate2 & pred2,
414  const TBinaryFunctor & boolFunctor )
415  : myPred1( &pred1 ), myPred2( &pred2 ), myBoolFunctor( &boolFunctor )
416  {
417  }
418 
424  : myPred1( other.pred1 ), myPred2( other.pred2 ), myBoolFunctor( other.boolFunctor )
425  {
426  }
427 
434  {
435  if (this != &other)
436  {
437  myPred1 = other.myPred1;
438  myPred2 = other.myPred2;
439  myBoolFunctor = other.myBoolFunctor;
440  }
441  return *this;
442  }
443 
448 
455  template<typename T>
456  bool operator()( const T & t ) const
457  {
458  return myBoolFunctor->operator()( myPred1->operator()( t ),
459  myPred2->operator()( t ) );
460  }
461 
467  const TBinaryFunctor* myBoolFunctor;
468  };
469 
477 template <typename T>
479 {
480 public:
481  BOOST_CONCEPT_ASSERT(( boost::EqualityComparable<T> ));
482  BOOST_CONCEPT_ASSERT(( boost::LessThanComparable<T> ));
483 
485  typedef T Input;
486 
491 
497  IntervalThresholder(const Input& low, const Input& up)
498  : myTlow( low), myTup ( up ),
499  myPred( myTlow, myTup, AndBoolFct2() ) {};
500 
506  bool operator()(const Input& aI) const
507  {
508  return myPred(aI);
509  }
510 private:
523 };
524 
525 
533  template <typename ReturnType>
534  class Pair1st
535  {
536  public:
537 
546  template <typename TPair>
547  inline
548  ReturnType operator()(const TPair& aPair) const
549  {
550  return aPair.first;
551  }
552 
553  };
554 
562  template <typename ReturnType>
563  class Pair2nd
564  {
565  public:
566 
575  template <typename TPair>
576  inline
577  ReturnType operator()(const TPair& aPair) const
578  {
579  return aPair.second;
580  }
581 
582  };
583 
592  template <typename ReturnType>
594  {
595  public:
596 
605  template <typename TPair>
606  inline
607  ReturnType& operator()(TPair& aPair) const
608  {
609  return aPair.first;
610  }
611 
620  template <typename TPair>
621  inline
622  const ReturnType& operator()(const TPair& aPair) const
623  {
624  return aPair.first;
625  }
626 
627  };
628 
637  template <typename ReturnType>
639  {
640  public:
641 
650  template <typename TPair>
651  inline
652  ReturnType& operator()(TPair& aPair) const
653  {
654  return aPair.second;
655  }
656 
665  template <typename TPair>
666  inline
667  const ReturnType& operator()(const TPair& aPair) const
668  {
669  return aPair.second;
670  }
671 
672  };
673 
674 }
676 
677 
678 #endif // !defined BasicFunctors_h
679 
680 #undef BasicFunctors_RECURSES
681 #endif // else defined(BasicFunctors_RECURSES)