c++ - How does boost::copy_graph's vertex_copy work? -


i using boost::copy_graph copy adjacency_list adjacency_list different vertexproperties template. this, trying use vertex_copy parameter (doc here). running compiler error tells have wrong type vertex properties of second (to-be-copied) graph.

minimal example

#include <boost/graph/adjacency_list.hpp> #include <boost/graph/copy.hpp>  typedef boost::adjacency_list<boost::vecs,                               boost::vecs,                               boost::undirecteds,                               uint32_t,                               float> adjacencylist;  typedef adjacencylist::vertex_descriptor vertexid;  struct custom_property {     uint32_t label;     float f; };  typedef boost::adjacency_list<boost::vecs,                               boost::vecs,                               boost::undirecteds,                               custom_property,                               float> adjacencylistcustom;  struct vertex_copier {     void operator() (uint32_t &input, custom_property &output)     {         output.label = input;         output.f = 0.;     } };   int main(int argc, char** argv) {     adjacencylist adj;     vertexid id_0 = boost::add_vertex(0, adj);     vertexid id_1 = boost::add_vertex(1, adj);     vertexid id_2 = boost::add_vertex(2, adj);     vertexid id_3 = boost::add_vertex(4, adj);     boost::add_edge(id_0, id_1, 1.0f, adj);     boost::add_edge(id_2, id_3, 2.0f, adj);      adjacencylistcustom adj_custom;     boost::copy_graph(adj, adj_custom,  boost::vertex_copy(vertex_copier()));  } 

g++ compilation error

... /usr/include/boost/graph/copy.hpp:164:22: error: no match call '(vertex_copier) (boost::iterators::detail::iterator_facade_base<boost::range_detail::integer_iterator<long unsigned int>, long unsigned int, boost::iterators::random_access_traversal_tag, long unsigned int, long int, false, false>::reference, boost::graph_traits<boost::adjacency_list<boost::vecs, boost::vecs, boost::undirecteds, custom_property, float> >::vertex_descriptor&)'            copy_vertex(*vi, new_v);            ~~~~~~~~~~~^~~~~~~~~~~~ /path/to/file.cpp: note: candidate: void vertex_copier::operator()(uint32_t&, custom_property&)      void operator() (uint32_t &input, custom_property &output)           ^~~~~~~~ /path/to/file.cpp: note:   no known conversion argument 2 'boost::graph_traits<boost::adjacency_list<boost::vecs, boost::vecs, boost::undirecteds, custom_property, float> >::vertex_descriptor {aka long unsigned int}' 'custom_property&' 

this tells me that vertex_copy trying copy vertex_descriptor, long unsigned int, not custom_property. seems go against stated in docs:

this binary function copies properties of vertex in original graph corresponding vertex in copy.

how vertex_copy work? can used set/define vertex properties in copied graph not in original? if not, these properties have set after copying iterating through graph? can apply mapping en masse or each vertex have visited , updated?

edit: if attempt use copy_graph without specifying vertex_copy, there error because = operator not exist between custom_property , uint32_t.

sidestepping question second, simplest way achieve things add appropriate conversion constructor custom property:

struct custom_property {     custom_property(uint32_t label = 0, float f = 0) : label(label), f(f) {}     uint32_t label;     float f; }; 

in case, simple copy work:

boost::copy_graph(adj, adj_custom); 

see live on coliru


regarding vertex copier, receives vertex decriptors. access vertex properties, need have graph reference:

struct vertex_copier {     adjacencylist& from;     adjacencylistcustom& to;      void operator()(adjacencylist::vertex_descriptor input, adjacencylistcustom::vertex_descriptor output) const {         to[output] = { from[input], 0.f };     } }; 

in case you'd invoke things:

boost::copy_graph(adj, adj_custom, boost::vertex_copy(vertex_copier{adj, adj_custom})); 

again, live on coliru

#include <boost/graph/adjacency_list.hpp> #include <boost/graph/copy.hpp> #include <iostream>  typedef boost::adjacency_list<boost::vecs, boost::vecs, boost::undirecteds, uint32_t, float> adjacencylist;  typedef adjacencylist::vertex_descriptor vertexid;      struct custom_property {         //custom_property(uint32_t label = 0, float f = 0) : label(label), f(f) {}         uint32_t label;         float f;     };  typedef boost::adjacency_list<boost::vecs, boost::vecs, boost::undirecteds, custom_property, float> adjacencylistcustom;  struct vertex_copier {     adjacencylist& from;     adjacencylistcustom& to;      void operator()(adjacencylist::vertex_descriptor input, adjacencylistcustom::vertex_descriptor output) const {         to[output] = { from[input], 0.f };     } };  int main(int argc, char **argv) {     adjacencylist adj;     vertexid id_0 = boost::add_vertex(0, adj);     vertexid id_1 = boost::add_vertex(1, adj);     vertexid id_2 = boost::add_vertex(2, adj);     vertexid id_3 = boost::add_vertex(4, adj);     boost::add_edge(id_0, id_1, 1.0f, adj);     boost::add_edge(id_2, id_3, 2.0f, adj);      adjacencylistcustom adj_custom;     boost::copy_graph(adj, adj_custom, boost::vertex_copy(vertex_copier{adj, adj_custom})); } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -