【发布时间】:2015-08-30 11:45:36
【问题描述】:
我的要求是有一个图结构,其中每个顶点都由boost::uuids::uuid 唯一标识。所有顶点都有一个颜色属性,相似类别的顶点将根据该属性进行分组。我不是在处理静态地图,顶点和边将被动态创建和删除。
typedef boost::adjacency_list<
boost::listS,
boost::listS,
boost::bidirectionalS,
boost::property<boost::vertex_index_t, boost::uuids::uuid,
boost::property<boost::vertex_color_t, resource_color,
boost::property<boost::vertex_underlying_t, boost::shared_ptr<actual_object*> > > >,
detail::edge_property
> graph_type;
graph_type _graph;
boost::property_map<graph_type, boost::vertex_index_t>::type _index_map;
boost::property_map<graph_type, boost::vertex_color_t>::type _color_map;
boost::property_map<graph_type, boost::vertex_underlying_t>::type _underlying_map;
在 constructor 我正在创建所有 3 个地图
_index_map = boost::get(boost::vertex_index_t(), _graph);
_color_map = boost::get(boost::vertex_color_t(), _graph);
_underlying_map = boost::get(boost::vertex_underlying_t(), _graph);
同时添加一个顶点
add_resource(resource_color c, actual_object* o){
graph_type::vertex_descriptor v = boost::add_vertex(o->uuid(), _graph);
_color_map[v] = c;
_underlying_map[v] = o;
}
列出顶点的 UUID
uuid_list list;
boost::graph_traits<graph_type>::vertex_iterator vi, vi_end;
for(boost::tie(vi, vi_end) = boost::vertices(_graph); vi != vi_end; ++vi){
list.push_back(_index_map[*vi]);
}
return list;
这样我总是遍历图的顶点并获取它的属性。但是我也想要另一种方式。从 UUID 到顶点,例如并行 std::map,它将通过添加/删除操作或类似操作自动更新。
此外,我无法保留外部 std::map 并手动同步,因为 boost::adjacency_list<boost::listS, boost::listS>::vertex_descriptor 的计算结果为 void*,我需要序列化支持。
下面的事情是否可行
- 通过
boost::vertex_index_t值查找顶点 - 遍历
boost::property_map - 将外部
std::map或bimap与index属性同步
【问题讨论】:
标签: c++ boost boost-graph