$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
count_labels.hh
1 // Copyright (C) 2009, 2012 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_LABELS_HH
28 # define MLN_ACCU_COUNT_LABELS_HH
29 
33 
34 # include <vector>
35 # include <mln/accu/internal/base.hh>
36 # include <mln/core/concept/meta_accumulator.hh>
37 # include <mln/metal/is_a.hh>
38 
39 namespace mln
40 {
41 
42  // Forward declaration.
43  namespace value { template <typename E> struct Symbolic; }
44 
45  namespace accu
46  {
47 
52  //
53  template <typename L>
54  struct count_labels
55  : public mln::accu::internal::base< unsigned , count_labels<L> >
56  // mlc_is_a(L, mln::value::Symbolic)::check_t
57  {
58  typedef L argument;
59 
60  count_labels();
61 
64  void init();
65  void take(const argument&);
66  void take(const count_labels<L>& other);
67 
69  void set_value(unsigned c);
71 
73  unsigned to_result() const;
74 
77  bool is_valid() const;
78 
79  protected:
81  unsigned count_labels_;
82  std::vector<bool> deja_vu_;
83  };
84 
85 
86  namespace meta
87  {
88 
90  struct count_labels : public Meta_Accumulator< count_labels >
91  {
92  template <typename L>
93  struct with
94  {
96  };
97  };
98 
99  } // end of namespace mln::accu::meta
100 
101 
102 
103 # ifndef MLN_INCLUDE_ONLY
104 
105  template <typename L>
106  inline
108  {
109  init();
110  }
111 
112  template <typename L>
113  inline
114  void
116  {
117  count_labels_ = 0;
118  deja_vu_.resize(mln_max(L), false);
119  }
120 
121  template <typename L>
122  inline
123  void
124  count_labels<L>::take(const argument& l)
125  {
126  if (!deja_vu_[l])
127  {
128  ++count_labels_;
129  deja_vu_[l] = true;
130  }
131  //else
132  // No-op
133  }
134 
135  template <typename L>
136  inline
137  void
138  count_labels<L>::take(const count_labels<L>& other)
139  {
140  count_labels_ += other.count_labels_;
141  for (unsigned i = 0; i < deja_vu_.size(); ++i)
142  deja_vu_[i] = deja_vu_[i] || other.deja_vu_[i];
143  }
144 
145  template <typename L>
146  inline
147  unsigned
149  {
150  // The background label MUST not be counted.
151  return count_labels_ - 1;
152  }
153 
154  template <typename L>
155  inline
156  void
157  count_labels<L>::set_value(unsigned c)
158  {
159  count_labels_ = c;
160  }
161 
162  template <typename L>
163  inline
164  bool
166  {
167  return true;
168  }
169 
170 # endif // ! MLN_INCLUDE_ONLY
171 
172  } // end of namespace mln::accu
173 
174 } // end of namespace mln
175 
176 
177 #endif // ! MLN_ACCU_COUNT_LABELS_HH