00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #ifndef MLN_CORE_INTERNAL_SET_OF_HH
00027 # define MLN_CORE_INTERNAL_SET_OF_HH
00028 
00032 
00033 # include <vector>
00034 # include <set>
00035 # include <iterator>
00036 # include <algorithm>
00037 
00038 # include <mln/core/contract.hh>
00039 # include <mln/core/internal/force_exact.hh>
00040 
00041 
00042 namespace mln
00043 {
00044 
00045   namespace internal
00046   {
00047 
00058     template <typename E>
00059     class set_of_
00060     {
00061     public:
00062 
00063 
00072       set_of_<E>& insert(const E& elt);
00073 
00074 
00083       set_of_<E>& remove(const E& elt);
00084 
00085 
00094       const E& element(unsigned i) const;
00095 
00096 
00099       unsigned nelements() const;
00100 
00101 
00108       bool has(const E& elt) const;
00109 
00110 
00113       bool is_empty() const;
00114 
00115 
00123       void clear();
00124 
00125 
00132       const std::vector<E>& vect() const;
00133 
00134     protected:
00135 
00137       set_of_();
00138 
00139     private:
00140 
00146       mutable std::vector<E> v_;
00147 
00148     protected:
00153       std::set<E> s_;
00154 
00155     private:
00160       void update_() const;
00161 
00163       mutable bool needs_update_;
00164     };
00165 
00166 
00177     
00178     
00179     
00180 
00181 
00182 
00183 # ifndef MLN_INCLUDE_ONLY
00184 
00185     template <typename E>
00186     inline
00187     set_of_<E>::set_of_()
00188     {
00189       needs_update_ = false;
00190     }
00191 
00192     template <typename E>
00193     inline
00194     set_of_<E>&
00195     set_of_<E>::insert(const E& elt)
00196     {
00197       s_.insert(elt);
00198       if (needs_update_ == false)
00199         needs_update_ = true;
00200       return internal::force_exact< set_of_<E> >(*this);
00201     }
00202 
00203     template <typename E>
00204     inline
00205     set_of_<E>&
00206     set_of_<E>::remove(const E& elt)
00207     {
00208       s_.erase(elt);
00209       if (needs_update_ == false)
00210         needs_update_ = true;
00211       return internal::force_exact< set_of_<E> >(*this);
00212     }
00213 
00214     template <typename E>
00215     inline
00216     const E&
00217     set_of_<E>::element(unsigned i) const
00218     {
00219       if (needs_update_)
00220         update_();
00221       mln_precondition(i < v_.size());
00222       return v_[i];
00223     }
00224 
00225     template <typename E>
00226     inline
00227     unsigned
00228     set_of_<E>::nelements() const
00229     {
00230       if (needs_update_)
00231         update_();
00232       return v_.size();
00233     }
00234 
00235     template <typename E>
00236     inline
00237     bool
00238     set_of_<E>::has(const E& elt) const
00239     {
00240       return s_.find(elt) != s_.end();
00241     }
00242 
00243     template <typename E>
00244     inline
00245     bool
00246     set_of_<E>::is_empty() const
00247     {
00248       return nelements() == 0;
00249     }
00250 
00251     template <typename E>
00252     inline
00253     void
00254     set_of_<E>::clear()
00255     {
00256       v_.clear();
00257       s_.clear();
00258       needs_update_ = false;
00259       mln_postcondition(is_empty());
00260     }
00261 
00262     template <typename E>
00263     inline
00264     const std::vector<E>&
00265     set_of_<E>::vect() const
00266     {
00267       if (needs_update_)
00268         update_();
00269       return v_;
00270     }
00271 
00272     template <typename E>
00273     inline
00274     void
00275     set_of_<E>::update_() const
00276     {
00277       mln_precondition(needs_update_);
00278       v_.clear();
00279       std::copy(s_.begin(), s_.end(), std::back_inserter(v_));
00280       
00281       
00282       
00283       needs_update_ = false;
00284     }
00285 
00286     
00287     
00288     
00289     
00290     
00291     
00292     
00293     
00294     
00295     
00296     
00297     
00298 
00299 # endif // ! MLN_INCLUDE_ONLY
00300 
00301   } 
00302 
00303 } 
00304 
00305 
00306 #endif // ! MLN_CORE_INTERNAL_SET_OF_HH