C++ boost图库:in_边迭代的确定顺序?

C++ boost图库:in_边迭代的确定顺序?,c++,boost,boost-graph,C++,Boost,Boost Graph,TL;DR:我非常想知道边中的迭代顺序 在我的图上(邻接列表,带有集合的边列表)将 决定论,但据我所知,迭代的顺序是 由只是指针的比较运算符确定 比较——因此,迭代顺序取决于 malloc。救命啊 具体而言,我的图表和相关类型如下: struct VertexCargo { int Id; ... }; typedef adjacency_list<setS, vecS, bidirectionalS, property<vertex_info_t, VertexCargo>

TL;DR:我非常想知道边中
的迭代顺序
在我的图上(
邻接列表
,带有
集合
的边列表)将 决定论,但据我所知,迭代的顺序是 由只是指针的比较运算符确定 比较——因此,迭代顺序取决于
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;
我真的很希望能有一种方法来进行比较 (因此迭代顺序)基于确定性的东西 (即,由mallloc确定的指针地址;例如 我编写的自定义运算符查看源代码和 边缘的目标),但看起来这在这里可能不可行 因为
void*
cast

从上面的代码来看,还可以(?)插入
属性
给出所需的顺序。但这让我觉得是个黑客

有人有智慧分享吗


注意答案 @sehe的回答是正确的——底层容器实际上是一个
std::set
,它定义了一个
操作符,正如我所期望的(从我的记忆中)边缘列表(包括输入/输出)已经按其目标排序,因此它们是确定性的

当VertexContainer选择器为
vecS
时,这一点尤其明确,因为
vertex\u描述符是一种简单的整数类型,它可以作为
vertex\u index\t
属性的两倍

下兔子洞 因为我不是Boost图形开发人员,因此我不知道 BGL类型的体系结构,如
邻接列表
,我天真地从我们的 顶级入口点:

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));
}
评估结果是

boost::detail::stored_edge_iter<
    long unsigned int,
    std::_List_iterator<boost::list_edge<long unsigned int, boost::no_property> >,
    boost::no_property>
演示 这是一个完整的演示,包括以上所有内容,并在一个大型随机生成的图上断言所有预期的顺序:

#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))));
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用boost::Adapters::transformed;
使用名称空间boost;
结构VertexCargo{int Id=rand()%1024;};
typedef邻接列表图;
typedef graph_traits::edge_描述符ED;
typedef图形特征::顶点描述符VD;
结构源{
使用结果_type=VD;
图常数*g;
sourcer(图const&g):g(&g){}
VD运算符()(ED e)常量{return boost::source(e,*g);}
};
结构目标器{
使用结果_type=VD;
图常数*g;
目标器(图常数&g):g(&g){}
VD运算符()(ED e)常量{return boost::target(e,*g);}
};
int main(){
std::mt19937 rng{std::random_设备{}()};
图g;

生成随机图(g,1ul目标是什么。按顺序迭代,还是按顺序底层存储?(你希望这是一个优化,还是一个清晰的代码问题)我的目标是按顺序迭代;如果不需要按顺序存储,我就不需要按顺序存储。但我希望不必转储到一个向量,然后手动排序,如果这就是你想要的……我希望这比看起来容易,事实上是这样。一些假定的类型不太好。请参阅我的答案:)感谢您进入兔子洞!您是使用IDE帮助还是仅仅阅读所有源代码?我必须承认,我发现boost源代码有点让人望而生畏,在某些情况下它会击败我的IDE。我注意您的答案:它似乎是正确的--仍然在阅读代码…--但事实上,由于这种顺序关系只是部分顺序,
_边
可能仍然无法确定在公共目标上会聚的边的顺序。Erm.
std::set
根据定义不包含具有等效顺序的元素,因此您的问题不存在:)顺便说一句,我使用了gcc+vim+YouCompleMe。我从种植
std::cerrAh开始,是的,关于 std::set
,当然……每个等价类只包含一个条目……无论如何,我对边缘存储方式的思维模式是不正确的,因此我在这里感到困惑。感谢你提醒我注意
存储的\u边缘
。比较是明确的。让我看看我的问题现在在哪里……嗨@sehe,感谢你的帮助我瘦了k我已经确定了问题所在。事实上,在我的代码库中,我的向量列表选择器是
列表
而不是
向量
,在这种情况下,您确定的比较取决于内存地址。坦率地说,我有点惊讶于这一点;对于BGL实现者来说,在
顶点上进行比较可能更正确_索引
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;
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;
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>
  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))));
#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))));
    }
}