Boost property_map测试密钥是否存在?

Boost's property_map test whether a key exists?

本文关键字:是否 存在 密钥 测试 property map Boost      更新时间:2023-10-16

在BGL的上下文中,我需要迭代in_edgesout_edges,但我想排除那些属于反向边的部分,即排除那些属于逆向边property_map的部分。下面的代码显示了我想要做的事情,但property_map当然没有findend方法。

更新:一个可能的解决方案是在构建图形时保持一个单独的结构,如包含反向边的贴图。如果我可以控制图形构建,但我不能,因为我使用函数read_dimacs_max_flow读取DIMACS格式的图形文件。所以我只能依靠BGL的可访问性方法来弄清楚什么是什么。

图形定义:

typedef adjacency_list_traits<vecS, vecS, bidirectionalS> ttraits;  
typedef adjacency_list<vecS, vecS, bidirectionalS,
        // vertex properties
        property<vertex_index_t, int,
        property<vertex_color_t, default_color_type> >,
        // edge properties
        property<edge_capacity_t, int,
        property<edge_residual_capacity_t, int,
        property<edge_reverse_t, ttraits::edge_descriptor> > >, no_property, vecS> tbgl_adjlist_bidir;
typedef graph_traits<tbgl_adjlist_bidir>::vertex_descriptor     tvertex;
typedef graph_traits<tbgl_adjlist_bidir>::edge_descriptor       tedge;
typedef property_map<tbgl_adjlist_bidir, edge_capacity_t>::type tedge_capacity_map;
typedef property_map<tbgl_adjlist_bidir, edge_reverse_t>::type  treverse_edge_map;
typedef property_map<tbgl_adjlist_bidir, vertex_color_t>::type  tvertex_color_map;
typedef property_map<tbgl_adjlist_bidir, vertex_index_t>::type  tvertex_index_map;
typedef graph_traits<tbgl_adjlist_bidir>::vertex_iterator       tvertex_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::edge_iterator         tedge_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::out_edge_iterator     tout_edge_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::in_edge_iterator      tin_edge_iterator;

以及我想做的事情的示例片段(但编译时没有出现以下错误):

tvertex_index_map indices = get(vertex_index, bgl_adjlist_bidir);
tedge_capacity_map capacities = get(edge_capacity, bgl_adjlist_bidir);
treverse_edge_map rev_edges = get(edge_reverse, bgl_adjlist_bidir);
// iterate all vertices in the right order
for (int current = 0; current < m_num_vertices; ++current) {
    printf("processing vertex=%dn", current);
    tin_edge_iterator ei1, ei1_end;
    for (tie(ei1, ei1_end) = in_edges(tvertex(current), bgl_adjlist_bidir); ei1 != ei1_end; ++ei1) {
        // exclude reverse edges <<<<<<<======= HOW DO I DO THIS??
        if (rev_edges.find(*ei1) != rev_edges.end()) {
            continue;
        }
        int in = indices[boost::source(*ei1, bgl_adjlist_bidir)];
        printf("in edge: %d <- %d n", current, in);
    }
}

和编译器错误:

/Users/bravegag/code/fastcode_project/build_debug$ make 2> out ; grep -i "error" ./out
[  2%] Building CXX object CMakeFiles/submodularity.dir/src/graph/hp_adjlist_bidir.cc.o
/Users/bravegag/code/fastcode_project/code/src/api/hp_adjlist_bidir.h:146:18: error: 'treverse_edge_map' has no member named 'find'
/Users/bravegag/code/fastcode_project/code/src/api/hp_adjlist_bidir.h:146:42: error: 'treverse_edge_map' has no member named 'end'
make[2]: *** [CMakeFiles/submodularity.dir/src/graph/hp_adjlist_bidir.cc.o] Error 1
make[1]: *** [CMakeFiles/submodularity.dir/all] Error 2
make: *** [all] Error 2

edge_reverse属性映射将把一个边描述符与图的每条边关联起来。因此,"find"函数没有任何意义,因为所有边都会在该属性映射中有一个相应的条目(记住,这种属性映射不一定是作为std::map对象实现的,事实上,它们不是在内部属性的情况下实现的)。

您可以也应该做的一件事是为每条边设置反向边属性的值,使其成为反向边的边描述符或无效的边描述符(对于非反向边)。然后,检查(而不是"查找")只是检查反向边属性是否是有效的边描述符。

不幸的是,BGL没有提供null_edge()静态函数(就像它提供null_vertex()一样)。这可能是开发人员的疏忽(我开发了一些自己的图结构,并包含了null_edge()函数,但在Boost中没有)。这意味着很难找到一个好的、可移植的"null edge"描述符值来使用。一种选择是使用特定的比特模式,比如:

ttraits::edge_descriptor my_null_edge;
memset((char*)&my_null_edge, 0xFF, sizeof(ttraits::edge_descriptor));

然后,确保非反向边的所有反向边属性都设置为my_null_edge,然后通过以下比较实现循环:

for (tie(ei1, ei1_end) = in_edges(tvertex(current), bgl_adjlist_bidir); ei1 != ei1_end; ++ei1) {
    // exclude reverse edges
    if (rev_edges[*ei1] != my_null_edge) {
        continue;
    }
    int in = indices[boost::source(*ei1, bgl_adjlist_bidir)];
    printf("in edge: %d <- %d n", current, in);
}

如果反向边属性非常稀疏,则可能需要使用像std::map(或std::unordered_map)这样的映射类来获得外部属性映射。在邻接列表类模板中指定为EdgeProperty或VertexProperty的内容按图形的每个顶点或边的值(种类)存储。如果你想要std::map行为(只存储具有指定属性的子集),那么你可以简单地在邻接列表图的外部执行,属性映射的好处是它们不必与内部属性相对应,这也很有用。所以,你可以这样做:

typedef std::map< tedge, tedge > treverse_edge_map;
treverse_edge_map rev_edges;

如果您需要使用rev_edges作为BGL属性映射,那么您可以使用:

typedef boost::associative_property_map< treverse_edge_map > tbgl_reverse_edge_map;
tbgl_reverse_edge_map bgl_rev_edges = boost::make_assoc_property_map(rev_edges);

但是,当然,一旦它是BGL样式的属性映射,就不能再使用"查找"机制来确定是否为给定的边设置了属性。