1 /**
2  *
3  *   Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6)
4  *   {prenom.nom}_at_lip6.fr
5  *
6  *  This library is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU Lesser General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public License
17  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief sum aggregator
25  *
26  * @author Gaspard DUCAMP
27  */
28 
29 #include <agrum/tools/multidim/aggregators/sum.h>
30 
31 namespace gum {
32 
33   namespace aggregator {
34     template < typename GUM_SCALAR >
Sum()35     INLINE Sum< GUM_SCALAR >::Sum() : MultiDimAggregator< GUM_SCALAR >() {
36       this->decomposable_ = true;
37       GUM_CONSTRUCTOR(Sum);
38     }
39 
40     template < typename GUM_SCALAR >
Sum(const Sum<GUM_SCALAR> & from)41     INLINE Sum< GUM_SCALAR >::Sum(const Sum< GUM_SCALAR >& from) :
42         MultiDimAggregator< GUM_SCALAR >(from) {
43       GUM_CONS_CPY(Sum);
44     }
45 
46     template < typename GUM_SCALAR >
~Sum()47     INLINE Sum< GUM_SCALAR >::~Sum() {
48       GUM_DESTRUCTOR(Sum);
49     }
50 
51     template < typename GUM_SCALAR >
neutralElt_()52     INLINE Idx Sum< GUM_SCALAR >::neutralElt_() const {
53       return (Idx)0;
54     }
55 
56     template < typename GUM_SCALAR >
fold_(const DiscreteVariable & v,Idx i1,Idx i2,bool & stop_iteration)57     INLINE Idx Sum< GUM_SCALAR >::fold_(const DiscreteVariable& v,
58                                         Idx                     i1,
59                                         Idx                     i2,
60                                         bool&                   stop_iteration) const {
61       GUM_SCALAR max_val
62          = this->variable((Idx)0).numerical(this->variable((Idx)0).domainSize() - 1);
63       if ((i1 + i2) > max_val) {
64         stop_iteration = true;
65         return (Idx)max_val;
66       }
67       return i1 + i2;
68     }
69 
70     template < typename GUM_SCALAR >
aggregatorName()71     INLINE std::string Sum< GUM_SCALAR >::aggregatorName() const {
72       return "sum";
73     }
74 
75     template < typename GUM_SCALAR >
newFactory()76     INLINE MultiDimContainer< GUM_SCALAR >* Sum< GUM_SCALAR >::newFactory() const {
77       return new Sum< GUM_SCALAR >;
78     }
79 
80   }   // namespace aggregator
81 }   // namespace gum