$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
count_value.hh
1 // Copyright (C) 2009, 2010 EPITA Research and Development Laboratory
2 // (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_COUNT_VALUE_HH
28 # define MLN_ACCU_COUNT_VALUE_HH
29 
33 
34 # include <mln/accu/internal/base.hh>
35 # include <mln/core/concept/meta_accumulator.hh>
36 # include <mln/metal/is_a.hh>
37 
38 namespace mln
39 {
40 
41  // Forward declaration.
42  namespace accu {
43  template <typename V> struct count_value;
44  }
45 
46 
47  // Traits.
48 
49  namespace trait
50  {
51 
52  template <typename V>
53  struct accumulator_< accu::count_value<V> >
54  {
55  typedef accumulator::has_untake::yes has_untake;
56  typedef accumulator::has_set_value::yes has_set_value;
57  typedef accumulator::has_stop::no has_stop;
58  typedef accumulator::when_pix::use_v when_pix;
59  };
60 
61  } // end of namespace mln::trait
62 
63 
64  namespace accu
65  {
66 
70  //
71  template <typename V>
72  struct count_value
73  : public mln::accu::internal::base< unsigned , count_value<V> >
74  {
75  typedef V argument;
76 
77  count_value();
78  explicit count_value(const V& ref);
79 
82  void init();
83  void take(const argument&);
84  void take(const count_value<V>& other);
85  void untake(const argument&);
86  void untake(const count_value<V>& other);
87 
89  void set_value(unsigned c);
91 
93  unsigned to_result() const;
94 
97  bool is_valid() const;
98 
99  protected:
101  unsigned count_;
103  V ref_;
104 
106  bool valid_;
107  };
108 
109 
110  namespace meta
111  {
112 
116  struct count_value : public Meta_Accumulator< count_value >
117  {
118 
119  template <typename V>
120  struct with
121  {
123  };
124 
125  };
126 
127  } // end of namespace mln::accu::meta
128 
129 
130 
131 # ifndef MLN_INCLUDE_ONLY
132 
133  template <typename V>
134  inline
136  {
137  init();
138  valid_ = false;
139  }
140 
141  template <typename V>
142  inline
143  count_value<V>::count_value(const V& ref)
144  {
145  ref_ = ref;
146  valid_ = true;
147  init();
148  }
149 
150  template <typename V>
151  inline
152  void
154  {
155  count_ = 0;
156  }
157 
158  template <typename V>
159  inline
160  void
161  count_value<V>::take(const argument& l)
162  {
163  if (l == ref_)
164  ++count_;
165  }
166 
167  template <typename V>
168  inline
169  void
170  count_value<V>::take(const count_value<V>& other)
171  {
172  mln_precondition(other.is_valid());
173  mln_precondition(other.ref_ == ref_);
174  count_ += other.count_;
175  }
176 
177  template <typename V>
178  inline
179  void
180  count_value<V>::untake(const argument& l)
181  {
182  if (l == ref_)
183  --count_;
184  }
185 
186  template <typename V>
187  inline
188  void
189  count_value<V>::untake(const count_value<V>& other)
190  {
191  mln_precondition(other.is_valid());
192  mln_precondition(other.ref_ == ref_);
193  count_ -= other.count_;
194  }
195 
196  template <typename V>
197  inline
198  unsigned
200  {
201  // The background label MUST not be counted.
202  return count_;
203  }
204 
205  template <typename V>
206  inline
207  void
208  count_value<V>::set_value(unsigned c)
209  {
210  count_ = c;
211  }
212 
213  template <typename V>
214  inline
215  bool
217  {
218  return valid_;
219  }
220 
221 # endif // ! MLN_INCLUDE_ONLY
222 
223  } // end of namespace mln::accu
224 
225 } // end of namespace mln
226 
227 
228 #endif // ! MLN_ACCU_COUNT_VALUE_HH