|
Boost : |
From: Wang Weiwei (wwwang_at_[hidden])
Date: 2006-07-15 09:07:55
>
>On Jul 14, 2006, at 10:54 AM, Wang Weiwei wrote:
>
>> Hello,
>>
>> I have a graph type as follows:
>>
>> namespace boost
>> {
>> enum vertex_flag_t { vertex_flag = 111 };
>> BOOST_INSTALL_PROPERTY(vertex, flag);
>> }
>>
>> typedef boost::adjacency_list<boost::vecS, boost::vecS,
>> boost::undirectedS,
>> property<vertex_name_t, std::string,
>> property<vertex_flag_t, bool,
>> property<vertex_color_t, default_color_type> > >,
>> no_property> graph_t;
>>
>> graph_t g;
>>
>> That means I want to associate a string as name, a flag, and a
>> default color to each vertex of my graph,
>> now I want to add edges to my graph directly using the vertex'
>> properties instead of their descriptors,
>> e.g.
>>
>> boost::add_edge("vertex1", "vertex2", g);
>>
>> How can I accomplish this?
>
>You'll need to create a separate mapping from vertex names to vertex
>descriptors:
>
> std::map<std::string, graph_t::vertex_descriptor> name_to_vertex;
>
> Doug
I've made the following template function to do the jod I want, as indicated by
its name - add vertices using their properties.
template<typename InputPropIterator, typename Graph,
typename VertexPropMap> ////
AddVerticesByProp(InputPropIterator first, InputPropIterator last, Graph& g,
VertexPropMap& pmap, ////
typename std::map<
typename std::iterator_traits<InputPropIterator>::value_type,
typename boost::graph_traits<Graph>::vertex_descriptor
>& Prop2VertexMap)
{
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
typedef std::iterator_traits<InputPropIterator>::value_type prop_t;
typedef std::map<prop_t, vertex_t> prop_vertex_map_t;
for(; first != last; ++last)
{
prop_vertex_map_t::iterator pos;
bool inserted;
vertex_t v;
boost::tie(pos, inserted) = Prop2VertexMap.insert(std::make_pair(*first, vertex_t()));
if(inserted)
{
v = add_vertex(g);
pmap[v] = *first;
pos->second = v;
}
else
{
v = pos->second;
}
}
}
I suppose it could more elegant if one can remove the template param 'VertexPropMap' and the
corresponding function param 'VertexPropMap& pmap', because the type 'VertexPropMap' is determined
and should be derived (I think) if the other 2 types are given.
To be clearer, the VertexPropMap is a map from 'typename std::iterator_traits<InputIterator>::value_type'
to 'typename boost::graph_traits<Graph>::vertex_descriptor'.
Similarly, as far as internal property map is concerned, the function param 'pmap' is also determined
and should be derived (I think) from 'g' and type info gien by the InputPropIterator.
What I need now is any help that can eliminate these 2 param's to make the funtion interface not so tedious.
Thanks.
Max
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk