From fadd33234edf9dc60f3cb5e0a016e0fade1faf7c Mon Sep 17 00:00:00 2001 From: rkohrt Date: Mon, 12 Sep 2022 17:41:36 +0200 Subject: [PATCH 1/6] align code to documentation --- include/boost/graph/depth_first_search.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/boost/graph/depth_first_search.hpp b/include/boost/graph/depth_first_search.hpp index 17d25e89c..3e50764a4 100644 --- a/include/boost/graph/depth_first_search.hpp +++ b/include/boost/graph/depth_first_search.hpp @@ -411,7 +411,7 @@ BOOST_GRAPH_MAKE_OLD_STYLE_PARAMETER_FUNCTION(depth_first_search, 1) template < class IncidenceGraph, class DFSVisitor, class ColorMap > void depth_first_visit(const IncidenceGraph& g, typename graph_traits< IncidenceGraph >::vertex_descriptor u, - DFSVisitor vis, ColorMap color) + DFSVisitor& vis, ColorMap color) { vis.start_vertex(u, g); detail::depth_first_visit_impl(g, u, vis, color, detail::nontruth2()); @@ -421,7 +421,7 @@ template < class IncidenceGraph, class DFSVisitor, class ColorMap, class TerminatorFunc > void depth_first_visit(const IncidenceGraph& g, typename graph_traits< IncidenceGraph >::vertex_descriptor u, - DFSVisitor vis, ColorMap color, TerminatorFunc func = TerminatorFunc()) + DFSVisitor& vis, ColorMap color, TerminatorFunc func = TerminatorFunc()) { vis.start_vertex(u, g); detail::depth_first_visit_impl(g, u, vis, color, func); From 04b9badb46ca73c36ef55b9cb19af5ef3a741197 Mon Sep 17 00:00:00 2001 From: Ralf Kohrt Date: Thu, 15 Sep 2022 13:39:56 +0200 Subject: [PATCH 2/6] return by value instead --- doc/depth_first_search.html | 4 ++-- doc/depth_first_visit.html | 8 ++++---- include/boost/graph/depth_first_search.hpp | 18 +++++++++++------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/doc/depth_first_search.html b/doc/depth_first_search.html index a376de3f1..65c99be30 100644 --- a/doc/depth_first_search.html +++ b/doc/depth_first_search.html @@ -28,10 +28,10 @@

(Python
 
 <i>// non-named parameter version</i>
 template <class Graph, class <a href=DFSVisitor, class ColorMap> -void depth_first_search(const Graph& g, DFSVisitor vis, ColorMap color) +DFSVisitor depth_first_search(const Graph& g, DFSVisitor vis, ColorMap color) template <class Graph, class DFSVisitor, class ColorMap> -void depth_first_search(const Graph& g, DFSVisitor vis, ColorMap color, +DFSVisitor depth_first_search(const Graph& g, DFSVisitor vis, ColorMap color, typename graph_traits<Graph>::vertex_descriptor start) diff --git a/doc/depth_first_visit.html b/doc/depth_first_visit.html index 81f73ba90..9a8fef9de 100644 --- a/doc/depth_first_visit.html +++ b/doc/depth_first_visit.html @@ -24,15 +24,15 @@

 template <class IncidenceGraph, class DFSVisitor, class ColorMap>
-void depth_first_visit(IncidenceGraph& g,
+DFSVisitor depth_first_visit(IncidenceGraph& g,
   typename graph_traits<IncidenceGraph>::vertex_descriptor s,
-  DFSVisitor& vis, ColorMap color)
+  DFSVisitor vis, ColorMap color)
 
 template <class IncidenceGraph, class DFSVisitor, class ColorMap,
           class TerminatorFunc>
-void depth_first_visit(IncidenceGraph& g,
+DFSVisitor depth_first_visit(IncidenceGraph& g,
   typename graph_traits<IncidenceGraph>::vertex_descriptor s,
-  DFSVisitor& vis, ColorMap color, TerminatorFunc func = TerminatorFunc())
+  DFSVisitor vis, ColorMap color, TerminatorFunc func = TerminatorFunc())
 

diff --git a/include/boost/graph/depth_first_search.hpp b/include/boost/graph/depth_first_search.hpp index 3e50764a4..e1523d4cb 100644 --- a/include/boost/graph/depth_first_search.hpp +++ b/include/boost/graph/depth_first_search.hpp @@ -263,7 +263,7 @@ namespace detail } // namespace detail template < class VertexListGraph, class DFSVisitor, class ColorMap > -void depth_first_search(const VertexListGraph& g, DFSVisitor vis, +DFSVisitor depth_first_search(const VertexListGraph& g, DFSVisitor vis, ColorMap color, typename graph_traits< VertexListGraph >::vertex_descriptor start_vertex) { @@ -298,18 +298,20 @@ void depth_first_search(const VertexListGraph& g, DFSVisitor vis, g, u, vis, color, detail::nontruth2()); } } + return vis; } template < class VertexListGraph, class DFSVisitor, class ColorMap > -void depth_first_search( +DFSVisitor depth_first_search( const VertexListGraph& g, DFSVisitor vis, ColorMap color) { typedef typename boost::graph_traits< VertexListGraph >::vertex_iterator vi; std::pair< vi, vi > verts = vertices(g); if (verts.first == verts.second) - return; + return vis; depth_first_search(g, vis, color, detail::get_default_starting_vertex(g)); + return vis; } template < class Visitors = null_visitor > class dfs_visitor @@ -409,22 +411,24 @@ namespace graph BOOST_GRAPH_MAKE_OLD_STYLE_PARAMETER_FUNCTION(depth_first_search, 1) template < class IncidenceGraph, class DFSVisitor, class ColorMap > -void depth_first_visit(const IncidenceGraph& g, +DFSVisitor depth_first_visit(const IncidenceGraph& g, typename graph_traits< IncidenceGraph >::vertex_descriptor u, - DFSVisitor& vis, ColorMap color) + DFSVisitor vis, ColorMap color) { vis.start_vertex(u, g); detail::depth_first_visit_impl(g, u, vis, color, detail::nontruth2()); + return vis; } template < class IncidenceGraph, class DFSVisitor, class ColorMap, class TerminatorFunc > -void depth_first_visit(const IncidenceGraph& g, +DFSVisitor depth_first_visit(const IncidenceGraph& g, typename graph_traits< IncidenceGraph >::vertex_descriptor u, - DFSVisitor& vis, ColorMap color, TerminatorFunc func = TerminatorFunc()) + DFSVisitor vis, ColorMap color, TerminatorFunc func = TerminatorFunc()) { vis.start_vertex(u, g); detail::depth_first_visit_impl(g, u, vis, color, func); + return vis; } } // namespace boost From cf9bf2b7a3d8497a7dc6942818c3c187dda47964 Mon Sep 17 00:00:00 2001 From: Ralf Kohrt Date: Thu, 15 Sep 2022 14:27:37 +0200 Subject: [PATCH 3/6] return the right copy --- include/boost/graph/depth_first_search.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/boost/graph/depth_first_search.hpp b/include/boost/graph/depth_first_search.hpp index e1523d4cb..89bd3f212 100644 --- a/include/boost/graph/depth_first_search.hpp +++ b/include/boost/graph/depth_first_search.hpp @@ -310,8 +310,7 @@ DFSVisitor depth_first_search( if (verts.first == verts.second) return vis; - depth_first_search(g, vis, color, detail::get_default_starting_vertex(g)); - return vis; + return depth_first_search(g, vis, color, detail::get_default_starting_vertex(g)); } template < class Visitors = null_visitor > class dfs_visitor From 393ba40d175530393a433e76659efdbf6c111492 Mon Sep 17 00:00:00 2001 From: Ralf Kohrt Date: Tue, 27 Sep 2022 12:33:16 +0200 Subject: [PATCH 4/6] add test to verify visitor is being copied --- test/Jamfile.v2 | 1 + test/dfv_test.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 test/dfv_test.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index a0c15ca7b..0fc3b1dfb 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -44,6 +44,7 @@ alias graph_test_regular : [ run csr_graph_test.cpp : : : : : release ] [ run dag_longest_paths.cpp ] [ run dfs.cpp ] + [ run dfv_test.cpp ] [ run undirected_dfs.cpp ] [ compile dfs_cc.cpp ] [ compile dijkstra_cc.cpp ] diff --git a/test/dfv_test.cpp b/test/dfv_test.cpp new file mode 100644 index 000000000..0fd274bb4 --- /dev/null +++ b/test/dfv_test.cpp @@ -0,0 +1,100 @@ +//======================================================================= +// Copyright 2022 Ralf Kohrt + +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +//======================================================================= +#include +#include +#include +#include +#include +#include +#include + +using namespace boost; + +// Set up the vertex names +enum vertex_id_t { u, v, w, x, y, z, N }; + + +struct counting_dfs_visitor +{ + template + void initialize_vertex(Vertex v, const Graph& g) + { + ++vertex_events; + } + + template + void start_vertex(Vertex v, const Graph& g) + { + ++vertex_events; + } + + template + void discover_vertex(Vertex v, const Graph& g) + { + ++vertex_events; + } + + template + void examine_edge(Edge e, const Graph& g) + { + ++seen_edges; + } + + template + void tree_edge(Edge e, const Graph& g) + {} + + template + void back_edge(Edge e, const Graph& g) + {} + + template + void forward_or_cross_edge(Edge e, const Graph& g) + {} + + template + void finish_vertex(Vertex v, const Graph& g) + { + ++vertex_events; + } + + size_t vertex_events = 0; + size_t seen_edges = 0; + +}; + +void +test_dfv_returns_copied_visitor() +{ + typedef adjacency_list > + Graph; + typedef typename boost::property_map< Graph, boost::vertex_color_t >::type + ColorMap; + + // Specify the edges in the graph + typedef std::pair E; + E edge_array[] = { E(u, v), E(u, w), E(u, x), E(x, v), E(y, x), + E(v, y), E(w, y), E(w, z), E(z, z) }; + Graph g(edge_array, edge_array + sizeof(edge_array) / sizeof(E), N); + + ColorMap color = get(boost::vertex_color, g); + counting_dfs_visitor visitor_copy = depth_first_visit(g, vertex(u, g), counting_dfs_visitor(), color); + BOOST_TEST(visitor_copy.vertex_events == 6u*2u + 1u); // discover_vertex + finish_vertex for each vertex and once start_vertex + BOOST_TEST(visitor_copy.seen_edges == 2u*9u); +} + +int +main(int argc, char* argv[]) +{ + test_dfv_returns_copied_visitor(); + return boost::report_errors(); +} From e6b9ac5054cf47cc6846a528a74d34f992806343 Mon Sep 17 00:00:00 2001 From: Ralf Kohrt Date: Tue, 27 Sep 2022 12:34:53 +0200 Subject: [PATCH 5/6] remove includes --- test/dfv_test.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/dfv_test.cpp b/test/dfv_test.cpp index 0fd274bb4..7fa2847a8 100644 --- a/test/dfv_test.cpp +++ b/test/dfv_test.cpp @@ -5,13 +5,9 @@ // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) //======================================================================= -#include -#include -#include #include #include #include -#include using namespace boost; From 1036e56d5ec7e77e870714d124b3f5f0a7180811 Mon Sep 17 00:00:00 2001 From: Ralf Kohrt Date: Wed, 28 Sep 2022 10:48:05 +0200 Subject: [PATCH 6/6] review comments --- test/dfv_test.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/test/dfv_test.cpp b/test/dfv_test.cpp index 7fa2847a8..967f61cae 100644 --- a/test/dfv_test.cpp +++ b/test/dfv_test.cpp @@ -17,6 +17,11 @@ enum vertex_id_t { u, v, w, x, y, z, N }; struct counting_dfs_visitor { + counting_dfs_visitor(): + vertex_events(0), + seen_edges(0) + {} + template void initialize_vertex(Vertex v, const Graph& g) { @@ -59,8 +64,8 @@ struct counting_dfs_visitor ++vertex_events; } - size_t vertex_events = 0; - size_t seen_edges = 0; + size_t vertex_events; + size_t seen_edges; }; @@ -84,8 +89,8 @@ test_dfv_returns_copied_visitor() ColorMap color = get(boost::vertex_color, g); counting_dfs_visitor visitor_copy = depth_first_visit(g, vertex(u, g), counting_dfs_visitor(), color); - BOOST_TEST(visitor_copy.vertex_events == 6u*2u + 1u); // discover_vertex + finish_vertex for each vertex and once start_vertex - BOOST_TEST(visitor_copy.seen_edges == 2u*9u); + BOOST_TEST(visitor_copy.vertex_events == num_vertices(g)*2u + 1u); // discover_vertex + finish_vertex for each vertex and once start_vertex + BOOST_TEST(visitor_copy.seen_edges == num_edges(g)*2u); } int