OpenGM  2.3.x
Discrete Graphical Model Library
functors.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_FUNCTORS_HXX
3 #define OPENGM_FUNCTORS_HXX
4 
5 #include<cmath>
6 
7 namespace opengm {
8 
10 
12  template<class ACC,class VALUE_TYPE>
13  class AccumulationFunctor{
14  public:
15  AccumulationFunctor(const VALUE_TYPE v)
16  :accValue_(v) {
17  }
19  AccumulationFunctor()
20  :accValue_(ACC::template neutral<VALUE_TYPE>()) {
21  }
24  void operator()(const VALUE_TYPE v) {
25  ACC::op(v,accValue_);
26  }
28  VALUE_TYPE value() {
29  return accValue_;
30  }
31  private:
32  VALUE_TYPE accValue_;
33  };
34 
36  template<class T>
37  class MinMaxFunctor{
38  public:
40  typedef T ValueType;
41  MinMaxFunctor()
42  : first_(true),
43  min_(T()),
44  max_(T()) {
45  }
46  MinMaxFunctor(T min,T max)
47  : first_(false),
48  min_(min),
49  max_(max) {
50  }
52  ValueType min() {
53  return min_;
54  }
56  ValueType max() {
57  return max_;
58  }
60  void operator()(const ValueType v) {
61  if(first_) {
62  min_=v;
63  max_=v;
64  first_=false;
65  }
66  else{
67  if(v<min_) {
68  min_=v;
69  }
70  if(v>max_) {
71  max_=v;
72  }
73  }
74  }
75  private:
76  bool first_;
77  ValueType min_;
78  ValueType max_;
79  };
80 
82  template<class T>
83  struct PowFunctor {
86  template<class T_In>
87  PowFunctor(T_In w)
88  : w_(w) {
89  }
91  PowFunctor(const PowFunctor& other)
92  : w_(other.w_) {
93  }
96  template<class T_In>
97  const T operator()(T_In value) {
98  return std::pow(value, w_);
99  }
100  T w_;
101  };
102 
104  template<class T_ReturnType, class T_Functor>
105  class SwapArgumemtFunctor {
106  public:
109  SwapArgumemtFunctor(const SwapArgumemtFunctor& other)
110  : functor_(other.functor_) {
111  }
114  SwapArgumemtFunctor(T_Functor functor)
115  : functor_(functor) {
116  }
120  template<class T_A, class T_B>
121  T_ReturnType operator()(T_A a, T_B b) {
122  return functor_(b, a);
123  }
124  private:
125  T_Functor functor_;
126  };
127 
129  template<class T_Scalar, class T_Functor, bool ScalarLeft>
130  class BinaryToUnaryFunctor;
131 
133  template<class T_Scalar, class T_Functor>
134  class BinaryToUnaryFunctor<T_Scalar, T_Functor, true> {
135  public:
136  BinaryToUnaryFunctor(const BinaryToUnaryFunctor& other)
137  : functor_(other.functor_),
138  scalar_(other.scalar_) {
139  }
140  BinaryToUnaryFunctor(const T_Scalar& scalar, T_Functor& functor)
141  :functor_(functor),
142  scalar_(scalar)
143  {
144  }
145  template<class TIN>
146  T_Scalar operator()(const TIN in) {
147  return functor_(scalar_, in);
148  }
149  private:
150  T_Functor functor_;
151  T_Scalar scalar_;
152 
153  };
154 
156  template<class T_Scalar, class T_Functor>
157  class BinaryToUnaryFunctor<T_Scalar, T_Functor, false> {
158  public:
159  BinaryToUnaryFunctor(const BinaryToUnaryFunctor& other)
160  : functor_(other.functor_),
161  scalar_(other.scalar_) {
162  }
163  BinaryToUnaryFunctor(const T_Scalar& scalar, T_Functor& functor)
164  :functor_(functor),
165  scalar_(scalar){
166  }
167  template<class TIN>
168  T_Scalar operator()(const TIN in) {
169  return functor_(in, scalar_);
170  }
171  private:
172  T_Functor functor_;
173  T_Scalar scalar_;
174 
175  };
176 
177  // a copy functor
178  template<class OUT_ITERATOR>
179  class CopyFunctor{
180  public:
181  CopyFunctor(OUT_ITERATOR iterator):outIterator_(iterator){}
182  template<class T>
183  void operator()(const T & value){
184  (*outIterator_)=value;
185  ++outIterator_;
186  }
187  private:
188  OUT_ITERATOR outIterator_;
189  };
190 
191 
193 
194 } // namespace opengm
195 
196 #endif // #ifndef OPENGM_FUNCTORS_HXX
The OpenGM namespace.
Definition: config.hxx:43