1743: Enrich the p_graph interface.

https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Nicolas Ballas <ballas@lrde.epita.fr> Enrich the p_graph interface. * mln/core/p_graph.hh: Improve the p_graph interface. * mln/core/graph_psite.hh, * mln/core/graph_window_piter.hh, * mln/core/graph_image.hh, * mln/core/p_graph_piter.hh, * mln/draw/graph.hh: Remove graph implementation details. core/graph_image.hh | 10 ---- core/graph_psite.hh | 4 - core/graph_window_piter.hh | 37 +++------------ core/p_graph.hh | 106 ++++++++++++++++++++++++++++++++++++++++++++- core/p_graph_piter.hh | 2 draw/graph.hh | 2 6 files changed, 119 insertions(+), 42 deletions(-) Index: mln/core/graph_psite.hh --- mln/core/graph_psite.hh (revision 1742) +++ mln/core/graph_psite.hh (working copy) @@ -130,7 +130,7 @@ inline graph_psite<P>::operator P() const { - return pg_.gr_.node_data(id_); + return pg_.point_from_id(id_); } template<typename P> @@ -138,7 +138,7 @@ const P& graph_psite<P>::to_point() const { - return pg_.gr_.node_data(id_); + return pg_.point_from_id(id_); } template<typename P> Index: mln/core/p_graph.hh --- mln/core/p_graph.hh (revision 1742) +++ mln/core/p_graph.hh (working copy) @@ -72,7 +72,31 @@ bool has(const psite& p) const; - // FIXME: Should be private. + /// Return the graph point (FIXME site?) from an index + const P& point_from_id(const util::node_id& id) const; + P& point_from_id(const util::node_id& id); + + + /// Return the point contained in the first node adjacent + // to the edge id \a e. + const P& node1(const util::edge_id& e) const; + /// Return the point contained in the second node adjacent + /// to the edge id \a e. + const P& node2(const util::edge_id& e) const; + + /// Return true if the psites lhs and rhs are adjacent, or equal. + bool adjacent_or_equal(const psite& lhs, const psite& rhs) const; + + /// Return true if the nodes lhs and rhs are adjacent, or equal + bool adjacent_or_equal(const util::node_id& lhs, + const util::node_id& rhs) const; + + /// Return the graph associated to the p_graph domain: + const graph& to_graph() const; + graph& to_graph(); + + + private: graph gr_; // FIXME: (Roland) Is it really useful/needed? /* 2007-12-19: It seems so, since graph_image must implement a method @@ -134,6 +158,86 @@ (p.id() < gr_.nnodes()); } + template <typename P> + const P& + p_graph<P>::point_from_id(const util::node_id& id) const + { + return this->gr_.node_data(id); + } + + template <typename P> + P& + p_graph<P>::point_from_id(const util::node_id& id) + { + return this->gr_.node_data(id); + } + + template <typename P> + const P& + p_graph<P>::node1(const util::edge_id& e) const + { + util::node_id n1 = this->gr_.edge(e).n1(); + return this->point_from_id(n1); + } + + template <typename P> + const P& + p_graph<P>::node2(const util::edge_id& e) const + { + util::node_id n2 = this->gr_.edge(e).n2(); + return this->point_from_id(n2); + } + + template <typename P> + inline + bool + p_graph<P>::adjacent_or_equal(const psite& lhs, const psite& rhs) const + { + assert (&lhs.pg() == this && rhs.pg() == this); + return adjacent_or_equal(lhs.id(), rhs.id()); + } + + template <typename P> + inline + bool + p_graph<P>::adjacent_or_equal(const util::node_id& lhs, + const util::node_id& rhs) const + { + // FIXME: Likewise, this is inefficient. + + assert (lhs < this->npoints()); + assert (rhs < this->npoints()); + + if (rhs == lhs) + return true; + + // Check whether the iterator is among the neighbors of P_REF_. + typedef std::vector<util::node_id> adjacency_type; + const adjacency_type& lhs_neighbs = gr_.nodes()[lhs]->edges; + + adjacency_type::const_iterator j = + std::find (lhs_neighbs.begin(), lhs_neighbs.end(), rhs); + if (j != lhs_neighbs.end()) + return true; + + return false; + } + + template <typename P> + const typename p_graph<P>::graph& + p_graph<P>::to_graph() const + { + return this->gr_; + } + + template <typename P> + typename p_graph<P>::graph& + p_graph<P>::to_graph() + { + return this->gr_; + } + + # endif // ! MLN_INCLUDE_ONLY } // end of mln Index: mln/core/graph_window_piter.hh --- mln/core/graph_window_piter.hh (revision 1742) +++ mln/core/graph_window_piter.hh (working copy) @@ -94,6 +94,7 @@ void start(); void next_(); + bool adjacent_or_equal_to_p_ref_() const; /// Update the internal data of the iterator. void update_(); @@ -147,10 +148,7 @@ bool graph_window_fwd_piter<P>::is_valid() const { - // FIXME: We depend too much on the implementation of util::graph - // here. The util::graph should provide the service to abstract - // these manipulations. - return id_ < p_ref_.pg().gr_.nnodes(); + return id_ < p_ref_.pg().npoints(); } template <typename P> @@ -158,7 +156,7 @@ void graph_window_fwd_piter<P>::invalidate() { - id_ = p_ref_.pg().gr_.nnodes(); + id_ = p_ref_.pg().npoints(); } template <typename P> @@ -189,40 +187,21 @@ window, which is not the case now! (Currently, next_() behaves as win was always an elementary window.) */ do + { ++id_; + } while (is_valid() && !adjacent_or_equal_to_p_ref_()); if (is_valid()) update_(); } + template <typename P> inline bool graph_window_fwd_piter<P>::adjacent_or_equal_to_p_ref_() const { - // FIXME: Likewise, this is inefficient. - - // Check wether the iterator points to P_REF_. - if (id_ == p_ref_.id()) - return true; - - // Check whether the iterator is among the neighbors of P_REF_. - { - // Paranoid assertion. - assert (p_ref_.id() < p_ref_.pg().gr_.nnodes()); - // FIXME: This is too low-level. Yet another service the graph - // should provide. - typedef std::vector<util::node_id> adjacency_type; - const adjacency_type& p_ref_neighbs = - p_ref_.pg().gr_.nodes()[p_ref_.id()]->edges; - adjacency_type::const_iterator j = - std::find (p_ref_neighbs.begin(), p_ref_neighbs.end(), id_); - if (j != p_ref_neighbs.end()) - return true; - } - - // Otherwise, the iterator is not adjacent to P_REF_. - return false; + return p_ref_.pg().adjacent_or_equal(p_ref_.id(), id_); } template <typename P> @@ -233,7 +212,7 @@ // Update psite_. psite_ = graph_psite<P>(p_ref_.pg(), id_); // Update p_. - p_ = p_ref_.pg().gr_.node_data(id_); + p_ = p_ref_.pg().point_from_id(id_); } template <typename P> Index: mln/core/graph_image.hh --- mln/core/graph_image.hh (revision 1742) +++ mln/core/graph_image.hh (working copy) @@ -282,10 +282,7 @@ const P& graph_image<P, V>::node1(const util::edge_id& e) const { - // FIXME: Improve the interface of graph to avoid these low-level - // manipulations. - util::node_id n1 = this->domain().gr_.edge(e).n1(); - return this->domain().gr_.node_data(n1); + return this->domain().node1(e); } template <typename P, typename V> @@ -293,10 +290,7 @@ const P& graph_image<P, V>::node2(const util::edge_id& e) const { - // FIXME: Improve the interface of graph to avoid these low-level - // manipulations. - util::node_id n2 = this->domain().gr_.edge(e).n2(); - return this->domain().gr_.node_data(n2); + return this->domain().node2(e); } # endif // ! MLN_INCLUDE_ONLY Index: mln/core/p_graph_piter.hh --- mln/core/p_graph_piter.hh (revision 1742) +++ mln/core/p_graph_piter.hh (working copy) @@ -171,7 +171,7 @@ // Update psite_. psite_ = graph_psite<P>(pg_, id_); // Update p_. - p_ = pg_.gr_.node_data(id_); + p_ = pg_.point_from_id(id_); } template<typename P> Index: mln/draw/graph.hh --- mln/draw/graph.hh (revision 1742) +++ mln/draw/graph.hh (working copy) @@ -120,7 +120,7 @@ line (exact(ima), gi.node1(l), gi.node2(l), link_v); // Draw the points (nodes). for (size_t p = 0; p < gi.domain().npoints(); ++p) - exact(ima)(gi.domain().gr_.node_data(p)) = gi.node_values()[p]; + exact(ima)(gi.domain().point_from_id(p)) = gi.node_values()[p]; } # endif // ! MLN_INCLUDE_ONLY

Le 21 févr. 08 à 15:50, Nicolas Ballas a écrit :
https://svn.lrde.epita.fr/svn/oln/trunk/milena
Index: ChangeLog from Nicolas Ballas <ballas@lrde.epita.fr>
Enrich the p_graph interface.
* mln/core/p_graph.hh: Improve the p_graph interface. * mln/core/graph_psite.hh, * mln/core/graph_window_piter.hh, * mln/core/graph_image.hh, * mln/core/p_graph_piter.hh, * mln/draw/graph.hh: Remove graph implementation details.
Thanks! It's much more readable now.
participants (2)
-
Nicolas Ballas
-
Roland Levillain