插入/删除时,它对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<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,并且有prev和next指针。