Skip to content

Commit 62ed6e2

Browse files
committed
fix: size CSR copies by counting out edges
1 parent 039723b commit 62ed6e2

1 file changed

Lines changed: 36 additions & 56 deletions

File tree

‎include/boost/graph/compressed_sparse_row_graph.hpp‎

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <boost/functional/hash.hpp>
4444
#include <boost/next_prior.hpp>
4545
#include <boost/property_map/transform_value_property_map.hpp>
46+
#include <boost/range/iterator_range.hpp>
4647

4748
namespace boost
4849
{
@@ -154,6 +155,25 @@ enum edges_are_unsorted_global_t
154155
compressed_sparse_row_graph< bidirectionalS, VertexProperty, EdgeProperty, \
155156
GraphProperty, Vertex, EdgeIndex >
156157

158+
namespace graph
159+
{
160+
namespace detail
161+
{
162+
// Counts out_edges rather than num_edges so undirected sources,
163+
// whose out_edges report each edge from both endpoints, fit.
164+
template < typename EdgesSize, typename Graph >
165+
EdgesSize count_out_edges(const Graph& g)
166+
{
167+
EdgesSize count = 0;
168+
for (const auto& v : ::boost::make_iterator_range(vertices(g)))
169+
{
170+
count += static_cast< EdgesSize >(out_degree(v, g));
171+
}
172+
return count;
173+
}
174+
}
175+
}
176+
157177
namespace detail
158178
{
159179
template < typename T >
@@ -588,13 +608,8 @@ class compressed_sparse_row_graph< directedS, VertexProperty, EdgeProperty,
588608
compressed_sparse_row_graph(const Graph& g, const VertexIndexMap& vi)
589609
: m_property()
590610
{
591-
typename graph_traits< Graph >::edges_size_type numedges = num_edges(g);
592-
if (is_same< typename graph_traits< Graph >::directed_category,
593-
undirectedS >::value)
594-
{
595-
numedges *= 2; // Double each edge (actual doubling done by
596-
// out_edges function)
597-
}
611+
edges_size_type numedges
612+
= ::boost::graph::detail::count_out_edges< edges_size_type >(g);
598613
vertices_size_type numverts = num_vertices(g);
599614
assign(g, vi, numverts, numedges);
600615
inherited_vertex_properties::resize(numverts);
@@ -604,13 +619,8 @@ class compressed_sparse_row_graph< directedS, VertexProperty, EdgeProperty,
604619
template < typename Graph >
605620
explicit compressed_sparse_row_graph(const Graph& g) : m_property()
606621
{
607-
typename graph_traits< Graph >::edges_size_type numedges = num_edges(g);
608-
if (is_same< typename graph_traits< Graph >::directed_category,
609-
undirectedS >::value)
610-
{
611-
numedges *= 2; // Double each edge (actual doubling done by
612-
// out_edges function)
613-
}
622+
edges_size_type numedges
623+
= ::boost::graph::detail::count_out_edges< edges_size_type >(g);
614624
assign(g, get(vertex_index, g), num_vertices(g), numedges);
615625
}
616626

@@ -630,13 +640,8 @@ class compressed_sparse_row_graph< directedS, VertexProperty, EdgeProperty,
630640
template < typename Graph, typename VertexIndexMap >
631641
void assign(const Graph& g, const VertexIndexMap& vi)
632642
{
633-
typename graph_traits< Graph >::edges_size_type numedges = num_edges(g);
634-
if (is_same< typename graph_traits< Graph >::directed_category,
635-
undirectedS >::value)
636-
{
637-
numedges *= 2; // Double each edge (actual doubling done by
638-
// out_edges function)
639-
}
643+
edges_size_type numedges
644+
= ::boost::graph::detail::count_out_edges< edges_size_type >(g);
640645
vertices_size_type numverts = num_vertices(g);
641646
m_forward.assign(g, vi, numverts, numedges);
642647
inherited_vertex_properties::resize(numverts);
@@ -645,13 +650,8 @@ class compressed_sparse_row_graph< directedS, VertexProperty, EdgeProperty,
645650
// Requires the above, plus a vertex_index map.
646651
template < typename Graph > void assign(const Graph& g)
647652
{
648-
typename graph_traits< Graph >::edges_size_type numedges = num_edges(g);
649-
if (is_same< typename graph_traits< Graph >::directed_category,
650-
undirectedS >::value)
651-
{
652-
numedges *= 2; // Double each edge (actual doubling done by
653-
// out_edges function)
654-
}
653+
edges_size_type numedges
654+
= ::boost::graph::detail::count_out_edges< edges_size_type >(g);
655655
vertices_size_type numverts = num_vertices(g);
656656
m_forward.assign(g, get(vertex_index, g), numverts, numedges);
657657
inherited_vertex_properties::resize(numverts);
@@ -1027,13 +1027,8 @@ class compressed_sparse_row_graph< bidirectionalS, VertexProperty, EdgeProperty,
10271027
compressed_sparse_row_graph(const Graph& g, const VertexIndexMap& vi)
10281028
: m_property()
10291029
{
1030-
typename graph_traits< Graph >::edges_size_type numedges = num_edges(g);
1031-
if (is_same< typename graph_traits< Graph >::directed_category,
1032-
undirectedS >::value)
1033-
{
1034-
numedges *= 2; // Double each edge (actual doubling done by
1035-
// out_edges function)
1036-
}
1030+
edges_size_type numedges
1031+
= ::boost::graph::detail::count_out_edges< edges_size_type >(g);
10371032
vertices_size_type numverts = num_vertices(g);
10381033
assign(g, vi, numverts, numedges);
10391034
inherited_vertex_properties::resize(numverts);
@@ -1043,13 +1038,8 @@ class compressed_sparse_row_graph< bidirectionalS, VertexProperty, EdgeProperty,
10431038
template < typename Graph >
10441039
explicit compressed_sparse_row_graph(const Graph& g) : m_property()
10451040
{
1046-
typename graph_traits< Graph >::edges_size_type numedges = num_edges(g);
1047-
if (is_same< typename graph_traits< Graph >::directed_category,
1048-
undirectedS >::value)
1049-
{
1050-
numedges *= 2; // Double each edge (actual doubling done by
1051-
// out_edges function)
1052-
}
1041+
edges_size_type numedges
1042+
= ::boost::graph::detail::count_out_edges< edges_size_type >(g);
10531043
assign(g, get(vertex_index, g), num_vertices(g), numedges);
10541044
}
10551045

@@ -1070,13 +1060,8 @@ class compressed_sparse_row_graph< bidirectionalS, VertexProperty, EdgeProperty,
10701060
template < typename Graph, typename VertexIndexMap >
10711061
void assign(const Graph& g, const VertexIndexMap& vi)
10721062
{
1073-
typename graph_traits< Graph >::edges_size_type numedges = num_edges(g);
1074-
if (is_same< typename graph_traits< Graph >::directed_category,
1075-
undirectedS >::value)
1076-
{
1077-
numedges *= 2; // Double each edge (actual doubling done by
1078-
// out_edges function)
1079-
}
1063+
edges_size_type numedges
1064+
= ::boost::graph::detail::count_out_edges< edges_size_type >(g);
10801065
vertices_size_type numverts = num_vertices(g);
10811066
m_forward.assign(g, vi, numverts, numedges);
10821067
inherited_vertex_properties::resize(numverts);
@@ -1086,13 +1071,8 @@ class compressed_sparse_row_graph< bidirectionalS, VertexProperty, EdgeProperty,
10861071
// Requires the above, plus a vertex_index map.
10871072
template < typename Graph > void assign(const Graph& g)
10881073
{
1089-
typename graph_traits< Graph >::edges_size_type numedges = num_edges(g);
1090-
if (is_same< typename graph_traits< Graph >::directed_category,
1091-
undirectedS >::value)
1092-
{
1093-
numedges *= 2; // Double each edge (actual doubling done by
1094-
// out_edges function)
1095-
}
1074+
edges_size_type numedges
1075+
= ::boost::graph::detail::count_out_edges< edges_size_type >(g);
10961076
vertices_size_type numverts = num_vertices(g);
10971077
m_forward.assign(g, get(vertex_index, g), numverts, numedges);
10981078
inherited_vertex_properties::resize(numverts);

0 commit comments

Comments
 (0)