【问题标题】:What is the Node structure of Boost list?Boost列表的节点结构是什么?
【发布时间】:2016-07-28 06:12:27
【问题描述】:

here 看不懂Boost列表的Node结构是什么?不理解这一点让我很难理解为什么插入是(摊销)恒定时间,正如代码cmets中提到的那样:

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

【问题讨论】:

  • 插入/删除时,它对iterator进行操作,该iterator已经指向插入/删除位置的节点。那么当然可以实现恒定时间的插入/取出。
  • 不确定问题是什么。你知道常数时间和摊销常数时间的区别吗?

标签: c++ list boost


【解决方案1】:

插入/删除时,它对iterator进行操作,该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&lt;list_node_traits&gt;作为第一个模板参数:

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::nodelist_node_traits::nodeintrusive/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,并且有prevnext指针。

【讨论】:

  • 但您总是将position 迭代器提供给inserterase。那么它总是 O(1) 没有摊销,对吧?还有节点结构呢?我没有看到左右链接。
  • 更新了关于节点结构的答案。但有趣的是为什么它摊销恒定时间,我也很好奇:)
  • 非常感谢Node的解释:)
猜你喜欢
  • 2021-01-18
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 2013-11-19
  • 2021-02-10
  • 2015-03-17
相关资源
最近更新 更多