Here is a complete snippet to copy a graph with bundled properties, but results in bunch of compiler errors. What is needed to fix the problems?
struct NodeI
That's slightly misrepresenting the question. You're not actually copying the adjacency list, you're copying the labeled_graph adaptor, which happens to not satisfy the concepts required by copy_graph
:
/** @name Labeled Mutable Graph * The labeled mutable graph hides the add_ and remove_ vertex functions from * the mutable graph concept. Note that the remove_vertex is hidden because * removing the vertex without its key could leave a dangling reference in * the map. */
Here's copying the adjacency_list: ¹
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, NodeInfo1, EdgeInfo1> AList;
typedef boost::labeled_graph<AList, std::string> Graph;
void TestCopyGraph()
{
std::string names[3] = { "A", "B", "C" };
Graph grid(3, names);
EdgeInfo1 ei;
/*auto e =*/ add_edge_by_label("C", "B", ei, grid);
AList g1;
copy_graph(grid, g1);
}
Is much easier. No copy_graph
required, just copy-construct the object:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/copy.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/graph_utility.hpp>
struct NodeInfo1 { int i; };
struct EdgeInfo1 { int j; };
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, NodeInfo1, EdgeInfo1> AList;
typedef boost::labeled_graph<AList, std::string> Graph;
auto TestCopyGraph()
{
std::string names[3] = { "A", "B", "C" };
NodeInfo1 props[3] = { {11}, {22}, {33} };
Graph grid(3, names, props);
/*auto e =*/ add_edge_by_label("C", "B", EdgeInfo1{17}, grid);
Graph g1 = grid; // just copy-construct
return g1;
}
int main() {
auto copied = TestCopyGraph();
print_graph(copied);
// check that properties were copied: vertex B has NodeInfo1 22
{
auto pmap = boost::get(&NodeInfo1::i, copied);
std::cout << "Vertex B NodeInfo1.i after copy: " << pmap[copied.vertex("B")] << "\n";
}
// edge properties too:
for (auto e : boost::make_iterator_range(edges(copied)))
std::cout << "Edge has property EdgeInfo1 " << copied[e].j << "\n";
std::cout << "Removed A:\n";
copied.remove_vertex("A");
print_graph(copied);
}
Prints
0 <-->
1 <--> 2
2 <--> 1
Vertex B NodeInfo1.i after copy: 22
Edge has property EdgeInfo1 17
Removed A:
0 <--> 1
1 <--> 0
¹ Note that you need this patch because of bugs in labeled_graph: https://github.com/boostorg/graph/pull/58