$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
set_of.hh
1 // Copyright (C) 2007, 2008, 2009, 2012, 2013 EPITA Research and
2 // Development 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_INTERNAL_SET_OF_HH
28 # define MLN_CORE_INTERNAL_SET_OF_HH
29 
33 
34 # include <vector>
35 # include <set>
36 # include <iterator>
37 # include <algorithm>
38 
39 # include <mln/core/contract.hh>
40 # include <mln/core/internal/force_exact.hh>
41 
42 
43 namespace mln
44 {
45 
46  namespace internal
47  {
48 
61  template <typename E>
62  class set_of_
63  {
64  public:
65 
66 
75  set_of_<E>& insert(const E& elt);
76 
77 
86  set_of_<E>& remove(const E& elt);
87 
88 
97  const E& element(unsigned i) const;
98 
99 
102  unsigned nelements() const;
103 
104 
111  bool has(const E& elt) const;
112 
113 
116  bool is_empty() const;
117 
118 
126  void clear();
127 
128 
135  const std::vector<E>& vect() const;
136 
137  protected:
138 
140  set_of_();
141 
142  private:
143 
149  mutable std::vector<E> v_;
150 
151  protected:
156  std::set<E> s_;
157 
158  private:
163  void update_() const;
164 
166  mutable bool needs_update_;
167  };
168 
169 
170  /* Print a set \p s into the output stream \p
171  * ostr.
172  *
173  * \param[in,out] ostr An output stream.
174  * \param[in] s A set.
175  *
176  * \return The modified output stream \p ostr.
177  *
178  * \relates mln::internal::set_of_
179  */
180  // FIXME : ambiguous with site_set operator <<
181  // template <typename E>
182  // std::ostream& operator<<(std::ostream& ostr, const set_of_<E>& s);
183 
184 
185 
186 # ifndef MLN_INCLUDE_ONLY
187 
188  template <typename E>
189  inline
191  {
192  needs_update_ = false;
193  }
194 
195  template <typename E>
196  inline
197  set_of_<E>&
198  set_of_<E>::insert(const E& elt)
199  {
200  s_.insert(elt);
201  if (needs_update_ == false)
202  needs_update_ = true;
203  return internal::force_exact< set_of_<E> >(*this);
204  }
205 
206  template <typename E>
207  inline
208  set_of_<E>&
209  set_of_<E>::remove(const E& elt)
210  {
211  s_.erase(elt);
212  if (needs_update_ == false)
213  needs_update_ = true;
214  return internal::force_exact< set_of_<E> >(*this);
215  }
216 
217  template <typename E>
218  inline
219  const E&
220  set_of_<E>::element(unsigned i) const
221  {
222  if (needs_update_)
223  update_();
224  mln_precondition(i < v_.size());
225  return v_[i];
226  }
227 
228  template <typename E>
229  inline
230  unsigned
231  set_of_<E>::nelements() const
232  {
233  if (needs_update_)
234  update_();
235  return v_.size();
236  }
237 
238  template <typename E>
239  inline
240  bool
241  set_of_<E>::has(const E& elt) const
242  {
243  return s_.find(elt) != s_.end();
244  }
245 
246  template <typename E>
247  inline
248  bool
249  set_of_<E>::is_empty() const
250  {
251  return nelements() == 0;
252  }
253 
254  template <typename E>
255  inline
256  void
258  {
259  v_.clear();
260  s_.clear();
261  needs_update_ = false;
262  mln_postcondition(is_empty());
263  }
264 
265  template <typename E>
266  inline
267  const std::vector<E>&
268  set_of_<E>::vect() const
269  {
270  if (needs_update_)
271  update_();
272  return v_;
273  }
274 
275  template <typename E>
276  inline
277  void
278  set_of_<E>::update_() const
279  {
280  mln_precondition(needs_update_);
281  v_.clear();
282  std::copy(s_.begin(), s_.end(), std::back_inserter(v_));
283  // no s_.clear() here:
284  // - we want to keep information up-to-date in s_
285  // - we want to save some execution time
286  needs_update_ = false;
287  }
288 
289  // FIXME : ambiguous with site_set operator <<
290  // template <typename E>
291  // std::ostream& operator<<(std::ostream& ostr,
292  // const set_of_<E>& s)
293  // {
294  // ostr << '[';
295  // const unsigned n = s.nelements();
296  // for (unsigned i = 0; i < n; ++i)
297  // ostr << s.element(i)
298  // << (i == s.nelements() - 1 ? ']' : ',');
299  // return ostr;
300  // }
301 
302 # endif // ! MLN_INCLUDE_ONLY
303 
304  } // end of namespace mln::internal
305 
306 } // end of namespace mln
307 
308 
309 #endif // ! MLN_CORE_INTERNAL_SET_OF_HH