functors.hxx
Go to the documentation of this file.00001 #pragma once
00002 #ifndef OPENGM_FUNCTORS_HXX
00003 #define OPENGM_FUNCTORS_HXX
00004
00005 #include<cmath>
00006
00007 namespace opengm {
00008
00010
00012 template<class ACC,class VALUE_TYPE>
00013 class AccumulationFunctor{
00014 public:
00015 AccumulationFunctor(const VALUE_TYPE v)
00016 :accValue_(v) {
00017 }
00019 AccumulationFunctor()
00020 :accValue_(ACC::template neutral<VALUE_TYPE>()) {
00021 }
00024 void operator()(const VALUE_TYPE v) {
00025 ACC::op(v,accValue_);
00026 }
00028 VALUE_TYPE value() {
00029 return accValue_;
00030 }
00031 private:
00032 VALUE_TYPE accValue_;
00033 };
00034
00036 template<class T>
00037 class MinMaxFunctor{
00038 public:
00040 typedef T ValueType;
00041 MinMaxFunctor()
00042 : first_(true),
00043 min_(T()),
00044 max_(T()) {
00045 }
00046 MinMaxFunctor(T min,T max)
00047 : first_(false),
00048 min_(min),
00049 max_(max) {
00050 }
00052 ValueType min() {
00053 return min_;
00054 }
00056 ValueType max() {
00057 return max_;
00058 }
00060 void operator()(const ValueType v) {
00061 if(first_) {
00062 min_=v;
00063 max_=v;
00064 first_=false;
00065 }
00066 else{
00067 if(v<min_) {
00068 min_=v;
00069 }
00070 if(v>max_) {
00071 max_=v;
00072 }
00073 }
00074 }
00075 private:
00076 bool first_;
00077 ValueType min_;
00078 ValueType max_;
00079 };
00080
00082 template<class T>
00083 struct PowFunctor {
00086 template<class T_In>
00087 PowFunctor(T_In w)
00088 : w_(w) {
00089 }
00091 PowFunctor(const PowFunctor& other)
00092 : w_(other.w_) {
00093 }
00096 template<class T_In>
00097 const T operator()(T_In value) {
00098 return std::pow(value, w_);
00099 }
00100 T w_;
00101 };
00102
00104 template<class T_ReturnType, class T_Functor>
00105 class SwapArgumemtFunctor {
00106 public:
00109 SwapArgumemtFunctor(const SwapArgumemtFunctor& other)
00110 : functor_(other.functor_) {
00111 }
00114 SwapArgumemtFunctor(T_Functor functor)
00115 : functor_(functor) {
00116 }
00120 template<class T_A, class T_B>
00121 T_ReturnType operator()(T_A a, T_B b) {
00122 return functor_(b, a);
00123 }
00124 private:
00125 T_Functor functor_;
00126 };
00127
00129 template<class T_Scalar, class T_Functor, bool ScalarLeft>
00130 class BinaryToUnaryFunctor;
00131
00133 template<class T_Scalar, class T_Functor>
00134 class BinaryToUnaryFunctor<T_Scalar, T_Functor, true> {
00135 public:
00136 BinaryToUnaryFunctor(const BinaryToUnaryFunctor& other)
00137 : functor_(other.functor_),
00138 scalar_(other.scalar_) {
00139 }
00140 BinaryToUnaryFunctor(const T_Scalar& scalar, T_Functor& functor)
00141 :functor_(functor),
00142 scalar_(scalar)
00143 {
00144 }
00145 template<class TIN>
00146 T_Scalar operator()(const TIN in) {
00147 return functor_(scalar_, in);
00148 }
00149 private:
00150 T_Functor functor_;
00151 T_Scalar scalar_;
00152
00153 };
00154
00156 template<class T_Scalar, class T_Functor>
00157 class BinaryToUnaryFunctor<T_Scalar, T_Functor, false> {
00158 public:
00159 BinaryToUnaryFunctor(const BinaryToUnaryFunctor& other)
00160 : functor_(other.functor_),
00161 scalar_(other.scalar_) {
00162 }
00163 BinaryToUnaryFunctor(const T_Scalar& scalar, T_Functor& functor)
00164 :functor_(functor),
00165 scalar_(scalar){
00166 }
00167 template<class TIN>
00168 T_Scalar operator()(const TIN in) {
00169 return functor_(in, scalar_);
00170 }
00171 private:
00172 T_Functor functor_;
00173 T_Scalar scalar_;
00174
00175 };
00176
00177
00178 template<class OUT_ITERATOR>
00179 class CopyFunctor{
00180 public:
00181 CopyFunctor(OUT_ITERATOR iterator):outIterator_(iterator){}
00182 template<class T>
00183 void operator()(const T & value){
00184 (*outIterator_)=value;
00185 ++outIterator_;
00186 }
00187 private:
00188 OUT_ITERATOR outIterator_;
00189 };
00190
00191
00193
00194 }
00195
00196 #endif // #ifndef OPENGM_FUNCTORS_HXX