【问题标题】:C++ Boost Graph: How to customize callback function vf2_print_callback in vf2_subgrap_iso.hppC++ Boost Graph:如何在 vf2_subgrap_iso.hpp 中自定义回调函数 vf2_print_callback
【发布时间】:2017-05-02 03:12:34
【问题描述】:

背景:

我在vf2_sub_graph_iso.hpp 中使用boost::vf2_subgraph_iso(graph1, graph2, callback)。 在boost提供的sample中,他们使用boost::vf2_print_callback<graph_type, graph_type> callback(graph1, graph2)将顶点对打印到终端。

我做了什么:

我想自定义回调函数,这样一旦boost::vf2_subgraph_iso(graph1, graph2, callback) 调用它就会调用我的回调函数。我想自定义回调函数的原因是我想将顶点对存储在向量中,而不是在终端中打印它们。

以下是我目前的工作:

#include <utility>
#include <vector>
#include <fstream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/vf2_sub_graph_iso.hpp>
#include <boost/graph/graphviz.hpp>

template <typename Graph1,
            typename Graph2>
class my_call_back {

public:
    // constructor
    my_call_back(const Graph1& graph1, const Graph2& graph2) : graph1_(graph1), graph2_(graph2) {}

    template <typename CorrespondenceMap1To2, typename CorrespondenceMap2To1>
    bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1) {

        BGL_FORALL_VERTICES_T(v, graph1_, Graph1)
            vertex_iso_map.push_back(std::make_pair( get(boost::vertex_index_t(), graph1_, v) , get(boost::vertex_index_t(), graph2_, get(f, v))));
            set_of_vertex_iso_map.push_back(vertex_iso_map);
            vertex_iso_map.clear();

        return true;
    }
    std::vector <std::vector <std::pair<int, int>>> get_setvmap() { return set_of_vertex_iso_map; }

private:
    const Graph1& graph1_;
    const Graph2& graph2_;
    std::vector <std::vector <std::pair<int, int>>> set_of_vertex_iso_map;
    std::vector <std::pair<int, int>> vertex_iso_map;   
};

int main()
{
    typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS> GraphType;

    std::ifstream dot_small("small.dot");
    std::ifstream dot_large("large.dot");
    std::ofstream subgraph_iso_output("large1.dot");

    GraphType graph_small, graph_large;
    boost::dynamic_properties dp(boost::ignore_other_properties);

    boost::read_graphviz(dot_small, graph_small, dp);
    boost::read_graphviz(dot_large, graph_large, dp);

    my_call_back<GraphType, GraphType> callback(graph_small, graph_large);
    boost::vf2_subgraph_iso(graph_small, graph_large, callback);

    // get vector from callback
    auto set_of_vertex_iso_map = callback.get_setvmap();

    // output vector size here
    std::cout << set_of_vertex_iso_map.size() << std::endl;
    for (auto set_of_v : set_of_vertex_iso_map)
    {
        for (auto v : set_of_v)
            std::cout << "(" << v.first << ", " << v.second << ")" << " ";
        std::cout << std::endl;
    }

    boost::write_graphviz(subgraph_iso_output, graph_small);

    return 0;
}

我的问题:

调用boost::vf2_subgraph_iso函数后,我决定从对象callback获取向量。但似乎向量总是空的。我认为与iteration_macros.hpp 中定义的BGL_FORALL_VERTICES_T 宏有关,但是当我查看它时,我找不到任何线索。我在猜测:vf2_subgraph_iso 函数调用的回调可能会生成新的 my_callback 对象并在函数完成时销毁它。但是该函数如何生成新的my_callback 对象呢?我不明白。我该如何解决? (目前我只能将vector设置为全局变量来使其工作,这是一个可怕的选择)

【问题讨论】:

  • 不完全一样的情况,但是this是相关的。

标签: c++ boost callback


【解决方案1】:

两个问题:

bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1) {

    BGL_FORALL_VERTICES_T(v, graph1_, Graph1)
        vertex_iso_map.push_back(std::make_pair( get(boost::vertex_index_t(), graph1_, v) , get(boost::vertex_index_t(), graph2_, get(f, v))));
        set_of_vertex_iso_map.push_back(vertex_iso_map);
        vertex_iso_map.clear();

    return true;
}

至少修复格式以表达意图:

bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1) {

    BGL_FORALL_VERTICES_T(v, graph1_, Graph1) {
        vertex_iso_map.emplace_back(get(boost::vertex_index_t(), graph1_, v), get(boost::vertex_index_t(), graph2_, get(f, v)));
    }
    set_of_vertex_iso_map.push_back(vertex_iso_map);
    vertex_iso_map.clear();

    return true;
}

真正的问题是:

boost::vf2_subgraph_iso(graph_small, graph_large, callback);

按值传递callback,作用于副本!通过引用传递:

boost::vf2_subgraph_iso(graph_small, graph_large, std::ref(callback));

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多