Skip to content

depth_first_search: Return visitor by value #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ alias graph_test_regular :
[ run csr_graph_test.cpp : : : : : <variant>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 ]
Expand Down
96 changes: 96 additions & 0 deletions test/dfv_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//=======================================================================
// 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 <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/core/lightweight_test.hpp>

using namespace boost;

// Set up the vertex names
enum vertex_id_t { u, v, w, x, y, z, N };


struct counting_dfs_visitor
{
template<typename Vertex, typename Graph>
void initialize_vertex(Vertex v, const Graph& g)
{
++vertex_events;
}

template<typename Vertex, typename Graph>
void start_vertex(Vertex v, const Graph& g)
{
++vertex_events;
}

template<typename Vertex, typename Graph>
void discover_vertex(Vertex v, const Graph& g)
{
++vertex_events;
}

template<typename Edge, typename Graph>
void examine_edge(Edge e, const Graph& g)
{
++seen_edges;
}

template<typename Edge, typename Graph>
void tree_edge(Edge e, const Graph& g)
{}

template<typename Edge, typename Graph>
void back_edge(Edge e, const Graph& g)
{}

template<typename Edge, typename Graph>
void forward_or_cross_edge(Edge e, const Graph& g)
{}

template<typename Vertex, typename Graph>
void finish_vertex(Vertex v, const Graph& g)
{
++vertex_events;
}

size_t vertex_events = 0;
size_t seen_edges = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is a compilation error in C++03, sorry. You'll have to initialize them the old-fashioned way, in the constructor.


};

void
test_dfv_returns_copied_visitor()
{
typedef adjacency_list<listS,
vecS,
undirectedS,
// Vertex properties
property<vertex_color_t, default_color_type> >
Graph;
typedef typename boost::property_map< Graph, boost::vertex_color_t >::type
ColorMap;

// Specify the edges in the graph
typedef std::pair<int, int> 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic numbers are easily confusing; presumably 6 = g.num_vertices(), and 9 = g.num_edges()?

}

int
main(int argc, char* argv[])
{
test_dfv_returns_copied_visitor();
return boost::report_errors();
}