动态分配usign Boost Graph顶点属性 [英] Dynamic allocation usign Boost Graph vertex properties

查看:44
本文介绍了动态分配usign Boost Graph顶点属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Boost Graph库以读取GraphML文件.我想要做的是使用图管理的Boost功能来创建自己的动态分配的对象结构,以便可以在其上运行自定义算法.

I'm using Boost Graph library in order to read a GraphML file. What I want to do is to use Boost capabilities of graph management to create my own dynamically allocated objects structure so that I can run my custom algorithm on it.

struct VertexProperties {
    std::string vertex_name;
    bool defect;
    bool node_logic;
    custom_node * node;
};

typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;

但是问题是,在使用Deep First Search自定义访问者时,我似乎无法为 custom_node 指针分配内存.

But the problem is that I can't seem to allocate memory for the custom_node pointer when using a Deep First Search custom visitor.

node = new custom_node(g[v].vertex_name, 0, standby, normal);

由于出现只读"编译错误.

As I am getting "read-only" compilation errors.

我在徘徊,是否有一种方法可以将图形映射到可以使用动态分配重新创建图形结构的其他地方?

I was wandering if there was a way to map the graph to something else where I could use dynamic allocation to re-create my graph structure ?

修饰后的主:

#include <iostream>
#include <fstream>
#include <boost/graph/graphml.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>

#include "node.h"

using namespace boost;

struct VertexProperties {
    std::string vertex_name;
    bool node_logic;
    custom_node * node;
};

typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;
typedef graph_traits<DirectedGraph>::vertex_descriptor custom_vertex;
typedef graph_traits<DirectedGraph>::edge_descriptor custom_edge;

class custom_dfs_visitor : public default_dfs_visitor {
public:
    void discover_vertex(custom_vertex v, const DirectedGraph& g) const
    {
        // Looking for adjacent vertices
        DirectedGraph::adjacency_iterator neighbourIt, neighbourEnd;

        if(true == g[v].node_logic)
        {
            g[v].node = new custom_node(g[v].vertex_name, 0, standby, normal);
        }

        std::cout << g[v].vertex_name << " is connected with ";

        tie(neighbourIt, neighbourEnd) = adjacent_vertices(v, g);
        for (; neighbourIt != neighbourEnd; ++neighbourIt) {
            std::cout << g[*neighbourIt].vertex_name << " ";
        }
        std::cout << std::endl;
    }

    void examine_edge(custom_edge e, const DirectedGraph& g) const
    {
        std::cout << "Examining edges : " << g[e.m_source].vertex_name << " >> "
                  << g[e.m_target].vertex_name << std::endl;
    }
};

int main(int argc, char* argv[])
{
    int i;
    bool is_config = false;
    DirectedGraph g;
    int verbose;
    std::ifstream infile;
    dynamic_properties dp(ignore_other_properties);
    custom_dfs_visitor vis;

    dp.property("node_name", boost::get(&VertexProperties::vertex_name, g));
    dp.property("node_logic", boost::get(&VertexProperties::node_logic, g));

    /* Argument check */
    if (argc <= 1 || (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')) {
        usage();
        return 0;
    }

    /* Parse command line options */
    for (i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) {
        switch (argv[i][1]) {
        case 'v': /* verbose */
            verbose = 10;
            break;
        case 'c': /* read *.ini configuration file */
            // d_printf(D_INFO, "parsing '%s'... \n", argv[++i]);
            infile.open(argv[++i], std::ifstream::in);
            if (!infile.is_open()) {
                std::cout << "Loading file '" << argv[i] << "'failed" << std::endl;
                throw "Could not load file";
            }
            else {
                boost::read_graphml(infile, g, dp);
                is_config = true;
            }
            break;
        case 's': {
            is_defect = true;
            std::string temp_defect(argv[++i]);
            defect = temp_defect;
            std::cout << defect;
            break;
        }
        default: /* something's wrong */
            usage();
            break;
        }
    }

    if (true == is_config) {
        depth_first_search(g, boost::visitor(vis));
    }

    return 0;
}

感谢您的回答

推荐答案

好,depht_first_visit必须在常量图上工作.但是,显然常数图无法修改.

Ok, depht_first_visit must work on constant graphs. However, obviously constant graphs cannot be modified.

因此,您想告诉访问者对图形的非恒定引用,以便您可以对其进行修改.

So, you want to tell your visitor a non-constant reference to your graph so you can modify through that.

我建议进行以下最小更改:

I'd propose the following minimal change:

在Coliru上直播

#include <iostream>
#include <fstream>
#include <boost/graph/graphml.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>

//#include "cgfnode.h"
enum S{standby};
enum M{normal};

struct custom_node{
    custom_node(std::string, int, S, M){}
};
void usage(){}

using namespace boost;

struct VertexProperties {
    std::string vertex_name;
    bool node_logic;
    std::unique_ptr<custom_node> node;
};

typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;
typedef graph_traits<DirectedGraph>::vertex_descriptor custom_vertex;
typedef graph_traits<DirectedGraph>::edge_descriptor custom_edge;

struct custom_dfs_visitor : default_dfs_visitor {
    custom_dfs_visitor(DirectedGraph& g) : _mutable_graph(&g) {}

    void discover_vertex(custom_vertex v, DirectedGraph const& g) const {
        assert(&g == _mutable_graph);
        return discover_vertex(v, *_mutable_graph);
    }
    void discover_vertex(custom_vertex v, DirectedGraph& g) const
    {
        // Looking for adjacent vertices
        DirectedGraph::adjacency_iterator neighbourIt, neighbourEnd;

        if(g[v].node_logic) {
            g[v].node.reset(new custom_node(g[v].vertex_name, 0, standby, normal));
        }

        std::cout << g[v].vertex_name << " is connected with ";

        tie(neighbourIt, neighbourEnd) = adjacent_vertices(v, g);
        for (; neighbourIt != neighbourEnd; ++neighbourIt) {
            std::cout << g[*neighbourIt].vertex_name << " ";
        }
        std::cout << "\n";
    }

    void examine_edge(custom_edge e, const DirectedGraph& g) const {
        std::cout << "Examining edges : " << g[e.m_source].vertex_name << " >> " << g[e.m_target].vertex_name << "\n";
    }

  private:
    DirectedGraph* _mutable_graph;
};

int main() {
    DirectedGraph g;

    {
        dynamic_properties dp(ignore_other_properties);
        dp.property("node_name",  boost::get(&VertexProperties::vertex_name, g));
        dp.property("node_logic", boost::get(&VertexProperties::node_logic, g));

        std::ifstream infile("input.xml");
        boost::read_graphml(infile, g, dp);
    }

    custom_dfs_visitor vis (g);
    depth_first_search(g, boost::visitor(vis));
}

这篇关于动态分配usign Boost Graph顶点属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆