Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
p_mutable_array_of.hh
1 // Copyright (C) 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_MUTABLE_ARRAY_OF_HH
27 # define MLN_CORE_SITE_SET_P_MUTABLE_ARRAY_OF_HH
28 
41 # include <mln/core/site_set/p_double.hh>
42 # include <mln/core/internal/site_set_base.hh>
43 # include <mln/util/array.hh>
44 
45 
46 
47 namespace mln
48 {
49 
50  // Forward declaration.
51  template <typename S> class p_mutable_array_of;
52 
53 
54  namespace trait
55  {
56 
57  template <typename S>
58  struct site_set_< p_mutable_array_of<S> >
59  {
60  typedef trait::site_set::nsites::unknown nsites;
61  typedef trait::site_set::bbox::unknown bbox;
62  typedef trait::site_set::contents::growing contents;
63  typedef trait::site_set::arity::multiple arity;
64  };
65 
66  } // end of namespace trait
67 
68 
69 
75  template <typename S>
76  class p_mutable_array_of : public internal::site_set_base_< mln_site(S),
77  p_mutable_array_of<S> >,
78  private mlc_is_a(S, Site_Set)::check_t
79  {
81  typedef util::array<S> array_;
82  public:
83 
85  typedef S element;
86 
87 
89  typedef p_double_psite<self_, element> psite;
90 
92  typedef p_double_piter<self_,
93  mln_fwd_eiter(array_),
94  mln_fwd_piter(S)> fwd_piter;
95 
97  typedef p_double_piter<self_,
98  mln_bkd_eiter(array_),
99  mln_bkd_piter(S)> bkd_piter;
100 
102  typedef fwd_piter piter;
103 
104 
107 
108 
110  void reserve(unsigned n);
111 
112 
114  bool has(const psite& p) const;
115 
117  bool is_valid() const;
118 
119 
121  typedef S i_element;
122 
125  void insert(const S& s);
126 
127 
129  const S& operator[](unsigned i) const;
130 
132  S& operator[](unsigned i);
133 
135  unsigned nelements() const;
136 
137 
139  void clear();
140 
142  bool is_empty_() const; // Override the default impl since we have not .nsites().
143 
144 
146  std::size_t memory_size() const;
147 
149  const util::array<S>& array_hook_() const;
150 
151 
152  // Required by p_double-related classes.
153  const util::array<S>& set_1_() const;
154  template <typename I>
155  const S& set_2_(const I& it) const;
156 
157  protected:
158 
160  util::array<S> arr_;
161  };
162 
163 
164 
165  template <typename S>
166  std::ostream& operator<<(std::ostream& ostr, const p_mutable_array_of<S>& r);
167 
168 
169 
170 
171 # ifndef MLN_INCLUDE_ONLY
172 
173  template <typename S>
174  inline
176  {
177  }
178 
179  template <typename S>
180  inline
181  void
183  {
184  arr_.reserve(n);
185  }
186 
187  template <typename S>
188  inline
189  bool
191  {
192  return p.index() < arr_.nelements() && arr_[p.index()].has(p.p());
193  }
194 
195  template <typename S>
196  inline
197  bool
199  {
200  return true;
201  }
202 
203  template <typename S>
204  inline
205  void
207  {
208  mln_precondition(s.is_valid());
209  arr_.append(s);
210  }
211 
212  template <typename S>
213  inline
214  const S&
216  {
217  mln_precondition(i < arr_.nelements());
218  return arr_[i];
219  }
220 
221  template <typename S>
222  inline
223  S&
225  {
226  mln_precondition(i < arr_.nelements());
227  return arr_[i];
228  }
229 
230  template <typename S>
231  inline
232  unsigned
234  {
235  return arr_.nelements();
236  }
237 
238  template <typename S>
239  inline
240  void
242  {
243  arr_.clear();
244  mln_postcondition(this->is_empty());
245  }
246 
247  template <typename S>
248  inline
249  bool
251  {
252  return arr_.is_empty();
253  }
254 
255  template <typename S>
256  inline
257  std::size_t
259  {
260  return arr_.memory_size();
261  }
262 
263  template <typename S>
264  inline
265  const util::array<S>&
267  {
268  return arr_;
269  }
270 
271  template <typename S>
272  inline
273  const util::array<S>&
274  p_mutable_array_of<S>::set_1_() const
275  {
276  return arr_;
277  }
278 
279  template <typename S>
280  template <typename I>
281  inline
282  const S&
283  p_mutable_array_of<S>::set_2_(const I& it) const
284  {
285  return it.element();
286  }
287 
288 
289  template <typename S>
290  std::ostream& operator<<(std::ostream& ostr, const p_mutable_array_of<S>& a)
291  {
292  return ostr << a.array_hook_();
293  }
294 
295 # endif // ! MLN_INCLUDE_ONLY
296 
297 } // end of namespace mln
298 
299 
300 #endif // ! MLN_CORE_SITE_SET_P_MUTABLE_ARRAY_OF_HH