Skip to content

Commit ac794e1

Browse files
authored
Merge pull request #636 from Becheler/feature/shortest-paths-visitor-std-ref
2 parents 910ac24 + bfc6252 commit ac794e1

9 files changed

Lines changed: 206 additions & 30 deletions

‎doc/modules/ROOT/pages/algorithms/shortest_paths/bellman_ford_shortest.adoc‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ for an example of using the Bellman-Ford algorithm.
310310

311311
== Notes
312312

313-
[#1]#[1]# Since the visitor parameter is passed by value, if your
314-
visitor contains state then any changes to the state during the algorithm
315-
will be made to a copy of the visitor object, not the visitor object
316-
passed in. Therefore you may want the visitor to hold this state by
317-
pointer or reference.
313+
[#1]#[1]# The visitor is taken by value, so the algorithm works on a copy.
314+
To keep state, give the visitor ordinary data members and pass it with
315+
`std::ref`. The algorithm then operates on the referenced object and its
316+
state survives the call.

‎doc/modules/ROOT/pages/algorithms/shortest_paths/dag_shortest_paths.adoc‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,7 @@ for an example of using this algorithm.
301301

302302
== Notes
303303

304-
[#1]#[1]# Since the visitor parameter is passed by value, if
305-
your visitor contains state then any changes to the state during the
306-
algorithm will be made to a copy of the visitor object, not the visitor
307-
object passed in. Therefore you may want the visitor to hold this state
308-
by pointer or reference.
304+
[#1]#[1]# The visitor is taken by value, so the algorithm works on a copy.
305+
To keep state, give the visitor ordinary data members and pass it with
306+
`std::ref`. The algorithm then operates on the referenced object and its
307+
state survives the call.

‎doc/modules/ROOT/pages/algorithms/shortest_paths/dijkstra_shortest_paths_no_color_map.adoc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ finish vertex u
197197

198198
| OUT
199199
| `visitor(DijkstraVisitor v)`
200-
| Use this to specify actions that you would like to happen during certain event points within the algorithm. The type `DijkstraVisitor` must be a model of the xref:visitors/DijkstraVisitor.adoc[Dijkstra Visitor] concept. The visitor object is passed by value. Default: `dijkstra_visitor<null_visitor>`
200+
| Use this to specify actions that you would like to happen during certain event points within the algorithm. The type `DijkstraVisitor` must be a model of the xref:visitors/DijkstraVisitor.adoc[Dijkstra Visitor] concept. The visitor is taken by value, so the algorithm works on a copy. To keep state, give the visitor ordinary data members and pass it with `std::ref`. The algorithm then operates on the referenced object and its state survives the call. Default: `dijkstra_visitor<null_visitor>`
201201
|===
202202

203203
== Complexity

‎include/boost/graph/bellman_ford_shortest_paths.hpp‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <boost/config.hpp>
2525
#include <boost/graph/graph_traits.hpp>
26+
#include <boost/graph/detail/visitor_wrapper.hpp>
2627
#include <boost/graph/graph_concepts.hpp>
2728
#include <boost/graph/properties.hpp>
2829
#include <boost/graph/relax.hpp>
@@ -101,21 +102,23 @@ bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N, WeightMap weight,
101102
BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept< DistanceMap, Vertex >));
102103
BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< WeightMap, Edge >));
103104

105+
auto& v_ref = ::boost::graph::detail::deref_visitor(v);
106+
104107
typename GTraits::edge_iterator i, end;
105108

106109
for (Size k = 0; k < N; ++k)
107110
{
108111
bool at_least_one_edge_relaxed = false;
109112
for (boost::tie(i, end) = edges(g); i != end; ++i)
110113
{
111-
v.examine_edge(*i, g);
114+
v_ref.examine_edge(*i, g);
112115
if (relax(*i, g, weight, pred, distance, combine, compare))
113116
{
114117
at_least_one_edge_relaxed = true;
115-
v.edge_relaxed(*i, g);
118+
v_ref.edge_relaxed(*i, g);
116119
}
117120
else
118-
v.edge_not_relaxed(*i, g);
121+
v_ref.edge_not_relaxed(*i, g);
119122
}
120123
if (!at_least_one_edge_relaxed)
121124
break;
@@ -125,11 +128,11 @@ bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N, WeightMap weight,
125128
if (compare(combine(get(distance, source(*i, g)), get(weight, *i)),
126129
get(distance, target(*i, g))))
127130
{
128-
v.edge_not_minimized(*i, g);
131+
v_ref.edge_not_minimized(*i, g);
129132
return false;
130133
}
131134
else
132-
v.edge_minimized(*i, g);
135+
v_ref.edge_minimized(*i, g);
133136

134137
return true;
135138
}

‎include/boost/graph/dag_shortest_paths.hpp‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP
1111
#define BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP
1212

13+
#include <boost/graph/detail/visitor_wrapper.hpp>
1314
#include <boost/graph/topological_sort.hpp>
1415
#include <boost/graph/dijkstra_shortest_paths.hpp>
1516

@@ -48,25 +49,27 @@ inline void dag_shortest_paths(const VertexListGraph& g,
4849
put(pred, *ui, *ui);
4950
}
5051

52+
auto& vis_ref = ::boost::graph::detail::deref_visitor(vis);
53+
5154
put(distance, s, zero);
52-
vis.discover_vertex(s, g);
55+
vis_ref.discover_vertex(s, g);
5356
typename std::vector< Vertex >::reverse_iterator i;
5457
for (i = rev_topo_order.rbegin(); i != rev_topo_order.rend(); ++i)
5558
{
5659
Vertex u = *i;
57-
vis.examine_vertex(u, g);
60+
vis_ref.examine_vertex(u, g);
5861
typename graph_traits< VertexListGraph >::out_edge_iterator e, e_end;
5962
for (boost::tie(e, e_end) = out_edges(u, g); e != e_end; ++e)
6063
{
61-
vis.discover_vertex(target(*e, g), g);
64+
vis_ref.discover_vertex(target(*e, g), g);
6265
bool decreased
6366
= relax(*e, g, weight, pred, distance, combine, compare);
6467
if (decreased)
65-
vis.edge_relaxed(*e, g);
68+
vis_ref.edge_relaxed(*e, g);
6669
else
67-
vis.edge_not_relaxed(*e, g);
70+
vis_ref.edge_not_relaxed(*e, g);
6871
}
69-
vis.finish_vertex(u, g);
72+
vis_ref.finish_vertex(u, g);
7073
}
7174
}
7275

‎include/boost/graph/dijkstra_shortest_paths_no_color_map.hpp‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <boost/pending/indirect_cmp.hpp>
1515
#include <boost/graph/relax.hpp>
1616
#include <boost/graph/detail/d_ary_heap.hpp>
17+
#include <boost/graph/detail/visitor_wrapper.hpp>
1718
#include <boost/graph/dijkstra_shortest_paths.hpp>
1819
#include <boost/throw_exception.hpp>
1920
#include <boost/graph/iteration_macros.hpp>
@@ -58,18 +59,20 @@ void dijkstra_shortest_paths_no_color_map_no_init(const Graph& graph,
5859
graph, index_map, index_in_heap_map_holder);
5960
VertexQueue vertex_queue(distance_map, index_in_heap, distance_compare);
6061

62+
auto& visitor_ref = ::boost::graph::detail::deref_visitor(visitor);
63+
6164
// Add vertex to the queue
6265
vertex_queue.push(start_vertex);
6366

6467
// Starting vertex will always be the first discovered vertex
65-
visitor.discover_vertex(start_vertex, graph);
68+
visitor_ref.discover_vertex(start_vertex, graph);
6669

6770
while (!vertex_queue.empty())
6871
{
6972
Vertex min_vertex = vertex_queue.top();
7073
vertex_queue.pop();
7174

72-
visitor.examine_vertex(min_vertex, graph);
75+
visitor_ref.examine_vertex(min_vertex, graph);
7376

7477
// Check if any other vertices can be reached
7578
Distance min_vertex_distance = get(distance_map, min_vertex);
@@ -83,7 +86,7 @@ void dijkstra_shortest_paths_no_color_map_no_init(const Graph& graph,
8386
// Examine neighbors of min_vertex
8487
BGL_FORALL_OUTEDGES_T(min_vertex, current_edge, graph, Graph)
8588
{
86-
visitor.examine_edge(current_edge, graph);
89+
visitor_ref.examine_edge(current_edge, graph);
8790

8891
// Check if the edge has a negative weight
8992
if (distance_compare(get(weight_map, current_edge), distance_zero))
@@ -105,10 +108,10 @@ void dijkstra_shortest_paths_no_color_map_no_init(const Graph& graph,
105108

106109
if (was_edge_relaxed)
107110
{
108-
visitor.edge_relaxed(current_edge, graph);
111+
visitor_ref.edge_relaxed(current_edge, graph);
109112
if (is_neighbor_undiscovered)
110113
{
111-
visitor.discover_vertex(neighbor_vertex, graph);
114+
visitor_ref.discover_vertex(neighbor_vertex, graph);
112115
vertex_queue.push(neighbor_vertex);
113116
}
114117
else
@@ -118,12 +121,12 @@ void dijkstra_shortest_paths_no_color_map_no_init(const Graph& graph,
118121
}
119122
else
120123
{
121-
visitor.edge_not_relaxed(current_edge, graph);
124+
visitor_ref.edge_not_relaxed(current_edge, graph);
122125
}
123126

124127
} // end out edge iteration
125128

126-
visitor.finish_vertex(min_vertex, graph);
129+
visitor_ref.finish_vertex(min_vertex, graph);
127130
} // end while queue not empty
128131
}
129132

@@ -141,10 +144,12 @@ void dijkstra_shortest_paths_no_color_map(const Graph& graph,
141144
DistanceInfinity distance_infinity, DistanceZero distance_zero,
142145
DijkstraVisitor visitor)
143146
{
147+
auto& visitor_ref = ::boost::graph::detail::deref_visitor(visitor);
148+
144149
// Initialize vertices
145150
BGL_FORALL_VERTICES_T(current_vertex, graph, Graph)
146151
{
147-
visitor.initialize_vertex(current_vertex, graph);
152+
visitor_ref.initialize_vertex(current_vertex, graph);
148153

149154
// Default all distances to infinity
150155
put(distance_map, current_vertex, distance_infinity);

‎test/bellman-test.cpp‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,61 @@ B: 2147483647 B
2020
#include <boost/cstdlib.hpp>
2121
#include <boost/core/lightweight_test.hpp>
2222

23+
#include <cstddef>
24+
#include <functional>
25+
#include <limits>
26+
#include <vector>
27+
28+
// state in a plain data member, so it survives only through std::ref
29+
struct relaxed_tally : boost::bellman_visitor<>
30+
{
31+
template < class Edge, class Graph > void edge_relaxed(Edge, Graph&)
32+
{
33+
++count;
34+
}
35+
std::size_t count = 0;
36+
};
37+
38+
void test_stateful_visitor_with_ref()
39+
{
40+
using graph_t = boost::adjacency_list< boost::vecS, boost::vecS,
41+
boost::directedS, boost::no_property,
42+
boost::property< boost::edge_weight_t, int > >;
43+
graph_t g(3);
44+
boost::add_edge(0, 1, 1, g);
45+
boost::add_edge(1, 2, 1, g);
46+
47+
// bellman_ford does not initialise, the caller seeds the distances
48+
std::vector< std::size_t > parent(boost::num_vertices(g));
49+
for (std::size_t i = 0; i < parent.size(); ++i)
50+
parent[i] = i;
51+
std::vector< int > distance(
52+
boost::num_vertices(g), (std::numeric_limits< int >::max)());
53+
distance[0] = 0;
54+
55+
auto index_map = boost::get(boost::vertex_index, g);
56+
auto parent_map
57+
= boost::make_iterator_property_map(parent.begin(), index_map);
58+
auto distance_map
59+
= boost::make_iterator_property_map(distance.begin(), index_map);
60+
61+
relaxed_tally tracked;
62+
BOOST_TEST(boost::bellman_ford_shortest_paths(g,
63+
static_cast< int >(boost::num_vertices(g)),
64+
boost::get(boost::edge_weight, g), parent_map, distance_map,
65+
boost::closed_plus< int >(), std::less< int >(), std::ref(tracked)));
66+
BOOST_TEST(tracked.count > 0u);
67+
BOOST_TEST_EQ(distance[2], 2);
68+
69+
// by value the caller's visitor is left untouched
70+
relaxed_tally copied;
71+
BOOST_TEST(boost::bellman_ford_shortest_paths(g,
72+
static_cast< int >(boost::num_vertices(g)),
73+
boost::get(boost::edge_weight, g), parent_map, distance_map,
74+
boost::closed_plus< int >(), std::less< int >(), copied));
75+
BOOST_TEST_EQ(copied.count, static_cast< std::size_t >(0));
76+
}
77+
2378
int main(int, char*[])
2479
{
2580
using namespace boost;
@@ -120,5 +175,7 @@ int main(int, char*[])
120175
}
121176
#endif
122177

178+
test_stateful_visitor_with_ref();
179+
123180
return boost::report_errors();
124181
}

‎test/dag_longest_paths.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,67 @@
99
#include <boost/property_map/vector_property_map.hpp>
1010
#include <boost/core/lightweight_test.hpp>
1111

12+
#include <algorithm>
13+
#include <cstddef>
14+
#include <functional>
15+
#include <limits>
16+
#include <vector>
17+
1218
using namespace boost;
1319

1420
#include <iostream>
1521
using namespace std;
1622

23+
// state in a plain data member, so it survives only through std::ref
24+
struct examine_tally : boost::dijkstra_visitor<>
25+
{
26+
template < class Vertex, class Graph > void examine_vertex(Vertex, Graph&)
27+
{
28+
++count;
29+
}
30+
std::size_t count = 0;
31+
};
32+
33+
void test_stateful_visitor_with_ref()
34+
{
35+
using graph_t = boost::adjacency_list< boost::vecS, boost::vecS,
36+
boost::directedS, boost::no_property,
37+
boost::property< boost::edge_weight_t, int > >;
38+
graph_t g(3);
39+
boost::add_edge(0, 1, 1, g);
40+
boost::add_edge(1, 2, 1, g);
41+
42+
std::vector< int > distance(boost::num_vertices(g));
43+
std::vector< std::size_t > parent(boost::num_vertices(g));
44+
std::vector< boost::default_color_type > color(boost::num_vertices(g));
45+
46+
auto index_map = boost::get(boost::vertex_index, g);
47+
auto distance_map
48+
= boost::make_iterator_property_map(distance.begin(), index_map);
49+
auto parent_map
50+
= boost::make_iterator_property_map(parent.begin(), index_map);
51+
auto color_map
52+
= boost::make_iterator_property_map(color.begin(), index_map);
53+
54+
// every vertex is reachable from 0, so each one is examined once
55+
examine_tally tracked;
56+
boost::dag_shortest_paths(g, 0, distance_map,
57+
boost::get(boost::edge_weight, g), color_map, parent_map,
58+
std::ref(tracked), std::less< int >(), boost::closed_plus< int >(),
59+
(std::numeric_limits< int >::max)(), 0);
60+
BOOST_TEST_EQ(tracked.count, boost::num_vertices(g));
61+
BOOST_TEST_EQ(distance[2], 2);
62+
63+
// by value the caller's visitor is left untouched
64+
std::fill(color.begin(), color.end(), boost::white_color);
65+
examine_tally copied;
66+
boost::dag_shortest_paths(g, 0, distance_map,
67+
boost::get(boost::edge_weight, g), color_map, parent_map, copied,
68+
std::less< int >(), boost::closed_plus< int >(),
69+
(std::numeric_limits< int >::max)(), 0);
70+
BOOST_TEST_EQ(copied.count, static_cast< std::size_t >(0));
71+
}
72+
1773
int main(int, char*[])
1874
{
1975
typedef adjacency_list< vecS, vecS, directedS, no_property,
@@ -50,5 +106,7 @@ int main(int, char*[])
50106

51107
BOOST_TEST(distance[2] == 2);
52108

109+
test_stateful_visitor_with_ref();
110+
53111
return boost::report_errors();
54112
}

0 commit comments

Comments
 (0)