From c11c698becaef5f23989771680401725971cd7c7 Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Mon, 7 May 2001 21:26:16 +0000 Subject: [PATCH] fixed constness of property maps to not depend on the constness of the property map object itself [SVN r10053] --- include/boost/graph/stanford_graph.hpp | 211 ++++++++++++++----------- 1 file changed, 116 insertions(+), 95 deletions(-) diff --git a/include/boost/graph/stanford_graph.hpp b/include/boost/graph/stanford_graph.hpp index 9c3e108d..022688c1 100644 --- a/include/boost/graph/stanford_graph.hpp +++ b/include/boost/graph/stanford_graph.hpp @@ -86,7 +86,9 @@ class sgb_adj_iterator; class sgb_vertex_iterator; namespace boost { - template <> struct graph_traits { + typedef Graph* sgb_graph_ptr; + + template <> struct graph_traits { typedef Vertex* vertex_descriptor; typedef boost::sgb_edge edge_descriptor; typedef sgb_out_edge_iterator out_edge_iterator; @@ -101,7 +103,7 @@ namespace boost { typedef vertex_list_graph_tag traversal_category; typedef allow_parallel_edge_tag edge_parallel_category; }; - template <> struct graph_traits { + template <> struct graph_traits { typedef Vertex* vertex_descriptor; typedef boost::sgb_edge edge_descriptor; typedef sgb_out_edge_iterator out_edge_iterator; @@ -120,6 +122,10 @@ namespace boost { namespace boost { + struct edge_length_t { + typedef edge_property_tag kind; + }; + // We could just use Arc* as the edge descriptor type, but // we want to add the source(e,g) function which requires // that we carry along a pointer to the source vertex. @@ -128,15 +134,17 @@ namespace boost { public: sgb_edge() : _arc(0), _src(0) { } sgb_edge(Arc* a, Vertex* s) : _arc(a), _src(s) { } - friend Vertex* source(self& e, Graph*) { return e._src; } - friend Vertex* target(self& e, Graph*) { return e._arc->tip; } + friend Vertex* source(self& e, sgb_graph_ptr) { return e._src; } + friend Vertex* target(self& e, sgb_graph_ptr) { return e._arc->tip; } friend bool operator==(const self& a, const self& b) { return a._arc == b._arc; } friend bool operator!=(const self& a, const self& b) { return a._arc != b._arc; } #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) - friend class sgb_edge_length_map; - template friend class sgb_edge_util_map; + template friend class sgb_edge_length_map; + template friend class sgb_edge_util_map; + friend long get(edge_length_t, const sgb_graph_ptr&, const sgb_edge& key); + friend void put(edge_length_t, sgb_graph_ptr&, const sgb_edge& key, long value); protected: #endif Arc* _arc; @@ -201,23 +209,23 @@ namespace boost { namespace boost { inline std::pair - vertices(Graph* g) + vertices(sgb_graph_ptr g) { return std::make_pair(sgb_vertex_iterator(g->vertices), sgb_vertex_iterator(g->vertices + g->n)); } inline std::pair - out_edges(Vertex* u, Graph*) + out_edges(Vertex* u, sgb_graph_ptr) { return std::make_pair( sgb_out_edge_iterator(u, u->arcs), sgb_out_edge_iterator(u, 0) ); } - inline boost::graph_traits::degree_size_type - out_degree(Vertex* u, Graph* g) + inline boost::graph_traits::degree_size_type + out_degree(Vertex* u, sgb_graph_ptr g) { - boost::graph_traits::out_edge_iterator i, i_end; + boost::graph_traits::out_edge_iterator i, i_end; boost::tie(i, i_end) = out_edges(u, g); return std::distance(i, i_end); } @@ -225,16 +233,16 @@ namespace boost { // in_edges? inline std::pair - adjacent_vertices(Vertex* u, Graph*) + adjacent_vertices(Vertex* u, sgb_graph_ptr) { return std::make_pair( sgb_adj_iterator(u->arcs), sgb_adj_iterator(0) ); } - inline long num_vertices(Graph* g) { return g->n; } - inline long num_edges(Graph* g) { return g->m; } + inline long num_vertices(sgb_graph_ptr g) { return g->n; } + inline long num_edges(sgb_graph_ptr g) { return g->m; } - inline Vertex* vertex(long v, Graph* g) { return g->vertices + v; } + inline Vertex* vertex(long v, sgb_graph_ptr g) { return g->vertices + v; } // Various Property Maps @@ -246,13 +254,14 @@ namespace boost { typedef boost::readable_property_map_tag category; typedef long value_type; typedef Vertex* key_type; + typedef long reference; sgb_vertex_id_map() : _g(0) { } - sgb_vertex_id_map(Graph* g) : _g(g) { } + sgb_vertex_id_map(sgb_graph_ptr g) : _g(g) { } long operator[](Vertex* v) const { return v - _g->vertices; } protected: - Graph* _g; + sgb_graph_ptr _g; }; - inline sgb_vertex_id_map get(vertex_index_t, Graph* g) { + inline sgb_vertex_id_map get(vertex_index_t, sgb_graph_ptr g) { return sgb_vertex_id_map(g); } @@ -264,9 +273,10 @@ namespace boost { typedef boost::readable_property_map_tag category; typedef char* value_type; typedef Vertex* key_type; + typedef char* reference; char* operator[](Vertex* v) const { return v->name; } }; - inline sgb_vertex_name_t_map get(vertex_name_t, Graph*) { + inline sgb_vertex_name_t_map get(vertex_name_t, sgb_graph_ptr) { return sgb_vertex_name_t_map(); } @@ -287,16 +297,12 @@ namespace boost { SGB_PROPERTY_TAG(edge, a) SGB_PROPERTY_TAG(edge, b) - struct edge_length_t { - typedef edge_property_tag kind; - }; - // Various Utility Maps // helpers inline Vertex*& get_util(util& u, Vertex*) { return u.V; } inline Arc*& get_util(util& u, Arc*) { return u.A; } - inline Graph*& get_util(util& u, Graph*) { return u.G; } + inline sgb_graph_ptr& get_util(util& u, sgb_graph_ptr) { return u.G; } inline char*& get_util(util& u, char*) { return u.S; } inline long& get_util(util& u, long) { return u.I; } @@ -316,37 +322,33 @@ namespace boost { SGB_GET_UTIL_FIELD(Arc, b) // Vertex Utility Map - template + template class sgb_vertex_util_map : public boost::put_get_at_helper< typename Tag::type, - sgb_vertex_util_map > + sgb_vertex_util_map > { public: typedef boost::lvalue_property_map_tag category; typedef typename Tag::type value_type; typedef Vertex* key_type; - const value_type& operator[](Vertex* v) const { - return get_util_field(v, Tag()); - } - value_type& operator[](Vertex* v) { + typedef Ref reference; + reference operator[](Vertex* v) const { return get_util_field(v, Tag()); } }; // Edge Utility Map - template + template class sgb_edge_util_map : public boost::put_get_at_helper< typename Tag::type, - sgb_edge_util_map > + sgb_edge_util_map > { public: typedef boost::lvalue_property_map_tag category; typedef typename Tag::type value_type; typedef Vertex* key_type; - value_type& operator[](const sgb_edge& e) { - return get_util_field(e._arc, Tag()); - } - const value_type& operator[](const sgb_edge& e) const { + typedef Ref reference; + reference operator[](const sgb_edge& e) const { return get_util_field(e._arc, Tag()); } }; @@ -354,71 +356,75 @@ namespace boost { #if !defined BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION template - inline sgb_vertex_util_map - get_property_map(Tag, const Graph* g, vertex_property_tag) { - return sgb_vertex_util_map(); + inline sgb_vertex_util_map + get_property_map(Tag, const sgb_graph_ptr& g, vertex_property_tag) { + return sgb_vertex_util_map(); } template - inline sgb_edge_util_map - get_property_map(Tag, const Graph* g, edge_property_tag) { - return sgb_edge_util_map(); + inline sgb_vertex_util_map + get_property_map(Tag, sgb_graph_ptr& g, vertex_property_tag) { + return sgb_vertex_util_map(); } - template - struct sgb_util_map { }; - - template struct sgb_util_map { - typedef typename sgb_vertex_util_map::type type; - }; - template struct sgb_util_map { - typedef typename sgb_edge_util_map::type type; - }; + template + inline sgb_edge_util_map + get_property_map(Tag, const sgb_graph_ptr& g, edge_property_tag) { + return sgb_edge_util_map(); + } + template + inline sgb_edge_util_map + get_property_map(Tag, sgb_graph_ptr& g, edge_property_tag) { + return sgb_edge_util_map(); + } #endif // ! BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION // Edge Length Access + template class sgb_edge_length_map - : public boost::put_get_at_helper + : public boost::put_get_at_helper > { public: typedef boost::lvalue_property_map_tag category; typedef long value_type; typedef sgb_edge key_type; - long& operator[](const sgb_edge& e) { - return e._arc->len; - } - const long& operator[](const sgb_edge& e) const { + typedef Ref reference; + reference operator[](const sgb_edge& e) const { return e._arc->len; } }; - inline sgb_edge_length_map - get(edge_length_t, const Graph*) { - return sgb_edge_length_map(); + + inline sgb_edge_length_map + get(edge_length_t, const sgb_graph_ptr&) { + return sgb_edge_length_map(); } - inline sgb_edge_length_map::value_type - get(edge_length_t, const Graph*, const sgb_edge_length_map::key_type& key) { - return sgb_edge_length_map()[key]; + inline sgb_edge_length_map + get(edge_length_t, sgb_graph_ptr&) { + return sgb_edge_length_map(); + } + inline long + get(edge_length_t, const sgb_graph_ptr&, const sgb_edge& key) { + return key._arc->len; } inline void - put(edge_length_t, Graph*, const sgb_edge_length_map::key_type& key, - const sgb_edge_length_map::value_type& value) + put(edge_length_t, sgb_graph_ptr&, const sgb_edge& key, long value) { - sgb_edge_length_map()[key] = value; + key._arc->len = value; } // Property Map Traits Classes template <> - struct property_map { - typedef sgb_edge_length_map type; - typedef sgb_edge_length_map const_type; + struct property_map { + typedef sgb_edge_length_map type; + typedef sgb_edge_length_map const_type; }; template <> - struct property_map { + struct property_map { typedef sgb_vertex_id_map type; typedef sgb_vertex_id_map const_type; }; template <> - struct property_map { + struct property_map { typedef sgb_vertex_name_t_map type; typedef sgb_vertex_name_t_map const_type; }; @@ -430,63 +436,78 @@ namespace boost { struct sgb_choose_property_map { }; template struct sgb_choose_property_map { - typedef sgb_vertex_util_map type; + typedef typename PropertyTag::type value_type; + typedef sgb_vertex_util_map type; + typedef sgb_vertex_util_map const_type; }; template struct sgb_choose_property_map { - typedef sgb_edge_util_map type; + typedef typename PropertyTag::type value_type; + typedef sgb_edge_util_map type; + typedef sgb_edge_util_map const_type; }; } // namespace detail template - struct property_map { + struct property_map { typedef typename property_kind::type Kind; - typedef typename detail::sgb_choose_property_map::type type; - typedef type const_type; + typedef detail::sgb_choose_property_map Choice; + typedef typename Choice::type type; + typedef typename Choice::const_type const_type; }; #define SGB_UTIL_ACCESSOR(KIND,X) \ template \ - inline sgb_##KIND##_util_map< X##_property > \ - get(X##_property, Graph*) { \ - return sgb_##KIND##_util_map< X##_property >(); \ + inline sgb_##KIND##_util_map< X##_property, T&> \ + get(X##_property, sgb_graph_ptr&) { \ + return sgb_##KIND##_util_map< X##_property, T&>(); \ + } \ + template \ + inline sgb_##KIND##_util_map< X##_property, const T&> \ + get(X##_property, const sgb_graph_ptr&) { \ + return sgb_##KIND##_util_map< X##_property, const T&>(); \ } \ template \ - inline typename sgb_##KIND##_util_map< X##_property >::value_type \ - get(X##_property, const Graph*, const Key& key) { \ - return sgb_##KIND##_util_map< X##_property >()[key]; \ + inline typename \ + sgb_##KIND##_util_map< X##_property, const T&>::value_type \ + get(X##_property, const sgb_graph_ptr&, const Key& key) { \ + return sgb_##KIND##_util_map< X##_property, const T&>()[key]; \ } \ template \ inline void \ - put(X##_property, Graph*, const Key& key, const Value& value) { \ - sgb_##KIND##_util_map< X##_property >()[key] = value; \ + put(X##_property, sgb_graph_ptr&, const Key& key, const Value& value) { \ + sgb_##KIND##_util_map< X##_property, T&>()[key] = value; \ } #else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION #define SGB_UTIL_ACCESSOR_TYPE(KIND,TAG,TYPE) \ - inline sgb_##KIND##_util_map< TAG > \ - get(TAG, Graph*) { \ - return sgb_##KIND##_util_map< TAG >(); \ + inline sgb_##KIND##_util_map< TAG, TYPE& > \ + get(TAG, sgb_graph_ptr&) { \ + return sgb_##KIND##_util_map< TAG, TYPE& >(); \ + } \ + inline sgb_##KIND##_util_map< TAG, const TYPE& > \ + get(TAG, const sgb_graph_ptr&) { \ + return sgb_##KIND##_util_map< TAG, const TYPE& >(); \ } \ template \ - inline typename sgb_##KIND##_util_map< TAG >::value_type \ - get(TAG, const Graph*, const Key& key) { \ - return sgb_##KIND##_util_map< TAG >()[key]; \ + inline typename sgb_##KIND##_util_map< TAG, const TYPE& >::value_type \ + get(TAG, const sgb_graph_ptr&, const Key& key) { \ + return sgb_##KIND##_util_map< TAG, const TYPE& >()[key]; \ } \ template \ inline void \ - put(TAG, Graph*, const Key& key, const Value& value) { \ - sgb_##KIND##_util_map< TAG >()[key] = value; \ + put(TAG, sgb_graph_ptr&, const Key& key, const Value& value) { \ + sgb_##KIND##_util_map< TAG, TYPE& >()[key] = value; \ } \ - template <> struct property_map > { \ - typedef sgb_##KIND##_util_map< TAG > type; \ + template <> struct property_map > { \ + typedef sgb_##KIND##_util_map< TAG, TYPE&> type; \ + typedef sgb_##KIND##_util_map< TAG, const TYPE&> const_type; \ } #define SGB_UTIL_ACCESSOR(KIND,TAG) \ SGB_UTIL_ACCESSOR_TYPE(KIND, TAG##_property, Vertex*); \ SGB_UTIL_ACCESSOR_TYPE(KIND, TAG##_property, Arc*); \ - SGB_UTIL_ACCESSOR_TYPE(KIND, TAG##_property, Graph*); \ + SGB_UTIL_ACCESSOR_TYPE(KIND, TAG##_property, sgb_graph_ptr); \ SGB_UTIL_ACCESSOR_TYPE(KIND, TAG##_property, long); \ SGB_UTIL_ACCESSOR_TYPE(KIND, TAG##_property, char*);