$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
accu/histo.hh
1 // Copyright (C) 2007, 2008, 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_ACCU_HISTO_HH
27 # define MLN_ACCU_HISTO_HH
28 
34 
35 # include <vector>
36 # include <algorithm>
37 
38 # include <mln/core/concept/value_set.hh>
39 # include <mln/core/concept/meta_accumulator.hh>
40 # include <mln/accu/internal/base.hh>
41 # include <mln/value/set.hh>
42 # include <mln/histo/array.hh>
43 
44 
45 namespace mln
46 {
47 
48  namespace accu
49  {
50 
51 
55  template <typename V>
56  struct histo :
57  public mln::accu::internal::base<const std::vector<unsigned>& ,
58  histo<V> >
59  {
60  histo();
61 
62  typedef V argument;
63 
66  void take(const argument& t);
67  void take(const histo<V>& other);
68  void untake(const argument& t);
69  void init();
70 
71  unsigned operator()(const argument& t) const;
72  unsigned operator[](unsigned i) const;
73  unsigned nvalues() const;
74  unsigned sum() const;
76 
79  const std::vector<unsigned>& vect() const;
80  const std::vector<unsigned>& to_result() const;
82 
83  const value::set<V>& vset() const;
84 
87  bool is_valid() const;
88 
89  protected:
90 
92  unsigned sum_;
93  };
94 
95  template <typename V>
96  std::ostream& operator<<(std::ostream& ostr, const histo<V>& h);
97 
98  namespace meta
99  {
100 
102  struct histo : public Meta_Accumulator< histo >
103  {
104  template <typename V>
105  struct with
106  {
108  };
109  };
110 
111  } // end of namespace mln::accu::meta
112 
113 
114 
115 
116 # ifndef MLN_INCLUDE_ONLY
117 
118  template <typename V>
119  inline
121  : h_(),
122  sum_(0)
123  {
124  }
125 
126  template <typename V>
127  inline
128  void
129  histo<V>::take(const argument& t)
130  {
131  ++h_[h_.vset().index_of(t)];
132  ++sum_;
133  }
134 
135  template <typename V>
136  inline
137  void
138  histo<V>::take(const histo<V>& other)
139  {
140  for (unsigned i = 0; i < h_.nvalues(); ++i)
141  h_[i] += other.h_[i];
142  sum_ += other.sum_;
143  }
144 
145  template <typename V>
146  inline
147  void
148  histo<V>::untake(const argument& t)
149  {
150  mln_precondition(h_[h_.vset().index_of(t)] > 0);
151  mln_precondition(sum_ > 0);
152  --h_[h_.vset().index_of(t)];
153  --sum_;
154  }
155 
156  template <typename V>
157  inline
158  void
159  histo<V>::init()
160  {
161  h_.clear();
162  sum_ = 0;
163  }
164 
165  template <typename V>
166  inline
167  unsigned
168  histo<V>::operator()(const argument& t) const
169  {
170  return h_[h_.vset().index_of(t)];
171  }
172 
173  template <typename V>
174  inline
175  unsigned
176  histo<V>::operator[](unsigned i) const
177  {
178  mln_precondition(i < h_.vset().nvalues());
179  return h_[i];
180  }
181 
182  template <typename V>
183  inline
184  unsigned
185  histo<V>::nvalues() const
186  {
187  return h_.vset().nvalues();
188  }
189 
190  template <typename V>
191  inline
192  unsigned
193  histo<V>::sum() const
194  {
195  return sum_;
196  }
197 
198  template <typename V>
199  inline
200  const std::vector<unsigned>&
201  histo<V>::vect() const
202  {
203  return h_.vect();
204  }
205 
206  template <typename V>
207  inline
208  const std::vector<unsigned>&
209  histo<V>::to_result() const
210  {
211  return this->vect();
212  }
213 
214  template <typename V>
215  inline
216  const value::set<V>&
217  histo<V>::vset() const
218  {
219  return h_.vset();
220  }
221 
222  template <typename V>
223  inline
224  std::ostream& operator<<(std::ostream& ostr, const histo<V>& h)
225  {
226  mln_viter(value::set<V>) v(h.vset());
227  for_all(v)
228  if (h(v) != 0)
229  ostr << v << ':' << h(v) << ' ';
230  return ostr;
231  }
232 
233  template <typename V>
234  inline
235  bool
236  histo<V>::is_valid() const
237  {
238  return true;
239  }
240 
241 # endif // ! MLN_INCLUDE_ONLY
242 
243  } // end of namespace mln::accu
244 
245 } // end of namespace mln
246 
247 
248 #endif // ! MLN_ACCU_HISTO_HH