$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
accu/math/sum.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_ACCU_MATH_SUM_HH
28 # define MLN_ACCU_MATH_SUM_HH
29 
33 
34 # include <mln/core/concept/meta_accumulator.hh>
35 # include <mln/accu/internal/base.hh>
36 
37 # include <mln/util/pix.hh> // To prevent accu::math::sum to work on pixels (ambiguous).
38 
39 # include <mln/trait/value_.hh> // For mln_sum.
40 # include <mln/value/builtin/all.hh> // In the case of summing builtin values.
41 # include <mln/literal/zero.hh> // For initialization.
42 
43 
44 namespace mln
45 {
46 
47  namespace accu
48  {
49 
50  namespace math
51  {
52 
53  // Forward declaration.
54  template <typename T, typename S>
55  struct sum;
56 
57  } // end of namespace mln::accu::math
58 
59  namespace meta
60  {
61 
62  namespace math
63  {
64 
66  struct sum : public Meta_Accumulator< sum >
67  {
68  template <typename T, typename S = mln_sum(T)>
69  struct with
70  {
72  };
73  };
74 
75  } // end of namespace mln::accu::meta::math
76 
77  } // end of namespace mln::accu::meta
78 
79  } // end of namespace mln::accu
80 
81  // Traits.
82  namespace trait
83  {
84 
85  template <typename T, typename S>
86  struct accumulator_< accu::math::sum<T,S> >
87  {
88  typedef accumulator::has_untake::yes has_untake;
89  typedef accumulator::has_set_value::yes has_set_value;
90  typedef accumulator::has_stop::no has_stop;
91  typedef accumulator::when_pix::not_ok when_pix;
92  };
93 
94  } // end of namespace mln::trait
95 
96 
97  namespace accu
98  {
99 
100  namespace math
101  {
102 
110  //
111  template <typename T, typename S = mln_sum(T)>
112  struct sum : public mln::accu::internal::base< const S&, sum<T,S> >
113  {
114  typedef T argument;
115 
116  sum();
117 
120  void init();
121  void take(const argument& t);
122  void take_as_init_(const argument& t);
123  void take(const sum<T,S>& other);
124 
125  void untake(const argument& t);
126  void untake(const sum<T,S>& other);
127  void set_value(const S& s);
129 
131  const S& to_result() const;
132 
135  bool is_valid() const;
136 
137  protected:
138 
139  S s_;
140  };
141 
142 
143  template <typename I, typename S>
144  struct sum< util::pix<I>, S >;
145 
146 
147 # ifndef MLN_INCLUDE_ONLY
148 
149 
150 
151  template <typename T, typename S>
152  inline
153  sum<T,S>::sum()
154  {
155  init();
156  }
157 
158  template <typename T, typename S>
159  inline
160  void
162  {
163  s_ = literal::zero;
164  }
165 
166  template <typename T, typename S>
167  inline
168  void sum<T,S>::take(const argument& t)
169  {
170  s_ += static_cast<S>(t);
171  }
172 
173  template <typename T, typename S>
174  inline
175  void sum<T,S>::untake(const argument& t)
176  {
177  s_ -= static_cast<S>(t);
178  }
179 
180  template <typename T, typename S>
181  inline
182  void sum<T,S>::take_as_init_(const argument& t)
183  {
184  s_ = static_cast<S>(t);
185  }
186 
187  template <typename T, typename S>
188  inline
189  void
190  sum<T,S>::take(const sum<T,S>& other)
191  {
192  s_ += other.s_;
193  }
194 
195  template <typename T, typename S>
196  inline
197  void
198  sum<T,S>::untake(const sum<T,S>& other)
199  {
200  s_ -= other.s_;
201  }
202 
203  template <typename T, typename S>
204  inline
205  const S&
206  sum<T,S>::to_result() const
207  {
208  return s_;
209  }
210 
211  template <typename T, typename S>
212  inline
213  void
214  sum<T,S>::set_value(const S& s)
215  {
216  s_ = s;
217  }
218 
219  template <typename T, typename S>
220  inline
221  bool
222  sum<T,S>::is_valid() const
223  {
224  return true;
225  }
226 
227 # endif // ! MLN_INCLUDE_ONLY
228 
229  } // end of namespace mln::accu::math
230 
231  } // end of namespace mln::accu
232 
233 } // end of namespace mln
234 
235 
236 #endif // ! MLN_ACCU_MATH_SUM_HH