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_UTIL_INTERNAL_GRAPH_NBH_ITER_BASE_HH
00027 # define MLN_UTIL_INTERNAL_GRAPH_NBH_ITER_BASE_HH
00028 
00029 # include <mln/core/concept/proxy.hh>
00030 
00034 
00035 namespace mln
00036 {
00037 
00038   
00039   template <typename S> class p_indexed_psite;
00040 
00041   namespace internal
00042   {
00043 
00044     template <typename G, typename C, typename Elt, typename E>
00045     class nbh_iterator_base
00046       : public Proxy< E >,
00047         public internal::proxy_impl< const Elt&, E >
00048     {
00049       public:
00050 
00054         bool is_valid() const;
00056         void invalidate();
00057 
00059         void start();
00060 
00062         void next();
00063 
00066         unsigned index() const;
00067 
00069         typename Elt::id_t id() const;
00070 
00072         operator typename Elt::id_t() const;
00073 
00076         operator typename Elt::id_value_t() const;
00077 
00079         const C& center() const;
00080 
00082         template <typename S>
00083         void center_at(const p_indexed_psite<S>& c);
00084 
00085         template <typename C2>
00086         void center_at(const C2& c);
00087 
00089         void change_target(const G& g);
00090 
00092         const Elt& element() const;
00093 
00095         const Elt& elt_hook_() const;
00096 
00098 
00102         const Elt& subj_();
00104 
00105       protected:
00108         nbh_iterator_base();
00109         template <typename C2>
00110         nbh_iterator_base(const C2& c);
00112 
00113         const C* c_; 
00114         Elt elt_;
00115         unsigned i_;
00116     };
00117 
00118 # ifndef MLN_INCLUDE_ONLY
00119 
00120     template <typename G, typename C, typename Elt, typename E>
00121     inline
00122     nbh_iterator_base<G,C,Elt,E>::nbh_iterator_base()
00123     {
00124     }
00125 
00126     template <typename G, typename C, typename Elt, typename E>
00127     template <typename C2>
00128     inline
00129     nbh_iterator_base<G,C,Elt,E>::nbh_iterator_base(const C2& c)
00130       : elt_(c.graph()), i_(0)
00131     {
00132       
00133       center_at(c);
00134     }
00135 
00136     template <typename G, typename C, typename Elt, typename E>
00137     inline
00138     bool
00139     nbh_iterator_base<G,C,Elt,E>::is_valid() const
00140     {
00141       return exact(this)->is_valid_();
00142     }
00143 
00144     template <typename G, typename C, typename Elt, typename E>
00145     inline
00146     void
00147     nbh_iterator_base<G,C,Elt,E>::invalidate()
00148     {
00149       i_ = mln_max(unsigned);
00150     }
00151 
00152     template <typename G, typename C, typename Elt, typename E>
00153     inline
00154     void
00155     nbh_iterator_base<G,C,Elt,E>::start()
00156     {
00157       i_ = exact(this)->start_id_();
00158       if (is_valid())
00159         exact(this)->update_();
00160     }
00161 
00162     template <typename G, typename C, typename Elt, typename E>
00163     inline
00164     void
00165     nbh_iterator_base<G,C,Elt,E>::next()
00166     {
00167       mln_precondition(is_valid());
00168       mln_precondition(c_->is_valid());
00169 
00170       i_ = exact(this)->next_id_();
00171       if (is_valid())
00172         exact(this)->update_();
00173     }
00174 
00175     template <typename G, typename C, typename Elt, typename E>
00176     inline
00177     unsigned
00178     nbh_iterator_base<G,C,Elt,E>::index() const
00179     {
00180       return i_;
00181     }
00182 
00183     template <typename G, typename C, typename Elt, typename E>
00184     inline
00185     typename Elt::id_t
00186     nbh_iterator_base<G,C,Elt,E>::id() const
00187     {
00188       return elt_.id();
00189     }
00190 
00191     template <typename G, typename C, typename Elt, typename E>
00192     inline
00193     nbh_iterator_base<G,C,Elt,E>::operator typename Elt::id_t() const
00194     {
00195       return elt_.id();
00196     }
00197 
00198     template <typename G, typename C, typename Elt, typename E>
00199     inline
00200     nbh_iterator_base<G,C,Elt,E>::operator typename Elt::id_value_t() const
00201     {
00202       return elt_.id();
00203     }
00204 
00205     template <typename G, typename C, typename Elt, typename E>
00206     inline
00207     const C&
00208     nbh_iterator_base<G,C,Elt,E>::center() const
00209     {
00210       mln_precondition(c_ != 0);
00211       return *c_;
00212     }
00213 
00214     template <typename G, typename C, typename Elt, typename E>
00215     inline
00216     const Elt&
00217     nbh_iterator_base<G,C,Elt,E>::subj_()
00218     {
00219       return elt_;
00220     }
00221 
00222     template <typename G, typename C, typename Elt, typename E>
00223     template <typename S>
00224     inline
00225     void
00226     nbh_iterator_base<G,C,Elt,E>::center_at(const p_indexed_psite<S>& c)
00227     {
00228       
00229       
00230       c_ = & static_cast< const C& >(c.unproxy_());
00231 
00232       
00233       
00234       
00235       elt_.change_graph(c_->graph());
00236 
00237       invalidate();
00238     }
00239 
00240     template <typename G, typename C, typename Elt, typename E>
00241     template <typename C2>
00242     inline
00243     void
00244     nbh_iterator_base<G,C,Elt,E>::center_at(const C2& c)
00245     {
00246       mlc_converts_to(C2, const C&)::check();
00247       c_ = & static_cast< const C& >(exact(c));
00248 
00249       
00250       
00251       
00252       elt_.change_graph(c_->graph());
00253 
00254       invalidate();
00255     }
00256 
00257     template <typename G, typename C, typename Elt, typename E>
00258     inline
00259     void
00260     nbh_iterator_base<G,C,Elt,E>::change_target(const G& g)
00261     {
00262       elt_.change_graph(g);
00263     }
00264 
00265     template <typename G, typename C, typename Elt, typename E>
00266     inline
00267     const Elt&
00268     nbh_iterator_base<G,C,Elt,E>::element() const
00269     {
00270       return elt_;
00271     }
00272 
00273     template <typename G, typename C, typename Elt, typename E>
00274     inline
00275     const Elt&
00276     nbh_iterator_base<G,C,Elt,E>::elt_hook_() const
00277     {
00278       return elt_;
00279     }
00280 
00281 # endif // !MLN_INCLUDE_ONLY
00282 
00283   } 
00284 
00285 } 
00286 
00287 
00288 #endif // ! MLN_UTIL_INTERNAL_GRAPH_NBH_ITER_BASE_HH