$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sumpow.hh
1 // Copyright (C) 2011 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_SUMPOW_HH
28 # define MLN_ACCU_MATH_SUMPOW_HH
29 
33 
34 # include <cmath>
35 # include <mln/core/concept/meta_accumulator.hh>
36 # include <mln/accu/internal/base.hh>
37 
38 # include <mln/util/pix.hh> // To prevent accu::math::sumpow to work on pixels (ambiguous).
39 
40 # include <mln/trait/value_.hh> // For mln_sum.
41 # include <mln/value/builtin/all.hh> // In the case of summing builtin values.
42 # include <mln/literal/zero.hh> // For initialization.
43 
44 
45 namespace mln
46 {
47 
48  namespace accu { namespace math { template <unsigned n, typename T, typename S> struct sumpow; } };
49 
50  // Traits.
51  namespace trait
52  {
53 
54  template <unsigned n, typename T, typename S>
55  struct accumulator_< accu::math::sumpow<n,T,S> >
56  {
57  typedef accumulator::has_untake::yes has_untake;
58  typedef accumulator::has_set_value::yes has_set_value;
59  typedef accumulator::has_stop::no has_stop;
60  typedef accumulator::when_pix::not_ok when_pix;
61  };
62 
63  } // end of namespace mln::trait
64 
65 
66  namespace accu
67  {
68 
69  namespace math
70  {
71 
75  //
76  template <unsigned n, typename T, typename S = mln_sum(T)>
77  struct sumpow : public mln::accu::internal::base< const S&, sumpow<n,T,S> >
78  {
79  typedef T argument;
80 
81  sumpow();
82 
85  void init();
86  void take(const argument& t);
87  void take_as_init_(const argument& t);
88  void take(const sumpow<n,T,S>& other);
89 
90  void untake(const argument& t);
91  void untake(const sumpow<n,T,S>& other);
92  void set_value(const S& s);
94 
96  const S& to_result() const;
97 
100  bool is_valid() const;
101 
102  protected:
103 
104  S s_;
105  };
106 
107 
108  template <unsigned n, typename I, typename S>
109  struct sumpow< n, util::pix<I>, S >;
110 
111 
112 # ifndef MLN_INCLUDE_ONLY
113 
114 
115 
116  template <unsigned n, typename T, typename S>
117  inline
118  sumpow<n,T,S>::sumpow()
119  {
120  init();
121  }
122 
123  template <unsigned n, typename T, typename S>
124  inline
125  void
127  {
128  s_ = literal::zero;
129  }
130 
131  template <unsigned n, typename T, typename S>
132  inline
133  void sumpow<n,T,S>::take(const argument& t)
134  {
135  s_ += std::pow(static_cast<S>(t), n);
136  }
137 
138  template <unsigned n, typename T, typename S>
139  inline
140  void sumpow<n,T,S>::untake(const argument& t)
141  {
142  s_ -= std::pow(static_cast<S>(t), n);
143  }
144 
145  template <unsigned n, typename T, typename S>
146  inline
147  void sumpow<n,T,S>::take_as_init_(const argument& t)
148  {
149  s_ = std::pow(static_cast<S>(t), n);
150  }
151 
152  template <unsigned n, typename T, typename S>
153  inline
154  void
155  sumpow<n,T,S>::take(const sumpow<n,T,S>& other)
156  {
157  s_ += other.s_;
158  }
159 
160  template <unsigned n, typename T, typename S>
161  inline
162  void
163  sumpow<n,T,S>::untake(const sumpow<n,T,S>& other)
164  {
165  s_ -= other.s_;
166  }
167 
168  template <unsigned n, typename T, typename S>
169  inline
170  const S&
172  {
173  return s_;
174  }
175 
176  template <unsigned n, typename T, typename S>
177  inline
178  void
179  sumpow<n,T,S>::set_value(const S& s)
180  {
181  s_ = s;
182  }
183 
184  template <unsigned n, typename T, typename S>
185  inline
186  bool
188  {
189  return true;
190  }
191 
192 # endif // ! MLN_INCLUDE_ONLY
193 
194  } // end of namespace mln::accu::math
195 
196  } // end of namespace mln::accu
197 
198 } // end of namespace mln
199 
200 
201 #endif // ! MLN_ACCU_MATH_SUMPOW_HH