c++ - boost::dynamic_properties 和不可变图形对象

标签 c++ boost boost-graph dynamic-properties

在使用 BGL 实现一些算法后,我尝试使用 GraphML 提供 io 函数。但是,我没有设法编译一个合适的 operator<<,它需要一个 const Graph 引用。

这是一个简单的例子:

// use bundled properties for vertices and edges
struct VertexProperty
{
   double error;
};

typedef boost::adjacency_list< boost::setS, boost::setS, boost::undirectedS, VertexProperty> Graph;

typedef typename boost::graph_traits<Graph>::edge_descriptor edge_descriptor;
typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;

std::ostream& operator<<(std::ostream& os, const Graph& graph)
{
    typedef std::map<vertex_descriptor, std::size_t> IndexMap;
    IndexMap index_map;
    boost::associative_property_map<IndexMap> index_properties(index_map);

    std::size_t i = 0;
    for (const vertex_descriptor& v : boost::make_iterator_range(boost::vertices(graph)))
        index_properties[v] = i++;

    boost::dynamic_properties dp;
    typename boost::property_map<Graph, double VertexProperty::*>::const_type error_map = get(&VertexProperty::error, graph);

    dp.property("error", error_map);

    boost::write_graphml(os, graph,index_properties,dp);

    return os;
}

int main()
{
  Graph g;
  std::cout << g <<std::endl;
}

编译失败:

boost/property_map/property_map.hpp:309:44: error: assignment of read-only location '(&((const boost::adj_list_vertex_property_map, double, const double&, double VertexProperty::*>&)pa))->boost::adj_list_vertex_property_map::operator[], double, const double&, double VertexProperty::*>(k)' static_cast(pa)[k] = v;

据我了解 dynamic_properties 文档,那些只读检查应该在运行时发生(这不是整个类型删除的目标之一)。当然,如果有人试图修改不可变属性,它们应该会失败。但是对 write write_graphml() 的调用采用对动态属性的 const ref,并且不应更改任何内容。

陈述问题:

  • 为什么会编译失败?
  • 我该如何正确地做到这一点?
  • 通过使用其他一些 property_map(是/否/哪个)?

对于(非)运行示例@ coliru.stacked-crooked.com:See here!

问候, 马蒂

最佳答案

手头的真正问题是顶点属性映射的类别被推断为 LvaluePropertyMap(它是)。

但是,LvaluePropertyMap 概念有望成为 ReadablePropertyMapWritablePropertyMap 的超集。当使用图形属性的 const_type 时,这会带来问题:它们是左值,但不可写。

我想出的唯一可行的解​​决方案是包装属性映射类型以否决类别:

namespace detail {
    template <typename Map>
        struct readable_only_pmap : Map {
            readable_only_pmap(Map map) : Map(map) { }

            // overrule the category tag
            typedef boost::readable_property_map_tag category;
        };
}

现在,您可以像这样使用它:

using pmap_t = typename boost::property_map<Graph, double VertexProperty::*>::const_type;
detail::readable_only_pmap<pmap_t> error_map = get(&VertexProperty::error, graph);

虽然几乎相同,但现在 dynamic_properties::property 检测到 map 是只读的,并且不会尝试生成放置助手(相反,如果 将引发异常code>put 被尝试)。

带有演示图的完整代码:

Live On Coliru

#include <iostream>
#include <string>
#include <vector>
#include <functional>

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphml.hpp>
// #include <Eigen/Core>

// use bundled properties for vertices and edges
struct VertexProperty
{
    double error;
    // Eigen::Matrix<real,dim,1> location;
};

typedef boost::adjacency_list< boost::setS, boost::setS, boost::undirectedS, VertexProperty> Graph;

typedef typename boost::graph_traits<Graph>::edge_descriptor edge_descriptor;
typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;

namespace detail {
    template <typename Map>
        struct readable_only_pmap : Map {
            readable_only_pmap(Map map) : Map(map) { }

            // overrule the category tag
            typedef boost::readable_property_map_tag category;
        };
}

std::ostream& operator<<(std::ostream& os, const Graph& graph)
{
    typedef std::map<vertex_descriptor, std::size_t> IndexMap;
    IndexMap index_map;
    boost::associative_property_map<IndexMap> index_properties(index_map);

    std::size_t i = 0;
    for (const vertex_descriptor& v : boost::make_iterator_range(boost::vertices(graph)))
        index_properties[v] = i++;

    using pmap_t = typename boost::property_map<Graph, double VertexProperty::*>::const_type;
    detail::readable_only_pmap<pmap_t> error_map = get(&VertexProperty::error, graph);

    boost::dynamic_properties dp;
    dp.property("error", error_map);
    boost::write_graphml(os, graph, index_properties, dp);

    return os;
}

int main()
{
  Graph g;
  auto v1 = boost::add_vertex(VertexProperty{0.1}, g);
  auto v2 = boost::add_vertex(VertexProperty{0.2}, g);
  auto v3 = boost::add_vertex(VertexProperty{0.3}, g);
  auto v4 = boost::add_vertex(VertexProperty{0.4}, g);
  auto v5 = boost::add_vertex(VertexProperty{0.5}, g);

  add_edge(v1,v2,g);
  add_edge(v5,v2,g);
  add_edge(v4,v2,g);
  add_edge(v2,v3,g);
  add_edge(v3,v4,g);
  add_edge(v4,v1,g);

  std::cout << g <<std::endl;
}

输出:(稍微重新格式化)

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="key0" for="node" attr.name="error" attr.type="double" />
<graph id="G" edgedefault="undirected" parse.nodeids="free" parse.edgeids="canonical" parse.order="nodesfirst">
    <node id="n0"> <data key="key0">0.1</data> </node>
    <node id="n1"> <data key="key0">0.2</data> </node>
    <node id="n2"> <data key="key0">0.3</data> </node>
    <node id="n3"> <data key="key0">0.4</data> </node>
    <node id="n4"> <data key="key0">0.5</data> </node>
    <edge id="e0" source="n0" target="n1"> </edge>
    <edge id="e1" source="n4" target="n1"> </edge>
    <edge id="e2" source="n3" target="n1"> </edge>
    <edge id="e3" source="n1" target="n2"> </edge>
    <edge id="e4" source="n2" target="n3"> </edge>
    <edge id="e5" source="n3" target="n0"> </edge>
</graph>
</graphml>

关于c++ - boost::dynamic_properties 和不可变图形对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27232196/

相关文章:

c++ - 解决 Visual C++ 中缺少 vwscanf 的更好方法?

c++ - 使用虚拟方法组织单例的最佳方式

c++ - 通过引用传递,源在操作中被修改

c++ - 使用 Boost 状态图,如何无条件转换到状态?

c - Python C API : Passing two functions of many parameters with special types as module

c++ - 如何在boost中遍历图使用BFS

c++ - BGL : geting edge indices from Stoer-Wagner min-cut?

c++ - Prim 算法以下代码的运行时间

c++ - 使用版本 2 语法的良好/完整 Boot Spirit 示例

c++ - 如何在提升图中添加自定义边标签?