1768: Fix mln::graph_image<P> initializations and associated entitites.

https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Roland Levillain <roland@lrde.epita.fr> Fix mln::graph_image<P> initializations and associated entitites. * mln/core/graph_image.hh: Add some more documentation. (mln::init_(tag::image_t, graph_image<P,V>&, const graph_image<P,V>): Turn the signature of this routine into... (mln::init_(tag::image_t, graph_image<P,V>&, const graph_image<P,W>): ...this. Don't copy values from the model. Instead, initialize with a vector of the same size. (mln::graph_image<P, V>::graph_image(const p_graph<P>&)): s/g.npoints()/g.nnodes()/. (mln::graph_image<P, V>::init_): Ensure there are no data before initializing new ones. * mln/core/graph_psite.hh (mln::graph_psite<P>::graph_psite): New ctor. (mln::graph_psite<P>::pg_): Change the type of this member from const p_graph<P>& to const p_graph<P>*. (mln::graph_psite<P>::graph_psite(const p_graph<P>&, util::node_id)) (mln::graph_psite<P>::operator= (const mln::graph_psite<P>&)) (mln::graph_psite<P>::to_point): Adjust. (mln::graph_psite<P>::pg): Likewise: ensure member pg_ is not null. (mln::graph_psite<P>::is_valid_): New method. Use it... (mln::graph_psite<P>::operator[]): ...here. (mln::graph_psite<P>::operator P): Remove. (operator==(const graph_psite<P>&, const graph_psite<P>&)): New. * mln/util/graph.hh, mln/util/internal/graph_base.hh: More FIXMEs. core/graph_image.hh | 31 +++++++++++------------- core/graph_psite.hh | 55 ++++++++++++++++++++++++++++++++------------ util/graph.hh | 2 + util/internal/graph_base.hh | 2 + 4 files changed, 59 insertions(+), 31 deletions(-) Index: mln/core/graph_image.hh --- mln/core/graph_image.hh (revision 1767) +++ mln/core/graph_image.hh (working copy) @@ -90,20 +90,21 @@ } // end of namespace mln::trait - /*! \brief Kind of image based on graph structure. - * - */ + + /// \brief Image based on a line graph. + /// + /// Values are stored on the edges of the graph, not on its vertices. template <typename P, typename V> struct graph_image : public internal::image_primary_< p_graph<P>, graph_image<P, V> > { - + /// Super type. typedef mln::internal::image_base_< p_graph<P>, graph_image<P, V> > super_; /// Value associated type. typedef V value; - /// Return type of read-write access. + /// \brief Return type of read-write access. /// /// We use the associated type \c reference instead of a plain /// reference on th value type (\v V), because it's the only way @@ -117,7 +118,6 @@ /// Value set associated type. typedef mln::value::set<value> vset; - /// Skeleton. typedef graph_image< tag::psite_<P>, tag::value_<V> > skeleton; @@ -160,9 +160,9 @@ }; // Fwd decl. - template <typename P, typename V> + template <typename P, typename V, typename W> void init_(tag::image_t, - graph_image<P, V>& target, const graph_image<P, V>& model); + graph_image<P, V>& target, const graph_image<P, W>& model); # ifndef MLN_INCLUDE_ONLY @@ -171,12 +171,13 @@ | Initialization. | `-----------------*/ - template <typename P, typename V> + template <typename P, typename V, typename W> inline void init_(tag::image_t, - graph_image<P, V>& target, const graph_image<P, V>& model) + graph_image<P, V>& target, const graph_image<P, W>& model) { - target.init_(model.domain(), model.node_values ()); + target.init_(model.domain(), + std::vector<V>(model.node_values().size())); } /*-------. @@ -210,7 +211,7 @@ inline graph_image<P, V>::graph_image(const p_graph<P>& g) { - init_(g, std::vector<V>(g.npoints())); + init_(g, std::vector<V>(g.nnodes())); } template <typename P, typename V> @@ -225,11 +226,7 @@ void graph_image<P, V>::init_(const p_graph<P>& g, const std::vector<V>& val) { - /* FIXME: We leak memory here: calling init_ twice loses the - previous content pointed by data_. - - We should definitely write down formal guidelines on - initialization and memory management in general! */ + mln_precondition(! this->has_data()); this->data_ = new internal::data_< graph_image<P, V> > (g, val); } Index: mln/core/graph_psite.hh --- mln/core/graph_psite.hh (revision 1767) +++ mln/core/graph_psite.hh (working copy) @@ -56,9 +56,9 @@ /// Construction and assignment. /// \{ + graph_psite(); graph_psite(const p_graph<P>& pg_, unsigned id); graph_psite(const self_& rhs); - /// \pre This psite must have the same graph point set as \a rhs. self_& operator= (const self_& rhs); /// \} @@ -67,7 +67,6 @@ /// Access to point. /// \{ - operator P() const; const point& to_point() const; coord operator[](unsigned id) const; /// \} @@ -78,20 +77,39 @@ util::node_id id() const; private: + /// Is this psite valid? + bool is_valid_() const; + + private: /// The p_graph this point site belongs to. - const p_graph<P>& pg_; + const p_graph<P>* pg_; /// The id of the node this psite is pointing towards. util::node_id id_; }; + /// Compare two mln::graph_psite<P> instances. + /* FIXME: Shouldn't this comparison be part of a much general + mechanism? */ + template <typename P> + bool + operator==(const graph_psite<P>& lhs, const graph_psite<P>& rhs); # ifndef MLN_INCLUDE_ONLY template<typename P> inline + graph_psite<P>::graph_psite() + // Dummy initializations. + : pg_(0), + id_(-1) + { + } + + template<typename P> + inline graph_psite<P>::graph_psite(const p_graph<P>& g, util::node_id id) - : pg_(g), + : pg_(&g), id_(id) { } @@ -111,26 +129,25 @@ { if (&rhs == this) return *this; - // Assigning a psite from a graph point set to a psite from - // another graph point set is meaningless. - mln_assertion(&pg_ == &rhs.pg_); + pg_ = rhs.pg_; id_ = rhs.id_; return *this; } template<typename P> inline - const graph_psite<P>& - graph_psite<P>::to_psite() const + bool + graph_psite<P>::is_valid_() const { - return *this; + return pg_ && id_ < pg_->gr_.nnodes(); } template<typename P> inline - graph_psite<P>::operator P() const + const graph_psite<P>& + graph_psite<P>::to_psite() const { - return pg_.point_from_id(id_); + return *this; } template<typename P> @@ -138,7 +155,7 @@ const P& graph_psite<P>::to_point() const { - return pg_.point_from_id(id_); + return pg().point_from_id(id_); } template<typename P> @@ -146,6 +163,7 @@ mln_coord(P) graph_psite<P>::operator[](unsigned i) const { + mln_assertion(is_valid_()); return to_point()[i]; } @@ -154,7 +172,8 @@ const p_graph<P>& graph_psite<P>::pg() const { - return pg_; + mln_assertion(pg_); + return *pg_; } template<typename P> @@ -165,6 +184,14 @@ return id_; } + template <typename P> + bool + operator==(const graph_psite<P>& lhs, const graph_psite<P>& rhs) + { + return &lhs.pg() == &rhs.pg() && lhs.id() == rhs.id(); + } + + # endif // ! MLN_INCLUDE_ONLY Index: mln/util/graph.hh --- mln/util/graph.hh (revision 1767) +++ mln/util/graph.hh (working copy) @@ -36,6 +36,8 @@ // FIXME: More doc! +// FIXME: Rename node(s) as vertex (vertices). + namespace mln { Index: mln/util/internal/graph_base.hh --- mln/util/internal/graph_base.hh (revision 1767) +++ mln/util/internal/graph_base.hh (working copy) @@ -39,6 +39,8 @@ # include <algorithm> # include <mln/util/ordpair.hh> +// FIXME: Rename node(s) as vertex (vertices). + namespace mln {
participants (1)
-
Roland Levillain