I want to replace the BGL iteration over vertexes or edges with pure C++11 equivalent. The BGL code (from: http://www.boost.org/doc/libs/1_52_0/libs/graph/doc/quick_tour.html) is:
typename boost::graph_traits<Graph>::out_edge_iterator out_i, out_end;
typename boost::graph_traits<Graph>::edge_descriptor e;
for (std::tie(out_i, out_end) = out_edges(v, g);
out_i != out_end; ++out_i)
{
e = *out_i;
Vertex src = source(e, g), targ = target(e, g);
std::cout << "(" << name[get(vertex_id, src)]
<< "," << name[get(vertex_id, targ)] << ") ";
}
I tried several suggestions from here: Replace BOOST_FOREACH with "pure" C++11 alternative? but without luck.
I want to be able to write something like:
for (auto &e : out_edges(v, g))
{ ... }
or something like:
for (std::tie(auto out_i, auto out_end) = out_edges(v, g);
out_i != out_end; ++out_i)
{...}
Is it possible?
A simple wrapper over out_edges
should suffice:
#include <boost/range/iterator_range.hpp>
#include <type_traits>
template<class T> using Invoke = typename T::type
template<class T> using RemoveRef = Invoke<std::remove_reference<T>>;
template<class G> using OutEdgeIterator = typename boost::graph_traits<G>::out_edge_iterator;
template<class V, class G>
auto out_edges_range(V&& v, G&& g)
-> boost::iterator_range<OutEdgeIterator<RemoveRef<G>>>
{
auto edge_pair = out_edges(std::forward<V>(v), std::forward<G>(g));
return boost::make_iterator_range(edge_pair.first, edge_pair.second);
}
Or even simpler, a function that turns a std::pair
into a valid range:
template<class It>
boost::iterator_range<It> pair_range(std::pair<It, It> const& p){
return boost::make_iterator_range(p.first, p.second);
}
and then
for(auto e : pair_range(out_edges(v, g))){
// ...
}