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
00027
00028 #ifndef BZ_REDUCE_H
00029 #define BZ_REDUCE_H
00030
00031 #ifndef BZ_BLITZ_H
00032 #include <blitz/blitz.h>
00033 #endif
00034
00035 #ifndef BZ_NUMTRAIT_H
00036 #include <blitz/numtrait.h>
00037 #endif
00038
00039 #ifndef BZ_NUMINQUIRE_H
00040 #include <blitz/numinquire.h>
00041 #endif
00042
00043
00044
00045
00046
00047 BZ_NAMESPACE(blitz)
00048
00049 template<typename P_sourcetype, typename P_resulttype = BZ_SUMTYPE(P_sourcetype)>
00050 class ReduceSum {
00051 public:
00052
00053 typedef P_sourcetype T_sourcetype;
00054 typedef P_resulttype T_resulttype;
00055 typedef T_resulttype T_numtype;
00056
00057 static const bool needIndex = false, needInit = false;
00058
00059 ReduceSum() { }
00060
00061 bool operator()(const T_sourcetype& x,const int=0) {
00062 sum_ += x;
00063 return true;
00064 }
00065
00066 T_resulttype result(const int) const { return sum_; }
00067
00068 void reset() { sum_ = zero(T_resulttype()); }
00069
00070 static const char* name() { return "sum"; }
00071
00072 protected:
00073
00074 T_resulttype sum_;
00075 };
00076
00077 template<typename P_sourcetype, typename P_resulttype = BZ_FLOATTYPE(P_sourcetype)>
00078 class ReduceMean {
00079 public:
00080
00081 typedef P_sourcetype T_sourcetype;
00082 typedef P_resulttype T_resulttype;
00083 typedef T_resulttype T_numtype;
00084
00085 static const bool needIndex = false, needInit = false;
00086
00087 ReduceMean() { }
00088
00089 bool operator()(const T_sourcetype& x,const int=0) {
00090 sum_ += x;
00091 return true;
00092 }
00093
00094 T_resulttype result(const int count) const { return sum_ / count; }
00095
00096 void reset() { sum_ = zero(T_resulttype()); }
00097
00098 static const char* name() { return "mean"; }
00099
00100 protected:
00101
00102 T_resulttype sum_;
00103 };
00104
00105 template<typename P_sourcetype>
00106 class ReduceMin {
00107 public:
00108
00109 typedef P_sourcetype T_sourcetype;
00110 typedef P_sourcetype T_resulttype;
00111 typedef T_resulttype T_numtype;
00112
00113 static const bool needIndex = false, needInit = false;
00114
00115 ReduceMin() { }
00116
00117 bool operator()(const T_sourcetype& x,const int=0) {
00118 if (x < min_)
00119 min_ = x;
00120 return true;
00121 }
00122
00123 T_resulttype result(const int) const { return min_; }
00124
00125 void reset() { min_ = huge(P_sourcetype()); }
00126
00127 static const char* name() { return "min"; }
00128
00129 protected:
00130
00131 T_resulttype min_;
00132 };
00133
00134 template<typename P_sourcetype>
00135 class ReduceMax {
00136 public:
00137
00138 typedef P_sourcetype T_sourcetype;
00139 typedef P_sourcetype T_resulttype;
00140 typedef T_resulttype T_numtype;
00141
00142 static const bool needIndex = false, needInit = false;
00143
00144 ReduceMax() { }
00145
00146 bool operator()(const T_sourcetype& x,const int=0) {
00147 if (x > max_)
00148 max_ = x;
00149 return true;
00150 }
00151
00152 T_resulttype result(const int) const { return max_; }
00153
00154 void reset() { max_ = neghuge(P_sourcetype()); }
00155
00156 static const char* name() { return "max"; }
00157
00158 protected:
00159
00160 T_resulttype max_;
00161 };
00162
00163 template <typename T>
00164 struct MinMaxValue {
00165 void operator=(const T& val) { min = max = val; }
00166 T min;
00167 T max;
00168 };
00169
00170 template<typename P_sourcetype>
00171 class ReduceMinMax {
00172 public:
00173
00174 typedef P_sourcetype T_sourcetype;
00175 typedef MinMaxValue<P_sourcetype> T_resulttype;
00176 typedef T_resulttype T_numtype;
00177
00178 static const bool needIndex = false, needInit = true;
00179
00180 ReduceMinMax() { }
00181
00182 bool operator()(T_sourcetype x,const int=0) {
00183 if (x > minmax_.max)
00184 minmax_.max = x;
00185 else if (x < minmax_.min)
00186 minmax_.min = x;
00187 return true;
00188 }
00189
00190 T_resulttype result(int) { return minmax_; }
00191
00192 void reset(P_sourcetype initialValue) { minmax_ = initialValue; }
00193
00194 static const char* name() { return "minmax"; }
00195
00196 protected:
00197
00198 T_resulttype minmax_;
00199 };
00200
00201 template<typename P_sourcetype>
00202 class ReduceMinIndex {
00203 public:
00204
00205 typedef P_sourcetype T_sourcetype;
00206 typedef int T_resulttype;
00207 typedef T_resulttype T_numtype;
00208
00209 static const bool needIndex = true, needInit = false;
00210
00211 ReduceMinIndex() { }
00212
00213 bool operator()(const T_sourcetype& x,const T_resulttype& index) {
00214 if (x < min_) {
00215 min_ = x;
00216 index_ = index;
00217 }
00218 return true;
00219 }
00220
00221 T_resulttype result(const int) const { return index_; }
00222
00223 void reset(const T_resulttype& index) {
00224 min_ = huge(T_sourcetype());
00225 index_ = index;
00226 }
00227
00228 static const char* name() { return "minIndex"; }
00229
00230 protected:
00231
00232 T_sourcetype min_;
00233 T_resulttype index_;
00234 };
00235
00236 template<typename P_sourcetype, int N>
00237 class ReduceMinIndexVector {
00238 public:
00239
00240 typedef P_sourcetype T_sourcetype;
00241 typedef TinyVector<int,N> T_resulttype;
00242 typedef T_resulttype T_numtype;
00243
00244 static const bool needIndex = true, needInit = false;
00245
00246 ReduceMinIndexVector() { }
00247
00248 bool operator()(const T_sourcetype& x, const T_resulttype& index) {
00249 if (x < min_) {
00250 min_ = x;
00251 index_ = index;
00252 }
00253 return true;
00254 }
00255
00256 T_resulttype result(const int) const { return index_; }
00257
00258 void reset(const T_resulttype& index) {
00259 min_ = huge(T_sourcetype());
00260 index_ = index;
00261 }
00262
00263 static const char* name() { return "minIndexVector"; }
00264
00265 protected:
00266
00267 T_sourcetype min_;
00268 T_resulttype index_;
00269 };
00270
00271 template<typename P_sourcetype>
00272 class ReduceMaxIndex {
00273 public:
00274
00275 typedef P_sourcetype T_sourcetype;
00276 typedef int T_resulttype;
00277 typedef T_resulttype T_numtype;
00278
00279 static const bool needIndex = true, needInit = false;
00280
00281 ReduceMaxIndex() { }
00282
00283 bool operator()(const T_sourcetype& x,const T_resulttype& index) {
00284 if (x > max_) {
00285 max_ = x;
00286 index_ = index;
00287 }
00288 return true;
00289 }
00290
00291 T_resulttype result(int) const { return index_; }
00292
00293 void reset(const T_resulttype& index) {
00294 max_ = neghuge(T_sourcetype());
00295 index_ = index;
00296 }
00297
00298 static const char* name() { return "maxIndex"; }
00299
00300 protected:
00301
00302 T_sourcetype max_;
00303 T_resulttype index_;
00304 };
00305
00306 template<typename P_sourcetype, int N_rank>
00307 class ReduceMaxIndexVector {
00308 public:
00309
00310 typedef P_sourcetype T_sourcetype;
00311 typedef TinyVector<int,N_rank> T_resulttype;
00312 typedef T_resulttype T_numtype;
00313
00314 static const bool needIndex = true, needInit = false;
00315
00316 ReduceMaxIndexVector() { }
00317
00318 bool operator()(const T_sourcetype& x, const T_resulttype& index) {
00319 if (x > max_) {
00320 max_ = x;
00321 index_ = index;
00322 }
00323 return true;
00324 }
00325
00326 T_resulttype result(const int) const { return index_; }
00327
00328 void reset(const T_resulttype& index) {
00329 max_ = neghuge(T_sourcetype());
00330 index_ = index;
00331 }
00332
00333 static const char* name() { return "maxIndexVector"; }
00334
00335 protected:
00336
00337 T_sourcetype max_;
00338 T_resulttype index_;
00339 };
00340
00341 template<typename P_sourcetype>
00342 class ReduceFirst {
00343 public:
00344
00345 typedef P_sourcetype T_sourcetype;
00346 typedef int T_resulttype;
00347 typedef T_resulttype T_numtype;
00348
00349 static const bool needIndex = false, needInit = false;
00350
00351 ReduceFirst() { }
00352
00353 bool operator()(const T_sourcetype& x,const T_resulttype& index) {
00354 if (x) {
00355 index_ = index;
00356 return false;
00357 } else
00358 return true;
00359 }
00360
00361 T_resulttype result(const int) const { return index_; }
00362
00363 void reset() { index_ = tiny(int()); }
00364
00365 static const char* name() { return "first"; }
00366
00367 protected:
00368
00369 T_resulttype index_;
00370 };
00371
00372 template<typename P_sourcetype>
00373 class ReduceLast {
00374 public:
00375
00376 typedef P_sourcetype T_sourcetype;
00377 typedef int T_resulttype;
00378 typedef T_resulttype T_numtype;
00379
00380 static const bool needIndex = false, needInit = false;
00381
00382 ReduceLast() { }
00383
00384 bool operator()(const T_sourcetype& x,const T_resulttype& index) {
00385 if (x) {
00386 index_ = index;
00387 return true;
00388 } else
00389 return true;
00390 }
00391
00392 T_resulttype result(const int) const { return index_; }
00393
00394 void reset() { index_ = huge(int()); }
00395
00396 static const char* name() { return "last"; }
00397
00398 protected:
00399
00400 T_resulttype index_;
00401 };
00402
00403 template<typename P_sourcetype, typename P_resulttype = BZ_SUMTYPE(P_sourcetype)>
00404 class ReduceProduct {
00405 public:
00406
00407 typedef P_sourcetype T_sourcetype;
00408 typedef P_resulttype T_resulttype;
00409 typedef T_resulttype T_numtype;
00410
00411 static const bool needIndex = false, needInit = false;
00412
00413 ReduceProduct() { }
00414
00415 bool operator()(const T_sourcetype& x,const int=0) {
00416 product_ *= x;
00417 return true;
00418 }
00419
00420 T_resulttype result(const int) const { return product_; }
00421
00422 void reset() { product_ = one(T_resulttype()); }
00423
00424 static const char* name() { return "product"; }
00425
00426 protected:
00427
00428 T_resulttype product_;
00429 };
00430
00431 template<typename P_sourcetype>
00432 class ReduceCount {
00433 public:
00434
00435 typedef P_sourcetype T_sourcetype;
00436 typedef int T_resulttype;
00437 typedef T_resulttype T_numtype;
00438
00439 static const bool needIndex = false, needInit = false;
00440
00441 ReduceCount() { }
00442
00443 bool operator()(const T_sourcetype& x,const int=0) {
00444 if (bool(x))
00445 ++count_;
00446 return true;
00447 }
00448
00449 T_resulttype result(const int) const { return count_; }
00450
00451 void reset() { count_ = zero(T_resulttype()); }
00452
00453 static const char* name() { return "count"; }
00454
00455 protected:
00456
00457 T_resulttype count_;
00458 };
00459
00460 template<typename P_sourcetype>
00461 class ReduceAny {
00462 public:
00463
00464 typedef P_sourcetype T_sourcetype;
00465 typedef bool T_resulttype;
00466 typedef T_resulttype T_numtype;
00467
00468 static const bool needIndex = false, needInit = false;
00469
00470 ReduceAny() { }
00471
00472 bool operator()(const T_sourcetype& x,const int=0) {
00473 if (bool(x)) {
00474 any_ = true;
00475 return false;
00476 }
00477
00478 return true;
00479 }
00480
00481 T_resulttype result(const int) const { return any_; }
00482
00483 void reset() { any_ = false; }
00484
00485 static const char* name() { return "any"; }
00486
00487 protected:
00488
00489 T_resulttype any_;
00490 };
00491
00492 template<typename P_sourcetype>
00493 class ReduceAll {
00494 public:
00495
00496 typedef P_sourcetype T_sourcetype;
00497 typedef bool T_resulttype;
00498 typedef T_resulttype T_numtype;
00499
00500 static const bool needIndex = false, needInit = false;
00501
00502 ReduceAll() { }
00503
00504 bool operator()(const T_sourcetype& x,const int=0) {
00505 if (!bool(x)) {
00506 all_ = false;
00507 return false;
00508 } else
00509 return true;
00510 }
00511
00512 T_resulttype result(const int) const { return all_; }
00513
00514 void reset() { all_ = true; }
00515
00516 static const char* name() { return "all"; }
00517
00518 protected:
00519
00520 T_resulttype all_;
00521 };
00522
00523 BZ_NAMESPACE_END
00524
00525 #endif // BZ_REDUCE_H