From 779f88a10b7f3883085f27f1ec5950f3c8e684b1 Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Tue, 11 Oct 2016 13:57:39 +0200 Subject: [PATCH 1/3] grid_graph: Put get() in boost namespace. Implementing the get() functions as friend functions in the class declaration just puts them in the global namespace, or at least doesn't make them callable from the boost:: namespace. --- example/grid_graph_example.cpp | 4 +- include/boost/graph/grid_graph.hpp | 135 ++++++++++++++++++++++--------------- test/grid_graph_test.cpp | 18 ++--- 3 files changed, 92 insertions(+), 65 deletions(-) diff --git a/example/grid_graph_example.cpp b/example/grid_graph_example.cpp index 1406f2ef..4e371f62 100644 --- a/example/grid_graph_example.cpp +++ b/example/grid_graph_example.cpp @@ -36,7 +36,7 @@ int main(int argc, char* argv[]) { // The two indicies should always be equal std::cout << "Index of vertex " << v_index << " is " << - get(boost::vertex_index, graph, vertex(v_index, graph)) << std::endl; + boost::get(boost::vertex_index, graph, vertex(v_index, graph)) << std::endl; } @@ -46,7 +46,7 @@ int main(int argc, char* argv[]) { // The two indicies should always be equal std::cout << "Index of edge " << e_index << " is " << - get(boost::edge_index, graph, edge_at(e_index, graph)) << std::endl; + boost::get(boost::edge_index, graph, edge_at(e_index, graph)) << std::endl; } diff --git a/include/boost/graph/grid_graph.hpp b/include/boost/graph/grid_graph.hpp index 9a09fca3..7805691e 100644 --- a/include/boost/graph/grid_graph.hpp +++ b/include/boost/graph/grid_graph.hpp @@ -63,17 +63,21 @@ namespace boost { return (m_graph->index_of(key)); } - friend inline Index - get(const grid_graph_index_map& index_map, - const typename grid_graph_index_map::key_type& key) - { - return (index_map[key]); - } protected: const Graph* m_graph; }; + template + inline Index + get(const grid_graph_index_map& index_map, + const typename grid_graph_index_map::key_type& key) + { + return (index_map[key]); + } + template struct property_map { typedef grid_graph_index_map& rev_map, - const typename grid_graph_reverse_edge_map::key_type& key) - { - return (rev_map[key]); - } }; + template + inline Descriptor + get(const grid_graph_reverse_edge_map& rev_map, + const typename grid_graph_reverse_edge_map::key_type& key) + { + return (rev_map[key]); + } + template struct property_map { typedef grid_graph_reverse_edge_map type; @@ -429,6 +435,7 @@ namespace boost { return (m_edge_count[dimension_index]); } + public: // Returns the index of [vertex] (See also vertex_at) vertices_size_type index_of(vertex_descriptor vertex) const { @@ -446,6 +453,8 @@ namespace boost { return (vertex_index); } + protected: + // Returns the vertex whose index is [vertex_index] (See also // index_of(vertex_descriptor)) vertex_descriptor vertex_at @@ -536,6 +545,7 @@ namespace boost { return (std::make_pair(vertex_source, vertex_target)); } + public: // Returns the index for [edge] (See also edge_at) edges_size_type index_of(edge_descriptor edge) const { vertex_descriptor source_vertex = source(edge, *this); @@ -619,6 +629,8 @@ namespace boost { return (edge_index); } + protected: + // Returns the number of out-edges for [vertex] degree_size_type out_degree(vertex_descriptor vertex) const { @@ -953,48 +965,6 @@ namespace boost { // Index Property Map Functions //============================= - friend inline typename type::vertices_size_type - get(vertex_index_t, - const type& graph, - typename type::vertex_descriptor vertex) { - return (graph.index_of(vertex)); - } - - friend inline typename type::edges_size_type - get(edge_index_t, - const type& graph, - typename type::edge_descriptor edge) { - return (graph.index_of(edge)); - } - - friend inline grid_graph_index_map< - type, - typename type::vertex_descriptor, - typename type::vertices_size_type> - get(vertex_index_t, const type& graph) { - return (grid_graph_index_map< - type, - typename type::vertex_descriptor, - typename type::vertices_size_type>(graph)); - } - - friend inline grid_graph_index_map< - type, - typename type::edge_descriptor, - typename type::edges_size_type> - get(edge_index_t, const type& graph) { - return (grid_graph_index_map< - type, - typename type::edge_descriptor, - typename type::edges_size_type>(graph)); - } - - friend inline grid_graph_reverse_edge_map< - typename type::edge_descriptor> - get(edge_reverse_t, const type& graph) { - return (grid_graph_reverse_edge_map< - typename type::edge_descriptor>()); - } template + inline VertexIndex + get(vertex_index_t, + const grid_graph & graph, + typename grid_graph::vertex_descriptor vertex) { + return (graph.index_of(vertex)); + } + + template + inline VertexIndex + get(edge_index_t, + const grid_graph& graph, + typename grid_graph::edge_descriptor edge) { + return (graph.index_of(edge)); + } + + template + inline grid_graph_index_map< + grid_graph, + typename grid_graph::vertex_descriptor, + VertexIndex> + get(vertex_index_t, const grid_graph& graph) { + return (grid_graph_index_map< + grid_graph, + typename grid_graph::vertex_descriptor, + VertexIndex>(graph)); + } + + template + inline grid_graph_index_map< + grid_graph, + typename grid_graph::edge_descriptor, + EdgeIndex> + get(edge_index_t, const grid_graph& graph) { + return (grid_graph_index_map< + grid_graph, + typename grid_graph::edge_descriptor, + EdgeIndex>(graph)); + } + + template + inline grid_graph_reverse_edge_map< + typename grid_graph::edge_descriptor> + get(edge_reverse_t, const grid_graph& graph) { + return (grid_graph_reverse_edge_map< + typename grid_graph::edge_descriptor>()); + } } // namespace boost #undef BOOST_GRID_GRAPH_TYPE diff --git a/test/grid_graph_test.cpp b/test/grid_graph_test.cpp index d88e68d2..dd7648ad 100644 --- a/test/grid_graph_test.cpp +++ b/test/grid_graph_test.cpp @@ -79,7 +79,7 @@ void do_test(minstd_rand& generator) { for (vertices_size_type vertex_index = 0; vertex_index < num_vertices(graph); ++vertex_index) { - BOOST_REQUIRE(get(boost::vertex_index, graph, vertex(vertex_index, graph)) == vertex_index); + BOOST_REQUIRE(boost::get(boost::vertex_index, graph, vertex(vertex_index, graph)) == vertex_index); } for (edges_size_type edge_index = 0; @@ -87,7 +87,7 @@ void do_test(minstd_rand& generator) { ++edge_index) { edge_descriptor current_edge = edge_at(edge_index, graph); - BOOST_REQUIRE(get(boost::edge_index, graph, current_edge) == edge_index); + BOOST_REQUIRE(boost::get(boost::edge_index, graph, current_edge) == edge_index); } // Verify all vertices are within bounds @@ -95,7 +95,7 @@ void do_test(minstd_rand& generator) { BOOST_FOREACH(vertex_descriptor current_vertex, vertices(graph)) { vertices_size_type current_index = - get(boost::vertex_index, graph, current_vertex); + boost::get(boost::vertex_index, graph, current_vertex); for (unsigned int dimension_index = 0; dimension_index < Dims; @@ -112,7 +112,7 @@ void do_test(minstd_rand& generator) { out_edges(current_vertex, graph)) { target_vertices.insert - (get(boost::vertex_index, graph, target(out_edge, graph))); + (boost::get(boost::vertex_index, graph, target(out_edge, graph))); ++out_edge_count; } @@ -126,7 +126,7 @@ void do_test(minstd_rand& generator) { in_edges(current_vertex, graph)) { BOOST_REQUIRE(target_vertices.count - (get(boost::vertex_index, graph, source(in_edge, graph))) > 0); + (boost::get(boost::vertex_index, graph, source(in_edge, graph))) > 0); ++in_edge_count; } @@ -145,7 +145,7 @@ void do_test(minstd_rand& generator) { adjacent_vertices(current_vertex, graph)) { BOOST_REQUIRE(target_vertices.count - (get(boost::vertex_index, graph, adjacent_vertex)) > 0); + (boost::get(boost::vertex_index, graph, adjacent_vertex)) > 0); ++adjacent_count; } @@ -157,7 +157,7 @@ void do_test(minstd_rand& generator) { BOOST_FOREACH(vertex_descriptor unconnected_vertex, vertices(graph)) { vertices_size_type unconnected_index = - get(boost::vertex_index, graph, unconnected_vertex); + boost::get(boost::vertex_index, graph, unconnected_vertex); if ((unconnected_index == current_index) || (target_vertices.count(unconnected_index) > 0)) { @@ -178,10 +178,10 @@ void do_test(minstd_rand& generator) { BOOST_FOREACH(edge_descriptor current_edge, edges(graph)) { vertices_size_type source_index = - get(boost::vertex_index, graph, source(current_edge, graph)); + boost::get(boost::vertex_index, graph, source(current_edge, graph)); vertices_size_type target_index = - get(boost::vertex_index, graph, target(current_edge, graph)); + boost::get(boost::vertex_index, graph, target(current_edge, graph)); BOOST_REQUIRE(source_index != target_index); BOOST_REQUIRE(/* (source_index >= 0) : always true && */ (source_index < num_vertices(graph))); From 9b90280cd2520db940e992420a9e38f88eb1bfb9 Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Tue, 11 Oct 2016 14:15:03 +0200 Subject: [PATCH 2/3] grid_graph: Put vertices(), etc, in boost namespace. Implementing these functions as friend functions in the class declaration just puts them in the global namespace, or at least doesn't make them callable from the boost:: namespace. --- include/boost/graph/grid_graph.hpp | 74 ++++++++++++++++++++------------------ test/grid_graph_test.cpp | 4 +-- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/include/boost/graph/grid_graph.hpp b/include/boost/graph/grid_graph.hpp index 7805691e..6ce86ef3 100644 --- a/include/boost/graph/grid_graph.hpp +++ b/include/boost/graph/grid_graph.hpp @@ -417,8 +417,7 @@ namespace boost { return (vertex); } - protected: - + public: // Returns the number of vertices in the graph inline vertices_size_type num_vertices() const { return (m_num_vertices); @@ -435,7 +434,6 @@ namespace boost { return (m_edge_count[dimension_index]); } - public: // Returns the index of [vertex] (See also vertex_at) vertices_size_type index_of(vertex_descriptor vertex) const { @@ -453,8 +451,6 @@ namespace boost { return (vertex_index); } - protected: - // Returns the vertex whose index is [vertex_index] (See also // index_of(vertex_descriptor)) vertex_descriptor vertex_at @@ -751,35 +747,6 @@ namespace boost { public: - //================ - // VertexListGraph - //================ - - friend inline std::pair - vertices(const type& graph) { - typedef typename type::vertex_iterator vertex_iterator; - typedef typename type::vertex_function vertex_function; - typedef typename type::vertex_index_iterator vertex_index_iterator; - - return (std::make_pair - (vertex_iterator(vertex_index_iterator(0), - vertex_function(&graph)), - vertex_iterator(vertex_index_iterator(graph.num_vertices()), - vertex_function(&graph)))); - } - - friend inline typename type::vertices_size_type - num_vertices(const type& graph) { - return (graph.num_vertices()); - } - - friend inline typename type::vertex_descriptor - vertex(typename type::vertices_size_type vertex_index, - const type& graph) { - - return (graph.vertex_at(vertex_index)); - } //=============== // IncidenceGraph @@ -976,6 +943,45 @@ namespace boost { }; // grid_graph + //================ + // VertexListGraph + //================ + + template + inline std::pair::vertex_iterator, + typename grid_graph::vertex_iterator> + vertices(const grid_graph& graph) { + typedef typename grid_graph::vertex_iterator vertex_iterator; + typedef typename grid_graph::vertex_function vertex_function; + typedef typename grid_graph::vertex_index_iterator vertex_index_iterator; + + return (std::make_pair + (vertex_iterator(vertex_index_iterator(0), + vertex_function(&graph)), + vertex_iterator(vertex_index_iterator(graph.num_vertices()), + vertex_function(&graph)))); + } + + template + inline typename grid_graph::vertices_size_type + num_vertices(const grid_graph& graph) { + return (graph.num_vertices()); + } + + template + inline typename grid_graph::vertex_descriptor + vertex(typename grid_graph::vertices_size_type vertex_index, + const grid_graph& graph) { + + return (graph.vertex_at(vertex_index)); + } + template diff --git a/test/grid_graph_test.cpp b/test/grid_graph_test.cpp index dd7648ad..827b69c7 100644 --- a/test/grid_graph_test.cpp +++ b/test/grid_graph_test.cpp @@ -92,7 +92,7 @@ void do_test(minstd_rand& generator) { // Verify all vertices are within bounds vertices_size_type vertex_count = 0; - BOOST_FOREACH(vertex_descriptor current_vertex, vertices(graph)) { + BOOST_FOREACH(vertex_descriptor current_vertex, boost::vertices(graph)) { vertices_size_type current_index = boost::get(boost::vertex_index, graph, current_vertex); @@ -154,7 +154,7 @@ void do_test(minstd_rand& generator) { // Verify that this vertex is not listed as connected to any // vertices outside of its adjacent vertices. - BOOST_FOREACH(vertex_descriptor unconnected_vertex, vertices(graph)) { + BOOST_FOREACH(vertex_descriptor unconnected_vertex, boost::vertices(graph)) { vertices_size_type unconnected_index = boost::get(boost::vertex_index, graph, unconnected_vertex); From ae5192753446cad09a5e24d7cd3e187d28e458fa Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Mon, 17 Oct 2016 08:38:47 +0200 Subject: [PATCH 3/3] grid_graph: Put num_edges(), etc, in boost namespace. Implementing these functions as friend functions in the class declaration just puts them in the global namespace, or at least doesn't make them callable from the boost:: namespace. --- example/grid_graph_example.cpp | 8 ++--- include/boost/graph/grid_graph.hpp | 66 ++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/example/grid_graph_example.cpp b/example/grid_graph_example.cpp index 4e371f62..c525f213 100644 --- a/example/grid_graph_example.cpp +++ b/example/grid_graph_example.cpp @@ -32,17 +32,17 @@ int main(int argc, char* argv[]) { // Do a round-trip test of the vertex index functions for (Traits::vertices_size_type v_index = 0; - v_index < num_vertices(graph); ++v_index) { + v_index < boost::num_vertices(graph); ++v_index) { // The two indicies should always be equal std::cout << "Index of vertex " << v_index << " is " << - boost::get(boost::vertex_index, graph, vertex(v_index, graph)) << std::endl; + boost::get(boost::vertex_index, graph, boost::vertex(v_index, graph)) << std::endl; } // Do a round-trip test of the edge index functions for (Traits::edges_size_type e_index = 0; - e_index < num_edges(graph); ++e_index) { + e_index < boost::num_edges(graph); ++e_index) { // The two indicies should always be equal std::cout << "Index of edge " << e_index << " is " << @@ -63,7 +63,7 @@ int main(int argc, char* argv[]) { (graph.wrapped(2) ? "W" : "U") << std::endl; // prints "W, U, W" // Start with the first vertex in the graph - Traits::vertex_descriptor first_vertex = vertex(0, graph); + Traits::vertex_descriptor first_vertex = boost::vertex(0, graph); print_vertex(first_vertex); // prints "(0, 0, 0)" // Print the next vertex in dimension 0 diff --git a/include/boost/graph/grid_graph.hpp b/include/boost/graph/grid_graph.hpp index 6ce86ef3..8d183281 100644 --- a/include/boost/graph/grid_graph.hpp +++ b/include/boost/graph/grid_graph.hpp @@ -800,34 +800,6 @@ namespace boost { adjacent_vertex_function(vertex, &graph)))); } - //============== - // EdgeListGraph - //============== - - friend inline typename type::edges_size_type - num_edges(const type& graph) { - return (graph.num_edges()); - } - - friend inline typename type::edge_descriptor - edge_at(typename type::edges_size_type edge_index, - const type& graph) { - return (graph.edge_at(edge_index)); - } - - friend inline std::pair - edges(const type& graph) { - typedef typename type::edge_index_iterator edge_index_iterator; - typedef typename type::edge_function edge_function; - typedef typename type::edge_iterator edge_iterator; - - return (std::make_pair - (edge_iterator(edge_index_iterator(0), - edge_function(&graph)), - edge_iterator(edge_index_iterator(graph.num_edges()), - edge_function(&graph)))); - } //=================== // BiDirectionalGraph @@ -1039,6 +1011,44 @@ namespace boost { return (grid_graph_reverse_edge_map< typename grid_graph::edge_descriptor>()); } + + //============== + // EdgeListGraph + //============== + + template + inline EdgeIndex + num_edges(const grid_graph& graph) { + return (graph.num_edges()); + } + + template + inline typename grid_graph::edge_descriptor + edge_at(EdgeIndex edge_index, + const grid_graph& graph) { + return (graph.edge_at(edge_index)); + } + + template + inline std::pair::edge_iterator, + typename grid_graph::edge_iterator> + edges(const grid_graph& graph) { + typedef typename grid_graph::edge_index_iterator edge_index_iterator; + typedef typename grid_graph::edge_function edge_function; + typedef typename grid_graph::edge_iterator edge_iterator; + + return (std::make_pair + (edge_iterator(edge_index_iterator(0), + edge_function(&graph)), + edge_iterator(edge_index_iterator(graph.num_edges()), + edge_function(&graph)))); + } } // namespace boost #undef BOOST_GRID_GRAPH_TYPE