list概述
list在存储空间不是连续存在的,迭代器必须指向list的节点,并能进行正确的递增、递减、取值、成员存取等操作。
list每次插入或删除一个元素,就配置或释放一个元素空间,对于任何位置元素的插入或移除,永远是常数时间。
list本身和节点是不同的结构,需要分开设计。
STL list是一个双向链表,提供的是Bidirectional Iterators。
list的插入和删除操作不会导致所有迭代器失效,删除操作只有指向被删除元素的迭代器失效,其他迭代器不受影响。
list不仅是一个双向链表,还是一个环状双向链表,只要刻意在环状链表的尾端加上一个空白节点,便符合STL规范之前闭后开区间,可完整表现整个链表。
list缺省使用std::allocator作为空间配置器。
namespace std _GLIBCXX_VISIBILITY(default)
{ //节点设计
namespace __detail
{
struct _List_node_base //list节点结构,设定为双向链表
{
_List_node_base* _M_next; //后一节点
_List_node_base* _M_prev; //前一节点
static void //两节点交换
swap(_List_node_base& __x, _List_node_base& __y) _GLIBCXX_USE_NOEXCEPT;
//以下省略部分内容
};
struct _List_node_header : public _List_node_base //环状设计,空白节点
{
std::size_t _M_size; //长度
_List_node_header() _GLIBCXX_NOEXCEPT //默认构造函数
{ _M_init(); }
_List_node_header(_List_node_header&& __x) noexcept //移动构造函数,通知标准库不抛出异常
: _List_node_base{ __x._M_next, __x._M_prev } //当前空白节点前后置节点等于__x的前后置
, _M_size(__x._M_size) //赋值长度
{
if (__x._M_base()->_M_next == __x._M_base()) //环状双向链表,判断__x指向的环是否为空环
this->_M_next = this->_M_prev = this;
else
{ //当前空白节点前后置已设置,将节点__x指向的环与当前节点形成环结构,将__x置为空环
this->_M_next->_M_prev = this->_M_prev->_M_next = this->_M_base();
__x._M_init(); //__x指向空环
}
}
void //与移动构造函数功能类似
_M_move_nodes(_List_node_header&& __x) //加入节点__x
{
_List_node_base* const __xnode = __x._M_base();
if (__xnode->_M_next == __xnode) //__x为空环时
_M_init(); //当前环置空环
else
{
_List_node_base* const __node = this->_M_base(); //获取当前空白节点
__node->_M_next = __xnode->_M_next; //当前空白节点后置节点为__x的后置节点
__node->_M_prev = __xnode->_M_prev; //当前空白节点前置节点为__x的前置节点
__node->_M_next->_M_prev = __node->_M_prev->_M_next = __node; //替换__x在环状结构的位置
_M_size = __x._M_size; //赋值长度
__x._M_init(); //__x置空环
}
}
void
_M_init() _GLIBCXX_NOEXCEPT //空环状态
{
this->_M_next = this->_M_prev = this;
this->_M_size = 0;
}
private:
_List_node_base* _M_base() { return this; }
};
} // namespace detail
//链表中的节点元素
template<typename _Tp>
struct _List_node : public __detail::_List_node_base
{
__gnu_cxx::__aligned_membuf<_Tp> _M_storage; //节点元素
_Tp* _M_valptr() { return _M_storage._M_ptr(); }
_Tp const* _M_valptr() const { return _M_storage._M_ptr(); }
};
//迭代器设计
template<typename _Tp> //list是双向链表,迭代器必须具备前移、后移能力,
struct _List_iterator //使用bidirectional_iterator_tag作为迭代器类型
{
typedef _List_iterator<_Tp> _Self; //迭代器
typedef _List_node<_Tp> _Node; //节点
typedef ptrdiff_t difference_type; //节点之间的距离
typedef std::bidirectional_iterator_tag iterator_category; //迭代器类型
typedef _Tp value_type; //元素类型
typedef _Tp* pointer; //指针
typedef _Tp& reference; //左值引用
//以下省略构造函数及其他部分内容
//以下为重载运算符
reference //取值运算符,取的是节点元素值
operator*() const _GLIBCXX_NOEXCEPT
{ return *static_cast<_Node*>(_M_node)->_M_valptr(); }
pointer //成员访问运算符,取节点元素值
operator->() const _GLIBCXX_NOEXCEPT
{ return static_cast<_Node*>(_M_node)->_M_valptr(); }
_Self& //递增运算符,前置版本
operator++() _GLIBCXX_NOEXCEPT
{
_M_node = _M_node->_M_next; //前进+1
return *this; //返回当前
}
_Self //递增运算符,后置版本
operator++(int) _GLIBCXX_NOEXCEPT
{
_Self __tmp = *this; //先备份
_M_node = _M_node->_M_next; //前进+1
return __tmp; //返回备份值
}
_Self& //递减运算符,()为前置版本,(int)为后置版本
operator--() _GLIBCXX_NOEXCEPT
{
_M_node = _M_node->_M_prev;
return *this;
}
_Self
operator--(int) _GLIBCXX_NOEXCEPT
{
_Self __tmp = *this;
_M_node = _M_node->_M_prev;
return __tmp;
}
bool //相等运算符,判断节点是否相等
operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
{ return _M_node == __x._M_node; }
bool //不等运算符,判断节点不等
operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
{ return _M_node != __x._M_node; }
__detail::_List_node_base* _M_node; //迭代器内部普通指针,指向list节点
};
//省略const_iterator设计
//以下为迭代器的运算符重载
template<typename _Val>
inline bool //相等运算符,判断迭代器是否相等
operator==(const _List_iterator<_Val>& __x,
const _List_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
{ return __x._M_node == __y._M_node; } //迭代器指向节点是否相等
template<typename _Val>
inline bool //不等运算符,判断迭代器不等
operator!=(const _List_iterator<_Val>& __x,
const _List_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
{ return __x._M_node != __y._M_node; } //迭代器指向节点不等
//链表基类设计
template<typename _Tp, typename _Alloc>
class _List_base
{
protected:
typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
rebind<_Tp>::other _Tp_alloc_type; //节点内元素内存分配器_Alloc<_Tp>别名
typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tp_alloc_traits; //元素内存分配器_Alloc<_Tp>的属性别名
typedef typename _Tp_alloc_traits::template
rebind<_List_node<_Tp> >::other _Node_alloc_type; //节点内存分配器_Alloc<_List_node<_Tp>>别名
typedef __gnu_cxx::__alloc_traits<_Node_alloc_type> _Node_alloc_traits; //节点内存分配器_Alloc<_List_node<_Tp>>属性别名
struct _List_impl //节点内存分配封装,容器内存分配设计
: public _Node_alloc_type //继承节点的内存分配器_Alloc<_List_node<_Tp>>
{
__detail::_List_node_header _M_node; //空白节点
//以下省略默认,拷贝、移动构造函数
};
_List_impl _M_impl;
//以下省略部分内容
};
//list为环状双向链表,继承自_List_base,类内有空白节点_M_impl._M_node
//list缺省使用std::allocator作为空间配置器,并定义_Base,方便以节点大小配置内存
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
class list : protected _List_base<_Tp, _Alloc>
{
static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
"std::list must have a non-const, non-volatile value_type");
//基类属性声明
typedef _List_base<_Tp, _Alloc> _Base; //基类
typedef typename _Base::_Tp_alloc_type _Tp_alloc_type; //基类中节点元素内存分配器
typedef typename _Base::_Tp_alloc_traits _Tp_alloc_traits; //节点元素内存分配器属性
typedef typename _Base::_Node_alloc_type _Node_alloc_type; //节点内存分配器
typedef typename _Base::_Node_alloc_traits _Node_alloc_traits; //节点内存分配器属性
public: //类型别名
typedef _Tp value_type; //元素类型
typedef typename _Tp_alloc_traits::pointer pointer; //元素指针类型
typedef typename _Tp_alloc_traits::const_pointer const_pointer;
typedef typename _Tp_alloc_traits::reference reference; //元素左值引用类型
typedef typename _Tp_alloc_traits::const_reference const_reference;
typedef _List_iterator<_Tp> iterator; //迭代器
typedef _List_const_iterator<_Tp> const_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator; //反向迭代器
typedef size_t size_type;
typedef ptrdiff_t difference_type; //元素之间的距离
typedef _Alloc allocator_type; //内存分配器
protected: //基类成员和成员函数
typedef _List_node<_Tp> _Node; //元素节点
using _Base::_M_impl; //节点内存分配器
using _Base::_M_put_node; //释放一个节点
using _Base::_M_get_node; //配置一个节点并返回
using _Base::_M_get_Node_allocator; //配置并构造节点
template<typename... _Args> //可变模板参数
_Node* //返回节点指针
_M_create_node(_Args&&... __args) //新建元素节点,可变函数参数
{
auto __p = this->_M_get_node();
auto& __alloc = _M_get_Node_allocator();
__allocated_ptr<_Node_alloc_type> __guard{__alloc, __p};
_Node_alloc_traits::construct(__alloc, __p->_M_valptr(),
std::forward<_Args>(__args)...);
__guard = nullptr;
return __p;
}
static size_t //返回首尾节点之间的距离
_S_distance(const_iterator __first, const_iterator __last)
{ return std::distance(__first, __last); }
size_t //返回链表长度
_M_node_count() const
{ return this->_M_get_size(); }
public:
list() = default; //默认构造函数,要求编绎器生成合成版本
explicit //禁止隐式类型转换
list(const allocator_type& __a) _GLIBCXX_NOEXCEPT //拷贝构造函数
: _Base(_Node_alloc_type(__a)) { }
explicit
list(size_type __n, const allocator_type& __a = allocator_type())
: _Base(_Node_alloc_type(__a))
{ _M_default_initialize(__n); }
list(size_type __n, const value_type& __value,
const allocator_type& __a = allocator_type())
: _Base(_Node_alloc_type(__a))
{ _M_fill_initialize(__n, __value); }
list(const list& __x)
: _Base(_Node_alloc_traits::
_S_select_on_copy(__x._M_get_Node_allocator()))
{ _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
list(list&&) = default; //移动构造函数,要求编绎器生成合成版本
list(initializer_list<value_type> __l,
const allocator_type& __a = allocator_type())
: _Base(_Node_alloc_type(__a))
{ _M_initialize_dispatch(__l.begin(), __l.end(), __false_type()); }
list(const list& __x, const allocator_type& __a)
: _Base(_Node_alloc_type(__a))
{ _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
private:
list(list&& __x, const allocator_type& __a, true_type) noexcept
: _Base(_Node_alloc_type(__a), std::move(__x))
{ }
list(list&& __x, const allocator_type& __a, false_type)
: _Base(_Node_alloc_type(__a))
{
if (__x._M_get_Node_allocator() == this->_M_get_Node_allocator())
this->_M_move_nodes(std::move(__x));
else
insert(begin(), std::__make_move_if_noexcept_iterator(__x.begin()),
std::__make_move_if_noexcept_iterator(__x.end()));
}
public:
list(list&& __x, const allocator_type& __a)
noexcept(_Node_alloc_traits::_S_always_equal())
: list(std::move(__x), __a,
typename _Node_alloc_traits::is_always_equal{})
{ }
template<typename _InputIterator,
typename = std::_RequireInputIter<_InputIterator>>
list(_InputIterator __first, _InputIterator __last,
const allocator_type& __a = allocator_type())
: _Base(_Node_alloc_type(__a))
{ _M_initialize_dispatch(__first, __last, __false_type()); }
~list() = default; //析构函数
//赋值运算符重载,接受左右值引用、初始值列表
list&
operator=(const list& __x);
list&
operator=(list&& __x)
noexcept(_Node_alloc_traits::_S_nothrow_move())
{
constexpr bool __move_storage =
_Node_alloc_traits::_S_propagate_on_move_assign()
|| _Node_alloc_traits::_S_always_equal();
_M_move_assign(std::move(__x), __bool_constant<__move_storage>());
return *this;
}
list&
operator=(initializer_list<value_type> __l)
{
this->assign(__l.begin(), __l.end());
return *this;
}
//assign
void
assign(size_type __n, const value_type& __val)
{ _M_fill_assign(__n, __val); }
template<typename _InputIterator,
typename = std::_RequireInputIter<_InputIterator>>
void
assign(_InputIterator __first, _InputIterator __last)
{ _M_assign_dispatch(__first, __last, __false_type()); }
void
assign(initializer_list<value_type> __l)
{ this->_M_assign_dispatch(__l.begin(), __l.end(), __false_type()); }
/// Get a copy of the memory allocation object.
allocator_type
get_allocator() const _GLIBCXX_NOEXCEPT
{ return allocator_type(_Base::_M_get_Node_allocator()); }
//list为双向环状链表,_M_impl._M_node指向空白节点
//end()返回该空白节点,begin()返回空白节点的下一个节点
// begin、end、rbegin、rend,省略返回const迭代器
iterator
begin() _GLIBCXX_NOEXCEPT
{ return iterator(this->_M_impl._M_node._M_next); }
iterator
end() _GLIBCXX_NOEXCEPT
{ return iterator(&this->_M_impl._M_node); }
reverse_iterator
rbegin() _GLIBCXX_NOEXCEPT
{ return reverse_iterator(end()); }
reverse_iterator
rend() _GLIBCXX_NOEXCEPT
{ return reverse_iterator(begin()); }
//cbegin、cend、crbegin、crend
const_iterator
cbegin() const noexcept
{ return const_iterator(this->_M_impl._M_node._M_next); }
const_iterator
cend() const noexcept
{ return const_iterator(&this->_M_impl._M_node); }
const_reverse_iterator
crbegin() const noexcept
{ return const_reverse_iterator(end()); }
const_reverse_iterator
crend() const noexcept
{ return const_reverse_iterator(begin()); }
bool
empty() const _GLIBCXX_NOEXCEPT //容器是否为空
{ return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
size_type
size() const _GLIBCXX_NOEXCEPT
{ return _M_node_count(); }
size_type
max_size() const _GLIBCXX_NOEXCEPT
{ return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
void
resize(size_type __new_size);
void
resize(size_type __new_size, const value_type& __x);
//front、back,省略返回const引用
reference
front() _GLIBCXX_NOEXCEPT
{ return *begin(); }
reference
back() _GLIBCXX_NOEXCEPT
{
iterator __tmp = end();
--__tmp;
return *__tmp;
}
//list是双向环状链表,处理好边际条件,在头部尾部插入元素操作几乎一样
void
push_front(const value_type& __x)
{ this->_M_insert(begin(), __x); }
void
push_front(value_type&& __x)
{ this->_M_insert(begin(), std::move(__x)); }
template<typename... _Args>
void
emplace_front(_Args&&... __args)
{
this->_M_insert(begin(), std::forward<_Args>(__args)...);
}
//边际条件处理好,在头部或尾部移除元素操作几乎一致
void
pop_front() _GLIBCXX_NOEXCEPT
{ this->_M_erase(begin()); }
void
push_back(const value_type& __x)
{ this->_M_insert(end(), __x); }
void
push_back(value_type&& __x)
{ this->_M_insert(end(), std::move(__x)); }
template<typename... _Args>
void
emplace_back(_Args&&... __args)
{
this->_M_insert(end(), std::forward<_Args>(__args)...);
}
void
pop_back() _GLIBCXX_NOEXCEPT
{ this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
template<typename... _Args>
iterator
emplace(const_iterator __position, _Args&&... __args);
//insert是重载函数,有多种形式
//返回指向插入节点之前节点的迭代器
iterator
insert(const_iterator __position, const value_type& __x);
iterator
insert(const_iterator __position, value_type&& __x)
{ return emplace(__position, std::move(__x)); }
iterator
insert(const_iterator __p, initializer_list<value_type> __l)
{ return this->insert(__p, __l.begin(), __l.end()); }
iterator
insert(const_iterator __position, size_type __n, const value_type& __x);
template<typename _InputIterator,
typename = std::_RequireInputIter<_InputIterator>>
iterator
insert(const_iterator __position, _InputIterator __first,
_InputIterator __last);
//移除节点
iterator
erase(const_iterator __position) noexcept;
iterator
erase(const_iterator __first, const_iterator __last) noexcept
{
while (__first != __last)
__first = erase(__first);
return __last._M_const_cast();
}
void
swap(list& __x) _GLIBCXX_NOEXCEPT
{
__detail::_List_node_base::swap(this->_M_impl._M_node,
__x._M_impl._M_node);
size_t __xsize = __x._M_get_size();
__x._M_set_size(this->_M_get_size());
this->_M_set_size(__xsize);
_Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
__x._M_get_Node_allocator());
}
//清除所有节点
void
clear() _GLIBCXX_NOEXCEPT
{
_Base::_M_clear();
_Base::_M_init();
}
//拼接操作
void
splice(const_iterator __position, list&& __x) noexcept
{
if (!__x.empty())
{
_M_check_equal_allocators(__x);
this->_M_transfer(__position._M_const_cast(),
__x.begin(), __x.end());
this->_M_inc_size(__x._M_get_size());
__x._M_set_size(0);
}
}
void
splice(const_iterator __position, list& __x) noexcept
{ splice(__position, std::move(__x)); }
splice(const_iterator __position, list&& __x, const_iterator __i) noexcept
{
iterator __j = __i._M_const_cast();
++__j;
if (__position == __i || __position == __j)
return;
if (this != std::__addressof(__x))
_M_check_equal_allocators(__x);
this->_M_transfer(__position._M_const_cast(),
__i._M_const_cast(), __j);
this->_M_inc_size(1);
__x._M_dec_size(1);
}
void
splice(const_iterator __position, list& __x, const_iterator __i) noexcept
{ splice(__position, std::move(__x), __i); }
void
splice(const_iterator __position, list&& __x, const_iterator __first,
const_iterator __last) noexcept
{
if (__first != __last)
{
if (this != std::__addressof(__x))
_M_check_equal_allocators(__x);
size_t __n = _S_distance(__first, __last);
this->_M_inc_size(__n);
__x._M_dec_size(__n);
this->_M_transfer(__position._M_const_cast(),
__first._M_const_cast(),
__last._M_const_cast());
}
}
void
splice(const_iterator __position, list& __x, const_iterator __first,
const_iterator __last) noexcept
{ splice(__position, std::move(__x), __first, __last); }
//移除所有数值为__value的元素
void
remove(const _Tp& __value);
template<typename _Predicate>
void
remove_if(_Predicate);
//移除数值相同的连续元素,连续且相同则被移除剩一下
void
unique();
template<typename _BinaryPredicate>
void
unique(_BinaryPredicate);
void
merge(list&& __x);
void
merge(list& __x)
{ merge(std::move(__x)); }
template<typename _StrictWeakOrdering>
void
merge(list&& __x, _StrictWeakOrdering __comp);
template<typename _StrictWeakOrdering>
void
merge(list& __x, _StrictWeakOrdering __comp)
{ merge(std::move(__x), __comp); }
void
reverse() _GLIBCXX_NOEXCEPT
{ this->_M_impl._M_node._M_reverse(); }
//list不能使用STL算法sort(),必须自定义sort成员函数
//STL算法sort()只接受RamonAccessIterator
void
sort();
template<typename _StrictWeakOrdering>
void
sort(_StrictWeakOrdering);
};
template<typename _Tp, typename _Alloc>
inline bool
operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{
if (__x.size() != __y.size())
return false;
typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
const_iterator __end1 = __x.end();
const_iterator __end2 = __y.end();
const_iterator __i1 = __x.begin();
const_iterator __i2 = __y.begin();
while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
{
++__i1;
++__i2;
}
return __i1 == __end1 && __i2 == __end2;
}
template<typename _Tp, typename _Alloc>
inline bool
operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{ return std::lexicographical_compare(__x.begin(), __x.end(),
__y.begin(), __y.end()); }
/// Based on operator==
template<typename _Tp, typename _Alloc>
inline bool
operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{ return !(__x == __y); }
/// Based on operator<
template<typename _Tp, typename _Alloc>
inline bool
operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{ return __y < __x; }
/// Based on operator<
template<typename _Tp, typename _Alloc>
inline bool
operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{ return !(__y < __x); }
/// Based on operator<
template<typename _Tp, typename _Alloc>
inline bool
operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{ return !(__x < __y); }
/// See std::list::swap().
template<typename _Tp, typename _Alloc>
inline void
swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
_GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
{ __x.swap(__y); }
// Detect when distance is used to compute the size of the whole list.
template<typename _Tp>
inline ptrdiff_t
__distance(_GLIBCXX_STD_C::_List_iterator<_Tp> __first,
_GLIBCXX_STD_C::_List_iterator<_Tp> __last,
input_iterator_tag __tag)
{
typedef _GLIBCXX_STD_C::_List_const_iterator<_Tp> _CIter;
return std::__distance(_CIter(__first), _CIter(__last), __tag);
}
template<typename _Tp>
inline ptrdiff_t
__distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp> __first,
_GLIBCXX_STD_C::_List_const_iterator<_Tp> __last,
input_iterator_tag)
{
typedef __detail::_List_node_header _Sentinel;
_GLIBCXX_STD_C::_List_const_iterator<_Tp> __beyond = __last;
++__beyond;
const bool __whole = __first == __beyond;
if (__builtin_constant_p (__whole) && __whole)
return static_cast<const _Sentinel*>(__last._M_node)->_M_size;
ptrdiff_t __n = 0;
while (__first != __last)
{
++__first;
++__n;
}
return __n;
}
} // namespace std