From a0919bdff6e453bbd8f4d5d16d2e5788cfa22b40 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Thu, 9 Mar 2006 19:52:39 +0000 Subject: [PATCH] Fix CSR properties [SVN r33289] --- .../graph/compressed_sparse_row_graph.hpp | 55 ++++++++++++++----- .../boost/graph/detail/indexed_properties.hpp | 7 +++ 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/include/boost/graph/compressed_sparse_row_graph.hpp b/include/boost/graph/compressed_sparse_row_graph.hpp index 4dde8068..55a4221c 100644 --- a/include/boost/graph/compressed_sparse_row_graph.hpp +++ b/include/boost/graph/compressed_sparse_row_graph.hpp @@ -67,15 +67,15 @@ class compressed_sparse_row_graph Vertex>, public detail::indexed_edge_properties > + EdgeIndex> > { typedef detail::indexed_vertex_properties + VertexProperty, Vertex> inherited_vertex_properties; typedef detail::indexed_edge_properties > + csr_edge_descriptor > inherited_edge_properties; public: @@ -279,7 +279,7 @@ class compressed_sparse_row_graph m_column[current_edge++] = get(vi, target(*ei, g)); } std::sort(m_column.begin() + num_edges_before_this_vertex, - m_column.begin() + current_edge); + m_column.begin() + current_edge); } m_rowstart[numverts] = current_edge; m_last_source = numverts; @@ -303,6 +303,11 @@ class compressed_sparse_row_graph using inherited_edge_properties::operator[]; // private: non-portable, requires friend templates + inherited_vertex_properties& vertex_properties() {return *this;} + const inherited_vertex_properties& vertex_properties() const {return *this;} + inherited_edge_properties& edge_properties() { return *this; } + const inherited_edge_properties& edge_properties() const { return *this; } + std::vector m_rowstart; std::vector m_column; GraphProperty m_property; @@ -349,16 +354,37 @@ add_vertices(Vertex count, BOOST_CSR_GRAPH_TYPE& g) { template inline typename BOOST_CSR_GRAPH_TYPE::edge_descriptor add_edge(Vertex src, Vertex tgt, BOOST_CSR_GRAPH_TYPE& g) { - assert ((g.m_last_source == 0 || src >= g.m_last_source - 1) && - src < num_vertices(g)); + assert ((g.m_last_source == 0 || src >= g.m_last_source - 1) && + src < num_vertices(g)); EdgeIndex num_edges_orig = g.m_column.size(); for (; g.m_last_source <= src; ++g.m_last_source) g.m_rowstart[g.m_last_source] = num_edges_orig; g.m_rowstart[src + 1] = num_edges_orig + 1; g.m_column.push_back(tgt); + typedef typename BOOST_CSR_GRAPH_TYPE::edge_push_back_type push_back_type; + g.edge_properties().push_back(push_back_type()); return typename BOOST_CSR_GRAPH_TYPE::edge_descriptor(src, num_edges_orig); } +// This function requires that (src, tgt) be lexicographically at least as +// large as the largest edge in the graph so far +template +inline typename BOOST_CSR_GRAPH_TYPE::edge_descriptor +add_edge(Vertex src, Vertex tgt, + typename BOOST_CSR_GRAPH_TYPE::edge_bundled const& p, + BOOST_CSR_GRAPH_TYPE& g) { + assert ((g.m_last_source == 0 || src >= g.m_last_source - 1) && + src < num_vertices(g)); + EdgeIndex num_edges_orig = g.m_column.size(); + for (; g.m_last_source <= src; ++g.m_last_source) + g.m_rowstart[g.m_last_source] = num_edges_orig; + g.m_rowstart[src + 1] = num_edges_orig + 1; + g.m_column.push_back(tgt); + g.edge_properties().push_back(p); + return typename BOOST_CSR_GRAPH_TYPE::edge_descriptor(src, num_edges_orig); +} + + // From VertexListGraph template inline Vertex @@ -455,8 +481,8 @@ adjacent_vertices(Vertex v, const BOOST_CSR_GRAPH_TYPE& g) EdgeIndex v_row_start = g.m_rowstart[v]; EdgeIndex next_row_start = g.m_rowstart[v + 1]; return std::make_pair(g.m_column.begin() + v_row_start, - g.m_column.begin() + - (std::max)(v_row_start, next_row_start)); + g.m_column.begin() + + (std::max)(v_row_start, next_row_start)); } // Extra, common functions @@ -508,8 +534,8 @@ edge_from_index(EdgeIndex idx, const BOOST_CSR_GRAPH_TYPE& g) assert (idx < num_edges(g)); row_start_iter src_plus_1 = std::upper_bound(g.m_rowstart.begin(), - g.m_rowstart.begin() + g.m_last_source + 1, - idx); + g.m_rowstart.begin() + g.m_last_source + 1, + idx); // Get last source whose rowstart is at most idx // upper_bound returns this position plus 1 Vertex src = (src_plus_1 - g.m_rowstart.begin()) - 1; @@ -532,7 +558,7 @@ class BOOST_CSR_GRAPH_TYPE::edge_iterator edge_iterator() : rowstart_array(0), current_edge(), end_of_this_vertex(0) {} edge_iterator(const compressed_sparse_row_graph& graph, - edge_descriptor current_edge, + edge_descriptor current_edge, EdgeIndex end_of_this_vertex) : rowstart_array(&graph.m_rowstart[0]), current_edge(current_edge), end_of_this_vertex(end_of_this_vertex) {} @@ -701,8 +727,8 @@ struct property_map { private: typedef graph_traits traits; - typedef typename BOOST_CSR_GRAPH_TYPE::vertex_bundled vertex_bundled; - typedef typename BOOST_CSR_GRAPH_TYPE::edge_bundled edge_bundled; + typedef VertexProperty vertex_bundled; + typedef EdgeProperty edge_bundled; typedef typename ct_if<(detail::is_vertex_bundle::value), typename traits::vertex_descriptor, typename traits::edge_descriptor>::type @@ -726,8 +752,7 @@ get(T Bundle::* p, BOOST_CSR_GRAPH_TYPE& g) return result_type(&g, p); } -template +template inline typename property_map::const_type get(T Bundle::* p, BOOST_CSR_GRAPH_TYPE const & g) diff --git a/include/boost/graph/detail/indexed_properties.hpp b/include/boost/graph/detail/indexed_properties.hpp index 31f13584..c115a4b8 100644 --- a/include/boost/graph/detail/indexed_properties.hpp +++ b/include/boost/graph/detail/indexed_properties.hpp @@ -102,6 +102,7 @@ class indexed_edge_properties public: typedef no_property edge_property_type; typedef Property edge_bundled; + typedef Property edge_push_back_type; // Directly access a edge or edge bundle Property& operator[](Descriptor v) @@ -129,12 +130,14 @@ protected: m_edge_properties.reserve(n); } + public: // Add a new property value to the back void push_back(const Property& prop) { m_edge_properties.push_back(prop); } + private: // Access to the derived object Derived& derived() { return *static_cast(this); } @@ -153,6 +156,7 @@ class indexed_edge_properties public: typedef no_property edge_property_type; typedef void edge_bundled; + typedef void* edge_push_back_type; secret operator[](secret) { return secret(); } @@ -162,6 +166,9 @@ class indexed_edge_properties indexed_edge_properties(std::size_t) { } void resize(std::size_t) { } void reserve(std::size_t) { } + + public: + void push_back(const edge_push_back_type&) { } }; }