00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _MPCL_UTIL_GENERAL_FUNCTIONS__
00027 #define _MPCL_UTIL_GENERAL_FUNCTIONS__
00028
00029 #include <cstdlib>
00030 #include <functional>
00031
00032
00034 namespace mpcl
00035 {
00036
00038 namespace util
00039 {
00040
00041
00042
00043
00044
00049 template <typename TTargetItem, typename TSourceItem>
00050 struct MAppend : public std::unary_function<TTargetItem, void>
00051 {
00052
00053 public:
00054
00056 const TSourceItem& rktSourceItem;
00057
00058
00059 public:
00060
00065 MAppend (const TSourceItem& rktSOURCE_ITEM) :
00066 rktSourceItem (rktSOURCE_ITEM) {}
00067
00073 void operator () (TTargetItem& rtTARGET_ITEM) const
00074 {
00075 operator << (*rtTARGET_ITEM, rktSourceItem);
00076 }
00077
00078 };
00079
00081 template < typename TItem ,
00082 typename TSizeType ,
00083 typename MOperation ,
00084 typename MFunction >
00085 struct MCompare : public std::unary_function<TItem, void>
00086 {
00087
00088 public:
00089
00091 MFunction mFunction;
00092
00094 MOperation mOperation;
00095
00097 TSizeType zComputedValue;
00098
00099
00100 public:
00101
00103 MCompare ( const TSizeType& rkzVALUE ,
00104 const MOperation& rkmOPERATION ,
00105 const MFunction& rkmFUNCTION ) :
00106 mFunction (rkmFUNCTION) ,
00107 mOperation (rkmOPERATION) ,
00108 zComputedValue (rkzVALUE) {}
00109
00111 MCompare (const TSizeType& rkzVALUE, const MFunction& rkmFUNCTION) :
00112 mFunction (rkmFUNCTION) ,
00113 mOperation () ,
00114 zComputedValue (rkzVALUE) {}
00115
00121 void operator () (const TItem& rktITEM)
00122 {
00123 TSizeType zItemResult (mFunction (rktITEM));
00124
00125 if ( mOperation (zComputedValue, zItemResult) )
00126 {
00127 zComputedValue = zItemResult;
00128 }
00129 }
00130
00135 operator TSizeType (void) const
00136 {
00137 return zComputedValue;
00138 }
00139
00140 };
00141
00143 template < typename TItem ,
00144 typename TSizeType ,
00145 typename MOperation ,
00146 typename MFunction >
00147 struct MComparePointer : public std::unary_function<TItem, void>
00148 {
00149
00150 public:
00151
00153 MFunction mFunction;
00154
00156 MOperation mOperation;
00157
00159 TSizeType zComputedValue;
00160
00161
00162 public:
00163
00165 MComparePointer ( const TSizeType& rkzVALUE ,
00166 const MOperation& rkmOPERATION ,
00167 const MFunction& rkmFUNCTION ) :
00168 mFunction (rkmFUNCTION) ,
00169 mOperation (rkmOPERATION) ,
00170 zComputedValue (rkzVALUE) {}
00171
00173 MComparePointer (const TSizeType& rkzVALUE, const MFunction& rkmFUNCTION) :
00174 mFunction (rkmFUNCTION) ,
00175 mOperation () ,
00176 zComputedValue (rkzVALUE) {}
00177
00183 void operator () (const TItem& rktITEM)
00184 {
00185 TSizeType zItemResult (mFunction (*rktITEM));
00186
00187 if ( mOperation (zComputedValue, zItemResult) )
00188 {
00189 zComputedValue = zItemResult;
00190 }
00191 }
00192
00198 void operator () (TItem& rtITEM)
00199 {
00200 TSizeType zItemResult (mFunction (*rtITEM));
00201
00202 if ( mOperation (zComputedValue, zItemResult) )
00203 {
00204 zComputedValue = zItemResult;
00205 }
00206 }
00207
00212 operator TSizeType (void) const
00213 {
00214 return zComputedValue;
00215 }
00216
00217 };
00218
00220 template < typename TItem ,
00221 typename TSizeType ,
00222 typename MFunction >
00223 struct MSum : public std::unary_function<TItem, void>
00224 {
00225
00226 private:
00227
00229 MFunction mFunction;
00230
00232 TSizeType zSum;
00233
00234
00235 public:
00236
00241 MSum (const MFunction& rkmFUNCTION) :
00242 mFunction (rkmFUNCTION) ,
00243 zSum (0) {}
00244
00249 void operator () (const TItem& rktITEM)
00250 {
00251 zSum += mFunction (rktITEM);
00252 }
00253
00258 operator TSizeType (void) const
00259 {
00260 return zSum;
00261 }
00262
00263 };
00264
00269 template < typename TItem ,
00270 typename TSizeType ,
00271 typename MFunction >
00272 struct MSumPointer : public std::unary_function<TItem, void>
00273 {
00274
00275 private:
00276
00278 MFunction mFunction;
00279
00281 TSizeType zSum;
00282
00283
00284 public:
00285
00290 MSumPointer (const MFunction& rkmFUNCTION) :
00291 mFunction (rkmFUNCTION) ,
00292 zSum (0) {}
00293
00298 void operator () (const TItem& rktITEM)
00299 {
00300 zSum += mFunction (*rktITEM);
00301 }
00302
00307 operator TSizeType (void) const
00308 {
00309 return zSum;
00310 }
00311
00312 };
00313
00315 template <typename TItem>
00316 struct MDelete : public std::unary_function<TItem, void>
00317 {
00318
00323 void operator () (const TItem& rktITEM) const
00324 {
00325 delete rktITEM;
00326 }
00327
00328 };
00329
00331 template <typename TItem>
00332 struct MRandomNumberGenerator
00333 {
00334
00352 TItem operator () (const size_t kzMAX) const
00353 {
00354 return TItem (((kzMAX + 1.0) / (RAND_MAX + 1.0)) * std::rand());
00355 }
00356
00357 };
00358
00359 }
00360
00361 }
00362
00363
00364 #endif // not _MPCL_UTIL_GENERAL_FUNCTIONS__