Boost列表的节点结构是什么?

时间:2016-07-28 06:12:28

标签: c++ list boost

here我无法理解Boost列表的节点结构是什么?而不理解这一点让我很难理解为什么插入(摊销)常量时间,如代码注释中所述:

  

列表是双向链表。也就是说,它是一个序列   //两个都支持!前向和后向遍历,并且(摊销)   恒定时间插入和//!在开始时删除元素   或结束,或中间

1 个答案:

答案 0 :(得分:1)

插入/移除时,它在iterator上运行,boost/container/list.hpp已指向插入/移除位置的节点。

当然,它可以实现恒定的插入/移除时间。

更新:我不知道为什么它已经“摊销”了常量时间,但你问的是内部节点,就在这里。

list_node中,template<class VoidPointer> struct list_hook { typedef typename container_detail::bi::make_list_base_hook <container_detail::bi::void_pointer<VoidPointer>, container_detail::bi::link_mode<container_detail::bi::normal_link> >::type type; }; template <class T, class VoidPointer> struct list_node : public list_hook<VoidPointer>::type { ... } 定义为:

list_hook::type

它继承了intrusive/list_hook.hpp,所以让我们看看它是什么。

template<class VoidPointer> struct get_list_node_algo { typedef circular_list_algorithms<list_node_traits<VoidPointer> > type; }; struct make_list_base_hook { ... typedef detail::generic_hook < get_list_node_algo<typename packed_options::void_pointer> , ... > implementation_defined; /// @endcond typedef implementation_defined type; };

generic_hook

因此它是circular_list_algorithms<list_node_traits>template < class GetNodeAlgorithms ,... > class generic_hook : ... , public make_node_holder<GetNodeAlgorithms, Tag, LinkMode, HookType>::type 作为第一个模板参数:

make_node_holder::type

它继承template < class GetNodeAlgorithms , class Tag , link_mode_type LinkMode , int HookType > struct make_node_holder { typedef typename detail::if_c <!detail::is_same<Tag, member_tag>::value , detail::node_holder < typename GetNodeAlgorithms::type::node , Tag , LinkMode , HookType> , typename GetNodeAlgorithms::type::node >::type type; }; ,即:

detail:node_holder

GetNodeAlgorithms::type::node类型为template<class Node, class Tag, link_mode_type LinkMode, int> struct node_holder : public Node {};

GetNodeAlgorithms::type::node

此处list_node_traits::node intrusive/detail/list_node.hpp template<class VoidPointer> struct list_node { ... node_ptr next_; node_ptr prev_; }; template<class VoidPointer> struct list_node_traits { typedef list_node<VoidPointer> node; ... } 定义next_

prev_

现在我们看到了list_node -> list_hook::type -> make_list_base_hook::type -> generic_hook::type -> make_node_holder::type -> node_holder -> Node Node指针!

总而言之,继承树是:

boost::intrusive::list_node

{{1}}的类型为{{1}},且上一个下一个指针。