Custom InputIterator for Boost graph (BGL)
I have a graph with custom properties to the verti开发者_如何学Cces and edges. I now want to create a copy of this graph, but I don't want the vertices to be as complex as in the original. By this I mean that it would suffice that the vertices have the same indices (vertex_index_t) as they do in the original graph.
Instead of doing the copying by hand I wanted to use the copy-functionality of boost::adjacency_list (s. http://www.boost.org/doc/libs/1_37_0/libs/graph/doc/adjacency_list.html):template <class EdgeIterator>
adjacency_list(EdgeIterator first, EdgeIterator last,
vertices_size_type n,
edges_size_type m = 0,
const GraphProperty& p = GraphProperty())
The description there says:
The EdgeIterator must be a model of InputIterator. The value type of the EdgeIterator must be a std::pair, where the type in the pair is an integer type. The integers will correspond to vertices, and they must all fall in the range of [0, n).
Unfortunately I have to admit that I don't quite get it how to define an EdgeIterator that is a model of InputIterator.
Here's what I've succeded so far:template< class EdgeIterator, class Edge >
class MyEdgeIterator// : public input_iterator< std::pair<int, int> >
{
public:
MyEdgeIterator() {};
MyEdgeIterator(EdgeIterator& rhs) : actual_edge_it_(rhs) {};
MyEdgeIterator(const MyEdgeIterator& to_copy) {};
bool operator==(const MyEdgeIterator& to_compare)
{
return actual_edge_it_ == to_compare.actual_edge_it_;
}
bool operator!=(const MyEdgeIterator& to_compare)
{
return !(*this == to_compare);
}
Edge operator*() const
{
return *actual_edge_it_;
}
const MyEdgeIterator* operator->() const;
MyEdgeIterator& operator ++()
{
++actual_edge_it_;
return *this;
}
MyEdgeIterator operator ++(int)
{
MyEdgeIterator<EdgeIterator, Edge> tmp = *this;
++*this;
return tmp;
}
private:
EdgeIterator& actual_edge_it_;
}
However, this doesn't work as it is supposed to and I ran out of clues.
So, how do I define the appropriate InputIterator?I encounter the same problem and I used Transform Iterator from boost.
I implemented copyGraph that does the conversion.
Example:
SimpleGraphType simple = copyGraph<ComplexGraphType, SimpleGraphType>(complex);
And the code:
#include <boost/iterator/transform_iterator.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <utility>
template <class Graph>
class EdgeToPair
{
public:
typedef std::pair<int, int> result_type;
typedef typename boost::graph_traits<Graph>::edge_iterator edge_iter;
typedef typename std::iterator_traits<edge_iter>::value_type edge_type;
EdgeToPair(const Graph& graph) : _graph(graph) {}
std::pair<int, int> operator()(edge_type edge) const
{
return std::make_pair(boost::source(edge, _graph),
boost::target(edge, _graph));
}
private:
const Graph& _graph;
};
template <class GraphIn, class GraphOut>
GraphOut copyGraph(const GraphIn& graphIn)
{
EdgeToPair<GraphIn> edgeToPair(graphIn);
typename boost::graph_traits<GraphIn>::edge_iterator ei, eend;
boost::tie(ei, eend) = boost::edges(graphIn);
return GraphOut(boost::make_transform_iterator(ei, edgeToPair),
boost::make_transform_iterator(eend, edgeToPair),
boost::num_vertices(graphIn));
}
I'm sure there exists a better solution using boost::copy_graph and by passing a custom edge_copy but at least this solution works.
精彩评论