修改访客的捆绑属性
我应该如何从访问者内部修改顶点的捆绑属性?
我想使用对图进行子脚本化的简单方法,但传递给访问者的图参数是 const,因此编译器不允许更改。
我可以在访问者中存储对图表的引用,但这看起来很奇怪。
/**
A visitor which identifies vertices as leafs or trees
*/
class bfs_vis_leaf_finder:public default_bfs_visitor {
public:
/**
Constructor
@param[in] total reference to int variable to store total number of leaves
@param[in] g reference to graph ( used to modify bundled properties )
*/
bfs_vis_leaf_finder( int& total, graph_t& g ) :
myTotal( total ), myGraph( g )
{
myTotal = 0;
}
/**
Called when the search finds a new vertex
If the vertex has no children, it is a leaf and the total leaf count is incremented
*/
template <typename Vertex, typename Graph>
void discover_vertex( Vertex u, Graph& g)
{
if( out_edges( u, g ).first == out_edges( u, g ).second ) {
myTotal++;
//g[u].myLevel = s3d::cV::leaf;
myGraph[u].myLevel = s3d::cV::leaf;
} else {
//g[u].myLevel = s3d::cV::tree;
myGraph[u].myLevel = s3d::cV::tree;
}
}
int& myTotal;
graph_t& myGraph;
};
How should I modify the bundled properties of a vertex from inside a visitor?
I would like to use the simple method of sub-scripting the graph, but the graph parameter passed into the visitor is const, so compiler disallows changes.
I can store a reference to the graph in the visitor, but this seems weird.
/**
A visitor which identifies vertices as leafs or trees
*/
class bfs_vis_leaf_finder:public default_bfs_visitor {
public:
/**
Constructor
@param[in] total reference to int variable to store total number of leaves
@param[in] g reference to graph ( used to modify bundled properties )
*/
bfs_vis_leaf_finder( int& total, graph_t& g ) :
myTotal( total ), myGraph( g )
{
myTotal = 0;
}
/**
Called when the search finds a new vertex
If the vertex has no children, it is a leaf and the total leaf count is incremented
*/
template <typename Vertex, typename Graph>
void discover_vertex( Vertex u, Graph& g)
{
if( out_edges( u, g ).first == out_edges( u, g ).second ) {
myTotal++;
//g[u].myLevel = s3d::cV::leaf;
myGraph[u].myLevel = s3d::cV::leaf;
} else {
//g[u].myLevel = s3d::cV::tree;
myGraph[u].myLevel = s3d::cV::tree;
}
}
int& myTotal;
graph_t& myGraph;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的解决方案是正确的。
要将图形类型与访问者解耦,您可以仅将有趣的属性映射传递给访问者构造函数,并使用 boost::get(property, u) = s3d::cV::leaf; 访问其元素。通过这种方式,您可以将任何类型兼容的顶点属性传递给访问者(访问者将更加通用,并且对图形类型中的名称更改不敏感)。
属性映射的类型将是访问者类的模板类型名称,类似于:
请参阅 此处有关捆绑属性的完整论文。
华泰
Your solution is right.
To decouple the graph type from the visitor you could pass only the interesting property map to the visitor constructor and access its elements using
boost::get(property, u) = s3d::cV::leaf;
. This way you can pass any type-compatible vertex property to the visitor (the visitor will be more general and not sensible to name changes in the graph type).The type for the property map will be a template type-name for the visitor class and will be something like:
See here for a complete dissertation about bundled properties.
HTH
我刚刚学习这些东西,但我认为您必须在访问者中存储对图表的引用是正确的。我不确定是否是因为这个原因,但这可能是因为他们不想提供所有函数的两个版本/需要派生程序来提供每个函数的两个版本。特别是当图表解决方法中的传递可用时。
即使感觉很奇怪,我认为传递对图表的引用可能是“正确的方法”。
I'm just learning this stuff, but I think it is correct that you must store a reference to the graph in the visitor. I'm not sure if it's for this reason, but it may be because they didn't want to provide two versions of all of the functions/require derivatives to provide two versions of each function. Especially when the pass in the graph workaround is available.
Even if it feels weird, I think passing in a reference to the graph may be the "right way".