26 #ifndef MLN_UTIL_LAZY_SET_HH
27 # define MLN_UTIL_LAZY_SET_HH
39 # include <mln/core/internal/force_exact.hh>
40 # include <mln/core/contract.hh>
81 lazy_set_<E>& insert(
const E& elt);
92 lazy_set_<E>&
remove(
const E& elt);
103 const E& element(
unsigned i)
const;
113 const E& operator[](
unsigned i)
const;
117 unsigned nelements()
const;
126 bool has(
const E& elt)
const;
131 bool is_empty()
const;
152 const std::vector<E>& vect()
const;
167 void set_const_mode(
bool mode);
170 bool get_mode()
const;
179 mutable std::vector<E> v_;
192 void update_()
const;
195 mutable bool needs_update_;
202 # ifndef MLN_INCLUDE_ONLY
204 template <
typename E>
206 lazy_set_<E>::lazy_set_()
208 needs_update_ =
false;
212 template <
typename E>
215 lazy_set_<E>::insert(
const E& elt)
217 mln_assertion(!mode_);
219 if (needs_update_ ==
false)
220 needs_update_ =
true;
221 return mln::internal::force_exact< lazy_set_<E> >(*this);
224 template <
typename E>
227 lazy_set_<E>::remove(
const E& elt)
229 mln_assertion(!mode_);
231 std::remove(s_.begin(), s_.end(), elt);
232 if (needs_update_ ==
false)
233 needs_update_ =
true;
234 return mln::internal::force_exact< lazy_set_<E> >(*this);
237 template <
typename E>
240 lazy_set_<E>::element(
unsigned i)
const
242 assert((!mode_ && i < s_.size())
250 template <
typename E>
253 lazy_set_<E>::operator[](
unsigned i)
const
255 return this->element(i);
258 template <
typename E>
261 lazy_set_<E>::nelements()
const
269 template <
typename E>
272 lazy_set_<E>::has(
const E& elt)
const
275 return s_.find(elt) != s_.end();
277 return v_.find(elt) != v_.end();
280 template <
typename E>
283 lazy_set_<E>::is_empty()
const
285 return nelements() == 0;
288 template <
typename E>
291 lazy_set_<E>::clear()
295 needs_update_ =
false;
297 mln_postcondition(is_empty());
300 template <
typename E>
302 const std::vector<E>&
303 lazy_set_<E>::vect()
const
311 template <
typename E>
314 lazy_set_<E>::set_const_mode(
bool mode)
326 mln_assertion(s_.size() == 0);
327 for (
typename std::vector<E>::iterator it = v_.begin();
328 it != v_.end(); it++)
330 needs_update_ =
false;
336 template <
typename E>
339 lazy_set_<E>::get_mode()
const
344 template <
typename E>
347 lazy_set_<E>::update_()
const
349 mln_precondition(needs_update_ && !mode_);
351 std::copy(s_.begin(), s_.end(), std::back_inserter(v_));
355 needs_update_ =
false;
371 # endif // ! MLN_INCLUDE_ONLY
378 #endif // ! MLN_UTIL_LAZY_SET_HH