$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
p_mutable_array_of.hh
1 // Copyright (C) 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_MUTABLE_ARRAY_OF_HH
28 # define MLN_CORE_SITE_SET_P_MUTABLE_ARRAY_OF_HH
29 
42 # include <mln/core/site_set/p_double.hh>
43 # include <mln/core/internal/site_set_base.hh>
44 # include <mln/util/array.hh>
45 
46 
47 
48 namespace mln
49 {
50 
51  // Forward declaration.
52  template <typename S> class p_mutable_array_of;
53 
54 
55  namespace trait
56  {
57 
58  template <typename S>
59  struct site_set_< p_mutable_array_of<S> >
60  {
61  typedef trait::site_set::nsites::unknown nsites;
62  typedef trait::site_set::bbox::unknown bbox;
63  typedef trait::site_set::contents::growing contents;
64  typedef trait::site_set::arity::multiple arity;
65  };
66 
67  } // end of namespace trait
68 
69 
70 
76  template <typename S>
77  class p_mutable_array_of : public internal::site_set_base_< mln_site(S),
78  p_mutable_array_of<S> >,
79  private mlc_is_a(S, Site_Set)::check_t
80  {
82  typedef util::array<S> array_;
83  public:
84 
86  typedef S element;
87 
88 
91 
93  typedef p_double_piter<self_,
94  mln_fwd_eiter(array_),
95  mln_fwd_piter(S)> fwd_piter;
96 
98  typedef p_double_piter<self_,
99  mln_bkd_eiter(array_),
100  mln_bkd_piter(S)> bkd_piter;
101 
103  typedef fwd_piter piter;
104 
105 
108 
109 
111  void reserve(unsigned n);
112 
113 
115  bool has(const psite& p) const;
116 
118  bool is_valid() const;
119 
120 
122  typedef S i_element;
123 
126  void insert(const S& s);
127 
128 
130  const S& operator[](unsigned i) const;
131 
133  S& operator[](unsigned i);
134 
136  unsigned nelements() const;
137 
138 
140  void clear();
141 
144  bool is_empty_() const; // Override the default impl since we have not .nsites().
146 
148  std::size_t memory_size() const;
149 
152  const util::array<S>& array_hook_() const;
153 
154 
155  // Required by p_double-related classes.
156  const util::array<S>& set_1_() const;
157  template <typename I>
158  const S& set_2_(const I& it) const;
160 
161  protected:
162 
165  };
166 
167 
169  template <typename S>
170  std::ostream& operator<<(std::ostream& ostr, const p_mutable_array_of<S>& r);
171 
172 
173 
174 
175 # ifndef MLN_INCLUDE_ONLY
176 
177  template <typename S>
178  inline
180  {
181  }
182 
183  template <typename S>
184  inline
185  void
187  {
188  arr_.reserve(n);
189  }
190 
191  template <typename S>
192  inline
193  bool
194  p_mutable_array_of<S>::has(const psite& p) const
195  {
196  return p.index() < arr_.nelements() && arr_[p.index()].has(p.p());
197  }
198 
199  template <typename S>
200  inline
201  bool
203  {
204  return true;
205  }
206 
207  template <typename S>
208  inline
209  void
211  {
212  mln_precondition(s.is_valid());
213  arr_.append(s);
214  }
215 
216  template <typename S>
217  inline
218  const S&
219  p_mutable_array_of<S>::operator[](unsigned i) const
220  {
221  mln_precondition(i < arr_.nelements());
222  return arr_[i];
223  }
224 
225  template <typename S>
226  inline
227  S&
229  {
230  mln_precondition(i < arr_.nelements());
231  return arr_[i];
232  }
233 
234  template <typename S>
235  inline
236  unsigned
238  {
239  return arr_.nelements();
240  }
241 
242  template <typename S>
243  inline
244  void
246  {
247  arr_.clear();
248  mln_postcondition(this->is_empty());
249  }
250 
251  template <typename S>
252  inline
253  bool
255  {
256  return arr_.is_empty();
257  }
258 
259  template <typename S>
260  inline
261  std::size_t
263  {
264  return arr_.memory_size();
265  }
266 
267  template <typename S>
268  inline
269  const util::array<S>&
271  {
272  return arr_;
273  }
274 
275  template <typename S>
276  inline
277  const util::array<S>&
279  {
280  return arr_;
281  }
282 
283  template <typename S>
284  template <typename I>
285  inline
286  const S&
287  p_mutable_array_of<S>::set_2_(const I& it) const
288  {
289  return it.element();
290  }
291 
292 
293  template <typename S>
294  std::ostream& operator<<(std::ostream& ostr, const p_mutable_array_of<S>& a)
295  {
296  return ostr << a.array_hook_();
297  }
298 
299 # endif // ! MLN_INCLUDE_ONLY
300 
301 } // end of namespace mln
302 
303 
304 #endif // ! MLN_CORE_SITE_SET_P_MUTABLE_ARRAY_OF_HH