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 17d25e89c..89bd3f212 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,19 @@ 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 depth_first_search(g, vis, color, detail::get_default_starting_vertex(g)); } template < class Visitors = null_visitor > class dfs_visitor @@ -409,22 +410,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) { 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()) { vis.start_vertex(u, g); detail::depth_first_visit_impl(g, u, vis, color, func); + return vis; } } // namespace boost 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..967f61cae --- /dev/null +++ b/test/dfv_test.cpp @@ -0,0 +1,101 @@ +//======================================================================= +// 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 + +using namespace boost; + +// Set up the vertex names +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) + { + ++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; + size_t seen_edges; + +}; + +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 == 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 +main(int argc, char* argv[]) +{ + test_dfv_returns_copied_visitor(); + return boost::report_errors(); +}