blitz  Version 0.9
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
reduce.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  * blitz/reduce.h Reduction operators: sum, mean, min, max,
4  * minIndex, maxIndex, product, count, any, all
5  *
6  * $Id: reduce.h,v 1.5 2005/05/07 04:17:56 julianc Exp $
7  *
8  * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * Suggestions: blitz-dev@oonumerics.org
21  * Bugs: blitz-bugs@oonumerics.org
22  *
23  * For more information, please see the Blitz++ Home Page:
24  * http://oonumerics.org/blitz/
25  *
26  ***************************************************************************/
27 
28 #ifndef BZ_REDUCE_H
29 #define BZ_REDUCE_H
30 
31 #ifndef BZ_BLITZ_H
32  #include <blitz/blitz.h>
33 #endif
34 
35 #ifndef BZ_NUMTRAIT_H
36  #include <blitz/numtrait.h>
37 #endif
38 
39 #ifndef BZ_NUMINQUIRE_H
40  #include <blitz/numinquire.h>
41 #endif
42 
43 BZ_NAMESPACE(blitz)
44 
45 template<typename P_sourcetype, typename P_resulttype = BZ_SUMTYPE(P_sourcetype)>
46 class ReduceSum {
47 
48 public:
49  typedef P_sourcetype T_sourcetype;
50  typedef P_resulttype T_resulttype;
51  typedef T_resulttype T_numtype;
52 
53  static const bool needIndex = false, canProvideInitialValue = true;
54 
55  ReduceSum()
56  { reset(); }
57 
58  ReduceSum(T_resulttype initialValue)
59  { sum_ = initialValue; }
60 
61  bool operator()(T_sourcetype x)
62  {
63  sum_ += x;
64  return true;
65  }
66 
67  bool operator()(T_sourcetype x, int)
68  {
69  sum_ += x;
70  return true;
71  }
72 
73  T_resulttype result(int)
74  { return sum_; }
75 
76  void reset()
77  { sum_ = zero(T_resulttype()); }
78 
79  void reset(T_resulttype initialValue)
80  { sum_ = initialValue; }
81 
82  static const char* name()
83  { return "sum"; }
84 
85 protected:
86  T_resulttype sum_;
87 };
88 
89 template<typename P_sourcetype, typename P_resulttype = BZ_FLOATTYPE(P_sourcetype)>
90 class ReduceMean {
91 
92 public:
93  typedef P_sourcetype T_sourcetype;
94  typedef P_resulttype T_resulttype;
96 
97  static const bool needIndex = false, canProvideInitialValue = false;
98 
100  { reset(); }
101 
103  {
104  BZPRECHECK(0, "Provided an initial value for ReduceMean");
105  reset();
106  }
107 
109  {
110  sum_ += x;
111  return true;
112  }
113 
115  {
116  sum_ += x;
117  return true;
118  }
119 
120  T_resulttype result(int count)
121  { return sum_ / count; }
122 
123  void reset()
124  { sum_ = zero(T_resulttype()); }
125 
127  {
128  BZPRECHECK(0, "Provided an initial value for ReduceMean");
129  reset();
130  }
131 
132  static const char* name()
133  { return "mean"; }
134 
135 protected:
137 };
138 
139 template<typename P_sourcetype>
140 class ReduceMin {
141 
142 public:
143  typedef P_sourcetype T_sourcetype;
144  typedef P_sourcetype T_resulttype;
146 
147  static const bool needIndex = false, canProvideInitialValue = false;
148 
150  { reset(); }
151 
153  {
154  min_ = min;
155  }
156 
158  {
159  if (x < min_)
160  min_ = x;
161  return true;
162  }
163 
165  {
166  if (x < min_)
167  min_ = x;
168  return true;
169  }
170 
172  { return min_; }
173 
174  void reset()
175  { min_ = huge(P_sourcetype()); }
176 
177  void reset(T_resulttype initialValue)
178  { min_ = initialValue; }
179 
180  static const char* name()
181  { return "min"; }
182 
183 protected:
185 };
186 
187 template<typename P_sourcetype>
188 class ReduceMax {
189 
190 public:
191  typedef P_sourcetype T_sourcetype;
192  typedef P_sourcetype T_resulttype;
194 
195  static const bool needIndex = false, canProvideInitialValue = true;
196 
198  { reset(); }
199 
201  {
202  max_ = max;
203  }
204 
206  {
207  if (x > max_)
208  max_ = x;
209  return true;
210  }
211 
213  {
214  if (x > max_)
215  max_ = x;
216  return true;
217  }
218 
220  { return max_; }
221 
222  void reset()
223  { max_ = neghuge(P_sourcetype()); }
224 
225  void reset(T_resulttype initialValue)
226  { max_ = initialValue; }
227 
228  static const char* name()
229  { return "max"; }
230 
231 protected:
233 };
234 
235 template<typename P_sourcetype>
237 
238 public:
239  typedef P_sourcetype T_sourcetype;
240  typedef int T_resulttype;
242 
243  static const bool needIndex = true, canProvideInitialValue = false;
244 
246  { reset(); }
247 
249  {
250  reset(min);
251  }
252 
254  {
255  BZPRECONDITION(0);
256  return false;
257  }
258 
259  bool operator()(T_sourcetype x, int index)
260  {
261  if (x < min_)
262  {
263  min_ = x;
264  index_ = index;
265  }
266  return true;
267  }
268 
270  { return index_; }
271 
272  void reset()
273  {
274  min_ = huge(T_sourcetype());
275  index_ = tiny(int());
276  }
277 
279  {
280  BZPRECHECK(0, "Provided initial value for ReduceMinIndex");
281  reset();
282  }
283 
284  static const char* name()
285  { return "minIndex"; }
286 
287 protected:
289  int index_;
290 };
291 
292 template<typename P_sourcetype, int N>
294 
295 public:
296  typedef P_sourcetype T_sourcetype;
299 
300  static const bool canProvideInitialValue = false;
301 
303  { reset(); }
304 
306  {
307  reset(min);
308  }
309 
311  {
312  BZPRECONDITION(0);
313  return false;
314  }
315 
317  {
318  BZPRECONDITION(0);
319  return false;
320  }
321 
323  {
324  if (x < min_)
325  {
326  min_ = x;
327  index_ = index;
328  }
329  return true;
330  }
331 
333  { return index_; }
334 
335  void reset()
336  {
337  min_ = huge(T_sourcetype());
338  index_ = tiny(int());
339  }
340 
342  {
343  BZPRECHECK(0, "Provided initial value for ReduceMinIndex");
344  reset();
345  }
346 
347  static const char* name()
348  { return "minIndex"; }
349 
350 protected:
353 };
354 
355 template<typename P_sourcetype>
357 
358 public:
359  typedef P_sourcetype T_sourcetype;
360  typedef int T_resulttype;
362 
363  static const bool needIndex = true, canProvideInitialValue = false;
364 
366  { reset(); }
367 
369  {
370  reset(max);
371  }
372 
374  {
375  BZPRECONDITION(0);
376  return false;
377  }
378 
379  bool operator()(T_sourcetype x, int index)
380  {
381  if (x > max_)
382  {
383  max_ = x;
384  index_ = index;
385  }
386  return true;
387  }
388 
390  { return index_; }
391 
392  void reset()
393  {
394  max_ = neghuge(T_sourcetype());
395  index_ = tiny(int());
396  }
397 
399  {
400  BZPRECHECK(0, "Provided initial value for ReduceMaxIndex");
401  reset();
402  }
403 
404  static const char* name()
405  { return "maxIndex"; }
406 
407 protected:
409  int index_;
410 };
411 
412 template<typename P_sourcetype, int N_rank>
414 
415 public:
416  typedef P_sourcetype T_sourcetype;
419 
420  static const bool canProvideInitialValue = false;
421 
423  { reset(); }
424 
426  {
427  reset(max);
428  }
429 
431  {
432  BZPRECONDITION(0);
433  return false;
434  }
435 
437  {
438  if (x > max_)
439  {
440  max_ = x;
441  index_ = index;
442  }
443  return true;
444  }
445 
447  { return index_; }
448 
449  void reset()
450  {
451  max_ = neghuge(T_sourcetype());
452  index_ = tiny(int());
453  }
454 
456  {
457  BZPRECHECK(0, "Provided initial value for ReduceMaxIndex");
458  reset();
459  }
460 
461  static const char* name()
462  { return "maxIndex"; }
463 
464 protected:
467 };
468 
469 template<typename P_sourcetype>
470 class ReduceFirst {
471 
472 public:
473  typedef P_sourcetype T_sourcetype;
474  typedef int T_resulttype;
476 
477  static const bool needIndex = true, canProvideInitialValue = false;
478 
480  { reset(); }
481 
483  {
484  BZPRECONDITION(0);
485  }
486 
488  {
489  BZPRECONDITION(0);
490  return false;
491  }
492 
493  bool operator()(T_sourcetype x, int index)
494  {
495  if (x)
496  {
497  index_ = index;
498  return false;
499  }
500  else
501  return true;
502  }
503 
505  { return index_; }
506 
507  void reset()
508  {
509  index_ = tiny(int());
510  }
511 
513  {
514  BZPRECHECK(0, "Provided initial value for ReduceFirst");
515  reset();
516  }
517 
518  static const char* name()
519  { return "first"; }
520 
521 protected:
522  int index_;
523 };
524 
525 template<typename P_sourcetype>
526 class ReduceLast {
527 
528 public:
529  typedef P_sourcetype T_sourcetype;
530  typedef int T_resulttype;
532 
533  static const bool needIndex = true, canProvideInitialValue = false;
534 
536  { reset(); }
537 
539  {
540  BZPRECONDITION(0);
541  }
542 
544  {
545  BZPRECONDITION(0);
546  return false;
547  }
548 
549  bool operator()(T_sourcetype x, int index)
550  {
551  if (x)
552  {
553  index_ = index;
554  return true;
555  }
556  else
557  return true;
558  }
559 
561  { return index_; }
562 
563  void reset()
564  {
565  index_ = huge(int());
566  }
567 
569  {
570  BZPRECHECK(0, "Provided initial value for ReduceFirst");
571  reset();
572  }
573 
574  static const char* name()
575  { return "last"; }
576 
577 protected:
578  int index_;
579 };
580 
581 template<typename P_sourcetype, typename P_resulttype = BZ_SUMTYPE(P_sourcetype)>
583 
584 public:
585  typedef P_sourcetype T_sourcetype;
586  typedef P_resulttype T_resulttype;
588 
589  static const bool needIndex = false, canProvideInitialValue = true;
590 
592  { product_ = one(T_resulttype()); }
593 
595  { product_ = initialValue; }
596 
598  {
599  product_ *= x;
600  return true;
601  }
602 
604  {
605  product_ *= x;
606  return true;
607  }
608 
610  { return product_; }
611 
612  void reset()
613  { product_ = one(T_resulttype()); }
614 
615  void reset(T_resulttype initialValue)
616  { product_ = initialValue; }
617 
618  static const char* name()
619  { return "product"; }
620 
621 protected:
623 };
624 
625 template<typename P_sourcetype>
626 class ReduceCount {
627 
628 public:
629  typedef P_sourcetype T_sourcetype;
630  typedef int T_resulttype;
632 
633  static const bool needIndex = false, canProvideInitialValue = true;
634 
636  { reset(); }
637 
639  {
640  count_ = count;
641  }
642 
644  {
645  if (x)
646  ++count_;
647  return true;
648  }
649 
651  {
652  if (x)
653  ++count_;
654  return true;
655  }
656 
658  { return count_; }
659 
660  void reset()
661  { count_ = zero(T_resulttype()); }
662 
663  void reset(T_resulttype initialValue)
664  { count_ = initialValue; }
665 
666  static const char* name()
667  { return "count"; }
668 
669 protected:
671 };
672 
673 template<typename P_sourcetype>
674 class ReduceAny {
675 
676 public:
677  typedef P_sourcetype T_sourcetype;
678  typedef bool T_resulttype;
680 
681  static const bool needIndex = false, canProvideInitialValue = false;
682 
684  { reset(); }
685 
686  ReduceAny(T_resulttype initialValue)
687  {
688  reset(initialValue);
689  }
690 
692  {
693  if (x)
694  {
695  any_ = true;
696  return false;
697  }
698 
699  return true;
700  }
701 
703  {
704  if (x)
705  {
706  any_ = true;
707  return false;
708  }
709 
710  return true;
711  }
712 
714  { return any_; }
715 
716  void reset()
717  { any_ = false; }
718 
720  {
721  BZPRECHECK(0, "Provided initial value for ReduceAny");
722  reset();
723  }
724 
725  static const char* name()
726  { return "any"; }
727 
728 protected:
730 };
731 
732 template<typename P_sourcetype>
733 class ReduceAll {
734 
735 public:
736  typedef P_sourcetype T_sourcetype;
737  typedef bool T_resulttype;
739 
740  static const bool needIndex = false, canProvideInitialValue = false;
741 
743  { reset(); }
744 
745  ReduceAll(T_resulttype initialValue)
746  {
747  reset(initialValue);
748  }
749 
751  {
752  if (!bool(x))
753  {
754  all_ = false;
755  return false;
756  }
757  else
758  return true;
759  }
760 
762  {
763  if (!bool(x))
764  {
765  all_ = false;
766  return false;
767  }
768  else
769  return true;
770  }
771 
773  { return all_; }
774 
775  void reset()
776  { all_ = true; }
777 
779  {
780  BZPRECHECK(0, "Provided initial value for ReduceAll");
781  reset();
782  }
783 
784  static const char* name()
785  { return "all"; }
786 
787 protected:
789 };
790 
792 
793 #endif // BZ_REDUCE_H