| [221] | 1 | /***************************************************************************
 | 
|---|
 | 2 |  * blitz/reduce.h        Reduction operators: sum, mean, min, max,
 | 
|---|
 | 3 |  *                       minIndex, maxIndex, product, count, any, all
 | 
|---|
 | 4 |  *
 | 
|---|
 | 5 |  * $Id: reduce.h,v 1.1.1.1 1999-04-09 17:59:02 ansari Exp $
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  * This program is free software; you can redistribute it and/or
 | 
|---|
 | 10 |  * modify it under the terms of the GNU General Public License
 | 
|---|
 | 11 |  * as published by the Free Software Foundation; either version 2
 | 
|---|
 | 12 |  * of the License, or (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  * This program is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  * GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  * Suggestions:          blitz-suggest@cybervision.com
 | 
|---|
 | 20 |  * Bugs:                 blitz-bugs@cybervision.com
 | 
|---|
 | 21 |  *
 | 
|---|
 | 22 |  * For more information, please see the Blitz++ Home Page:
 | 
|---|
 | 23 |  *    http://seurat.uwaterloo.ca/blitz/
 | 
|---|
 | 24 |  *
 | 
|---|
 | 25 |  ***************************************************************************
 | 
|---|
 | 26 |  * $Log: not supported by cvs2svn $
 | 
|---|
 | 27 |  * Revision 1.2  1998/03/14 00:04:47  tveldhui
 | 
|---|
 | 28 |  * 0.2-alpha-05
 | 
|---|
 | 29 |  *
 | 
|---|
 | 30 |  * Revision 1.1  1997/07/16 14:51:20  tveldhui
 | 
|---|
 | 31 |  * Update: Alpha release 0.2 (Arrays)
 | 
|---|
 | 32 |  *
 | 
|---|
 | 33 |  */
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | #ifndef BZ_REDUCE_H
 | 
|---|
 | 36 | #define BZ_REDUCE_H
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | #ifndef BZ_BLITZ_H
 | 
|---|
 | 39 |  #include <blitz/blitz.h>
 | 
|---|
 | 40 | #endif
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | #ifndef BZ_NUMTRAIT_H
 | 
|---|
 | 43 |  #include <blitz/numtrait.h>
 | 
|---|
 | 44 | #endif
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 | #ifdef BZ_HAVE_NUMERIC_LIMITS
 | 
|---|
 | 47 | #ifndef BZ_NUMINQUIRE_H
 | 
|---|
 | 48 |  #include <blitz/numinquire.h>
 | 
|---|
 | 49 | #endif
 | 
|---|
 | 50 | #endif
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 | BZ_NAMESPACE(blitz)
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | template<class P_sourcetype, class P_resulttype = BZ_SUMTYPE(P_sourcetype)>
 | 
|---|
 | 55 | class ReduceSum {
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 | public:
 | 
|---|
 | 58 |     typedef P_sourcetype T_sourcetype;
 | 
|---|
 | 59 |     typedef P_resulttype T_resulttype;
 | 
|---|
 | 60 |     typedef T_resulttype T_numtype;
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 |     enum { needIndex = 0, canProvideInitialValue = 1 };
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 |     ReduceSum()
 | 
|---|
 | 65 |     { reset(); }
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 |     ReduceSum(T_resulttype initialValue)
 | 
|---|
 | 68 |     { sum_ = initialValue; }
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 |     bool operator()(T_sourcetype x)
 | 
|---|
 | 71 |     { 
 | 
|---|
 | 72 |         sum_ += x; 
 | 
|---|
 | 73 |         return true;
 | 
|---|
 | 74 |     }
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 |     bool operator()(T_sourcetype x, int)
 | 
|---|
 | 77 |     { 
 | 
|---|
 | 78 |         sum_ += x; 
 | 
|---|
 | 79 |         return true;
 | 
|---|
 | 80 |     }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |     T_resulttype result(int)
 | 
|---|
 | 83 |     { return sum_; }
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 | #ifdef BZ_HAVE_NUMERIC_LIMITS
 | 
|---|
 | 86 |     void reset()
 | 
|---|
 | 87 |     { sum_ = zero(T_resulttype()); }
 | 
|---|
 | 88 | #else
 | 
|---|
 | 89 |     void reset()
 | 
|---|
 | 90 |     { sum_ = 0; }
 | 
|---|
 | 91 | #endif
 | 
|---|
 | 92 | 
 | 
|---|
 | 93 |     void reset(T_resulttype initialValue)
 | 
|---|
 | 94 |     { sum_ = initialValue; }
 | 
|---|
 | 95 |  
 | 
|---|
 | 96 |     static const char* name()
 | 
|---|
 | 97 |     { return "sum"; }
 | 
|---|
 | 98 |  
 | 
|---|
 | 99 | protected:
 | 
|---|
 | 100 |     T_resulttype sum_;
 | 
|---|
 | 101 | };
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 | template<class P_sourcetype, class P_resulttype = BZ_FLOATTYPE(P_sourcetype)>
 | 
|---|
 | 104 | class ReduceMean {
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 | public:
 | 
|---|
 | 107 |     typedef P_sourcetype T_sourcetype;
 | 
|---|
 | 108 |     typedef P_resulttype T_resulttype;
 | 
|---|
 | 109 |     typedef T_resulttype T_numtype;
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 |     enum { needIndex = 0, canProvideInitialValue = 0 };
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |     ReduceMean()
 | 
|---|
 | 114 |     { reset(); }
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 |     ReduceMean(T_resulttype)
 | 
|---|
 | 117 |     { 
 | 
|---|
 | 118 |         BZPRECHECK(0, "Provided an initial value for ReduceMean");
 | 
|---|
 | 119 |         reset();
 | 
|---|
 | 120 |     }
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 |     bool operator()(T_sourcetype x)
 | 
|---|
 | 123 |     { 
 | 
|---|
 | 124 |         sum_ += x; 
 | 
|---|
 | 125 |         return true;
 | 
|---|
 | 126 |     }
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 |     bool operator()(T_sourcetype x, int)
 | 
|---|
 | 129 |     { 
 | 
|---|
 | 130 |         sum_ += x; 
 | 
|---|
 | 131 |         return true;
 | 
|---|
 | 132 |     }
 | 
|---|
 | 133 | 
 | 
|---|
 | 134 |     T_resulttype result(int count)
 | 
|---|
 | 135 |     { return sum_ / count; }
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 | #ifdef BZ_HAVE_NUMERIC_LIMITS
 | 
|---|
 | 138 |     void reset()
 | 
|---|
 | 139 |     { sum_ = zero(T_resulttype()); }
 | 
|---|
 | 140 | #else
 | 
|---|
 | 141 |     void reset()
 | 
|---|
 | 142 |     { sum_ = 0; }
 | 
|---|
 | 143 | #endif
 | 
|---|
 | 144 | 
 | 
|---|
 | 145 |     void reset(T_resulttype)
 | 
|---|
 | 146 |     { 
 | 
|---|
 | 147 |         BZPRECHECK(0, "Provided an initial value for ReduceMean");
 | 
|---|
 | 148 |         reset();
 | 
|---|
 | 149 |     }
 | 
|---|
 | 150 | 
 | 
|---|
 | 151 |     static const char* name() 
 | 
|---|
 | 152 |     { return "mean"; }
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 | protected:
 | 
|---|
 | 155 |     T_resulttype sum_;
 | 
|---|
 | 156 | };
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | template<class P_sourcetype>
 | 
|---|
 | 159 | class ReduceMin {
 | 
|---|
 | 160 | 
 | 
|---|
 | 161 | public:
 | 
|---|
 | 162 |     typedef P_sourcetype T_sourcetype;
 | 
|---|
 | 163 |     typedef P_sourcetype T_resulttype;
 | 
|---|
 | 164 |     typedef T_resulttype T_numtype;
 | 
|---|
 | 165 | 
 | 
|---|
 | 166 |     enum { needIndex = 0, canProvideInitialValue = 1 };
 | 
|---|
 | 167 | 
 | 
|---|
 | 168 |     ReduceMin()
 | 
|---|
 | 169 |     { reset(); }
 | 
|---|
 | 170 | 
 | 
|---|
 | 171 |     ReduceMin(T_resulttype min)
 | 
|---|
 | 172 |     {
 | 
|---|
 | 173 |         min_ = min;
 | 
|---|
 | 174 |     }
 | 
|---|
 | 175 | 
 | 
|---|
 | 176 |     bool operator()(T_sourcetype x)
 | 
|---|
 | 177 |     { 
 | 
|---|
 | 178 |         if (x < min_)
 | 
|---|
 | 179 |             min_ = x;
 | 
|---|
 | 180 |         return true;
 | 
|---|
 | 181 |     }
 | 
|---|
 | 182 | 
 | 
|---|
 | 183 |     bool operator()(T_sourcetype x, int)
 | 
|---|
 | 184 |     {
 | 
|---|
 | 185 |         if (x < min_)
 | 
|---|
 | 186 |             min_ = x;
 | 
|---|
 | 187 |         return true;
 | 
|---|
 | 188 |     }
 | 
|---|
 | 189 | 
 | 
|---|
 | 190 |     T_resulttype result(int)
 | 
|---|
 | 191 |     { return min_; }
 | 
|---|
 | 192 | 
 | 
|---|
 | 193 | #ifdef BZ_HAVE_NUMERIC_LIMITS
 | 
|---|
 | 194 |     void reset()
 | 
|---|
 | 195 |     { min_ = huge(P_sourcetype()); }
 | 
|---|
 | 196 | #else
 | 
|---|
 | 197 |     void reset()
 | 
|---|
 | 198 |     {
 | 
|---|
 | 199 |         BZPRECHECK(0, "ReduceMin requires <limits> from the ISO/ANSI C++ standard");
 | 
|---|
 | 200 |     }
 | 
|---|
 | 201 | #endif
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 |     void reset(T_resulttype initialValue)
 | 
|---|
 | 204 |     { min_ = initialValue; }
 | 
|---|
 | 205 | 
 | 
|---|
 | 206 |     static const char* name()
 | 
|---|
 | 207 |     { return "min"; }
 | 
|---|
 | 208 | 
 | 
|---|
 | 209 | protected:
 | 
|---|
 | 210 |     T_resulttype min_;
 | 
|---|
 | 211 | };
 | 
|---|
 | 212 | 
 | 
|---|
 | 213 | template<class P_sourcetype>
 | 
|---|
 | 214 | class ReduceMax {
 | 
|---|
 | 215 | 
 | 
|---|
 | 216 | public:
 | 
|---|
 | 217 |     typedef P_sourcetype T_sourcetype;
 | 
|---|
 | 218 |     typedef P_sourcetype T_resulttype;
 | 
|---|
 | 219 |     typedef T_resulttype T_numtype;
 | 
|---|
 | 220 | 
 | 
|---|
 | 221 |     enum { needIndex = 0, canProvideInitialValue = 1 };
 | 
|---|
 | 222 | 
 | 
|---|
 | 223 |     ReduceMax()
 | 
|---|
 | 224 |     { reset(); }
 | 
|---|
 | 225 | 
 | 
|---|
 | 226 |     ReduceMax(T_resulttype max)
 | 
|---|
 | 227 |     {
 | 
|---|
 | 228 |         max_ = max;
 | 
|---|
 | 229 |     }
 | 
|---|
 | 230 | 
 | 
|---|
 | 231 |     bool operator()(T_sourcetype x)
 | 
|---|
 | 232 |     {
 | 
|---|
 | 233 |         if (x > max_)
 | 
|---|
 | 234 |             max_ = x;
 | 
|---|
 | 235 |         return true;
 | 
|---|
 | 236 |     }
 | 
|---|
 | 237 | 
 | 
|---|
 | 238 |     bool operator()(T_sourcetype x, int)
 | 
|---|
 | 239 |     {
 | 
|---|
 | 240 |         if (x > max_)
 | 
|---|
 | 241 |             max_ = x;
 | 
|---|
 | 242 |         return true;
 | 
|---|
 | 243 |     }
 | 
|---|
 | 244 | 
 | 
|---|
 | 245 |     T_resulttype result(int)
 | 
|---|
 | 246 |     { return max_; }
 | 
|---|
 | 247 | 
 | 
|---|
 | 248 | #ifdef BZ_HAVE_NUMERIC_LIMITS
 | 
|---|
 | 249 |     void reset()
 | 
|---|
 | 250 |     { max_ = tiny(P_sourcetype()); }
 | 
|---|
 | 251 | #else
 | 
|---|
 | 252 |     void reset()
 | 
|---|
 | 253 |     { 
 | 
|---|
 | 254 |         BZPRECHECK(0, "ReduceMax requires <limits> from the ISO/ANSI C++ standard");
 | 
|---|
 | 255 |     }
 | 
|---|
 | 256 | #endif
 | 
|---|
 | 257 | 
 | 
|---|
 | 258 |     void reset(T_resulttype initialValue)
 | 
|---|
 | 259 |     { max_ = initialValue; }
 | 
|---|
 | 260 | 
 | 
|---|
 | 261 |     static const char* name()
 | 
|---|
 | 262 |     { return "max"; }
 | 
|---|
 | 263 | 
 | 
|---|
 | 264 | protected:
 | 
|---|
 | 265 |     T_resulttype max_;
 | 
|---|
 | 266 | };
 | 
|---|
 | 267 | 
 | 
|---|
 | 268 | template<class P_sourcetype>
 | 
|---|
 | 269 | class ReduceMinIndex {
 | 
|---|
 | 270 | 
 | 
|---|
 | 271 | public:
 | 
|---|
 | 272 |     typedef P_sourcetype T_sourcetype;
 | 
|---|
 | 273 |     typedef int          T_resulttype;
 | 
|---|
 | 274 |     typedef T_resulttype T_numtype;
 | 
|---|
 | 275 | 
 | 
|---|
 | 276 |     enum { needIndex = 1, canProvideInitialValue = 0 };
 | 
|---|
 | 277 | 
 | 
|---|
 | 278 |     ReduceMinIndex()
 | 
|---|
 | 279 |     { reset(); }
 | 
|---|
 | 280 | 
 | 
|---|
 | 281 |     ReduceMinIndex(T_resulttype min)
 | 
|---|
 | 282 |     {
 | 
|---|
 | 283 |         reset(min);
 | 
|---|
 | 284 |     }
 | 
|---|
 | 285 | 
 | 
|---|
 | 286 |     bool operator()(T_sourcetype x)
 | 
|---|
 | 287 |     {
 | 
|---|
 | 288 |         BZPRECONDITION(0);
 | 
|---|
 | 289 |         return false;
 | 
|---|
 | 290 |     }
 | 
|---|
 | 291 | 
 | 
|---|
 | 292 |     bool operator()(T_sourcetype x, int index)
 | 
|---|
 | 293 |     {
 | 
|---|
 | 294 |         if (x < min_)
 | 
|---|
 | 295 |         {
 | 
|---|
 | 296 |             min_ = x;
 | 
|---|
 | 297 |             index_ = index;
 | 
|---|
 | 298 |         }
 | 
|---|
 | 299 |         return true;
 | 
|---|
 | 300 |     }
 | 
|---|
 | 301 | 
 | 
|---|
 | 302 |     T_resulttype result(int)
 | 
|---|
 | 303 |     { return index_; }
 | 
|---|
 | 304 | 
 | 
|---|
 | 305 | #ifdef BZ_HAVE_NUMERIC_LIMITS
 | 
|---|
 | 306 |     void reset()
 | 
|---|
 | 307 |     { 
 | 
|---|
 | 308 |         min_ = huge(T_sourcetype());
 | 
|---|
 | 309 |         index_ = tiny(int());        
 | 
|---|
 | 310 |     }
 | 
|---|
 | 311 | #else
 | 
|---|
 | 312 |     void reset()
 | 
|---|
 | 313 |     {
 | 
|---|
 | 314 |         BZPRECHECK(0, "ReduceMinIndex requires <limits> from the ISO/ANSI C++ standard");
 | 
|---|
 | 315 |     }
 | 
|---|
 | 316 | #endif
 | 
|---|
 | 317 | 
 | 
|---|
 | 318 |     void reset(T_resulttype)
 | 
|---|
 | 319 |     { 
 | 
|---|
 | 320 |         BZPRECHECK(0, "Provided initial value for ReduceMinIndex");
 | 
|---|
 | 321 |         reset();
 | 
|---|
 | 322 |     }
 | 
|---|
 | 323 | 
 | 
|---|
 | 324 |     static const char* name()
 | 
|---|
 | 325 |     { return "minIndex"; }
 | 
|---|
 | 326 | 
 | 
|---|
 | 327 | protected:
 | 
|---|
 | 328 |     T_sourcetype min_;
 | 
|---|
 | 329 |     int index_;
 | 
|---|
 | 330 | };
 | 
|---|
 | 331 | 
 | 
|---|
 | 332 | template<class P_sourcetype>
 | 
|---|
 | 333 | class ReduceMaxIndex {
 | 
|---|
 | 334 | 
 | 
|---|
 | 335 | public:
 | 
|---|
 | 336 |     typedef P_sourcetype T_sourcetype;
 | 
|---|
 | 337 |     typedef int          T_resulttype;
 | 
|---|
 | 338 |     typedef T_resulttype T_numtype;
 | 
|---|
 | 339 | 
 | 
|---|
 | 340 |     enum { needIndex = 1, canProvideInitialValue = 0 };
 | 
|---|
 | 341 | 
 | 
|---|
 | 342 |     ReduceMaxIndex()
 | 
|---|
 | 343 |     { reset(); }
 | 
|---|
 | 344 | 
 | 
|---|
 | 345 |     ReduceMaxIndex(T_resulttype max)
 | 
|---|
 | 346 |     {
 | 
|---|
 | 347 |         reset(max);
 | 
|---|
 | 348 |     }
 | 
|---|
 | 349 | 
 | 
|---|
 | 350 |     bool operator()(T_sourcetype x)
 | 
|---|
 | 351 |     {
 | 
|---|
 | 352 |         BZPRECONDITION(0);
 | 
|---|
 | 353 |         return false;
 | 
|---|
 | 354 |     }
 | 
|---|
 | 355 | 
 | 
|---|
 | 356 |     bool operator()(T_sourcetype x, int index)
 | 
|---|
 | 357 |     {
 | 
|---|
 | 358 |         if (x > max_)
 | 
|---|
 | 359 |         {
 | 
|---|
 | 360 |             max_ = x;
 | 
|---|
 | 361 |             index_ = index;
 | 
|---|
 | 362 |         }
 | 
|---|
 | 363 |         return true;
 | 
|---|
 | 364 |     }
 | 
|---|
 | 365 | 
 | 
|---|
 | 366 |     T_resulttype result(int)
 | 
|---|
 | 367 |     { return index_; }
 | 
|---|
 | 368 | 
 | 
|---|
 | 369 | #ifdef BZ_HAVE_NUMERIC_LIMITS
 | 
|---|
 | 370 |     void reset()
 | 
|---|
 | 371 |     {
 | 
|---|
 | 372 |         max_ = tiny(T_sourcetype());
 | 
|---|
 | 373 |         index_ = tiny(int());
 | 
|---|
 | 374 |     }
 | 
|---|
 | 375 | #else
 | 
|---|
 | 376 |     void reset()
 | 
|---|
 | 377 |     {
 | 
|---|
 | 378 |         BZPRECHECK(0, "ReduceMaxIndex requires <limits> from the ISO/ANSI C++ standard");
 | 
|---|
 | 379 |     }
 | 
|---|
 | 380 | #endif
 | 
|---|
 | 381 | 
 | 
|---|
 | 382 |     void reset(T_resulttype)
 | 
|---|
 | 383 |     {
 | 
|---|
 | 384 |         BZPRECHECK(0, "Provided initial value for ReduceMaxIndex");
 | 
|---|
 | 385 |         reset();
 | 
|---|
 | 386 |     }
 | 
|---|
 | 387 | 
 | 
|---|
 | 388 |     static const char* name()
 | 
|---|
 | 389 |     { return "maxIndex"; }
 | 
|---|
 | 390 | 
 | 
|---|
 | 391 | protected:
 | 
|---|
 | 392 |     T_sourcetype max_;
 | 
|---|
 | 393 |     int index_;
 | 
|---|
 | 394 | };
 | 
|---|
 | 395 | 
 | 
|---|
 | 396 | template<class P_sourcetype>
 | 
|---|
 | 397 | class ReduceFirst {
 | 
|---|
 | 398 | 
 | 
|---|
 | 399 | public:
 | 
|---|
 | 400 |     typedef P_sourcetype T_sourcetype;
 | 
|---|
 | 401 |     typedef int          T_resulttype;
 | 
|---|
 | 402 |     typedef T_resulttype T_numtype;
 | 
|---|
 | 403 | 
 | 
|---|
 | 404 |     enum { needIndex = 1, canProvideInitialValue = 0 };
 | 
|---|
 | 405 | 
 | 
|---|
 | 406 |     ReduceFirst()
 | 
|---|
 | 407 |     { reset(); }
 | 
|---|
 | 408 | 
 | 
|---|
 | 409 |     ReduceFirst(T_resulttype)
 | 
|---|
 | 410 |     {
 | 
|---|
 | 411 |         BZPRECONDITION(0);
 | 
|---|
 | 412 |     }
 | 
|---|
 | 413 | 
 | 
|---|
 | 414 |     bool operator()(T_sourcetype x)
 | 
|---|
 | 415 |     {
 | 
|---|
 | 416 |         BZPRECONDITION(0);
 | 
|---|
 | 417 |         return false;
 | 
|---|
 | 418 |     }
 | 
|---|
 | 419 | 
 | 
|---|
 | 420 |     bool operator()(T_sourcetype x, int index)
 | 
|---|
 | 421 |     {
 | 
|---|
 | 422 |         if (x)
 | 
|---|
 | 423 |         {
 | 
|---|
 | 424 |             index_ = index;
 | 
|---|
 | 425 |             return false;
 | 
|---|
 | 426 |         }
 | 
|---|
 | 427 |         else
 | 
|---|
 | 428 |             return true;
 | 
|---|
 | 429 |     }
 | 
|---|
 | 430 | 
 | 
|---|
 | 431 |     T_resulttype result(int)
 | 
|---|
 | 432 |     { return index_; }
 | 
|---|
 | 433 | 
 | 
|---|
 | 434 | #ifdef BZ_HAVE_NUMERIC_LIMITS
 | 
|---|
 | 435 |     void reset()
 | 
|---|
 | 436 |     {
 | 
|---|
 | 437 |         index_ = tiny(int());
 | 
|---|
 | 438 |     }
 | 
|---|
 | 439 | #else
 | 
|---|
 | 440 |     void reset()
 | 
|---|
 | 441 |     {
 | 
|---|
 | 442 |         index_ = INT_MIN;
 | 
|---|
 | 443 |     }
 | 
|---|
 | 444 | #endif
 | 
|---|
 | 445 | 
 | 
|---|
 | 446 |     void reset(T_resulttype)
 | 
|---|
 | 447 |     {
 | 
|---|
 | 448 |         BZPRECHECK(0, "Provided initial value for ReduceFirst");
 | 
|---|
 | 449 |         reset();
 | 
|---|
 | 450 |     }
 | 
|---|
 | 451 | 
 | 
|---|
 | 452 |     static const char* name()
 | 
|---|
 | 453 |     { return "first"; }
 | 
|---|
 | 454 | 
 | 
|---|
 | 455 | protected:
 | 
|---|
 | 456 |     int index_;
 | 
|---|
 | 457 | };
 | 
|---|
 | 458 | 
 | 
|---|
 | 459 | 
 | 
|---|
 | 460 | template<class P_sourcetype, class P_resulttype = BZ_SUMTYPE(P_sourcetype)>
 | 
|---|
 | 461 | class ReduceProduct {
 | 
|---|
 | 462 | 
 | 
|---|
 | 463 | public:
 | 
|---|
 | 464 |     typedef P_sourcetype T_sourcetype;
 | 
|---|
 | 465 |     typedef P_resulttype T_resulttype;
 | 
|---|
 | 466 |     typedef T_resulttype T_numtype;
 | 
|---|
 | 467 | 
 | 
|---|
 | 468 |     enum { needIndex = 0, canProvideInitialValue = 1 };
 | 
|---|
 | 469 | 
 | 
|---|
 | 470 | #ifdef BZ_HAVE_NUMERIC_LIMITS
 | 
|---|
 | 471 |     ReduceProduct()
 | 
|---|
 | 472 |     { product_ = one(T_resulttype()); }
 | 
|---|
 | 473 | #else
 | 
|---|
 | 474 |     ReduceProduct()
 | 
|---|
 | 475 |     { product_ = 1; }
 | 
|---|
 | 476 | #endif
 | 
|---|
 | 477 | 
 | 
|---|
 | 478 |     ReduceProduct(T_resulttype initialValue)
 | 
|---|
 | 479 |     { product_ = initialValue; }
 | 
|---|
 | 480 | 
 | 
|---|
 | 481 |     bool operator()(T_sourcetype x)
 | 
|---|
 | 482 |     { 
 | 
|---|
 | 483 |         product_ *= x; 
 | 
|---|
 | 484 |         return true;
 | 
|---|
 | 485 |     }
 | 
|---|
 | 486 | 
 | 
|---|
 | 487 |     bool operator()(T_sourcetype x, int)
 | 
|---|
 | 488 |     { 
 | 
|---|
 | 489 |         product_ *= x; 
 | 
|---|
 | 490 |         return true;
 | 
|---|
 | 491 |     }
 | 
|---|
 | 492 | 
 | 
|---|
 | 493 |     T_resulttype result(int)
 | 
|---|
 | 494 |     { return product_; }
 | 
|---|
 | 495 | 
 | 
|---|
 | 496 | #ifdef BZ_HAVE_NUMERIC_LIMITS
 | 
|---|
 | 497 |     void reset()
 | 
|---|
 | 498 |     { product_ = one(T_resulttype()); }
 | 
|---|
 | 499 | #else
 | 
|---|
 | 500 |     void reset()
 | 
|---|
 | 501 |     { product_ = T_resulttype(1.0); }
 | 
|---|
 | 502 | #endif
 | 
|---|
 | 503 | 
 | 
|---|
 | 504 |     void reset(T_resulttype initialValue)
 | 
|---|
 | 505 |     { product_ = initialValue; }
 | 
|---|
 | 506 | 
 | 
|---|
 | 507 |     static const char* name()
 | 
|---|
 | 508 |     { return "product"; }
 | 
|---|
 | 509 | 
 | 
|---|
 | 510 | protected:
 | 
|---|
 | 511 |     T_resulttype product_;
 | 
|---|
 | 512 | };
 | 
|---|
 | 513 | 
 | 
|---|
 | 514 | template<class P_sourcetype>
 | 
|---|
 | 515 | class ReduceCount {
 | 
|---|
 | 516 | 
 | 
|---|
 | 517 | public:
 | 
|---|
 | 518 |     typedef P_sourcetype T_sourcetype;
 | 
|---|
 | 519 |     typedef int          T_resulttype;
 | 
|---|
 | 520 |     typedef T_resulttype T_numtype;
 | 
|---|
 | 521 | 
 | 
|---|
 | 522 |     enum { needIndex = 0, canProvideInitialValue = 1 };
 | 
|---|
 | 523 | 
 | 
|---|
 | 524 |     ReduceCount()
 | 
|---|
 | 525 |     { reset(); }
 | 
|---|
 | 526 | 
 | 
|---|
 | 527 |     ReduceCount(T_resulttype count)
 | 
|---|
 | 528 |     {
 | 
|---|
 | 529 |         count_ = count;
 | 
|---|
 | 530 |     }
 | 
|---|
 | 531 | 
 | 
|---|
 | 532 |     bool operator()(T_sourcetype x)
 | 
|---|
 | 533 |     {
 | 
|---|
 | 534 |         if (x)
 | 
|---|
 | 535 |             ++count_;
 | 
|---|
 | 536 |         return true;
 | 
|---|
 | 537 |     }
 | 
|---|
 | 538 | 
 | 
|---|
 | 539 |     bool operator()(T_sourcetype x, int)
 | 
|---|
 | 540 |     {
 | 
|---|
 | 541 |         if (x)
 | 
|---|
 | 542 |             ++count_;
 | 
|---|
 | 543 |         return true;
 | 
|---|
 | 544 |     }
 | 
|---|
 | 545 | 
 | 
|---|
 | 546 |     T_resulttype result(int)
 | 
|---|
 | 547 |     { return count_; }
 | 
|---|
 | 548 | 
 | 
|---|
 | 549 | #ifdef BZ_HAVE_NUMERIC_LIMITS
 | 
|---|
 | 550 |     void reset()
 | 
|---|
 | 551 |     { count_ = zero(T_resulttype()); }
 | 
|---|
 | 552 | #else
 | 
|---|
 | 553 |     void reset()
 | 
|---|
 | 554 |     { count_ = 0; }
 | 
|---|
 | 555 | #endif
 | 
|---|
 | 556 | 
 | 
|---|
 | 557 |     void reset(T_resulttype initialValue)
 | 
|---|
 | 558 |     { count_ = initialValue; }
 | 
|---|
 | 559 | 
 | 
|---|
 | 560 |     static const char* name()
 | 
|---|
 | 561 |     { return "count"; }
 | 
|---|
 | 562 | 
 | 
|---|
 | 563 | protected:
 | 
|---|
 | 564 |     T_resulttype count_;
 | 
|---|
 | 565 | };
 | 
|---|
 | 566 | 
 | 
|---|
 | 567 | template<class P_sourcetype>
 | 
|---|
 | 568 | class ReduceAny {
 | 
|---|
 | 569 | 
 | 
|---|
 | 570 | public:
 | 
|---|
 | 571 |     typedef P_sourcetype T_sourcetype;
 | 
|---|
 | 572 |     typedef _bz_bool     T_resulttype;
 | 
|---|
 | 573 |     typedef T_resulttype T_numtype;
 | 
|---|
 | 574 | 
 | 
|---|
 | 575 |     enum { needIndex = 0, canProvideInitialValue = 0 };
 | 
|---|
 | 576 | 
 | 
|---|
 | 577 |     ReduceAny()
 | 
|---|
 | 578 |     { reset(); }
 | 
|---|
 | 579 | 
 | 
|---|
 | 580 |     ReduceAny(T_resulttype initialValue)
 | 
|---|
 | 581 |     {
 | 
|---|
 | 582 |         reset(initialValue);
 | 
|---|
 | 583 |     }
 | 
|---|
 | 584 | 
 | 
|---|
 | 585 |     bool operator()(T_sourcetype x)
 | 
|---|
 | 586 |     {
 | 
|---|
 | 587 |         if (x)
 | 
|---|
 | 588 |         {
 | 
|---|
 | 589 |             any_ = _bz_true;
 | 
|---|
 | 590 |             return false;
 | 
|---|
 | 591 |         }
 | 
|---|
 | 592 | 
 | 
|---|
 | 593 |         return true;
 | 
|---|
 | 594 |     }
 | 
|---|
 | 595 | 
 | 
|---|
 | 596 |     bool operator()(T_sourcetype x, int)
 | 
|---|
 | 597 |     {
 | 
|---|
 | 598 |         if (x)
 | 
|---|
 | 599 |         {
 | 
|---|
 | 600 |             any_ = _bz_true;
 | 
|---|
 | 601 |             return false;
 | 
|---|
 | 602 |         }
 | 
|---|
 | 603 | 
 | 
|---|
 | 604 |         return true;
 | 
|---|
 | 605 |     }
 | 
|---|
 | 606 | 
 | 
|---|
 | 607 |     T_resulttype result(int)
 | 
|---|
 | 608 |     { return any_; }
 | 
|---|
 | 609 | 
 | 
|---|
 | 610 |     void reset()
 | 
|---|
 | 611 |     { any_ = _bz_false; }
 | 
|---|
 | 612 | 
 | 
|---|
 | 613 |     void reset(T_resulttype)
 | 
|---|
 | 614 |     { 
 | 
|---|
 | 615 |         BZPRECHECK(0, "Provided initial value for ReduceAny");
 | 
|---|
 | 616 |         reset();
 | 
|---|
 | 617 |     }
 | 
|---|
 | 618 | 
 | 
|---|
 | 619 |     static const char* name()
 | 
|---|
 | 620 |     { return "any"; }
 | 
|---|
 | 621 | 
 | 
|---|
 | 622 | protected:
 | 
|---|
 | 623 |     T_resulttype any_;
 | 
|---|
 | 624 | };
 | 
|---|
 | 625 | 
 | 
|---|
 | 626 | template<class P_sourcetype>
 | 
|---|
 | 627 | class ReduceAll {
 | 
|---|
 | 628 | 
 | 
|---|
 | 629 | public:
 | 
|---|
 | 630 |     typedef P_sourcetype T_sourcetype;
 | 
|---|
 | 631 |     typedef _bz_bool     T_resulttype;
 | 
|---|
 | 632 |     typedef T_resulttype T_numtype;
 | 
|---|
 | 633 | 
 | 
|---|
 | 634 |     enum { needIndex = 0, canProvideInitialValue = 0 };
 | 
|---|
 | 635 | 
 | 
|---|
 | 636 |     ReduceAll()
 | 
|---|
 | 637 |     { reset(); }
 | 
|---|
 | 638 | 
 | 
|---|
 | 639 |     ReduceAll(T_resulttype initialValue)
 | 
|---|
 | 640 |     {
 | 
|---|
 | 641 |         reset(initialValue);
 | 
|---|
 | 642 |     }
 | 
|---|
 | 643 | 
 | 
|---|
 | 644 |     bool operator()(T_sourcetype x)
 | 
|---|
 | 645 |     {
 | 
|---|
 | 646 |         if (!_bz_bool(x))
 | 
|---|
 | 647 |         {
 | 
|---|
 | 648 |             all_ = _bz_false;
 | 
|---|
 | 649 |             return false;
 | 
|---|
 | 650 |         }
 | 
|---|
 | 651 |         else
 | 
|---|
 | 652 |             return true;
 | 
|---|
 | 653 |     }
 | 
|---|
 | 654 | 
 | 
|---|
 | 655 |     bool operator()(T_sourcetype x, int)
 | 
|---|
 | 656 |     {
 | 
|---|
 | 657 |         if (!_bz_bool(x))
 | 
|---|
 | 658 |         {
 | 
|---|
 | 659 |             all_ = _bz_false;
 | 
|---|
 | 660 |             return false;
 | 
|---|
 | 661 |         }
 | 
|---|
 | 662 |         else
 | 
|---|
 | 663 |             return true;
 | 
|---|
 | 664 |     }
 | 
|---|
 | 665 | 
 | 
|---|
 | 666 |     T_resulttype result(int)
 | 
|---|
 | 667 |     { return all_; }
 | 
|---|
 | 668 | 
 | 
|---|
 | 669 |     void reset()
 | 
|---|
 | 670 |     { all_ = _bz_true; }
 | 
|---|
 | 671 | 
 | 
|---|
 | 672 |     void reset(T_resulttype)
 | 
|---|
 | 673 |     {
 | 
|---|
 | 674 |         BZPRECHECK(0, "Provided initial value for ReduceAll");
 | 
|---|
 | 675 |         reset();
 | 
|---|
 | 676 |     }
 | 
|---|
 | 677 | 
 | 
|---|
 | 678 |     static const char* name()
 | 
|---|
 | 679 |     { return "all"; }
 | 
|---|
 | 680 | 
 | 
|---|
 | 681 | protected:
 | 
|---|
 | 682 |     T_resulttype all_;
 | 
|---|
 | 683 | }; 
 | 
|---|
 | 684 | 
 | 
|---|
 | 685 | BZ_NAMESPACE_END
 | 
|---|
 | 686 | 
 | 
|---|
 | 687 | #endif // BZ_REDUCE_H
 | 
|---|