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