【问题标题】:Boost graph with existing data structure or using it as the data structure使用现有数据结构或将其用作数据结构的 Boost 图
【发布时间】:2012-12-16 15:16:21
【问题描述】:

我正在编写一个应用程序来解析类似的数据结构

struct Block
{
  std::string foo;
  /* ... even more local data ... */
};

std::map<std::string, Block> blockContainer; // Each Block will have a name here

struct Signal
{
  // the direct links to the Blocks, no redundant storage of the name so that an
  // simple renaming of a Block would be possible
  std::map<std::string, Block>::iterator from; 
  std::map<std::string, Block>::iterator to;

  std::string bar;
  /* ... even more local data ... */
};

std::vector<Signal> signalContainer;

解析和填写此列表非常容易。现在我需要根据信号对块进行拓扑排序 - 当我使用 Boost::Graph 时也很容易。

但是首先在 STL 数据结构中解析它,然后将它们复制到 Boost::Graph 结构对我来说没有多大意义。尤其是之后对这些数据所做的所有事情可能只是一些简单的修改(添加/删除块和信号,一些信号重新路由;再次将其序列化),然后使用新的拓扑排序。

所以我可以接受以下任何可能的解决方案:

  1. 让 Boost::Graph 直接在我的容器上运行
  2. 将数据直接解析到 Boost::Graph 数据结构中(例如,使用带有 boost::adjacency_list&lt;boost::mapS, boost::vecS, boost::directedS, Block, Signal&gt; 等捆绑属性的图)

但我似乎不够聪明,无法理解这里的文档。此外,我在网上找到的所有示例都显示了如何使用捆绑属性 - 但没有显示如何使用这些属性构建图表。 (当然,不能同时使用节点和顶点属性,或者如何使用std::map 让节点通过它们的名称访问它们,...)

谁能帮帮我?

谢谢!

【问题讨论】:

    标签: c++ boost-graph


    【解决方案1】:

    对于 boost::graph 文档,没有人足够聪明;)学习如何使用它需要花费大量时间。

    您可以围绕数据创建提升图结构,但是,这可能会相当痛苦。那里有一个文档:http://www.boost.org/doc/libs/1_52_0/libs/graph/doc/leda_conversion.html

    我建议使用第二种方法,但我不确定你的数据结构。

    您是否看到这个问题:adding custom vertices to a boost graph 它是否满足您的需求?

    您还可以执行以下操作来创建节点(或边)并立即定义属性:

    vertex_t u = boost::add_vertex(Block(42, "hi"), g);
    

    您可能需要在解析时维护地图。

    【讨论】:

    • 好的,现在我采用了解决方案 2 - 它有效:) :) :) 除了(现在...)直接添加边和顶点参数之外,对我来说,剩下的最大问题是有效处理顶点的名称。解决方案是将名称保留在Blockstd::map&lt;std::string,vertex_t&gt; 中(并让两者同步)。所以我可以通过它的名称(通过map)快速查找存储的块,以及当我获得块时它的名称(通过属性)
    猜你喜欢
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    相关资源
    最近更新 更多