source: XMLIO_V2/external/include/blitz/reduce.h @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 14.7 KB
Line 
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
43BZ_NAMESPACE(blitz)
44
45template<typename P_sourcetype, typename P_resulttype = BZ_SUMTYPE(P_sourcetype)>
46class ReduceSum {
47
48public:
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 
85protected:
86    T_resulttype sum_;
87};
88
89template<typename P_sourcetype, typename P_resulttype = BZ_FLOATTYPE(P_sourcetype)>
90class ReduceMean {
91
92public:
93    typedef P_sourcetype T_sourcetype;
94    typedef P_resulttype T_resulttype;
95    typedef T_resulttype T_numtype;
96
97    static const bool needIndex = false, canProvideInitialValue = false;
98
99    ReduceMean()
100    { reset(); }
101
102    ReduceMean(T_resulttype)
103    { 
104        BZPRECHECK(0, "Provided an initial value for ReduceMean");
105        reset();
106    }
107
108    bool operator()(T_sourcetype x)
109    { 
110        sum_ += x; 
111        return true;
112    }
113
114    bool operator()(T_sourcetype x, int)
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
126    void reset(T_resulttype)
127    { 
128        BZPRECHECK(0, "Provided an initial value for ReduceMean");
129        reset();
130    }
131
132    static const char* name() 
133    { return "mean"; }
134
135protected:
136    T_resulttype sum_;
137};
138
139template<typename P_sourcetype>
140class ReduceMin {
141
142public:
143    typedef P_sourcetype T_sourcetype;
144    typedef P_sourcetype T_resulttype;
145    typedef T_resulttype T_numtype;
146
147    static const bool needIndex = false, canProvideInitialValue = false;
148
149    ReduceMin()
150    { reset(); }
151
152    ReduceMin(T_resulttype min)
153    {
154        min_ = min;
155    }
156
157    bool operator()(T_sourcetype x)
158    { 
159        if (x < min_)
160            min_ = x;
161        return true;
162    }
163
164    bool operator()(T_sourcetype x, int)
165    {
166        if (x < min_)
167            min_ = x;
168        return true;
169    }
170
171    T_resulttype result(int)
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
183protected:
184    T_resulttype min_;
185};
186
187template<typename P_sourcetype>
188class ReduceMax {
189
190public:
191    typedef P_sourcetype T_sourcetype;
192    typedef P_sourcetype T_resulttype;
193    typedef T_resulttype T_numtype;
194
195    static const bool needIndex = false, canProvideInitialValue = true;
196
197    ReduceMax()
198    { reset(); }
199
200    ReduceMax(T_resulttype max)
201    {
202        max_ = max;
203    }
204
205    bool operator()(T_sourcetype x)
206    {
207        if (x > max_)
208            max_ = x;
209        return true;
210    }
211
212    bool operator()(T_sourcetype x, int)
213    {
214        if (x > max_)
215            max_ = x;
216        return true;
217    }
218
219    T_resulttype result(int)
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
231protected:
232    T_resulttype max_;
233};
234
235template<typename P_sourcetype>
236class ReduceMinIndex {
237
238public:
239    typedef P_sourcetype T_sourcetype;
240    typedef int          T_resulttype;
241    typedef T_resulttype T_numtype;
242
243    static const bool needIndex = true, canProvideInitialValue = false;
244
245    ReduceMinIndex()
246    { reset(); }
247
248    ReduceMinIndex(T_resulttype min)
249    {
250        reset(min);
251    }
252
253    bool operator()(T_sourcetype x)
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
269    T_resulttype result(int)
270    { return index_; }
271
272    void reset()
273    { 
274        min_ = huge(T_sourcetype());
275        index_ = tiny(int());       
276    }
277
278    void reset(T_resulttype)
279    { 
280        BZPRECHECK(0, "Provided initial value for ReduceMinIndex");
281        reset();
282    }
283
284    static const char* name()
285    { return "minIndex"; }
286
287protected:
288    T_sourcetype min_;
289    int index_;
290};
291
292template<typename P_sourcetype, int N>
293class ReduceMinIndexVector {
294
295public:
296    typedef P_sourcetype T_sourcetype;
297    typedef TinyVector<int,N> T_resulttype;
298    typedef T_resulttype T_numtype;
299
300    static const bool canProvideInitialValue = false;
301
302    ReduceMinIndexVector()
303    { reset(); }
304
305    ReduceMinIndexVector(T_resulttype min)
306    {
307        reset(min);
308    }
309
310    bool operator()(T_sourcetype x)
311    {
312        BZPRECONDITION(0);
313        return false;
314    }
315
316    bool operator()(T_sourcetype, int)
317    {
318        BZPRECONDITION(0);
319        return false;
320    }
321   
322    bool operator()(T_sourcetype x, const TinyVector<int,N>& index)
323    {
324        if (x < min_)
325        {
326            min_ = x;
327            index_ = index;
328        }
329        return true;
330    }
331
332    T_resulttype result(int)
333    { return index_; }
334
335    void reset()
336    {
337        min_ = huge(T_sourcetype());
338        index_ = tiny(int());
339    }
340
341    void reset(T_resulttype)
342    {
343        BZPRECHECK(0, "Provided initial value for ReduceMinIndex");
344        reset();
345    }
346
347    static const char* name()
348    { return "minIndex"; }
349
350protected:
351    T_sourcetype min_;
352    TinyVector<int,N> index_;
353};
354
355template<typename P_sourcetype>
356class ReduceMaxIndex {
357
358public:
359    typedef P_sourcetype T_sourcetype;
360    typedef int          T_resulttype;
361    typedef T_resulttype T_numtype;
362
363    static const bool needIndex = true, canProvideInitialValue = false;
364
365    ReduceMaxIndex()
366    { reset(); }
367
368    ReduceMaxIndex(T_resulttype max)
369    {
370        reset(max);
371    }
372
373    bool operator()(T_sourcetype x)
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
389    T_resulttype result(int)
390    { return index_; }
391
392    void reset()
393    {
394        max_ = neghuge(T_sourcetype());
395        index_ = tiny(int());
396    }
397
398    void reset(T_resulttype)
399    {
400        BZPRECHECK(0, "Provided initial value for ReduceMaxIndex");
401        reset();
402    }
403
404    static const char* name()
405    { return "maxIndex"; }
406
407protected:
408    T_sourcetype max_;
409    int index_;
410};
411
412template<typename P_sourcetype, int N_rank>
413class ReduceMaxIndexVector {
414
415public:
416    typedef P_sourcetype T_sourcetype;
417    typedef TinyVector<int,N_rank> T_resulttype;
418    typedef T_resulttype T_numtype;
419
420    static const bool canProvideInitialValue = false;
421
422    ReduceMaxIndexVector()
423    { reset(); }
424
425    ReduceMaxIndexVector(T_resulttype max)
426    {
427        reset(max);
428    }
429
430    bool operator()(T_sourcetype x)
431    {
432        BZPRECONDITION(0);
433        return false;
434    }
435
436    bool operator()(T_sourcetype x, const TinyVector<int,N_rank>& index)
437    {
438        if (x > max_)
439        {
440            max_ = x;
441            index_ = index;
442        }
443        return true;
444    }
445
446    T_resulttype result(int)
447    { return index_; }
448
449    void reset()
450    {
451        max_ = neghuge(T_sourcetype());
452        index_ = tiny(int());
453    }
454
455    void reset(T_resulttype)
456    {
457        BZPRECHECK(0, "Provided initial value for ReduceMaxIndex");
458        reset();
459    }
460
461    static const char* name()
462    { return "maxIndex"; }
463
464protected:
465    T_sourcetype max_;
466    TinyVector<int,N_rank> index_;
467};
468
469template<typename P_sourcetype>
470class ReduceFirst {
471
472public:
473    typedef P_sourcetype T_sourcetype;
474    typedef int          T_resulttype;
475    typedef T_resulttype T_numtype;
476
477    static const bool needIndex = true, canProvideInitialValue = false;
478
479    ReduceFirst()
480    { reset(); }
481
482    ReduceFirst(T_resulttype)
483    {
484        BZPRECONDITION(0);
485    }
486
487    bool operator()(T_sourcetype x)
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
504    T_resulttype result(int)
505    { return index_; }
506
507    void reset()
508    {
509        index_ = tiny(int());
510    }
511
512    void reset(T_resulttype)
513    {
514        BZPRECHECK(0, "Provided initial value for ReduceFirst");
515        reset();
516    }
517
518    static const char* name()
519    { return "first"; }
520
521protected:
522    int index_;
523};
524
525template<typename P_sourcetype>
526class ReduceLast {
527
528public:
529    typedef P_sourcetype T_sourcetype;
530    typedef int          T_resulttype;
531    typedef T_resulttype T_numtype;
532
533    static const bool needIndex = true, canProvideInitialValue = false;
534
535    ReduceLast()
536    { reset(); }
537
538    ReduceLast(T_resulttype)
539    {
540        BZPRECONDITION(0);
541    }
542
543    bool operator()(T_sourcetype x)
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
560    T_resulttype result(int)
561    { return index_; }
562
563    void reset()
564    {
565        index_ = huge(int());
566    }
567
568    void reset(T_resulttype)
569    {
570        BZPRECHECK(0, "Provided initial value for ReduceFirst");
571        reset();
572    }
573
574    static const char* name()
575    { return "last"; }
576
577protected:
578    int index_;
579};
580
581template<typename P_sourcetype, typename P_resulttype = BZ_SUMTYPE(P_sourcetype)>
582class ReduceProduct {
583
584public:
585    typedef P_sourcetype T_sourcetype;
586    typedef P_resulttype T_resulttype;
587    typedef T_resulttype T_numtype;
588
589    static const bool needIndex = false, canProvideInitialValue = true;
590
591    ReduceProduct()
592    { product_ = one(T_resulttype()); }
593
594    ReduceProduct(T_resulttype initialValue)
595    { product_ = initialValue; }
596
597    bool operator()(T_sourcetype x)
598    { 
599        product_ *= x; 
600        return true;
601    }
602
603    bool operator()(T_sourcetype x, int)
604    { 
605        product_ *= x; 
606        return true;
607    }
608
609    T_resulttype result(int)
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
621protected:
622    T_resulttype product_;
623};
624
625template<typename P_sourcetype>
626class ReduceCount {
627
628public:
629    typedef P_sourcetype T_sourcetype;
630    typedef int          T_resulttype;
631    typedef T_resulttype T_numtype;
632
633    static const bool needIndex = false, canProvideInitialValue = true;
634
635    ReduceCount()
636    { reset(); }
637
638    ReduceCount(T_resulttype count)
639    {
640        count_ = count;
641    }
642
643    bool operator()(T_sourcetype x)
644    {
645        if (x)
646            ++count_;
647        return true;
648    }
649
650    bool operator()(T_sourcetype x, int)
651    {
652        if (x)
653            ++count_;
654        return true;
655    }
656
657    T_resulttype result(int)
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
669protected:
670    T_resulttype count_;
671};
672
673template<typename P_sourcetype>
674class ReduceAny {
675
676public:
677    typedef P_sourcetype T_sourcetype;
678    typedef bool     T_resulttype;
679    typedef T_resulttype T_numtype;
680
681    static const bool needIndex = false, canProvideInitialValue = false;
682
683    ReduceAny()
684    { reset(); }
685
686    ReduceAny(T_resulttype initialValue)
687    {
688        reset(initialValue);
689    }
690
691    bool operator()(T_sourcetype x)
692    {
693        if (x)
694        {
695            any_ = true;
696            return false;
697        }
698
699        return true;
700    }
701
702    bool operator()(T_sourcetype x, int)
703    {
704        if (x)
705        {
706            any_ = true;
707            return false;
708        }
709
710        return true;
711    }
712
713    T_resulttype result(int)
714    { return any_; }
715
716    void reset()
717    { any_ = false; }
718
719    void reset(T_resulttype)
720    { 
721        BZPRECHECK(0, "Provided initial value for ReduceAny");
722        reset();
723    }
724
725    static const char* name()
726    { return "any"; }
727
728protected:
729    T_resulttype any_;
730};
731
732template<typename P_sourcetype>
733class ReduceAll {
734
735public:
736    typedef P_sourcetype T_sourcetype;
737    typedef bool     T_resulttype;
738    typedef T_resulttype T_numtype;
739
740    static const bool needIndex = false, canProvideInitialValue = false;
741
742    ReduceAll()
743    { reset(); }
744
745    ReduceAll(T_resulttype initialValue)
746    {
747        reset(initialValue);
748    }
749
750    bool operator()(T_sourcetype x)
751    {
752        if (!bool(x))
753        {
754            all_ = false;
755            return false;
756        }
757        else
758            return true;
759    }
760
761    bool operator()(T_sourcetype x, int)
762    {
763        if (!bool(x))
764        {
765            all_ = false;
766            return false;
767        }
768        else
769            return true;
770    }
771
772    T_resulttype result(int)
773    { return all_; }
774
775    void reset()
776    { all_ = true; }
777
778    void reset(T_resulttype)
779    {
780        BZPRECHECK(0, "Provided initial value for ReduceAll");
781        reset();
782    }
783
784    static const char* name()
785    { return "all"; }
786
787protected:
788    T_resulttype all_;
789}; 
790
791BZ_NAMESPACE_END
792
793#endif // BZ_REDUCE_H
Note: See TracBrowser for help on using the repository browser.