STL源码剖析 - 第4章 序列式容器 - list
来源:互联网 发布:淘宝客服认证后解绑 编辑:程序博客网 时间:2024/06/02 23:17
4.3.1 list概述
在SGI STL中,list容器是一个循环的双向链表,它的内存空间效率较前文介绍的vector容器高。因为vector容器的内存空间是连续存储的,且在分配内存空间时,会分配额外的可用空间;而list容器的内存空间不一定是连续存储,内存之间是采用迭代器或节点指针进行连接,并且在插入或删除数据节点时,就配置或释放一个数据节点,并不会分配额外的内存空间,这两个操作过程都是常数时间。
与vector容器不同的是,list容器在进行插入操作或拼接操作时,迭代器并不会失效;且不能以普通指针作为迭代器,因为普通指针的+或-操作只能指向连续空间的前一个地址或后一个地址,不能保证指向list的下一个节点,迭代器必须是双向迭代器,因为list容器具备有前移和后移的能力。
注:本文所列的源码出自SGI STL中的文件<stl_list.h>,对于list容器类的详细信息也可以查看《list容器库》和《MSDN的list类》
4.3.3 list的迭代器
list容器的内存空间存储不一定是连续的,则不能用普通指针做为迭代器;list容器的迭代器是双向迭代器,这也是导致list容器的排序成员函数sort()不能使用STL算法中的排序函数,因为STL中的排序算法接受的迭代器是随机访问迭代器;list容器在进行插入和拼接操作时迭代器不会失效;以下源码的对迭代器的定义:
//以下是链表List_iterator_base的迭代器 struct _List_iterator_base { //数据类型 typedef size_t size_type; typedef ptrdiff_t difference_type; //list迭代器的类型是双向迭代器bidirectional_iterator typedef bidirectional_iterator_tag iterator_category; //定义指向链表节点的指针 _List_node_base* _M_node; //构造函数 _List_iterator_base(_List_node_base* __x) : _M_node(__x) {} _List_iterator_base() {} //更新节点指针,指向直接前驱或直接后继节点 void _M_incr() { _M_node = _M_node->_M_next; } void _M_decr() { _M_node = _M_node->_M_prev; } //操作符重载 bool operator==(const _List_iterator_base& __x) const { return _M_node == __x._M_node; } bool operator!=(const _List_iterator_base& __x) const { return _M_node != __x._M_node; } }; //以下是链表List_iterator的迭代器 template<class _Tp, class _Ref, class _Ptr> struct _List_iterator : public _List_iterator_base { typedef _List_iterator<_Tp,_Tp&,_Tp*> iterator; typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator; typedef _List_iterator<_Tp,_Ref,_Ptr> _Self; typedef _Tp value_type; typedef _Ptr pointer; typedef _Ref reference; typedef _List_node<_Tp> _Node; //构造函数 _List_iterator(_Node* __x) : _List_iterator_base(__x) {} _List_iterator() {} _List_iterator(const iterator& __x) : _List_iterator_base(__x._M_node) {} //以下都是基本操作符的重载,取出节点数据 reference operator*() const { return ((_Node*) _M_node)->_M_data; } #ifndef __SGI_STL_NO_ARROW_OPERATOR pointer operator->() const { return &(operator*()); } #endif /* __SGI_STL_NO_ARROW_OPERATOR */ _Self& operator++() { this->_M_incr(); return *this; } _Self operator++(int) { _Self __tmp = *this; this->_M_incr(); return __tmp; } _Self& operator--() { this->_M_decr(); return *this; } _Self operator--(int) { _Self __tmp = *this; this->_M_decr(); return __tmp; } }; #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION //返回迭代器的类型 inline bidirectional_iterator_tag iterator_category(const _List_iterator_base&) { return bidirectional_iterator_tag(); } template <class _Tp, class _Ref, class _Ptr> inline _Tp* value_type(const _List_iterator<_Tp, _Ref, _Ptr>&) { return 0; } inline ptrdiff_t* distance_type(const _List_iterator_base&) { return 0; } #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
4.3.2 list节点和list数据结构
在list容器中,list本身和list节点是分开设计的,list节点结构是存储数据和指向相邻节点的指针;如下源码所示:
//以下是list链表节点的数据结构 struct _List_node_base { _List_node_base* _M_next;//指向直接后继节点 _List_node_base* _M_prev;//指向直接前驱节点 }; template <class _Tp> struct _List_node : public _List_node_base { _Tp _M_data;//节点存储的数据 };
list本身的数据结构是只有一个指向链表节点的指针,因为list容器是循环双向链表,则足够遍历整个链表;如下源码所示:
//以下是双向链表list类的定义,分配器_Alloc默认为第二级配置器 template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) > class list : protected _List_base<_Tp, _Alloc> { ... public: typedef _List_node<_Tp> _Node; protected: //定义指向链表节点指针 _List_node<_Tp>* _M_node; ... };
下面给出list节点和list本身的数据结构图:
4.3.5 list的构造函数和析构函数
这里把list容器的构造函数列出来讲解,使我们对list容器的构造函数进行全面的了解,以便我们对其使用。
//以下是双向链表list类的定义,分配器_Alloc默认为第二级配置器 template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) > class list : protected _List_base<_Tp, _Alloc> { ... public: //********************************************************************** /***********************以下是构造函数********************************** //*******************默认构造函数*************************************** explicit list( const Allocator& alloc = Allocator() ); //**********************具有初值和大小的构造函数************************ explicit list( size_type count, const T& value = T(), const Allocator& alloc = Allocator()); list( size_type count, const T& value, const Allocator& alloc = Allocator()); //**************只有大小的构造函数************************************** explicit list( size_type count ); //************某个范围的值为初始值的构造函数**************************** template< class InputIt > list( InputIt first, InputIt last, const Allocator& alloc = Allocator() ); //************拷贝构造函数*********************************************** list( const list& other ); */ //********************************************************************** //构造函数 //链表的默认构造函数 explicit list(const allocator_type& __a = allocator_type()) : _Base(__a) {} list(size_type __n, const _Tp& __value, const allocator_type& __a = allocator_type()) : _Base(__a) { insert(begin(), __n, __value); } explicit list(size_type __n) : _Base(allocator_type()) { insert(begin(), __n, _Tp()); } #ifdef __STL_MEMBER_TEMPLATES // We don't need any dispatching tricks here, because insert does all of // that anyway. template <class _InputIterator> list(_InputIterator __first, _InputIterator __last, const allocator_type& __a = allocator_type()) : _Base(__a) { insert(begin(), __first, __last); } #else /* __STL_MEMBER_TEMPLATES */ list(const _Tp* __first, const _Tp* __last, const allocator_type& __a = allocator_type()) : _Base(__a) { this->insert(begin(), __first, __last); } list(const_iterator __first, const_iterator __last, const allocator_type& __a = allocator_type()) : _Base(__a) { this->insert(begin(), __first, __last); } #endif /* __STL_MEMBER_TEMPLATES */ list(const list<_Tp, _Alloc>& __x) : _Base(__x.get_allocator()) { insert(begin(), __x.begin(), __x.end()); }//拷贝构造函数 ~list() { }//析构函数 //赋值操作 list<_Tp, _Alloc>& operator=(const list<_Tp, _Alloc>& __x); //构造函数,析构函数,赋值操作 定义到此结束 //******************************************************************* ... };
4.3.6 list的元素操作
list容器的成员函数为我们使用该容器提供了很大的帮助,所以这里对其进行讲解,首先给出源码的剖析,然后在对其中一些重点的成员函数进行图文讲解;具体源码剖析如下所示:
//以下是双向链表list类的定义,分配器_Alloc默认为第二级配置器 template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) > class list : protected _List_base<_Tp, _Alloc> { // requirements: ... protected: //创建值为x的节点,并返回该节点的地址 _Node* _M_create_node(const _Tp& __x) { _Node* __p = _M_get_node();//分配一个节点空间 __STL_TRY {//把x值赋予指定的地址,即是data值 _Construct(&__p->_M_data, __x); } __STL_UNWIND(_M_put_node(__p)); return __p;//返回节点地址 } //创建默认值的节点 _Node* _M_create_node() { _Node* __p = _M_get_node(); __STL_TRY { _Construct(&__p->_M_data); } __STL_UNWIND(_M_put_node(__p)); return __p; } public: //以下是迭代器的定义 iterator begin() { return (_Node*)(_M_node->_M_next); } const_iterator begin() const { return (_Node*)(_M_node->_M_next); } iterator end() { return _M_node; } const_iterator end() const { return _M_node; } reverse_iterator rbegin() { return reverse_iterator(end()); } const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } reverse_iterator rend() { return reverse_iterator(begin()); } const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } //判断链表是否为空链表 bool empty() const { return _M_node->_M_next == _M_node; } //返回链表的大小 size_type size() const { size_type __result = 0; //返回两个迭代器之间的距离 distance(begin(), end(), __result); //返回链表的元素个数 return __result; } size_type max_size() const { return size_type(-1); } //返回第一个节点数据的引用,reference相当于value_type& reference front() { return *begin(); } const_reference front() const { return *begin(); } //返回最后一个节点数据的引用 reference back() { return *(--end()); } const_reference back() const { return *(--end()); } //交换链表容器的内容 void swap(list<_Tp, _Alloc>& __x) { __STD::swap(_M_node, __x._M_node); } //********************************************************************** //*********************插入节点***************************************** //******************以下是插入节点函数的原型,也是公共接口************** //在指定的位置pos之前插入值为value的数据节点 iterator insert( iterator pos, const T& value ); iterator insert( const_iterator pos, const T& value ); //在指定的位置pos之前插入n个值为value的数据节点 void insert( iterator pos, size_type count, const T& value ); iterator insert( const_iterator pos, size_type count, const T& value ); //在指定的位置pos之前插入[first,last)之间的数据节点 template< class InputIt > void insert( iterator pos, InputIt first, InputIt last); template< class InputIt > iterator insert( const_iterator pos, InputIt first, InputIt last ); ***********************************************************************/ /**在整个链表的操作中,插入操作是非常重要的,很多成员函数会调用该函数**/ //*********************************************************************** //在指定的位置插入初始值为x的节点 iterator insert(iterator __position, const _Tp& __x) { //首先创建一个初始值为x的节点,并返回该节点的地址 _Node* __tmp = _M_create_node(__x); //调整节点指针,把新节点插入到指定位置 __tmp->_M_next = __position._M_node; __tmp->_M_prev = __position._M_node->_M_prev; __position._M_node->_M_prev->_M_next = __tmp; __position._M_node->_M_prev = __tmp; //返回新节点地址 return __tmp; } //在指定的位置插入为默认值的节点 iterator insert(iterator __position) { return insert(__position, _Tp()); } //在指定位置插入n个初始值为x的节点 void insert(iterator __pos, size_type __n, const _Tp& __x) { _M_fill_insert(__pos, __n, __x); } void _M_fill_insert(iterator __pos, size_type __n, const _Tp& __x); #ifdef __STL_MEMBER_TEMPLATES // Check whether it's an integral type. If so, it's not an iterator. //这里采用__type_traits技术 //在指定位置插入指定范围内的数据 //首先判断输入迭代器类型_InputIterator是否为整数类型 template <class _InputIterator> void insert(iterator __pos, _InputIterator __first, _InputIterator __last) { typedef typename _Is_integer<_InputIterator>::_Integral _Integral; _M_insert_dispatch(__pos, __first, __last, _Integral()); } //若输入迭代器类型_InputIterator是为整数类型,调用此函数 template<class _Integer> void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, __true_type) { _M_fill_insert(__pos, (size_type) __n, (_Tp) __x); } //若输入迭代器类型_InputIterator是不为整数类型,调用此函数 template <class _InputIterator> void _M_insert_dispatch(iterator __pos, _InputIterator __first, _InputIterator __last, __false_type); #else /* __STL_MEMBER_TEMPLATES */ void insert(iterator __position, const _Tp* __first, const _Tp* __last); void insert(iterator __position, const_iterator __first, const_iterator __last); #endif /* __STL_MEMBER_TEMPLATES */ //在链表头插入节点 void push_front(const _Tp& __x) { insert(begin(), __x); } void push_front() {insert(begin());} //在链表尾插入节点 void push_back(const _Tp& __x) { insert(end(), __x); } void push_back() {insert(end());} //*********************************************************** //********************在指定位置删除节点********************* //********************以下是删除节点的公共接口*************** /************************************************************ //删除指定位置pos的节点 iterator erase( iterator pos ); iterator erase( const_iterator pos ); //删除指定范围[first,last)的数据节点 iterator erase( iterator first, iterator last ); iterator erase( const_iterator first, const_iterator last ); ************************************************************/ //*********************************************************** //在指定位置position删除节点,并返回直接后继节点的地址 iterator erase(iterator __position) { //调整前驱和后继节点的位置 _List_node_base* __next_node = __position._M_node->_M_next; _List_node_base* __prev_node = __position._M_node->_M_prev; _Node* __n = (_Node*) __position._M_node; __prev_node->_M_next = __next_node; __next_node->_M_prev = __prev_node; _Destroy(&__n->_M_data); _M_put_node(__n); return iterator((_Node*) __next_node); } //删除两个迭代器之间的节点 iterator erase(iterator __first, iterator __last); //清空链表,这里是调用父类的clear()函数 void clear() { _Base::clear(); } //调整链表的大小 void resize(size_type __new_size, const _Tp& __x); void resize(size_type __new_size) { this->resize(__new_size, _Tp()); } //取出第一个数据节点 void pop_front() { erase(begin()); } //取出最后一个数据节点 void pop_back() { iterator __tmp = end(); erase(--__tmp); } public: // assign(), a generalized assignment member function. Two // versions: one that takes a count, and one that takes a range. // The range version is a member template, so we dispatch on whether // or not the type is an integer. /********************************************************************* //assign()函数的两个版本原型,功能是在已定义的list容器填充值 void assign( size_type count, const T& value ); template< class InputIt > void assign( InputIt first, InputIt last ); //******************************************************************* 例子: #include <list> #include <iostream> int main() { std::list<char> characters; //若定义characters时并初始化为字符b,下面的填充操作一样有效 //std::list<char>characters(5,'b') characters.assign(5, 'a'); for (char c : characters) { std::cout << c << ' '; } return 0; } 输出结果:a a a a a *********************************************************************/ //这里是第一个版本void assign( size_type count, const T& value ); void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); } //这里为什么要把_M_fill_assign这个函数放在public呢??保护起来不是更好吗?? void _M_fill_assign(size_type __n, const _Tp& __val); #ifdef __STL_MEMBER_TEMPLATES //以下是针对assign()函数的第二个版本 /* template< class InputIt > void assign( InputIt first, InputIt last ); 这里有偏特化的现象,判断输入数据类型是否为整数型别 */ template <class _InputIterator> void assign(_InputIterator __first, _InputIterator __last) { typedef typename _Is_integer<_InputIterator>::_Integral _Integral; _M_assign_dispatch(__first, __last, _Integral()); } //若输入数据类型为整数型别,则派送到此函数 template <class _Integer> void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) { _M_fill_assign((size_type) __n, (_Tp) __val); } //若输入数据类型不是整数型别,则派送到此函数 template <class _InputIterator> void _M_assign_dispatch(_InputIterator __first, _InputIterator __last, __false_type); #endif /* __STL_MEMBER_TEMPLATES */ //assign()函数定义结束 //***************************************************************** protected: //把区间[first,last)的节点数据插入到指定节点position之前,position不能在区间内部 //这个函数是list类的protected属性,不是公共接口,只为list类成员服务 //为下面拼接函数void splice()服务 void transfer(iterator __position, iterator __first, iterator __last) { if (__position != __last) { // Remove [first, last) from its old position. __last._M_node->_M_prev->_M_next = __position._M_node; __first._M_node->_M_prev->_M_next = __last._M_node; __position._M_node->_M_prev->_M_next = __first._M_node; // Splice [first, last) into its new position. _List_node_base* __tmp = __position._M_node->_M_prev; __position._M_node->_M_prev = __last._M_node->_M_prev; __last._M_node->_M_prev = __first._M_node->_M_prev; __first._M_node->_M_prev = __tmp; } } public: //********************************************************** //*******************拼接操作对外接口*********************** //把链表拼接到当前链表指定位置position之前 /*void splice(const_iterator pos, list& other); //把it在链表other所指的位置拼接到当前链表pos之前,it和pos可指向同一链表 void splice(const_iterator pos, list& other, const_iterator it); //把链表other的节点范围[first,last)拼接在当前链表所指定的位置pos之前 //[first,last)和pos可指向同一链表 void splice(const_iterator pos, list& other, const_iterator first, const_iterator last); *************************************************************/ //********************************************************** //将链表x拼接到当前链表的指定位置position之前 //这里x和*this必须不同,即是两个不同的链表 void splice(iterator __position, list& __x) { if (!__x.empty()) this->transfer(__position, __x.begin(), __x.end()); } //将i所指向的节点拼接到position所指位置之前 //注意:i和position可以指向同一个链表 void splice(iterator __position, list&, iterator __i) { iterator __j = __i; ++__j; //若i和position指向同一个链表,且指向同一位置 //或者i和position指向同一个链表,且就在position的直接前驱位置 //针对以上这两种情况,不做任何操作 if (__position == __i || __position == __j) return; //否则,进行拼接操作 this->transfer(__position, __i, __j); } //将范围[first,last)内所有节点拼接到position所指位置之前 //注意:[first,last)和position可指向同一个链表, //但是position不能在[first,last)范围之内 void splice(iterator __position, list&, iterator __first, iterator __last) { if (__first != __last) this->transfer(__position, __first, __last); } //以下是成员函数声明,定义在list类外实现 //************************************************************ //删除链表中值等于value的所有节点 void remove(const _Tp& __value); //删除连续重复的元素节点,使之唯一 //注意:是连续的重复元素 void unique(); //合并两个已排序的链表 void merge(list& __x); //反转链表容器的内容 void reverse(); //按升序排序链表内容 void sort(); #ifdef __STL_MEMBER_TEMPLATES template <class _Predicate> void remove_if(_Predicate); template <class _BinaryPredicate> void unique(_BinaryPredicate); template <class _StrictWeakOrdering> void merge(list&, _StrictWeakOrdering); template <class _StrictWeakOrdering> void sort(_StrictWeakOrdering); #endif /* __STL_MEMBER_TEMPLATES */ };
在list容器的成员函数中最重要的几个成员函数是插入insert()、擦除erase()、拼接splice()和排序sort()函数。
list插入函数insert()
下面的插入函数是在指定的位置插入初始值为value的节点,具体实现见下面源码剖析:
//*********************插入节点***************************************** /******************以下是插入节点函数的原型,也是公共接口************** //在指定的位置pos之前插入值为value的数据节点 iterator insert( iterator pos, const T& value ); iterator insert( const_iterator pos, const T& value ); //在指定的位置pos之前插入n个值为value的数据节点 void insert( iterator pos, size_type count, const T& value ); iterator insert( const_iterator pos, size_type count, const T& value ); //在指定的位置pos之前插入[first,last)之间的数据节点 template< class InputIt > void insert( iterator pos, InputIt first, InputIt last); template< class InputIt > iterator insert( const_iterator pos, InputIt first, InputIt last ); ***********************************************************************/ /**在整个链表的操作中,插入操作是非常重要的,很多成员函数会调用该函数**/ /*********************************************************************** //在指定的位置插入初始值为x的节点 iterator insert(iterator __position, const _Tp& __x) { //首先创建一个初始值为x的节点,并返回该节点的地址 _Node* __tmp = _M_create_node(__x); //调整节点指针,把新节点插入到指定位置 __tmp->_M_next = __position._M_node; __tmp->_M_prev = __position._M_node->_M_prev; __position._M_node->_M_prev->_M_next = __tmp; __position._M_node->_M_prev = __tmp; //返回新节点地址 return __tmp; }
下面举一个例子对插入函数insert()进行图文分析:假设在以下list链表中节点5之前插入节点9,具体实现见下图步骤:注:图中的箭头旁边的数字表示语句的执行步骤
第一步:首先初始化节点9,并为其分配节点空间;
第二步:调整节点5指针方向,使其与节点9连接;
第三步:调整节点5的前驱结点7指针的方向,使其与节点9连接;
list拼接函数splice()
以下对迁移操作transfer()进行分析,该函数不是公共接口,属于list容器的保护成员函数,但是它为拼接函数服务,拼接函数的核心就是迁移函数;transfer()和splice()函数源代码剖析如下:
protected: //把区间[first,last)的节点数据插入到指定节点position之前,position不能在区间内部 //这个函数是list类的protected属性,不是公共接口,只为list类成员服务 //为下面拼接函数void splice()服务 void transfer(iterator __position, iterator __first, iterator __last) { if (__position != __last) { // Remove [first, last) from its old position. __last._M_node->_M_prev->_M_next = __position._M_node; __first._M_node->_M_prev->_M_next = __last._M_node; __position._M_node->_M_prev->_M_next = __first._M_node; // Splice [first, last) into its new position. _List_node_base* __tmp = __position._M_node->_M_prev; __position._M_node->_M_prev = __last._M_node->_M_prev; __last._M_node->_M_prev = __first._M_node->_M_prev; __first._M_node->_M_prev = __tmp; } } public: //********************************************************** //*******************拼接操作对外接口*********************** //把链表拼接到当前链表指定位置position之前 /*void splice(const_iterator pos, list& other); //把it在链表other所指的位置(只有这一个结点)拼接到当前链表pos之前,it和pos可指向同一链表 void splice(const_iterator pos, list& other, const_iterator it); //把链表other的节点范围[first,last)拼接在当前链表所指定的位置pos之前 //[first,last)和pos可指向同一链表 void splice(const_iterator pos, list& other, const_iterator first, const_iterator last); *************************************************************/ //********************************************************** //将链表x拼接到当前链表的指定位置position之前 //这里x和*this必须不同,即是两个不同的链表 void splice(iterator __position, list& __x) { if (!__x.empty()) this->transfer(__position, __x.begin(), __x.end()); } //将i所指向的节点拼接到position所指位置之前 //注意:i和position可以指向同一个链表 void splice(iterator __position, list&, iterator __i) { iterator __j = __i; ++__j; //若i和position指向同一个链表,且指向同一位置 //或者i和position指向同一个链表,且就在position的直接前驱位置 //针对以上这两种情况,不做任何操作 if (__position == __i || __position == __j) return; //否则,进行拼接操作 this->transfer(__position, __i, __j); } //将范围[first,last)内所有节点拼接到position所指位置之前 //注意:[first,last)和position可指向同一个链表, //但是position不能在[first,last)范围之内 void splice(iterator __position, list&, iterator __first, iterator __last) { if (__first != __last) this->transfer(__position, __first, __last); }
List的排序算法sort()
由于STL本身的排序算法sort接受的输入迭代器是随机访问迭代器,但是双向list链表容器的访问方式是双向迭代器,因此,不能使用STL本身的排序算法sort,必须自己定义属于自己访问的排序算法。
在该排序算法的实现过程中,定义了一个类似于搬运作用的链表carry和具有中转站作用的链表counter,这里首先对counter[i]里面存储数据的规则进行分析;counter[i]里面最多存储数据个数为2^(i+1),若存储数据超过该数字,则向相邻高位进位,即把counter[i]链表里的内容都合并到counter[i+1]链表。carry负责取出原始链表的头一个数据节点和交换数据中转站作用;源码中的fill表示当前可处理数据的个数为2^fill。下面给出sort的源码分析:
//按升序进行排序,list链表的迭代器访问时双向迭代器 //因为STL的排序算法函数sort()是接受随机访问迭代器,在这里并不适合 template <class _Tp, class _Alloc> void list<_Tp, _Alloc>::sort() { // Do nothing if the list has length 0 or 1. if (_M_node->_M_next != _M_node && _M_node->_M_next->_M_next != _M_node) { list<_Tp, _Alloc> __carry;//carry链表起到搬运的作用 //counter链表是中间存储作用 /* *其中对于counter[i]里面最多的存储数据为2^(i+1)个节点 *若超出则向高位进位即counter[i+1] */ list<_Tp, _Alloc> __counter[64]; int __fill = 0; while (!empty()) {//若不是空链表 //第一步: __carry.splice(__carry.begin(), *this, begin());//把当前链表的第一个节点放在carry链表头 int __i = 0; while(__i < __fill && !__counter[__i].empty()) { //第二步: __counter[__i].merge(__carry);//把链表carry合并到counter[i] //第三步: __carry.swap(__counter[__i++]);//交换链表carry和counter[i]内容 } //第四步: __carry.swap(__counter[__i]);//交换链表carry和counter[i]内容 //第五步: if (__i == __fill) ++__fill; } for (int __i = 1; __i < __fill; ++__i) //第六步: __counter[__i].merge(__counter[__i-1]);//把低位不满足进位的剩余数据全部有序的合并到上一位 //第七步: swap(__counter[__fill-1]);//最后把已排序好的链表内容交换到当前链表 } }
从源码中,我们可以看到,第一个while循环执行的条件是当前链表必须非空,该算法的核心就是while里面的处理,嵌套while(即第二个while)执行的条件是i小于fill且counter[i]链表是非空;下面给出实例进行分析,在分析之前先做一些规定:
把第20行语句carry.splice(carry.begin(), *this, begin())标记为下图中执行的步骤1;
把第25行语句counter[i].merge(carry)标记为下图中执行的步骤2;
把第27行语句carry.swap(counter[i++])标记为下图中执行的步骤3;
把第30行语句carry.swap(counter[i])标记为下图中执行的步骤4;
把第32行语句if (i == fill) ++fill标记为下图中执行的步骤5;
把第37行for循环里面语句counter[i].merge(counter[i-1])标记为下图中执行的步骤6;
把第39行最后一个语句swap(counter[fill-1])标记为下图中执行的步骤7;
下图是待排序的原始链表:
下面是排序算法sort的执行过程,定义初始值fill=0,因为原始链表有四个节点,即原始链表非空,则执行while循环里面的语句:注:由于方便画图,下面相邻方块之间表示是双向链表的连接;白色的方块表示空链表;
第一次执行while循环:步骤1搬运链表carry取出当前链表的第一个数据节点7,初始值i=0,由于嵌套while循环条件不成立,则跳过嵌套while循环,直接进入到步骤4,交换carry和counter[i](i=0)的内容;又因为此时i=fill,则更新值++fill,即fill=1;流图如下:
第二次执行while循环:步骤1搬运链表carry取出当前链表的第一个数据节点5,初始值i=0,由于嵌套while循环条件成立,步骤2将carry链表的内容有序的合并到counter[i](i=0)链表中,再执行步骤3交换carry和counter[0]内容且i++,此时carry有两个有序的节点5和7,counter[0]为空链表,i的值为i=1;这时嵌套while循环条件不成立;跳转到步骤4交换carry和counter[1]的内容,且执行步骤5更新fill的值++fill,第二次while循环执行结束时,数据节点的状况为:carry和counter[0]内容都为空,counter[1]有两个有序的节点5和7,值fill=2;流图如下:
第三次执行while循环:步骤1搬运链表carry取出当前链表的第一个数据节点8,初始值i=0,由于嵌套while循环条件不成立,则跳过嵌套while循环,直接进入到步骤4,交换carry和counter[i](i=0)的内容;又因为此时i不等于fill,则不更新fill值,即fill=2;第三次while循环执行结束时,数据节点的状况为:carry为空,counter[0]内容为一个节点8,counter[1]有两个有序的节点5和7,值fill=2;流图如下:
第四次执行while循环:步骤1搬运链表carry取出当前链表的第一个数据节点1,初始值i=0,由于嵌套while循环条件成立,步骤2将carry链表的内容有序的合并到counter[i](i=0)链表中,再执行步骤3交换carry和counter[0]内容且i++,此时carry有两个有序的节点1和8,counter[0]为空链表,i的值为i=1;这时嵌套while循环条件依然成立;继续执行步骤2将carry的内容有序的合并到counter[1]链表中,再执行步骤3交换carry和counter[1]的内容且++i,此时,carry内容为四个有序的节点1、5、7和8,counter[0]和counter[1]的内容都为空链表,i的值为i=2;这时嵌套while循环条件不成立;跳转到步骤4交换carry和counter[2]的内容,且执行步骤5更新fill的值++fill,第四次while循环执行结束时,数据节点的状况为:carry、counter[0]和counter[1]内容都为空,counter[2]有四个有序的节点1、5、7和8,值fill=3;流图如下:
最后,由于此时当前链表为空链表,则跳出while循环,执行for语句,因为i=2之前的counter[1]和counter[0]内容都为空,则执行完for语句之后链表都没变化,然后执行步骤7交换当前链表和counter[2]的内容,执行完后,counter[2]为空,当前链表内容为四个有序的节点1、5、7和8;流程如下:
以上是list排序算法的整个流程,关键是要理解其核心,不断地更新链表内容。
- STL源码剖析 - 第4章 序列式容器 - list
- 【STL源码剖析读书笔记】【第4章】序列式容器之list和slist
- 《STL源码剖析》 第4章 序列式容器总结
- STL源码剖析 - 第4章 序列式容器 - vector
- STL源码剖析 - 第4章 序列式容器 - deque
- STL源码剖析 - 第4章 序列式容器 - stack
- STL源码剖析 - 第4章 序列式容器 - queue
- STL源码剖析 - 第4章 序列式容器 - heap
- STL源码剖析 - 第4章 序列式容器 - priority_queue
- STL源码剖析 - 第4章 序列式容器 - ilist
- STL源码剖析: 第4章 序列式容器
- 《STL源码剖析》-序列式容器(二)list容器
- STL源码剖析之序列容器list
- 《STL源码剖析》读书笔记---第4章 序列式容器
- 【STL源码剖析读书笔记】【第4章】序列式容器之vector
- 【STL源码剖析读书笔记】【第4章】序列式容器之deque
- 【STL源码剖析读书笔记】【第4章】序列式容器之stack和queue
- 【STL源码剖析读书笔记】【第4章】序列式容器之heap和priority_queue
- 预处理语句的使用-代码典型缺陷分析(一)
- 部分和问题
- 当你读书走神……
- Chromium for linux, windows, android编译
- REST API 工具箱之HAL -- Hypertext Application Language
- STL源码剖析 - 第4章 序列式容器 - list
- bool 变量与“零值”进行比较的if 语句怎么写?
- Spring AOP
- CCLabel字体与文本
- JSON异步获取网络数据
- 传输层TCP协议——TCP套接字编程
- 给C++初学者的话
- popcount函数实现
- 2014年苹果ios开发者证书申请及xcode5应用上线发布