diff --git a/doc/read_graphml.html b/doc/read_graphml.html
index 42c542a1..21796d8e 100644
--- a/doc/read_graphml.html
+++ b/doc/read_graphml.html
@@ -20,7 +20,7 @@ http://www.boost.org/LICENSE_1_0.txt)
Authors: Tiago de Paula Peixoto -->
void read_graphml(std::istream& in, MutableGraph& graph,
- dynamic_properties& dp);
+ dynamic_properties& dp, size_t graph_index = 0);
The read_graphml function interprets a graph described using the
GraphML format and builds a BGL graph that captures that
@@ -39,6 +39,10 @@ this object, using the GraphML attribute names as the property names,
and with the appropriate C++ value type based on the GraphML attribute type
definition. Graph properties are also set with the same
dynamic_properties object, where the key type is the type of the graph itself.
+If the file contains multiple graphs, the graph_index parameter controls
+which graph will be loaded. It defaults to 0, meaning that the first graph
+in the file will be loaded. If graph_index is greater than or equal to the
+number of graphs in the file, an empty graph will be returned.
- Requirements:
@@ -156,7 +160,7 @@ graph.
diff --git a/doc/read_graphml.rst b/doc/read_graphml.rst
index c61156d8..1e8bd818 100644
--- a/doc/read_graphml.rst
+++ b/doc/read_graphml.rst
@@ -19,7 +19,7 @@ __ ../../../index.htm
::
void read_graphml(std::istream& in, MutableGraph& graph,
- dynamic_properties& dp);
+ dynamic_properties& dp, size_t graph_index = 0);
The ``read_graphml`` function interprets a graph described using the
@@ -42,6 +42,11 @@ and with the appropriate C++ value type based on the GraphML attribute type
definition. Graph properties are also set with the same
dynamic_properties_ object, where the key type is the type of the graph itself.
+If the file contains multiple graphs, the ``graph_index`` parameter controls
+which graph will be loaded. It defaults to ``0``, meaning that the first graph
+in the file will be loaded. If ``graph_index`` is greater than or equal to the
+number of graphs in the file, an empty graph will be returned.
+
Requirements:
- The type of the graph must model the `Mutable Graph`_ concept.
- The type of the iterator must model the `Multi-Pass Iterator`_
diff --git a/include/boost/graph/graphml.hpp b/include/boost/graph/graphml.hpp
index 028cdc26..be73def3 100644
--- a/include/boost/graph/graphml.hpp
+++ b/include/boost/graph/graphml.hpp
@@ -204,14 +204,14 @@ template
const char* mutate_graph_impl::m_type_names[] = {"boolean", "int", "long", "float", "double", "string"};
void BOOST_GRAPH_DECL
-read_graphml(std::istream& in, mutate_graph& g);
+read_graphml(std::istream& in, mutate_graph& g, size_t desired_idx);
template
void
-read_graphml(std::istream& in, MutableGraph& g, dynamic_properties& dp)
+read_graphml(std::istream& in, MutableGraph& g, dynamic_properties& dp, size_t desired_idx = 0)
{
mutate_graph_impl mg(g,dp);
- read_graphml(in, mg);
+ read_graphml(in, mg, desired_idx);
}
template
diff --git a/src/graphml.cpp b/src/graphml.cpp
index 4a17fcaa..30c58fe7 100644
--- a/src/graphml.cpp
+++ b/src/graphml.cpp
@@ -34,17 +34,23 @@ public:
}
static void get_graphs(const boost::property_tree::ptree& top,
+ size_t desired_idx /* or -1 for all */,
std::vector& result) {
using boost::property_tree::ptree;
+ size_t current_idx = 0;
BOOST_FOREACH(const ptree::value_type& n, top) {
if (n.first == "graph") {
- result.push_back(&n.second);
- get_graphs(n.second, result);
+ if (current_idx == desired_idx || desired_idx == (size_t)(-1)) {
+ result.push_back(&n.second);
+ get_graphs(n.second, (size_t)(-1), result);
+ if (desired_idx != (size_t)(-1)) break;
+ }
+ ++current_idx;
}
}
}
- void run(std::istream& in)
+ void run(std::istream& in, size_t desired_idx)
{
using boost::property_tree::ptree;
ptree pt;
@@ -74,7 +80,7 @@ public:
}
// Search for graphs
std::vector graphs;
- get_graphs(gml, graphs);
+ get_graphs(gml, desired_idx, graphs);
BOOST_FOREACH(const ptree* gr, graphs) {
// Search for nodes
BOOST_FOREACH(const ptree::value_type& node, *gr) {
@@ -209,9 +215,9 @@ private:
namespace boost
{
void BOOST_GRAPH_DECL
-read_graphml(std::istream& in, mutate_graph& g)
+read_graphml(std::istream& in, mutate_graph& g, size_t desired_idx)
{
graphml_reader reader(g);
- reader.run(in);
+ reader.run(in, desired_idx);
}
}