This article mainly analyzes the source code implementation of STL list in gcc4.8. Unlike the linear spatial structure of vector, list nodes are randomly dispersed and are connected by pointers, the advantage is that it takes only constant time to insert and delete elements at any position. The disadvantage is that random access is not allowed, the query complexity is O (n), and N is the number of elements in the list. Therefore, list is suitable for scenarios where applications and data insertion and deletion are frequent.
I. List nodes
The list node is defined as follows,
struct _List_node_base{ _List_node_base* _M_next; _List_node_base* _M_prev;}template<typename _Tp>struct _List_node : public __detail::_List_node_base{ _Tp _M_data;}
Obviously, list is a two-way linked list. _ m_next and _ m_prev point to the next node and the last node respectively. _ m_data is the data stored by the node.
Ii. List iterator
Because list is not a linear spatial structure, the general iterator cannot be moved normally, so list needs to define a special iterator.
Template <typename _ TP> struct _ list_iterator {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; _ list_iterator (): _ m_node () {} explicit _ list_iterator (_ detail :: _ list_node_base * _ x): _ m_node (_ x) {}// must downcast from _ list_node_base to _ list_node to get to _ m_data. reference operator * () const {return static_cast <_ node *> (_ m_node)-> _ m_data;} pointer operator-> () const {return STD :: __addressof (static_cast <_ node *> (_ m_node)-> _ m_data);} _ Self & operator ++ () {_ m_node = _ m_node-> _ m_next; return * This;} _ Self operator ++ (INT) {_ self _ TMP = * This; _ m_node = _ m_node-> _ m_next; return _ TMP ;} _ Self & operator -- () {_ m_node = _ m_node-> _ m_prev; return * This;} _ Self operator -- (INT) {_ self _ TMP = * this; _ m_node = _ m_node-> _ m_prev; return _ TMP;} // point to the list node pointer _ detail: _ list_node_base * _ m_node ;};
The list iterator points to the node of the list. Therefore, static_cast <_ node *> (_ m_node)-> _ m_data must be returned when the list iterator is referenced. The list iterator is not a random access iterator and must be moved step by step through the next and Prev pointers of nodes. The element access complexity is linear. The read-only iterator _ list_const_iterator is also defined.
Iii. Definition of list
Like vector, list inherits from _ list_base, and the memory allocation and release work is handled by _ list_base,
Template <typename _ TP, typename _ alloc> class _ list_base {protected: // use the traits method to get the typedef typename _ alloc :: template rebind <_ list_node <_ TP> >:: other _ node_alloc_type; typedef typename _ alloc: Template rebind <_ TP >:: other _ tp_alloc_type; struct _ list_impl: public _ node_alloc_type {// The data member has only one header node _ detail: _ list_node_base _ m_node; _ list_impl (): _ node_alloc_type (), _ m_node (){} _ List_impl (const _ node_alloc_type & _ A): _ node_alloc_type (_ A), _ m_node () {}}; _ list_impl _ m_impl; // interface for node allocation _ list_node <_ TP> * _ m_get_node () {return _ m_impl. _ node_alloc_type: allocate (1) ;}_ list_base (): _ m_impl () {_ m_init () ;}// the list is initially empty, both the next and Prev of the header nodes point to their own void _ m_init () {This-> _ m_impl. _ m_node. _ m_next = & this-> _ m_impl. _ m_node; this-> _ m_impl. _ m_node. _ m_prev = & this-> _ m_impl. _ m_node;} // the first data Nodes are deleted and the memory is released sequentially. Template <typename _ TP, typename _ alloc> void _ list_base <_ TP, _ alloc >:: _ m_clear () {typedef _ list_node <_ TP> _ node; _ node * _ cur = static_cast <_ node *> (_ m_impl. _ m_node. _ m_next); While (_ cur! = & _ M_impl. _ m_node) {_ node * _ TMP = _ cur; _ cur = static_cast <_ node *> (_ cur-> _ m_next); _ m_get_node_allocator (). destroy (_ TMP); _ m_put_node (_ TMP );}}
List inherits the header node of _ list_base,
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >class list : protected _List_base<_Tp, _Alloc>{ // concept requirements typedef typename _Alloc::value_type _Alloc_value_type; __glibcxx_class_requires(_Tp, _SGIAssignableConcept) __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept) typedef _List_base<_Tp, _Alloc> _Base; typedef typename _Base::_Tp_alloc_type _Tp_alloc_type; typedef typename _Base::_Node_alloc_type _Node_alloc_type; public: typedef _Tp value_type; typedef typename _Tp_alloc_type::pointer pointer; typedef typename _Tp_alloc_type::const_pointer const_pointer; typedef typename _Tp_alloc_type::reference reference; typedef typename _Tp_alloc_type::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;......
Iv. List Construction Method
List provides a variety of constructors,
// Default constructor list (): _ base () {}// initialize to N default values of explicit list (size_type _ n): _ base () {_ m_default_initialize (_ n);} // initialize to N _ value lists (size_type _ n, const value_type & _ value, const allocator_type & _ A = allocator_type (): _ base (_ node_alloc_type (_ A) {_ m_fill_initialize (_ n, _ value );} // copy the const list (const list & _ x): _ base (_ x. _ m_get_node_allocator () {_ m_initialize_dispatch (_ x. begin (), _ x. end (), _ false_type ();} // mobile constructor list (List & _ x) notest: _ base (STD :: move (_ x )){}
5. list element operations
Limited space. Only some functions are discussed.
Iterator begin () _ glibcxx_now.t {return iterator (this-> _ m_impl. _ m_node. _ m_next);} iterator end () _ glibcxx_no1_t {return iterator (& this-> _ m_impl. _ m_node);} end points to the header node, and begin points to the first data node. begin and end still form a pre-closed and post-open interval. Bool empty () const _ glibcxx_now.t {return this-> _ m_impl. _ m_node. _ m_next = & this-> _ m_impl. _ m_node;} checks whether the list is empty by checking whether the header node points to itself. Void push_front (const value_type & _ x) {This-> _ m_insert (begin (), _ x);} void push_back (const value_type & _ x) {This-> _ m_insert (end (), _ x);} template <typename... _ ARGs> void _ m_insert (iterator _ Position, _ ARGs &&... _ ARGs) {_ node * _ TMP = _ m_create_node (STD: Forward <_ ARGs> (_ ARGs )...); _ TMP-> _ m_hook (_ position. _ m_node);} template <typename _ TP, typename _ alloc> typename list <_ TP, _ alloc >:: iterator list <_ TP, _ alloc> :: insert (iterator _ Position, const value_type & _ x) {_ node * _ TMP = _ m_create_node (_ x ); _ TMP-> _ m_hook (_ position. _ m_node); Return iterator (_ TMP );}
The time complexity of push_front, push_back, and insert is O (1 ).
template<typename _Tp, typename _Alloc>void list<_Tp, _Alloc>::merge(list&& __x){ if (this != &__x) { _M_check_equal_allocators(__x); iterator __first1 = begin(); iterator __last1 = end(); iterator __first2 = __x.begin(); iterator __last2 = __x.end(); while (__first1 != __last1 && __first2 != __last2) if (*__first2 < *__first1) { iterator __next = __first2; _M_transfer(__first1, __first2, ++__next); __first2 = __next; } else ++__first1; if (__first2 != __last2) _M_transfer(__last1, __first2, __last2); }}