Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r77222 - in trunk: boost/graph libs/graph/test
From: jewillco_at_[hidden]
Date: 2012-03-04 15:21:22


Author: jewillco
Date: 2012-03-04 15:21:22 EST (Sun, 04 Mar 2012)
New Revision: 77222
URL: http://svn.boost.org/trac/boost/changeset/77222

Log:
Made Graphviz reading into CSR graphs work for external properties only; refs #5442
Text files modified:
   trunk/boost/graph/graphviz.hpp | 41 ++++++++++++++++++----------
   trunk/libs/graph/test/graphviz_test.cpp | 57 ++++++++++++++++++++++++---------------
   2 files changed, 61 insertions(+), 37 deletions(-)

Modified: trunk/boost/graph/graphviz.hpp
==============================================================================
--- trunk/boost/graph/graphviz.hpp (original)
+++ trunk/boost/graph/graphviz.hpp 2012-03-04 15:21:22 EST (Sun, 04 Mar 2012)
@@ -26,6 +26,7 @@
 #include <boost/graph/overloading.hpp>
 #include <boost/graph/dll_import_export.hpp>
 #include <boost/graph/compressed_sparse_row_graph.hpp>
+#include <boost/graph/iteration_macros.hpp>
 #include <boost/spirit/include/classic_multi_pass.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/static_assert.hpp>
@@ -808,13 +809,10 @@
 class mutate_graph_impl<compressed_sparse_row_graph<Directed, VertexProperty, EdgeProperty, GraphProperty, Vertex, EdgeIndex> >
   : public mutate_graph
 {
- // The first part of this is to make the code dependent on a template parameter.
- BOOST_STATIC_ASSERT_MSG(sizeof(Directed) == 0,
- "Graphviz loading for CSR graphs is broken");
-
   typedef compressed_sparse_row_graph<Directed, VertexProperty, EdgeProperty, GraphProperty, Vertex, EdgeIndex> CSRGraph;
   typedef typename graph_traits<CSRGraph>::vertices_size_type bgl_vertex_t;
   typedef typename graph_traits<CSRGraph>::edges_size_type bgl_edge_t;
+ typedef typename graph_traits<CSRGraph>::edge_descriptor edge_descriptor;
 
  public:
   mutate_graph_impl(CSRGraph& graph, dynamic_properties& dp,
@@ -824,13 +822,25 @@
   ~mutate_graph_impl() {}
 
   void finish_building_graph() {
- CSRGraph temp(edges_are_unsorted, edges.begin(), edges.end(), vertex_count);
+ typedef compressed_sparse_row_graph<directedS, no_property, bgl_edge_t, GraphProperty, Vertex, EdgeIndex> TempCSRGraph;
+ TempCSRGraph temp(edges_are_unsorted_multi_pass,
+ edges_to_add.begin(), edges_to_add.end(),
+ counting_iterator<bgl_edge_t>(0),
+ vertex_count);
     set_property(temp, graph_all, get_property(graph_, graph_all));
- graph_ = temp;
- typedef tuple<id_t, bgl_edge_t, id_t> ve_prop;
- BOOST_FOREACH(const ve_prop& t, vertex_and_edge_props) {
+ graph_.assign(temp); // Copies structure, not properties
+ std::vector<edge_descriptor> edge_permutation_from_sorting(num_edges(temp));
+ BGL_FORALL_EDGES_T(e, temp, TempCSRGraph) {
+ edge_permutation_from_sorting[temp[e]] = e;
+ }
+ typedef tuple<id_t, bgl_vertex_t, id_t> v_prop;
+ BOOST_FOREACH(const v_prop& t, vertex_props) {
       put(get<0>(t), dp_, get<1>(t), get<2>(t));
     }
+ typedef tuple<id_t, bgl_edge_t, id_t> e_prop;
+ BOOST_FOREACH(const e_prop& t, edge_props) {
+ put(get<0>(t), dp_, edge_permutation_from_sorting[get<1>(t)], get<2>(t));
+ }
   }
 
   bool is_directed() const
@@ -850,27 +860,27 @@
     bgl_nodes.insert(std::make_pair(node, v));
 
     // node_id_prop_ allows the caller to see the real id names for nodes.
- vertex_and_edge_props.push_back(make_tuple(node_id_prop_, v, node));
+ vertex_props.push_back(make_tuple(node_id_prop_, v, node));
   }
 
   void
   do_add_edge(const edge_t& edge, const node_t& source, const node_t& target)
   {
- bgl_edge_t result = edges.size();
- edges.push_back(std::make_pair(bgl_nodes[source], bgl_nodes[target]));
+ bgl_edge_t result = edges_to_add.size();
+ edges_to_add.push_back(std::make_pair(bgl_nodes[source], bgl_nodes[target]));
     bgl_edges.insert(std::make_pair(edge, result));
   }
 
   void
   set_node_property(const id_t& key, const node_t& node, const id_t& value)
   {
- vertex_and_edge_props.push_back(make_tuple(key, bgl_nodes[node], value));
+ vertex_props.push_back(make_tuple(key, bgl_nodes[node], value));
   }
 
   void
   set_edge_property(const id_t& key, const edge_t& edge, const id_t& value)
   {
- vertex_and_edge_props.push_back(make_tuple(key, bgl_edges[edge], value));
+ edge_props.push_back(make_tuple(key, bgl_edges[edge], value));
   }
 
   void
@@ -886,8 +896,9 @@
   dynamic_properties& dp_;
   bgl_vertex_t vertex_count;
   std::string node_id_prop_;
- std::vector<tuple<id_t, bgl_edge_t, id_t> > vertex_and_edge_props;
- std::vector<std::pair<bgl_vertex_t, bgl_vertex_t> > edges;
+ std::vector<tuple<id_t, bgl_vertex_t, id_t> > vertex_props;
+ std::vector<tuple<id_t, bgl_edge_t, id_t> > edge_props;
+ std::vector<std::pair<bgl_vertex_t, bgl_vertex_t> > edges_to_add;
   std::map<node_t, bgl_vertex_t> bgl_nodes;
   std::map<edge_t, bgl_edge_t> bgl_edges;
 };

Modified: trunk/libs/graph/test/graphviz_test.cpp
==============================================================================
--- trunk/libs/graph/test/graphviz_test.cpp (original)
+++ trunk/libs/graph/test/graphviz_test.cpp 2012-03-04 15:21:22 EST (Sun, 04 Mar 2012)
@@ -42,15 +42,16 @@
 typedef std::map<node_t,float> mass_map_t;
 typedef std::map<edge_t,double> weight_map_t;
 
-template <typename graph_t, typename NameMapKey, typename MassMapKey, typename WeightMapKey>
-bool test_graph(std::istream& dotfile, std::size_t correct_num_vertices,
+template <typename graph_t, typename NameMap, typename MassMap, typename WeightMap>
+bool test_graph(std::istream& dotfile, graph_t& graph,
+ std::size_t correct_num_vertices,
                 mass_map_t const& masses,
                 weight_map_t const& weights,
- std::string const& node_id = "node_id",
- std::string const& g_name = std::string(),
- NameMapKey name_map_key = boost::vertex_name,
- MassMapKey mass_map_key = boost::vertex_color,
- WeightMapKey weight_map_key = boost::edge_weight);
+ std::string const& node_id,
+ std::string const& g_name,
+ NameMap name,
+ MassMap mass,
+ WeightMap weight);
 
 template <typename graph_t>
 bool test_graph(std::istream& dotfile, std::size_t correct_num_vertices,
@@ -58,33 +59,29 @@
                 weight_map_t const& weights,
                 std::string const& node_id = "node_id",
                 std::string const& g_name = std::string()) {
- return test_graph<graph_t, boost::vertex_name_t, boost::vertex_color_t, boost::edge_weight_t>(dotfile, correct_num_vertices, masses, weights, node_id);
+ graph_t g;
+ return test_graph(dotfile, g, correct_num_vertices, masses, weights, node_id, g_name,
+ get(vertex_name, g), get(vertex_color, g), get(edge_weight, g));
 }
 
-template <typename graph_t, typename NameMapKey, typename MassMapKey, typename WeightMapKey>
-bool test_graph(std::istream& dotfile, std::size_t correct_num_vertices,
+template <typename graph_t, typename NameMap, typename MassMap, typename WeightMap>
+bool test_graph(std::istream& dotfile, graph_t& graph,
+ std::size_t correct_num_vertices,
                 mass_map_t const& masses,
                 weight_map_t const& weights,
- std::string const& node_id = "node_id",
- std::string const& g_name = std::string(),
- NameMapKey name_map_key = boost::vertex_name,
- MassMapKey mass_map_key = boost::vertex_color,
- WeightMapKey weight_map_key = boost::edge_weight) {
+ std::string const& node_id,
+ std::string const& g_name,
+ NameMap name,
+ MassMap mass,
+ WeightMap weight) {
 
   typedef typename graph_traits < graph_t >::edge_descriptor edge_t;
   typedef typename graph_traits < graph_t >::vertex_descriptor vertex_t;
 
   // Construct a graph and set up the dynamic_property_maps.
- graph_t graph;
   dynamic_properties dp(ignore_other_properties);
- typename property_map<graph_t, NameMapKey>::type name =
- get(name_map_key, graph);
   dp.property(node_id,name);
- typename property_map<graph_t, MassMapKey>::type mass =
- get(mass_map_key, graph);
   dp.property("mass",mass);
- typename property_map<graph_t, WeightMapKey>::type weight =
- get(weight_map_key, graph);
   dp.property("weight",weight);
 
   boost::ref_property_map<graph_t*,std::string> gname(
@@ -344,5 +341,21 @@
   }
 #endif
 
+ BOOST_AUTO_TEST_CASE (basic_csr_directed_graph_ext_props) {
+ weight_map_t weights;
+ insert( weights )(make_pair("a","b"),0.0)
+ (make_pair("c","d"),7.7)(make_pair("e","f"),6.66)
+ (make_pair("d","e"),0.5)(make_pair("e","a"),0.5);
+ gs_t gs("digraph { a -> b eDge [weight = 7.7] "
+ "c -> d e-> f [weight = 6.66] "
+ "d ->e->a [weight=.5]}");
+ typedef compressed_sparse_row_graph<directedS, no_property, no_property, graph_p > graph_t;
+ graph_t g;
+ vector_property_map<std::string, property_map<graph_t, vertex_index_t>::const_type> vertex_name(get(vertex_index, g));
+ vector_property_map<float, property_map<graph_t, vertex_index_t>::const_type> vertex_color(get(vertex_index, g));
+ vector_property_map<double, property_map<graph_t, edge_index_t>::const_type> edge_weight(get(edge_index, g));
+ BOOST_CHECK((test_graph(gs,g,6,mass_map_t(),weights,"node_id","",vertex_name,vertex_color,edge_weight)));
+ }
+
 // return 0;
 // }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk