简体   繁体   English

提升图表库和访问者

[英]Boost Graph Library and Visitors

I'm writing a library for manipulating bond graphs, and I'm using the Boost Graph Library to store the data for me. 我正在编写一个用于操作键图的库,我正在使用Boost Graph Library为我存储数据。 Unfortunately, I can't seem to figure out how to implement a proper visitor pattern using it, as you can't subclass out vertices - you must rely on 'properties' instead. 不幸的是,我似乎无法弄清楚如何使用它来实现正确的访问者模式,因为你不能将顶点子类化 - 你必须依赖'属性'。 The visitor framework provided in the library seems heavily geared towards working with certain algorithms where vertices are all of the same type, but store different information. 库中提供的访问者框架似乎非常适合使用某些算法,其中顶点都是相同的类型,但存储不同的信息。 In my problem, the vertices are of differing types and store differing types of information - some vertices are resistors, while some are capacitors, etc. How do I go about writing a visitor pattern that works based on a property of a vertex, instead of the vertex itself? 在我的问题中,顶点具有不同的类型并存储不同类型的信息 - 一些顶点是电阻器,而一些是电容器等。我如何编写基于顶点属性工作的访问者模式,而不是顶点本身?

My only thought so far has been to write a small class to represent the type of an object that points back to the original vertex that I need to get the graph information. 到目前为止,我唯一想到的是编写一个小类来表示一个对象的类型,该对象指向我需要获取图形信息的原始顶点。 However, this seems very kludgy, and evil to work with. 然而,这似乎非常kludgy,邪恶的工作。

What do you mean, you can't subclass out vertices? 你是什​​么意思,你不能将顶点子类化? You can use your own vertex class, it's just a matter of specifying it in the Graph typedef. 您可以使用自己的顶点类,只需在Graph typedef中指定它即可。 You can even use members as properties when working with BGL algorithms. 在使用BGL算法时,您甚至可以使用成员作为属性。

As for the other way around (which is harder IMO), you need to create a vertex property list and access it using a vertex descriptor... I think. 至于另一种方式(这是更难的IMO),你需要创建一个顶点属性列表并使用顶点描述符访问它......我想。

Edit: You specify vertex/edge classes when defining your graph type: 编辑:在定义图表类型时指定顶点/边缘类:

struct Vertex {
    double some_property;
};

struct Edge {
    double weight;
};

typedef boost::adjacency_list<
    boost::listS, boost::vecS, boost::undirectedS, Vertex, Edge
> Graph; //sorry about the formatting

Graph g;

From where on g[vertex_descriptor] should return a reference to Vertex, eg: 从g [vertex_descriptor]的位置返回对Vertex的引用,例如:

//add 100 vertices
for (int i=0; i<100; ++i) {
    Graph::vertex_descriptor v = add_vertex(g);
    g[v].some_property = -1.0;
}

//zero some_property for all vertices
for (Graph::vertex_iterator i = vertices(g).first;
                            i != vertices(g).second;
                            ++i)
{
    g[*i].some_property = 0.0;
}

I couldn't find my visitor code making use of these properties but I did find the relevant part of the BGL documentation: 我找不到我的访客代码使用这些属性,但我确实找到了BGL文档的相关部分:

1) The part about Internal Properties , which recommends you use instead: 1)关于内部属性的部分,建议您使用:
2) Bundled Properties 2) 捆绑属性

The second link seems to have a Boost function making use of bundled properties using a member pointer. 第二个链接似乎有一个Boost函数,它使用成员指针来使用捆绑属性。

Does this help? 这有帮助吗?

If anyone cares, after 2 months, here is a visitor that looks at the property. 如果有人关心,2个月后,这里有一位观看房产的访客。

class demo_visitor : public default_bfs_visitor {
public:
    template <typename Vertex, typename Graph>
    void discover_vertex( Vertex u, Graph& g)
    {
        printf("Visited vertex %d with property %f\n",
            u, g[u].some_property);
    }
};

If the visitor needs to modify the properties, then things are lightly more complicated. 如果访问者需要修改属性,那么事情就会变得更加复杂。 For the issues - click here 对于问题 - 点击这里

也许你可以使用boost :: variant来构造顶点类型的不相交的总和,然后将BGL访问者与每个顶点的boost :: variant访问者结合起来?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM