Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost Graph: edge indices
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2010-04-02 19:58:04


On Fri, 2 Apr 2010, Trevor Harmon wrote:

> On Apr 1, 2010, at 5:31 AM, Gábor Szuromi wrote:
>
>> The edge_index internal property map is not created by default, so you
>> have to define it explicitly:
>>
>> typedef adjacency_list<vecS, vecS, directedS, Foo*,
>> property<edge_index_t, std::size_t> > MyGraph;
>> typedef property_map<MyGraph, edge_index_t>::type MyEdgeIndexMap;
>> ...
>> // Retrieve the edge index map associated with the graph
>> MyEdgeIndexMap emap = get(edge_index, g);
>> ...
>> // Convert the edge descriptor to an integer value
>> cout << index[ get(emap, e, g) ] << endl;
>
> A follow-up question, if I may... It seems like an awful lot of setup is
> required just to iterate through the edge indices. For example, here's what
> I'm doing now:
>
> typedef property_map<MyGraph, vertex_index_t>::type IndexMap;
> IndexMap index = get(vertex_index, g);
> typedef graph_traits<MyGraph>::edge_iterator edge_iter;
> std::pair<edge_iter, edge_iter> ep;
> EdgeIndexMap edgeIndexMap = get(edge_index, g);
> for (ep = edges(g); ep.first != ep.second; ++ep.first) {
> Edge edge = *ep.first;
> cout << index[get(edgeIndexMap, edge)] + " ";
> }
>
> Is there any way to simplify the above code? That's a lot of typing for such
> a basic task... Thanks,

#include <boost/graph/iteration_macros.hpp>
EdgeIndexMap edgeIndexMap = get(edge_index, g);
BGL_FORALL_EDGES(edge, g, MyGraph) {
   cout << index[get(edgeIndexMap, edge)] + " ";
}

or (I think)

#include <boost/foreach.hpp>
EdgeIndexMap edgeIndexMap = get(edge_index, g);
BOOST_FOREACH (graph_traits<MyGraph>::edge_descriptor edge, edges(g)) {
   cout << index[get(edgeIndexMap, edge)] + " ";
}

-- Jeremiah Willcock


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net