Skip to content

Commit a3e5d00

Browse files
authored
Merge pull request #637 from Becheler/feature/adapter-visitor-std-ref
2 parents ac794e1 + aab8534 commit a3e5d00

7 files changed

Lines changed: 373 additions & 49 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,4 @@ See link:../example/astar-cities.cpp[`example/astar-cities.cpp`] for an example
541541

542542
== Notes
543543

544-
[#1]#[1]# Since the visitor parameter is passed by value, if your visitor contains state then any changes to the state during the algorithm will be made to a copy of the visitor object, not the visitor object passed in. Therefore you may want the visitor to hold this state by pointer or reference.
544+
[#1]#[1]# 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.

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,7 @@ putting all _V - S_ vertices in the priority queue at once, but instead
432432
only those vertices in _V - S_ that are discovered and therefore have a
433433
distance less than infinity.
434434

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

‎doc/modules/ROOT/pages/algorithms/spanning_trees/prim_minimum_spanning_tree.adoc‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,7 @@ for an example of using Prim's algorithm.
319319

320320
== Notes
321321

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

‎include/boost/graph/astar_search.hpp‎

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <boost/graph/relax.hpp>
2222
#include <boost/graph/exception.hpp>
2323
#include <boost/graph/breadth_first_search.hpp>
24+
#include <boost/graph/detail/visitor_wrapper.hpp>
2425
#include <boost/graph/iteration_macros.hpp>
2526
#include <boost/graph/detail/d_ary_heap.hpp>
2627
#include <boost/graph/property_maps/constant_property_map.hpp>
@@ -50,7 +51,7 @@ template < class Graph, class CostType > class astar_heuristic
5051
typedef Vertex argument_type;
5152
typedef CostType result_type;
5253
astar_heuristic() {}
53-
CostType operator()(Vertex u) { return static_cast< CostType >(0); }
54+
CostType operator()(Vertex /*u*/) { return static_cast< CostType >(0); }
5455
};
5556

5657
template < class Visitor, class Graph > struct AStarVisitorConcept
@@ -92,11 +93,11 @@ class astar_visitor : public bfs_visitor< Visitors >
9293
}
9394

9495
private:
95-
template < class Edge, class Graph > void tree_edge(Edge e, const Graph& g)
96+
template < class Edge, class Graph > void tree_edge(Edge /*e*/, const Graph& /*g*/)
9697
{
9798
}
9899
template < class Edge, class Graph >
99-
void non_tree_edge(Edge e, const Graph& g)
100+
void non_tree_edge(Edge /*e*/, const Graph& /*g*/)
100101
{
101102
}
102103
};
@@ -144,29 +145,29 @@ namespace detail
144145
template < class Vertex, class Graph >
145146
void initialize_vertex(Vertex u, const Graph& g)
146147
{
147-
m_vis.initialize_vertex(u, g);
148+
user_vis().initialize_vertex(u, g);
148149
}
149150
template < class Vertex, class Graph >
150151
void discover_vertex(Vertex u, const Graph& g)
151152
{
152-
m_vis.discover_vertex(u, g);
153+
user_vis().discover_vertex(u, g);
153154
}
154155
template < class Vertex, class Graph >
155156
void examine_vertex(Vertex u, const Graph& g)
156157
{
157-
m_vis.examine_vertex(u, g);
158+
user_vis().examine_vertex(u, g);
158159
}
159160
template < class Vertex, class Graph >
160161
void finish_vertex(Vertex u, const Graph& g)
161162
{
162-
m_vis.finish_vertex(u, g);
163+
user_vis().finish_vertex(u, g);
163164
}
164165
template < class Edge, class Graph >
165166
void examine_edge(Edge e, const Graph& g)
166167
{
167168
if (m_compare(get(m_weight, e), m_zero))
168169
BOOST_THROW_EXCEPTION(negative_edge());
169-
m_vis.examine_edge(e, g);
170+
user_vis().examine_edge(e, g);
170171
}
171172
template < class Edge, class Graph >
172173
void non_tree_edge(Edge, const Graph&)
@@ -182,13 +183,13 @@ namespace detail
182183

183184
if (m_decreased)
184185
{
185-
m_vis.edge_relaxed(e, g);
186+
user_vis().edge_relaxed(e, g);
186187
put(m_cost, target(e, g),
187188
m_combine(
188189
get(m_distance, target(e, g)), m_h(target(e, g))));
189190
}
190191
else
191-
m_vis.edge_not_relaxed(e, g);
192+
user_vis().edge_not_relaxed(e, g);
192193
}
193194

194195
template < class Edge, class Graph >
@@ -204,10 +205,10 @@ namespace detail
204205
m_combine(
205206
get(m_distance, target(e, g)), m_h(target(e, g))));
206207
m_Q.update(target(e, g));
207-
m_vis.edge_relaxed(e, g);
208+
user_vis().edge_relaxed(e, g);
208209
}
209210
else
210-
m_vis.edge_not_relaxed(e, g);
211+
user_vis().edge_not_relaxed(e, g);
211212
}
212213

213214
template < class Edge, class Graph >
@@ -219,16 +220,21 @@ namespace detail
219220

220221
if (m_decreased)
221222
{
222-
m_vis.edge_relaxed(e, g);
223+
user_vis().edge_relaxed(e, g);
223224
put(m_cost, target(e, g),
224225
m_combine(
225226
get(m_distance, target(e, g)), m_h(target(e, g))));
226227
m_Q.push(target(e, g));
227228
put(m_color, target(e, g), Color::gray());
228-
m_vis.black_target(e, g);
229+
user_vis().black_target(e, g);
229230
}
230231
else
231-
m_vis.edge_not_relaxed(e, g);
232+
user_vis().edge_not_relaxed(e, g);
233+
}
234+
235+
auto& user_vis()
236+
{
237+
return ::boost::graph::detail::deref_visitor(m_vis);
232238
}
233239

234240
AStarHeuristic m_h;
@@ -310,38 +316,40 @@ inline void astar_search_no_init_tree(const VertexListGraph& g,
310316
null_property_map< std::pair< Distance, Vertex >, std::size_t >(),
311317
compare);
312318

313-
vis.discover_vertex(s, g);
319+
auto& vis_ref = ::boost::graph::detail::deref_visitor(vis);
320+
321+
vis_ref.discover_vertex(s, g);
314322
Q.push(std::make_pair(get(cost, s), s));
315323
while (!Q.empty())
316324
{
317325
Vertex v;
318326
Distance v_rank;
319327
boost::tie(v_rank, v) = Q.top();
320328
Q.pop();
321-
vis.examine_vertex(v, g);
329+
vis_ref.examine_vertex(v, g);
322330
BGL_FORALL_OUTEDGES_T(v, e, g, VertexListGraph)
323331
{
324332
Vertex w = target(e, g);
325-
vis.examine_edge(e, g);
333+
vis_ref.examine_edge(e, g);
326334
Distance e_weight = get(weight, e);
327335
if (compare(e_weight, zero))
328336
BOOST_THROW_EXCEPTION(negative_edge());
329337
bool decreased
330338
= relax(e, g, weight, predecessor, distance, combine, compare);
331339
if (decreased)
332340
{
333-
vis.edge_relaxed(e, g);
341+
vis_ref.edge_relaxed(e, g);
334342
Distance w_rank = combine(get(distance, w), h(w));
335343
put(cost, w, w_rank);
336-
vis.discover_vertex(w, g);
344+
vis_ref.discover_vertex(w, g);
337345
Q.push(std::make_pair(w_rank, w));
338346
}
339347
else
340348
{
341-
vis.edge_not_relaxed(e, g);
349+
vis_ref.edge_not_relaxed(e, g);
342350
}
343351
}
344-
vis.finish_vertex(v, g);
352+
vis_ref.finish_vertex(v, g);
345353
}
346354
}
347355

@@ -361,14 +369,16 @@ inline void astar_search(const VertexListGraph& g,
361369

362370
typedef typename property_traits< ColorMap >::value_type ColorValue;
363371
typedef color_traits< ColorValue > Color;
372+
auto& vis_ref = ::boost::graph::detail::deref_visitor(vis);
373+
364374
typename graph_traits< VertexListGraph >::vertex_iterator ui, ui_end;
365375
for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
366376
{
367377
put(color, *ui, Color::white());
368378
put(distance, *ui, inf);
369379
put(cost, *ui, inf);
370380
put(predecessor, *ui, *ui);
371-
vis.initialize_vertex(*ui, g);
381+
vis_ref.initialize_vertex(*ui, g);
372382
}
373383
put(distance, s, zero);
374384
put(cost, s, h(s));
@@ -390,13 +400,15 @@ inline void astar_search_tree(const VertexListGraph& g,
390400
CostZero zero)
391401
{
392402

403+
auto& vis_ref = ::boost::graph::detail::deref_visitor(vis);
404+
393405
typename graph_traits< VertexListGraph >::vertex_iterator ui, ui_end;
394406
for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
395407
{
396408
put(distance, *ui, inf);
397409
put(cost, *ui, inf);
398410
put(predecessor, *ui, *ui);
399-
vis.initialize_vertex(*ui, g);
411+
vis_ref.initialize_vertex(*ui, g);
400412
}
401413
put(distance, s, zero);
402414
put(cost, s, h(s));

‎include/boost/graph/dijkstra_shortest_paths.hpp‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <boost/graph/overloading.hpp>
2727
#include <memory>
2828
#include <boost/graph/detail/d_ary_heap.hpp>
29+
#include <boost/graph/detail/visitor_wrapper.hpp>
2930
#include <boost/graph/two_bit_color_map.hpp>
3031
#include <boost/property_map/property_map.hpp>
3132
#include <boost/property_map/vector_property_map.hpp>
@@ -97,7 +98,7 @@ class dijkstra_visitor : public bfs_visitor< Visitors >
9798
}
9899

99100
private:
100-
template < class Edge, class Graph > void tree_edge(Edge u, Graph& g) {}
101+
template < class Edge, class Graph > void tree_edge(Edge /*u*/, Graph& /*g*/) {}
101102
};
102103
template < class Visitors >
103104
dijkstra_visitor< Visitors > make_dijkstra_visitor(Visitors vis)
@@ -136,9 +137,9 @@ namespace detail
136137
bool decreased = relax_target(e, g, m_weight, m_predecessor,
137138
m_distance, m_combine, m_compare);
138139
if (decreased)
139-
m_vis.edge_relaxed(e, g);
140+
user_vis().edge_relaxed(e, g);
140141
else
141-
m_vis.edge_not_relaxed(e, g);
142+
user_vis().edge_not_relaxed(e, g);
142143
}
143144
template < class Edge, class Graph > void gray_target(Edge e, Graph& g)
144145
{
@@ -149,27 +150,27 @@ namespace detail
149150
if (decreased)
150151
{
151152
dijkstra_queue_update(m_Q, target(e, g), old_distance);
152-
m_vis.edge_relaxed(e, g);
153+
user_vis().edge_relaxed(e, g);
153154
}
154155
else
155-
m_vis.edge_not_relaxed(e, g);
156+
user_vis().edge_not_relaxed(e, g);
156157
}
157158

158159
template < class Vertex, class Graph >
159160
void initialize_vertex(Vertex u, Graph& g)
160161
{
161-
m_vis.initialize_vertex(u, g);
162+
user_vis().initialize_vertex(u, g);
162163
}
163164
template < class Edge, class Graph > void non_tree_edge(Edge, Graph&) {}
164165
template < class Vertex, class Graph >
165166
void discover_vertex(Vertex u, Graph& g)
166167
{
167-
m_vis.discover_vertex(u, g);
168+
user_vis().discover_vertex(u, g);
168169
}
169170
template < class Vertex, class Graph >
170171
void examine_vertex(Vertex u, Graph& g)
171172
{
172-
m_vis.examine_vertex(u, g);
173+
user_vis().examine_vertex(u, g);
173174
}
174175
template < class Edge, class Graph > void examine_edge(Edge e, Graph& g)
175176
{
@@ -204,13 +205,18 @@ namespace detail
204205
boost::throw_exception(negative_edge());
205206
// End of test for negative-weight edges.
206207

207-
m_vis.examine_edge(e, g);
208+
user_vis().examine_edge(e, g);
208209
}
209210
template < class Edge, class Graph > void black_target(Edge, Graph&) {}
210211
template < class Vertex, class Graph >
211212
void finish_vertex(Vertex u, Graph& g)
212213
{
213-
m_vis.finish_vertex(u, g);
214+
user_vis().finish_vertex(u, g);
215+
}
216+
217+
auto& user_vis()
218+
{
219+
return ::boost::graph::detail::deref_visitor(m_vis);
214220
}
215221

216222
UniformCostVisitor m_vis;
@@ -250,8 +256,8 @@ namespace detail
250256
struct vertex_property_map_generator_helper< Graph, IndexMap, Value, false >
251257
{
252258
typedef boost::vector_property_map< Value, IndexMap > type;
253-
static type build(const Graph& g, const IndexMap& index,
254-
std::unique_ptr< Value[] >& array_holder)
259+
static type build(const Graph& /*g*/, const IndexMap& index,
260+
std::unique_ptr< Value[] >& /*array_holder*/)
255261
{
256262
return boost::make_vector_property_map< Value >(index);
257263
}
@@ -299,7 +305,7 @@ namespace detail
299305
typedef boost::vector_property_map< boost::two_bit_color_type,
300306
IndexMap >
301307
type;
302-
static type build(const Graph& g, const IndexMap& index)
308+
static type build(const Graph& /*g*/, const IndexMap& index)
303309
{
304310
return boost::make_vector_property_map< boost::two_bit_color_type >(
305311
index);
@@ -448,10 +454,12 @@ inline void dijkstra_shortest_paths(const VertexListGraph& g,
448454
{
449455
typedef typename property_traits< ColorMap >::value_type ColorValue;
450456
typedef color_traits< ColorValue > Color;
457+
auto& vis_ref = ::boost::graph::detail::deref_visitor(vis);
458+
451459
typename graph_traits< VertexListGraph >::vertex_iterator ui, ui_end;
452460
for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
453461
{
454-
vis.initialize_vertex(*ui, g);
462+
vis_ref.initialize_vertex(*ui, g);
455463
put(distance, *ui, inf);
456464
put(predecessor, *ui, *ui);
457465
put(color, *ui, Color::white());

0 commit comments

Comments
 (0)