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