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