$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
p_image.hh
1 // Copyright (C) 2007, 2008, 2009, 2013 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_CORE_SITE_SET_P_IMAGE_HH
28 # define MLN_CORE_SITE_SET_P_IMAGE_HH
29 
36 
37 
38 # include <mln/core/site_set/p_if.hh>
39 # include <mln/fun/ops.hh>
40 # include <mln/pw/value.hh>
41 # include <mln/pw/cst.hh>
42 # include <mln/data/fill_with_value.hh>
43 
44 
45 
46 namespace mln
47 {
48 
49  // Forward declaration.
50  template <typename I> class p_image;
51 
52 
53  namespace trait
54  {
55 
56  template <typename I>
57  struct site_set_< p_image<I> >
58  {
59  typedef trait::site_set::nsites::known nsites;
60  typedef trait::site_set::bbox::unknown bbox; // FIXME
61  typedef trait::site_set::contents::free contents;
62  typedef trait::site_set::arity::unique arity;
63  };
64 
65  } // end of namespace trait
66 
67 
68  namespace internal
69  {
70 
71  template <typename I>
72  struct p_image_site_set // Hack to help g++-2.95.
73  {
74  private:
75  typedef mln_domain(I) S_;
76  typedef fun::eq_v2b_expr_< pw::value_<I>, pw::cst_<bool> > F_;
77  public:
78  typedef p_if<S_, F_> ret;
79  };
80 
81 
82  } // end of namespace internal
83 
84 
88  template <typename I>
89  class p_image : public internal::site_set_base_< mln_psite(I), p_image<I> >
90  {
91  public:
92 
95 
97  operator typename internal::p_image_site_set<I>::ret () const;
98 
99 
101  typedef mln_psite(I) element;
102 
103 
105  typedef mln_psite(I) psite;
106 
108  typedef mln_fwd_piter(S) fwd_piter;
109 
111  typedef mln_bkd_piter(S) bkd_piter;
112 
114  typedef mln_piter(S) piter;
115 
116 
118  p_image();
119 
121  p_image(const I& ima);
122 
123 
124 
126  bool has(const psite&) const;
127 
128 
130  bool is_valid() const;
131 
133  unsigned nsites() const;
134 
135 
137  typedef psite i_element;
138 
140  void insert(const psite& p);
141 
143  typedef psite r_element;
144 
146  void remove(const psite& p);
147 
148 
150  void toggle(const psite& p);
151 
152 
154  std::size_t memory_size() const;
155 
156 
158  void clear();
159 
160 
163  const I& image_hook_() const;
165 
166  private:
167 
168  I ima_;
169  unsigned nsites_;
170  };
171 
172 
173 # ifndef MLN_INCLUDE_ONLY
174 
175  template <typename I>
176  inline
178  {
179  S tmp(ima_.domain(), pw::value(ima_) == pw::cst(true));
180  return tmp;
181  }
182 
183  template <typename I>
184  inline
186  {
187  nsites_ = 0;
188  }
189 
190  template <typename I>
191  inline
192  p_image<I>::p_image(const I& ima)
193  {
194  mln_precondition(ima.is_valid());
195  ima_ = ima;
196  clear();
197  }
198 
199  template <typename I>
200  inline
201  bool
202  p_image<I>::has(const psite& p) const
203  {
204  mln_precondition(is_valid());
205  return ima_.domain().has(p) && ima_(p) == true;
206  }
207 
208  template <typename I>
209  inline
210  bool
211  p_image<I>::is_valid() const
212  {
213  return ima_.is_valid();
214  }
215 
216  template <typename I>
217  inline
218  unsigned
219  p_image<I>::nsites() const
220  {
221  return nsites_;
222  }
223 
224  template <typename I>
225  inline
226  void
227  p_image<I>::insert(const psite& p)
228  {
229  mln_precondition(is_valid());
230  mln_precondition(ima_.domain().has(p));
231  if (ima_(p) == true)
232  return; // No-op.
233  ima_(p) = true;
234  ++nsites_;
235  }
236 
237  template <typename I>
238  inline
239  void
240  p_image<I>::remove(const psite& p)
241  {
242  mln_precondition(is_valid());
243  mln_precondition(ima_.domain().has(p));
244  if (ima_(p) == false)
245  return; // No-op.
246  ima_(p) = false;
247  mln_assertion(nsites_ > 0);
248  --nsites_;
249  }
250 
251  template <typename I>
252  inline
253  void
254  p_image<I>::toggle(const psite& p)
255  {
256  mln_precondition(is_valid());
257  mln_precondition(ima_.domain().has(p));
258  if (ima_(p) == true)
259  {
260  // Removal.
261  ima_(p) = false;
262  mln_assertion(nsites_ > 0);
263  --nsites_;
264  }
265  else
266  {
267  // Insertion.
268  ima_(p) = true;
269  ++nsites_;
270  }
271  }
272 
273  template <typename I>
274  inline
275  std::size_t
277  {
278  if (! is_valid())
279  return sizeof(*this);
280  return 0; // FIXME
281  }
282 
283  template <typename I>
284  inline
285  void
287  {
288  if (! is_valid())
289  return; // No-op.
290  nsites_ = 0;
291  data::fill_with_value(ima_, false);
292  }
293 
294  template <typename I>
295  inline
296  const I&
298  {
299  mln_precondition(is_valid());
300  return ima_;
301  }
302 
303 # endif // ! MLN_INCLUDE_ONLY
304 
305 } // end of namespace mln
306 
307 
308 #endif // ! MLN_CORE_SITE_SET_P_IMAGE_HH