[英]boost::dynamic_properties and immutable graph object
在使用BGL實現一些算法之后,我試圖使用GraphML提供io功能。 但是,我沒有設法編譯一個合適的運算符<<,它采用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:錯誤:分配只讀位置'(&((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文檔,那些只讀檢查應該在運行時發生(這不是整個類型擦除的目標之一)。 當然,如果嘗試修改不可變屬性,它們應該會失敗。 但是對wirte write_graphml()的調用需要const參考動力學屬性,並且不應該改變任何東西。
陳述問題:
對於(不)運行示例@coliru.stacked-crooked.com: 請看這里!
問候,馬蒂
手頭的真正問題是頂點屬性映射的類別被推導為LvaluePropertyMap
(它是)。
但是, LvaluePropertyMap
概念有望成為ReadablePropertyMap
和WritablePropertyMap
的超集。 當使用圖形屬性的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
檢測到映射只是可讀的,並且不會嘗試生成put幫助程序(相反,如果嘗試put
,則會引發異常)。
#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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.