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_SITE_SET_P_IMAGE_HH
00027 # define MLN_CORE_SITE_SET_P_IMAGE_HH
00028 
00035 
00036 
00037 # include <mln/core/site_set/p_if.hh>
00038 # include <mln/fun/ops.hh>
00039 # include <mln/pw/value.hh>
00040 # include <mln/pw/cst.hh>
00041 # include <mln/data/fill_with_value.hh>
00042 
00043 
00044 
00045 namespace mln
00046 {
00047 
00048   
00049   template <typename I> class p_image;
00050 
00051 
00052   namespace trait
00053   {
00054 
00055     template <typename I>
00056     struct site_set_< p_image<I> >
00057     {
00058       typedef trait::site_set::nsites::known  nsites;
00059       typedef trait::site_set::bbox::unknown  bbox; 
00060       typedef trait::site_set::contents::free contents;
00061       typedef trait::site_set::arity::unique  arity;
00062     };
00063 
00064   } 
00065 
00066 
00067   namespace internal
00068   {
00069 
00070     template <typename I>
00071     struct p_image_site_set 
00072     {
00073     private:
00074       typedef mln_domain(I) S_;
00075       typedef fun::eq_v2b_expr_< pw::value_<I>, pw::cst_<bool> > F_;
00076     public:
00077      typedef p_if<S_, F_> ret;
00078     };
00079     
00080 
00081   } 
00082 
00083 
00087   template <typename I>
00088   class p_image : public internal::site_set_base_< mln_psite(I), p_image<I> >
00089   {
00090   public:
00091 
00093     typedef typename internal::p_image_site_set<I>::ret S;
00094 
00096     operator typename internal::p_image_site_set<I>::ret () const;
00097 
00098 
00100     typedef mln_psite(I) element;
00101 
00102 
00104     typedef mln_psite(I) psite;
00105 
00107     typedef mln_fwd_piter(S) fwd_piter;
00108 
00110     typedef mln_bkd_piter(S) bkd_piter;
00111 
00113     typedef mln_piter(S) piter;
00114 
00115 
00117     p_image();
00118 
00120     p_image(const I& ima);
00121 
00122 
00123 
00125     bool has(const psite&) const;
00126 
00127 
00129     bool is_valid() const;
00130 
00132     unsigned nsites() const;
00133 
00134 
00136     typedef psite i_element;
00137 
00139     void insert(const psite& p);
00140 
00142     typedef psite r_element;
00143 
00145     void remove(const psite& p);
00146 
00147 
00149     void toggle(const psite& p);
00150 
00151 
00153     std::size_t memory_size() const;
00154 
00155 
00157     void clear();
00158 
00159 
00161     const I& image_hook_() const;
00162 
00163   private:
00164 
00165     I ima_;
00166     unsigned nsites_;
00167   };
00168 
00169 
00170 # ifndef MLN_INCLUDE_ONLY
00171 
00172   template <typename I>
00173   inline
00174   p_image<I>::operator typename internal::p_image_site_set<I>::ret () const
00175   {
00176     S tmp(ima_.domain(), pw::value(ima_) == pw::cst(true));
00177     return tmp;
00178   }
00179 
00180   template <typename I>
00181   inline
00182   p_image<I>::p_image()
00183   {
00184     nsites_ = 0;
00185   }
00186 
00187   template <typename I>
00188   inline
00189   p_image<I>::p_image(const I& ima)
00190   {
00191     mln_precondition(ima.is_valid());
00192     ima_ = ima;
00193     clear();
00194   }
00195 
00196   template <typename I>
00197   inline
00198   bool
00199   p_image<I>::has(const psite& p) const
00200   {
00201     mln_precondition(is_valid());
00202     return ima_.domain().has(p) && ima_(p) == true;
00203   }
00204 
00205   template <typename I>
00206   inline
00207   bool
00208   p_image<I>::is_valid() const
00209   {
00210     return ima_.is_valid();
00211   }
00212 
00213   template <typename I>
00214   inline
00215   unsigned
00216   p_image<I>::nsites() const
00217   {
00218     return nsites_;
00219   }
00220 
00221   template <typename I>
00222   inline
00223   void
00224   p_image<I>::insert(const psite& p)
00225   {
00226     mln_precondition(is_valid());
00227     mln_precondition(ima_.domain().has(p));
00228     if (ima_(p) == true)
00229       return; 
00230     ima_(p) = true;
00231     ++nsites_;
00232   }
00233 
00234   template <typename I>
00235   inline
00236   void
00237   p_image<I>::remove(const psite& p)
00238   {
00239     mln_precondition(is_valid());
00240     mln_precondition(ima_.domain().has(p));
00241     if (ima_(p) == false)
00242       return; 
00243     ima_(p) = false;
00244     mln_assertion(nsites_ > 0);
00245     --nsites_;
00246   }
00247 
00248   template <typename I>
00249   inline
00250   void
00251   p_image<I>::toggle(const psite& p)
00252   {
00253     mln_precondition(is_valid());
00254     mln_precondition(ima_.domain().has(p));
00255     if (ima_(p) == true)
00256       {
00257         
00258         ima_(p) = false;
00259         mln_assertion(nsites_ > 0);
00260         --nsites_;
00261       }
00262     else
00263       {
00264         
00265         ima_(p) = true;
00266         ++nsites_;
00267       }
00268   }
00269 
00270   template <typename I>
00271   inline
00272   std::size_t
00273   p_image<I>::memory_size() const
00274   {
00275     if (! is_valid())
00276       return sizeof(*this);
00277     return 0; 
00278   }
00279 
00280   template <typename I>
00281   inline
00282   void
00283   p_image<I>::clear()
00284   {
00285     if (! is_valid())
00286       return; 
00287     nsites_ = 0;
00288     data::fill_with_value(ima_, false);
00289   }
00290 
00291   template <typename I>
00292   inline
00293   const I&
00294   p_image<I>::image_hook_() const
00295   {
00296     mln_precondition(is_valid());
00297     return ima_;
00298   }
00299 
00300 # endif // ! MLN_INCLUDE_ONLY
00301 
00302 } 
00303 
00304 
00305 #endif // ! MLN_CORE_SITE_SET_P_IMAGE_HH