Dav*_*der 5 c++ boost boost-graph
TL; DR:我非常希望in_edges
图上的迭代顺序(adjacency_list
带有 edge_list of 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;
Run Code Online (Sandbox Code Playgroud)
我的逻辑,以防有人在任何地方发现谬误:
in_edges
迭代直接由边列表容器的迭代决定
setS
隐含底层容器的边缘列表std::set<edge_desc_impl>
注意:这个假设是不正确的;它实际上是一个std::set<StoredEdge
,它提供了一个比较边缘目标的比较器。
std::set<edge_desc_impl>
迭代顺序由
operator<(edge_desc_impl, edge_desc_impl)
operator<(edge_desc_impl, edge_desc_impl)
最终只是一个指针比较;
operator< 在boost/graph/detail/edge.hpp
(boost 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 }
Run Code Online (Sandbox Code Playgroud)
如果有一种方法可以基于某些确定性的东西(即不是由 mallloc 确定的指针地址;例如,我编写的一个自定义运算符来查找VertexCargo::Id
源和目标),我真的很喜欢它边缘),但由于void*
演员阵容的原因,这看起来在这里可能不可行。
从上面的代码看起来也有可能(?)注入一个property
给出所需的排序。但这让我觉得是一个黑客。
有人有智慧分享吗?
@sehe 的回答是正确的答案---底层容器实际上是 a std::set<StoredEdge>
,它定义了operator<
在边缘目标上进行比较的 a ;这在顶点容器选择器是但不是一般情况下是确定性的(因为其他情况下的顶点标识符基本上是从 malloc 获取的指针)。vecS
正如我所期望的(从我的回忆中)边缘列表(输入/输出)已经按其目标排序,因此它们是确定性的。
这在 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));
}
Run Code Online (Sandbox Code Playgroud)
实例化为:
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> &)
Run Code Online (Sandbox Code Playgroud)
和
Run Code Online (Sandbox Code Playgroud)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;
Run Code Online (Sandbox Code Playgroud)
将实例化指向我们,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;
Run Code Online (Sandbox Code Playgroud)
并且因为容器选择器是setS
,它将是 a std::set
of StoredEdge
,即
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;
Run Code Online (Sandbox Code Playgroud)
其中评估为
boost::detail::stored_edge_iter<
long unsigned int,
std::_List_iterator<boost::list_edge<long unsigned int, boost::no_property> >,
boost::no_property>
Run Code Online (Sandbox Code Playgroud)
现在,当然,这指向 EdgeList 的实现......
但最重要的是强加的总弱排序——所以我们不会进一步深入那个兔子洞,而是将我们的注意力转移到
stored_edge_iter<>::operator<
或类似的地方。
inline bool operator<(const stored_edge& x) const
{ return m_target < x.get_target(); }
Run Code Online (Sandbox Code Playgroud)
啊哈!排序已经确定。您可以使用例如直接访问它
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";
}
Run Code Online (Sandbox Code Playgroud)
但你不需要。使用通用图形访问器几乎相同:
std::cout << v << " --> ";
for (auto e : make_iterator_range(in_edges(v, g))) std::cout << source(e, g) << " ";
std::cout << "\n";
Run Code Online (Sandbox Code Playgroud)
您可以使用例如验证集合是否按预期正确排序
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))));
Run Code Online (Sandbox Code Playgroud)
这是一个完整的演示,包括上述所有内容,并在一个大型随机生成的图形上断言所有预期的顺序:
#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))));
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
814 次 |
最近记录: |