boost图库:in_e​​dges迭代的确定性顺序?

时间:2015-06-21 19:51:10

标签: c++ boost boost-graph

TL; DR :我非常希望in_edges的迭代顺序 在我的图表上(adjacency_list,其边缘列表为setS) 确定性,但据我所知,迭代的顺序是 由只是指针的比较运算符确定 比较---因此迭代顺序是由变幻无常的 malloc。救命啊!

具体而言,我的图表和相关类型是:

struct VertexCargo { int Id; ... };

typedef adjacency_list<setS, vecS, bidirectionalS, property<vertex_info_t, VertexCargo> > Graph;

typedef graph_traits<Graph>::edge_descriptor   ED;
typedef graph_traits<Graph>::vertex_descriptor VD;

我的逻辑,万一有人在任何地方发现谬误:

  1. in_edges迭代直接由边列表容器的迭代确定

  2. setS的边缘列表表示底层容器std::set<edge_desc_impl>  注意:这个假设不正确;它实际上是一个std::set<StoredEdge,它提供了一个比较边缘目标的比较器。

  3. std::set<edge_desc_impl>迭代顺序由  operator<(edge_desc_impl, edge_desc_impl)

  4. operator<(edge_desc_impl, edge_desc_impl)最终只是  指针比较;

  5. 运营商&lt;在boost/graph/detail/edge.hpp(提升1.58)中定义:

    30      template <typename Directed, typename Vertex>
    31      class edge_desc_impl  : public edge_base<Directed,Vertex> {
    
    ...
    
    35        typedef void                              property_type;
    36
    37        inline edge_desc_impl() : m_eproperty(0) {}
    38
    39        inline edge_desc_impl(Vertex s, Vertex d, const property_type* eplug)
    40          : Base(s,d), m_eproperty(const_cast<property_type*>(eplug)) { }
    41
    42        property_type* get_property() { return m_eproperty; }
    43        const property_type* get_property() const { return m_eproperty; }
    44
    45        //  protected:
    46        property_type* m_eproperty;
    47      };
    48
    
    ...
    
    63
    64      // Order edges according to the address of their property object
    65      template <class D, class V>
    66      inline bool
    67      operator<(const detail::edge_desc_impl<D,V>& a,
    68                 const detail::edge_desc_impl<D,V>& b)
    69      {
    70        return a.get_property() < b.get_property();
    71      }
    

    如果有办法诱导比较,我真的很喜欢它 (以及迭代顺序)基于确定性的东西 (即指针地址由mallloc确定;例如a 我编写的自定义运算符,查看VertexCargo::Id的源代码和 边缘的目标)但看起来这可能在这里不可行 因为void*演员。

    从上面的代码中可以看出(?)注入property 给出所需的顺序。但这让我感到非常黑客。

    有人有智慧分享吗?

    关于答案的说明

    @ sehe的回答是正确答案---底层容器实际上是std::set<StoredEdge>,它定义了一个operator<,用于比较边缘目标;当顶点容器选择器是vecS 而不是一般时,这个确定性的(因为在其他情况下顶点标识符基本上是从malloc获得的指针)。

1 个答案:

答案 0 :(得分:4)

正如我预期的那样(从我的回忆中)边缘列表(进/出)已经按其目标排序,因此它们是确定性的

当VertexContainer选择器为vecS时,这一点尤为明确,因为vertex_descriptor是一个简单的整数类型,无论如何都会成为vertex_index_t属性。

兔子洞

因为我不是Boost Graph开发人员,因此我不知道 BGL类型的架构,如adjacency_list,我天真地开始在我们的 顶级入口点:

template <class Config>
inline std::pair<typename Config::in_edge_iterator,
                 typename Config::in_edge_iterator>
in_edges(typename Config::vertex_descriptor u,
         const bidirectional_graph_helper<Config>& g_)
{
  typedef typename Config::graph_type graph_type;
  const graph_type& cg = static_cast<const graph_type&>(g_);
  graph_type& g = const_cast<graph_type&>(cg);
  typedef typename Config::in_edge_iterator in_edge_iterator;
  return
    std::make_pair(in_edge_iterator(in_edge_list(g, u).begin(), u),
                   in_edge_iterator(in_edge_list(g, u).end(), u));
}

将其实例化为:

std::pair<typename Config::in_edge_iterator, typename Config::in_edge_iterator>
boost::in_edges(typename Config::vertex_descriptor, const boost::bidirectional_graph_helper<C> &)
  

Config = boost::detail::adj_list_gen<
    boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, VertexCargo>, boost::vecS, boost::setS,
    boost::bidirectionalS, VertexCargo, boost::no_property, boost::no_property, boost::listS>::config;

typename Config::in_edge_iterator = boost::detail::in_edge_iter<
    std::_Rb_tree_const_iterator<boost::detail::stored_edge_iter<
        long unsigned int, std::_List_iterator<boost::list_edge<long unsigned int, boost::no_property> >,
        boost::no_property> >,
    long unsigned int, boost::detail::edge_desc_impl<boost::bidirectional_tag, long unsigned int>, long int>;

 typename Config::vertex_descriptor = long unsigned int

填写

using Config = boost::detail::adj_list_gen<
     boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, VertexCargo>, boost::vecS, boost::setS,
     boost::bidirectionalS, VertexCargo, boost::no_property, boost::no_property, boost::listS>::config;

指出adj_list_gen<...>::config处的实例化,并将迭代器声明为

typedef in_edge_iter<
    InEdgeIter, vertex_descriptor, edge_descriptor, InEdgeIterDiff
> in_edge_iterator;

// leading to
typedef OutEdgeIter InEdgeIter;

// leading to
typedef typename OutEdgeList::iterator OutEdgeIter;

// leading to
typedef typename container_gen<OutEdgeListS, StoredEdge>::type OutEdgeList;

因为容器选择器为setS,所以std::setStoredEdge,这是

typedef typename mpl::if_<on_edge_storage,
    stored_edge_property<vertex_descriptor, EdgeProperty>,
    typename mpl::if_<is_edge_ra,
    stored_ra_edge_iter<vertex_descriptor, EdgeContainer, EdgeProperty>,
    stored_edge_iter<vertex_descriptor, EdgeIter, EdgeProperty>
    >::type
>::type StoredEdge;

评估为

boost::detail::stored_edge_iter<
    long unsigned int,
    std::_List_iterator<boost::list_edge<long unsigned int, boost::no_property> >,
    boost::no_property>

现在,这当然指向EdgeList的实现......

现在抓住它!击中制动器

但最重要的是强加的弱订单 - 所以我们不去 再深入那个兔子洞,而是转移注意力 stored_edge_iter<>::operator<或类似的。

  inline bool operator<(const stored_edge& x) const
    { return m_target < x.get_target(); }

啊哈!排序已经确定性定义。您可以使用例如直接访问

for (auto v : make_iterator_range(vertices(g))) {
    std::cout << v <<  " --> ";

    auto const& iel = boost::in_edge_list(g, v);
    for (auto e : iel) std::cout << e.get_target() << " ";
    std::cout << "\n";
}

但你不需要。使用通用图形访问器几乎是一样的:

std::cout << v <<  " --> ";
for (auto e : make_iterator_range(in_edges(v, g)))  std::cout << source(e, g) << " ";
std::cout << "\n";

您可以使用例如

验证集合是否按预期正确排序
assert(boost::is_sorted(make_iterator_range(in_edges(v,g))  | transformed(sourcer(g))));
assert(boost::is_sorted(make_iterator_range(out_edges(v,g)) | transformed(targeter(g))));

样本

这是一个完整的演示,包括以上所有内容,并在大型随机生成的图表上声明所有预期的排序:

<强> Live On Coliru

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/random.hpp>
#include <boost/graph/graph_utility.hpp>
#include <random>

#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/algorithm_ext.hpp>

using boost::adaptors::transformed;
using namespace boost;

struct VertexCargo { int Id = rand() % 1024; };

typedef adjacency_list<setS, vecS, bidirectionalS, VertexCargo> Graph;

typedef graph_traits<Graph>::edge_descriptor   ED;
typedef graph_traits<Graph>::vertex_descriptor VD;

struct sourcer {
    using result_type = VD;

    Graph const* g;
    sourcer(Graph const& g) : g(&g) {}
    VD operator()(ED e) const { return boost::source(e, *g); }
};

struct targeter {
    using result_type = VD;

    Graph const* g;
    targeter(Graph const& g) : g(&g) {}
    VD operator()(ED e) const { return boost::target(e, *g); }
};

int main() {

    std::mt19937 rng { std::random_device{}() };

    Graph g;
    generate_random_graph(g, 1ul<<10, 1ul<<13, rng);

    for (auto v : make_iterator_range(vertices(g))) {
        std::cout << v <<  " --> ";

        //auto const& iel = boost::in_edge_list(g, v);
        //for (auto e : iel) std::cout << e.get_target() << " ";

        for (auto e : make_iterator_range(in_edges(v, g)))  std::cout << source(e, g) << " ";
        //for (auto e : make_iterator_range(out_edges(v, g))) std::cout << target(e, g) << " ";

        std::cout << "\n";

        assert(boost::is_sorted(make_iterator_range(in_edges(v,g))  | transformed(sourcer(g))));
        assert(boost::is_sorted(make_iterator_range(out_edges(v,g)) | transformed(targeter(g))));
    }
}