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