【问题标题】:Using boost graph library with a custom class for vertices使用带有自定义顶点类的 boost 图形库
【发布时间】:2017-10-20 21:55:15
【问题描述】:

我目前正在尝试在现有 c++ 项目中使用 boost 图形库。我想将自定义类的对象存储在提升图中。下面是一个小示例,其中包含一个自定义类定义,其中包含两个成员(一个字符串和一个 int)及其对应的 getter 方法。

我有几个问题:

  1. 如何在 Graphviz 输出中包含字符串和 int 值?我使用boost::make_label_writer 发现了这个answer 与一个类似的问题,但我不确定我的示例是否可以被采用(我正在使用自定义类和共享指针)。
  2. 在图中不能多次存储相同的对象(相同的字符串和 int 值)。因此,我在自定义类中重载了两个比较运算符。我还读到我必须将图形类型定义的第二个模板参数更改为 boost::setS 但这会导致编译器的错误消息非常长...
  3. 假设我创建了一个自定义类的新对象:如何检查它是否已存储在图中?

    #include <iostream>
    #include <boost/graph/graphviz.hpp>
    
    class my_custom_class {
        public:
    
        my_custom_class(const std::string &my_string,
                        int my_int) : my_string(my_string),
                                      my_int(my_int) {}
    
        virtual ~my_custom_class() {
        }
    
        std::string get_my_string() const {
            return my_string;
        }
    
        int get_int() const {
            return my_int;
        }
    
        bool operator==(const my_custom_class &rhs) const {
            return my_string == rhs.my_string &&
                   my_int == rhs.my_int;
        }
    
        bool operator!=(const my_custom_class &rhs) const {
            return !(rhs == *this);
        }
    
        private:
    
        std::string my_string;
    
        int my_int;
    };
    
    namespace boost {
        enum vertex_my_custom_class_t {
            vertex_my_custom_class = 123
        };
        BOOST_INSTALL_PROPERTY(vertex,
                               my_custom_class);
    }
    
    int main() {
        typedef boost::adjacency_list<boost::vecS,
                boost::vecS,
                boost::directedS,
                boost::property<boost::vertex_my_custom_class_t,
                                std::shared_ptr<my_custom_class>>> graph_t;
    
        typedef boost::graph_traits<graph_t>::vertex_descriptor vertex_t;
    
        std::shared_ptr<my_custom_class> object_one = std::make_shared<my_custom_class>("Lorem", 123);
        std::shared_ptr<my_custom_class> object_two = std::make_shared<my_custom_class>("ipsum", 456);
        std::shared_ptr<my_custom_class> object_three = std::make_shared<my_custom_class>("Lorem", 123);
    
        std::cout << "object one: " << object_one->get_int() << "; " << object_one->get_my_string() << std::endl;
        std::cout << "object two: " << object_two->get_int() << "; " << object_two->get_my_string() << std::endl;
        std::cout << "object three: " << object_three->get_int() << "; " << object_three->get_my_string() << std::endl;
    
        std::cout << std::endl;
    
        std::cout << "object one == object two: " << (*object_one == *object_two) << std::endl;
        std::cout << "object one == object three: " << (*object_one == *object_three) << std::endl;
    
        std::cout << std::endl;
    
        graph_t graph;
    
        vertex_t vertex_one = boost::add_vertex(object_one, graph);
        vertex_t vertex_two = boost::add_vertex(object_two, graph);
        vertex_t vertex_three = boost::add_vertex(object_three, graph);
    
        boost::add_edge(vertex_one, vertex_two, graph);
        boost::add_edge(vertex_one, vertex_three, graph);
    
        boost::write_graphviz(std::cout, graph);
    
        return 0;
    }
    

程序输出:

object one: 123; Lorem
object two: 456; ipsum
object three: 123; Lorem

object one == object two: 0
object one == object three: 1

digraph G {
0;
1;
2;
0->1 ;
0->2 ;
}

【问题讨论】:

    标签: boost boost-graph


    【解决方案1】:
    1. 不改变你的声明,这有点痛苦,但可能:

      {
          boost::dynamic_properties dp;
          boost::property_map<graph_t, boost::vertex_my_custom_class_t>::type custom = get(boost::vertex_my_custom_class, graph);
          dp.property("node_id", boost::make_transform_value_property_map(std::mem_fn(&my_custom_class::get_int), custom));
          dp.property("label", boost::make_transform_value_property_map(std::mem_fn(&my_custom_class::get_my_string), custom));
          boost::write_graphviz_dp(std::cout, graph, dp);
      }
      

      打印:Live On Coliru

      digraph G {
      123 [label=Lorem];
      456 [label=ipsum];
      123 [label=Lorem];
      123->456 ;
      123->123 ;
      }
      
    2. 您需要在外部处理此问题。为什么不拥有一组侵入性节点并以这种方式验证约束。如您所说更改顶点容器选择器没有任何效果(它只是最终以升序存储顶点描述符,而它们与以前一样保持唯一)。

      副作用是从连续分配的顶点存储更改为基于节点的存储(pro:迭代器/引用稳定性,con:分配开销,减少的引用局部性,非隐式 vertex_index)。后者是罪魁祸首:BGL 中的许多东西都需要顶点索引,如果没有隐含(例如使用 vecS),则必须传递一个。

      有趣的是,由于我使用 write_graphviz_dp 和特定的 node_id 属性,所以现在不需要隐式顶点索引,因此您可以vecS 更改为 setS 并观察行为:Live On Coliru

    3. 我认为现在不是检查的正确时间。没有比访问所有顶点更好的方法了,除非您保持更新外部索引。

    样式/简化

    既然std::shared_ptr 暗示你有c++11,那就用它吧。

    此外,使用自定义属性的整个过程主要是一种更笨拙的属性捆绑方式:这些在语法上更容易并且得到更好的支持。

    看看区别:

    注意我有点过于热情了,使用 shared-ptr 你仍然需要 transform-value-property-map:

    Live On Coliru

    #include <boost/graph/graphviz.hpp>
    #include <boost/property_map/transform_value_property_map.hpp>
    #include <iostream>
    
    struct MyVertex {
        MyVertex(std::string label, int id) : _label(std::move(label)), _id(id) {}
    
        std::string label() const { return _label; }
        int         id()    const { return _id;    }
    
        bool operator<(const MyVertex &rhs) const { return std::tie(_id, _label) < std::tie(rhs._id, rhs._label); }
      private:
        std::string _label;
        int _id;
    };
    
    using graph_t = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, std::shared_ptr<MyVertex>>;
    
    int main() {
        graph_t graph;
    
        auto v1 = add_vertex(std::make_shared<MyVertex>("Lorem", 123), graph);
        auto v2 = add_vertex(std::make_shared<MyVertex>("ipsum", 456), graph);
        auto v3 = add_vertex(std::make_shared<MyVertex>("Lorem", 123), graph);
    
        add_edge(v1, v2, graph);
        add_edge(v1, v3, graph);
    
        {
            boost::dynamic_properties dp;
            auto bundle = get(boost::vertex_bundle, graph);
            dp.property("node_id", make_transform_value_property_map(std::mem_fn(&MyVertex::id), bundle));
            dp.property("label", make_transform_value_property_map(std::mem_fn(&MyVertex::label), bundle));
    
            write_graphviz_dp(std::cout, graph, dp);
        }
    }
    

    比较:没有共享指针

    我并不是说你不能使用它,但我也不相信你需要它。因此,让我们将其删除,以便您看到区别:

    Live On Coliru

    #include <boost/graph/graphviz.hpp>
    #include <iostream>
    
    struct MyVertex {
        std::string label;
        int id;
        bool operator<(const MyVertex &rhs) const { return std::tie(id, label) < std::tie(rhs.id, rhs.label); }
    };
    
    using graph_t = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, MyVertex>;
    
    int main() {
        graph_t graph;
    
        auto v1 = add_vertex({"Lorem", 123}, graph);
        auto v2 = add_vertex({"ipsum", 456}, graph);
        auto v3 = add_vertex({"Lorem", 123}, graph);
    
        add_edge(v1, v2, graph);
        add_edge(v1, v3, graph);
    
        boost::dynamic_properties dp;
        dp.property("node_id", boost::get(&MyVertex::id, graph));
        dp.property("label", boost::get(&MyVertex::label, graph));
    
        write_graphviz_dp(std::cout, graph, dp);
    }
    

    这大约是代码的一半。从这里我们可以探索如何添加所需的功能

    独特性:让我们试试我们的手

    最简单的做法是在添加新节点之前检查现有节点:

    Live On Coliru

    #include <boost/graph/graphviz.hpp>
    #include <boost/range/iterator_range.hpp>
    #include <iostream>
    
    struct MyVertex {
        std::string label;
        int id;
    
        auto key() const { return std::tie(id,label); }
        bool operator< (const MyVertex &rhs) const { return key() <  rhs.key(); }
        bool operator==(const MyVertex &rhs) const { return key() == rhs.key(); }
        bool operator!=(const MyVertex &rhs) const { return key() != rhs.key(); }
    };
    
    using graph_t = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, MyVertex>;
    
    int main() {
        graph_t graph;
    
        auto node = [&graph](std::string name, int id) {
            for (auto&& v : boost::make_iterator_range(vertices(graph)))
                if (graph[v] == MyVertex{name, id})
                    return v;
            return add_vertex({name, id}, graph);
        };
    
        auto v1 = node("Lorem", 123);
        auto v2 = node("ipsum", 456);
        auto v3 = node("Lorem", 123);
    
        assert(v3==v1);
    
        add_edge(v1, v2, graph);
        add_edge(v1, v3, graph);
    
        boost::dynamic_properties dp;
        dp.property("node_id", boost::get(&MyVertex::id, graph));
        dp.property("label", boost::get(&MyVertex::label, graph));
    
        write_graphviz_dp(std::cout, graph, dp);
    }
    

    注意v3 现在如何等于v1

    digraph G {
    123 [label=Lorem];
    456 [label=ipsum];
    123->456 ;
    123->123 ;
    }
    

    【讨论】:

    • 感谢您的出色回答。 ;) 为什么你不相信我需要共享指针?如果我仍然想使用共享指针:for (auto&amp;&amp; v : boost::make_iterator_range(vertices(graph))) { std::cout &lt;&lt; v &lt;&lt; std::endl; } 输出所有顶点的索引,但我无法通过graph[v]-&gt; 访问我的顶点:Applying '-&gt;' operator to 'boost::no_property' instead of a pointer
    • 共享指针通常很少使用。在这里,您似乎使用共享性让节点参与多个集合。但是,总的来说,所有权问题仍然有明确的答案,所以参考/指针就可以了。而且如果你有一个shared_ptr发光的场景,你更有可能将它存储在内部MyVertex属性(并且可能作为weak_ptr)。
    • 发布一个独立的示例(该错误消息表明您没有捆绑的属性(boost.org/doc/libs/1_65_1/libs/graph/doc/bundles.html))
    猜你喜欢
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 2013-04-17
    • 2022-09-29
    • 1970-01-01
    • 2013-11-17
    相关资源
    最近更新 更多