blitz  Version 1.0.2
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$
7  *
8  * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
9  *
10  * This file is a part of Blitz.
11  *
12  * Blitz is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation, either version 3
15  * of the License, or (at your option) any later version.
16  *
17  * Blitz is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with Blitz. If not, see <http://www.gnu.org/licenses/>.
24  *
25  * Suggestions: blitz-devel@lists.sourceforge.net
26  * Bugs: blitz-support@lists.sourceforge.net
27  *
28  * For more information, please see the Blitz++ Home Page:
29  * https://sourceforge.net/projects/blitz/
30  *
31  ***************************************************************************/
32 
33 #ifndef BZ_REDUCE_H
34 #define BZ_REDUCE_H
35 
36 #include <blitz/blitz.h>
37 #include <blitz/numtrait.h>
38 #include <blitz/numinquire.h>
39 #include <blitz/tinyvec2.h>
40 
41 
42 // The various reduce classes.
43 // The prototype of the reset method is mandated by the class _bz_ReduceReset
44 // in file array/reduce.h
45 
46 namespace blitz {
47 
48 template<typename P_sourcetype, typename P_resulttype = BZ_SUMTYPE(P_sourcetype)>
49 class ReduceSum {
50 public:
51 
52  typedef P_sourcetype T_sourcetype;
53  typedef P_resulttype T_resulttype;
54  typedef T_resulttype T_numtype;
55 
56  static const bool needIndex = false, needInit = false;
57 
58  ReduceSum() { }
59 
60  bool operator()(const T_sourcetype& x,const int=0) const {
61  sum_ += x;
62  return true;
63  }
64 
65  T_resulttype result(const int) const { return sum_; }
66 
67  void reset() const { sum_ = zero(T_resulttype()); }
68 
69  static const char* name() { return "sum"; }
70 
71 protected:
72 
73  mutable T_resulttype sum_;
74 };
75 
76 template<typename P_sourcetype, typename P_resulttype = BZ_FLOATTYPE(P_sourcetype)>
77 class ReduceMean {
78 public:
79 
80  typedef P_sourcetype T_sourcetype;
81  typedef P_resulttype T_resulttype;
82  typedef T_resulttype T_numtype;
83 
84  static const bool needIndex = false, needInit = false;
85 
86  ReduceMean() { }
87 
88  bool operator()(const T_sourcetype& x,const int=0) const {
89  sum_ += x;
90  return true;
91  }
92 
93  T_resulttype result(const int count) const { return sum_ / count; }
94 
95  void reset() const { sum_ = zero(T_resulttype()); }
96 
97  static const char* name() { return "mean"; }
98 
99 protected:
100 
101  mutable T_resulttype sum_;
102 };
103 
104 template<typename P_sourcetype>
105 class ReduceMin {
106 public:
107 
108  typedef P_sourcetype T_sourcetype;
109  typedef P_sourcetype T_resulttype;
110  typedef T_resulttype T_numtype;
111 
112  static const bool needIndex = false, needInit = false;
113 
114  ReduceMin() { }
115 
116  bool operator()(const T_sourcetype& x,const int=0) const {
117  if (x < min_)
118  min_ = x;
119  return true;
120  }
121 
122  T_resulttype result(const int) const { return min_; }
123 
124  void reset() const { min_ = huge(P_sourcetype()); }
125 
126  static const char* name() { return "min"; }
127 
128 protected:
129 
130  mutable T_resulttype min_;
131 };
132 
133 template<typename P_sourcetype>
134 class ReduceMax {
135 public:
136 
137  typedef P_sourcetype T_sourcetype;
138  typedef P_sourcetype T_resulttype;
139  typedef T_resulttype T_numtype;
140 
141  static const bool needIndex = false, needInit = false;
142 
143  ReduceMax() { }
144 
145  bool operator()(const T_sourcetype& x,const int=0) const {
146  if (x > max_)
147  max_ = x;
148  return true;
149  }
150 
151  T_resulttype result(const int) const { return max_; }
152 
153  void reset() const { max_ = neghuge(P_sourcetype()); }
154 
155  static const char* name() { return "max"; }
156 
157 protected:
158 
159  mutable T_resulttype max_;
160 };
161 
162 template <typename T>
163 struct MinMaxValue {
164  void operator=(const T& val) { min = max = val; }
165  T min;
166  T max;
167 };
168 
169 template<typename P_sourcetype>
171 public:
172 
173  typedef P_sourcetype T_sourcetype;
175  typedef T_resulttype T_numtype;
176 
177  static const bool needIndex = false, needInit = true;
178 
180 
181  bool operator()(T_sourcetype x,const int=0) const {
182  if (x > minmax_.max)
183  minmax_.max = x;
184  else if (x < minmax_.min)
185  minmax_.min = x;
186  return true;
187  }
188 
189  T_resulttype result(int) { return minmax_; }
190 
191  void reset(P_sourcetype initialValue) const { minmax_ = initialValue; }
192 
193  static const char* name() { return "minmax"; }
194 
195 protected:
196 
197  mutable T_resulttype minmax_;
198 };
199 
200 template<typename P_sourcetype>
202 public:
203 
204  typedef P_sourcetype T_sourcetype;
205  typedef int T_resulttype;
206  typedef T_resulttype T_numtype;
207 
208  static const bool needIndex = true, needInit = false;
209 
211 
212  bool operator()(const T_sourcetype& x,const T_resulttype& index) const {
213  if (x < min_) {
214  min_ = x;
215  index_ = index;
216  }
217  return true;
218  }
219 
220  T_resulttype result(const int) const { return index_; }
221 
222  void reset(const T_resulttype& index) const {
223  min_ = huge(T_sourcetype());
224  index_ = index;
225  }
226 
227  static const char* name() { return "minIndex"; }
228 
229 protected:
230 
231  mutable T_sourcetype min_;
232  mutable T_resulttype index_;
233 };
234 
235 template<typename P_sourcetype, int N>
237 public:
238 
239  typedef P_sourcetype T_sourcetype;
241  typedef T_resulttype T_numtype;
242 
243  static const bool needIndex = true, needInit = false;
244 
246 
247  bool operator()(const T_sourcetype& x, const T_resulttype& index) const {
248  if (x < min_) {
249  min_ = x;
250  index_ = index;
251  }
252  return true;
253  }
254 
255  T_resulttype result(const int) const { return index_; }
256 
257  void reset(const T_resulttype& index) const {
258  min_ = huge(T_sourcetype());
259  index_ = index;
260  }
261 
262  static const char* name() { return "minIndexVector"; }
263 
264 protected:
265 
266  mutable T_sourcetype min_;
267  mutable T_resulttype index_;
268 };
269 
270 template<typename P_sourcetype>
272 public:
273 
274  typedef P_sourcetype T_sourcetype;
275  typedef int T_resulttype;
276  typedef T_resulttype T_numtype;
277 
278  static const bool needIndex = true, needInit = false;
279 
281 
282  bool operator()(const T_sourcetype& x,const T_resulttype& index) const {
283  if (x > max_) {
284  max_ = x;
285  index_ = index;
286  }
287  return true;
288  }
289 
290  T_resulttype result(int) const { return index_; }
291 
292  void reset(const T_resulttype& index) const {
293  max_ = neghuge(T_sourcetype());
294  index_ = index;
295  }
296 
297  static const char* name() { return "maxIndex"; }
298 
299 protected:
300 
301  mutable T_sourcetype max_;
302  mutable T_resulttype index_;
303 };
304 
305 template<typename P_sourcetype, int N_rank>
307 public:
308 
309  typedef P_sourcetype T_sourcetype;
311  typedef T_resulttype T_numtype;
312 
313  static const bool needIndex = true, needInit = false;
314 
316 
317  bool operator()(const T_sourcetype& x, const T_resulttype& index) const {
318  if (x > max_) {
319  max_ = x;
320  index_ = index;
321  }
322  return true;
323  }
324 
325  T_resulttype result(const int) const { return index_; }
326 
327  void reset(const T_resulttype& index) const {
328  max_ = neghuge(T_sourcetype());
329  index_ = index;
330  }
331 
332  static const char* name() { return "maxIndexVector"; }
333 
334 protected:
335 
336  mutable T_sourcetype max_;
337  mutable T_resulttype index_;
338 };
339 
340 template<typename P_sourcetype>
341 class ReduceFirst {
342 public:
343 
344  typedef P_sourcetype T_sourcetype;
345  typedef int T_resulttype;
346  typedef T_resulttype T_numtype;
347 
348  static const bool needIndex = false, needInit = false;
349 
351 
352  bool operator()(const T_sourcetype& x,const T_resulttype& index) const {
353  if (x) {
354  index_ = index;
355  return false;
356  } else
357  return true;
358  }
359 
360  T_resulttype result(const int) const { return index_; }
361 
362  void reset() const { index_ = tiny(int()); }
363 
364  static const char* name() { return "first"; }
365 
366 protected:
367 
368  mutable T_resulttype index_;
369 };
370 
371 template<typename P_sourcetype>
372 class ReduceLast {
373 public:
374 
375  typedef P_sourcetype T_sourcetype;
376  typedef int T_resulttype;
377  typedef T_resulttype T_numtype;
378 
379  static const bool needIndex = false, needInit = false;
380 
382 
383  bool operator()(const T_sourcetype& x,const T_resulttype& index) const {
384  if (x) {
385  index_ = index;
386  return true;
387  } else
388  return true;
389  }
390 
391  T_resulttype result(const int) const { return index_; }
392 
393  void reset() const { index_ = huge(int()); }
394 
395  static const char* name() { return "last"; }
396 
397 protected:
398 
399  mutable T_resulttype index_;
400 };
401 
402 template<typename P_sourcetype, typename P_resulttype = BZ_SUMTYPE(P_sourcetype)>
404 public:
405 
406  typedef P_sourcetype T_sourcetype;
407  typedef P_resulttype T_resulttype;
408  typedef T_resulttype T_numtype;
409 
410  static const bool needIndex = false, needInit = false;
411 
413 
414  bool operator()(const T_sourcetype& x,const int=0) const {
415  product_ *= x;
416  return true;
417  }
418 
419  T_resulttype result(const int) const { return product_; }
420 
421  void reset() const { product_ = one(T_resulttype()); }
422 
423  static const char* name() { return "product"; }
424 
425 protected:
426 
427  mutable T_resulttype product_;
428 };
429 
430 template<typename P_sourcetype>
431 class ReduceCount {
432 public:
433 
434  typedef P_sourcetype T_sourcetype;
435  typedef int T_resulttype;
436  typedef T_resulttype T_numtype;
437 
438  static const bool needIndex = false, needInit = false;
439 
441 
442  bool operator()(const T_sourcetype& x,const int=0) const {
443  if (bool(x))
444  ++count_;
445  return true;
446  }
447 
448  T_resulttype result(const int) const { return count_; }
449 
450  void reset() const { count_ = zero(T_resulttype()); }
451 
452  static const char* name() { return "count"; }
453 
454 protected:
455 
456  mutable T_resulttype count_;
457 };
458 
459 template<typename P_sourcetype>
460 class ReduceAny {
461 public:
462 
463  typedef P_sourcetype T_sourcetype;
464  typedef bool T_resulttype;
465  typedef T_resulttype T_numtype;
466 
467  static const bool needIndex = false, needInit = false;
468 
469  ReduceAny() { }
470 
471  bool operator()(const T_sourcetype& x,const int=0) const {
472  if (bool(x)) {
473  any_ = true;
474  return false;
475  }
476 
477  return true;
478  }
479 
480  T_resulttype result(const int) const { return any_; }
481 
482  void reset() const { any_ = false; }
483 
484  static const char* name() { return "any"; }
485 
486 protected:
487 
488  mutable T_resulttype any_;
489 };
490 
491 template<typename P_sourcetype>
492 class ReduceAll {
493 public:
494 
495  typedef P_sourcetype T_sourcetype;
496  typedef bool T_resulttype;
497  typedef T_resulttype T_numtype;
498 
499  static const bool needIndex = false, needInit = false;
500 
501  ReduceAll() { }
502 
503  bool operator()(const T_sourcetype& x,const int=0) const {
504  if (!bool(x)) {
505  all_ = false;
506  return false;
507  } else
508  return true;
509  }
510 
511  T_resulttype result(const int) const { return all_; }
512 
513  void reset() const { all_ = true; }
514 
515  static const char* name() { return "all"; }
516 
517 protected:
518 
519  mutable T_resulttype all_;
520 };
521 
522 }
523 
524 #endif // BZ_REDUCE_H
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:414
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:352
T_resulttype max_
Definition: reduce.h:159
T_resulttype T_numtype
Definition: reduce.h:139
static const bool needInit
Definition: reduce.h:112
void reset() const
Definition: reduce.h:450
T_resulttype product_
Definition: reduce.h:427
static const char * name()
Definition: reduce.h:155
P_sourcetype T_sourcetype
Definition: reduce.h:108
T_resulttype sum_
Definition: reduce.h:73
P_sourcetype T_sourcetype
Definition: reduce.h:274
static const char * name()
Definition: reduce.h:332
Definition: reduce.h:77
ReduceMaxIndex()
Definition: reduce.h:280
Definition: reduce.h:306
T_resulttype index_
Definition: reduce.h:368
T_resulttype T_numtype
Definition: reduce.h:241
P_resulttype T_resulttype
Definition: reduce.h:53
static const bool needIndex
Definition: reduce.h:410
static const bool needInit
Definition: reduce.h:379
static const bool needInit
Definition: reduce.h:278
P_sourcetype T_sourcetype
Definition: reduce.h:52
ReduceMean()
Definition: reduce.h:86
T_resulttype T_numtype
Definition: reduce.h:436
T_resulttype result(const int) const
Definition: reduce.h:65
Definition: reduce.h:201
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:88
ReduceMinIndex()
Definition: reduce.h:210
static const bool needInit
Definition: reduce.h:84
T_resulttype T_numtype
Definition: reduce.h:54
int T_resulttype
Definition: reduce.h:435
static const char * name()
Definition: reduce.h:364
T_resulttype all_
Definition: reduce.h:519
Definition: reduce.h:163
T_resulttype index_
Definition: reduce.h:302
void reset(const T_resulttype &index) const
Definition: reduce.h:292
T_resulttype index_
Definition: reduce.h:232
static const bool needInit
Definition: reduce.h:467
TinyVector< int, N > T_resulttype
Definition: reduce.h:240
void reset(P_sourcetype initialValue) const
Definition: reduce.h:191
static const bool needIndex
Definition: reduce.h:379
T_resulttype result(int) const
Definition: reduce.h:290
P_sourcetype T_sourcetype
Definition: reduce.h:375
ReduceMaxIndexVector()
Definition: reduce.h:315
P_sourcetype T_resulttype
Definition: reduce.h:109
T_resulttype T_numtype
Definition: reduce.h:276
T_resulttype T_numtype
Definition: reduce.h:110
P_resulttype T_resulttype
Definition: reduce.h:81
Definition: reduce.h:492
static const bool needInit
Definition: reduce.h:499
static const bool needIndex
Definition: reduce.h:56
ReduceLast()
Definition: reduce.h:381
void reset(const T_resulttype &index) const
Definition: reduce.h:327
T_resulttype result(const int) const
Definition: reduce.h:151
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:212
static const bool needIndex
Definition: reduce.h:84
void operator=(const T &val)
Definition: reduce.h:164
static const char * name()
Definition: reduce.h:97
static const bool needIndex
Definition: reduce.h:348
T_resulttype result(const int) const
Definition: reduce.h:220
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:317
P_sourcetype T_sourcetype
Definition: reduce.h:239
static const char * name()
Definition: reduce.h:515
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:471
static const char * name()
Definition: reduce.h:227
Definition: reduce.h:134
T_resulttype T_numtype
Definition: reduce.h:82
P_sourcetype T_sourcetype
Definition: reduce.h:434
ReduceMax()
Definition: reduce.h:143
int T_resulttype
Definition: reduce.h:376
T max
Definition: reduce.h:166
T_resulttype index_
Definition: reduce.h:337
void reset() const
Definition: reduce.h:393
static const bool needIndex
Definition: reduce.h:278
T_resulttype any_
Definition: reduce.h:488
ReduceAny()
Definition: reduce.h:469
Definition: reduce.h:105
bool T_resulttype
Definition: reduce.h:496
static const bool needIndex
Definition: reduce.h:208
static const char * name()
Definition: reduce.h:395
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:247
void reset() const
Definition: reduce.h:67
static const bool needIndex
Definition: reduce.h:499
Definition: array-impl.h:66
void reset() const
Definition: reduce.h:362
void reset() const
Definition: reduce.h:482
T_sourcetype max_
Definition: reduce.h:336
T_resulttype result(const int) const
Definition: reduce.h:255
T min
Definition: reduce.h:165
static const bool needIndex
Definition: reduce.h:438
ReduceMin()
Definition: reduce.h:114
P_sourcetype T_resulttype
Definition: reduce.h:138
T_resulttype count_
Definition: reduce.h:456
T_resulttype result(const int) const
Definition: reduce.h:511
T_resulttype T_numtype
Definition: reduce.h:377
int T_resulttype
Definition: reduce.h:275
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:60
T_resulttype result(const int) const
Definition: reduce.h:391
T_resulttype result(const int) const
Definition: reduce.h:448
T_resulttype index_
Definition: reduce.h:267
Definition: reduce.h:271
void reset() const
Definition: reduce.h:124
T_resulttype T_numtype
Definition: reduce.h:346
T_resulttype T_numtype
Definition: reduce.h:311
ReduceSum()
Definition: reduce.h:58
static const bool needIndex
Definition: reduce.h:243
void reset() const
Definition: reduce.h:421
static const bool needInit
Definition: reduce.h:208
T_resulttype min_
Definition: reduce.h:130
P_sourcetype T_sourcetype
Definition: reduce.h:309
void reset() const
Definition: reduce.h:95
void reset() const
Definition: reduce.h:153
static const char * name()
Definition: reduce.h:69
P_sourcetype T_sourcetype
Definition: reduce.h:173
T_resulttype minmax_
Definition: reduce.h:197
static const bool needIndex
Definition: reduce.h:141
ReduceMinIndexVector()
Definition: reduce.h:245
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:145
P_sourcetype T_sourcetype
Definition: reduce.h:463
static const bool needInit
Definition: reduce.h:348
P_sourcetype T_sourcetype
Definition: reduce.h:406
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:282
T_resulttype result(const int) const
Definition: reduce.h:325
T_resulttype T_numtype
Definition: reduce.h:408
P_sourcetype T_sourcetype
Definition: reduce.h:204
T_sourcetype max_
Definition: reduce.h:301
T_sourcetype min_
Definition: reduce.h:231
void reset(const T_resulttype &index) const
Definition: reduce.h:257
Definition: reduce.h:236
Definition: reduce.h:403
T_resulttype index_
Definition: reduce.h:399
T_resulttype result(int)
Definition: reduce.h:189
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:116
ReduceFirst()
Definition: reduce.h:350
ReduceMinMax()
Definition: reduce.h:179
T_resulttype T_numtype
Definition: reduce.h:175
void reset() const
Definition: reduce.h:513
static const char * name()
Definition: reduce.h:126
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:383
bool operator()(T_sourcetype x, const int=0) const
Definition: reduce.h:181
P_sourcetype T_sourcetype
Definition: reduce.h:80
TinyVector< int, N_rank > T_resulttype
Definition: reduce.h:310
static const bool needInit
Definition: reduce.h:438
ReduceAll()
Definition: reduce.h:501
T_resulttype T_numtype
Definition: reduce.h:497
ReduceCount()
Definition: reduce.h:440
static const char * name()
Definition: reduce.h:297
T_resulttype result(const int) const
Definition: reduce.h:480
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:503
static const bool needInit
Definition: reduce.h:177
P_sourcetype T_sourcetype
Definition: reduce.h:344
static const char * name()
Definition: reduce.h:262
MinMaxValue< P_sourcetype > T_resulttype
Definition: reduce.h:174
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:442
static const bool needIndex
Definition: reduce.h:112
T_resulttype result(const int) const
Definition: reduce.h:122
void reset(const T_resulttype &index) const
Definition: reduce.h:222
Definition: reduce.h:49
static const bool needInit
Definition: reduce.h:56
P_sourcetype T_sourcetype
Definition: reduce.h:495
static const char * name()
Definition: reduce.h:423
static const bool needInit
Definition: reduce.h:313
static const bool needIndex
Definition: reduce.h:313
Definition: reduce.h:460
T_sourcetype min_
Definition: reduce.h:266
P_resulttype T_resulttype
Definition: reduce.h:407
ReduceProduct()
Definition: reduce.h:412
T_resulttype result(const int) const
Definition: reduce.h:419
T_resulttype T_numtype
Definition: reduce.h:465
static const bool needIndex
Definition: reduce.h:177
Definition: reduce.h:372
int T_resulttype
Definition: reduce.h:345
static const char * name()
Definition: reduce.h:484
Definition: reduce.h:341
Definition: reduce.h:170
T_resulttype result(const int) const
Definition: reduce.h:360
static const bool needInit
Definition: reduce.h:243
static const char * name()
Definition: reduce.h:193
static const bool needInit
Definition: reduce.h:410
int T_resulttype
Definition: reduce.h:205
T_resulttype result(const int count) const
Definition: reduce.h:93
static const bool needInit
Definition: reduce.h:141
T_resulttype T_numtype
Definition: reduce.h:206
T_resulttype sum_
Definition: reduce.h:101
bool T_resulttype
Definition: reduce.h:464
static const bool needIndex
Definition: reduce.h:467
Definition: reduce.h:431
P_sourcetype T_sourcetype
Definition: reduce.h:137
static const char * name()
Definition: reduce.h:452