VS 2010 STL list源码分析

类结构框架(debug模式)

		// TEMPLATE CLASS _List_nod
template<class _Ty,
	class _Alloc>
	class _List_nod
		: public _Container_base//即为_Container_base12
	{	// base class for _List_val to hold storage
public:
	typedef typename _Alloc::template rebind<_Ty>::other _Alty;
	typedef typename _Alty::size_type size_type;

	struct _Node;
	typedef _Node *_Nodeptr;	// _Node allocator must have ordinary pointers
	typedef _Nodeptr& _Nodepref;

    //从这个_Node结果就可以看出来,list是一个双向链表结构
	struct _Node
		{	// list node
		_Nodeptr _Next;	// successor node, or first element if head
		_Nodeptr _Prev;	// predecessor node, or last element if head
		_Ty _Myval;	// the stored value, unused if head

	private:
		_Node& operator=(const _Node&);
		};

	_List_nod(_Alloc _Al)
		: _Alnod(_Al), _Alval(_Al)
		{	// construct allocators and proxy from _Al
        //下面这几句代码无非就是分配proxy,关于proxy的东西,list不再过大设计,去vector篇幅查看
		typename _Alloc::template rebind<_Container_proxy>::other
			_Alproxy(_Alnod);
		this->_Myproxy = _Alproxy.allocate(1);
		_Cons_val(_Alproxy, this->_Myproxy, _Container_proxy());
		this->_Myproxy->_Mycont = this;
		}

	~_List_nod()
		{	// destroy proxy
		typename _Alloc::template rebind<_Container_proxy>::other
			_Alproxy(_Alnod);
		this->_Orphan_all();
		_Dest_val(_Alproxy, this->_Myproxy);
		_Alproxy.deallocate(this->_Myproxy, 1);
		this->_Myproxy = 0;
		}

    //list的实现包括头指针
	_Nodeptr _Myhead;	// pointer to head node
    //因为不像vector可以last-first就可以算size,list里必须有一个保存元素数量的成员变量
	size_type _Mysize;	// number of elements

    //若是list<int>那么内存分配器_Alloc是用来分配int的,而_Alnod是用来分配_Node的
	typename _Alloc::template rebind<_Node>::other
		_Alnod;	// allocator object for nodes
	_Alty _Alval;	// allocator object for element values
	};
		// TEMPLATE CLASS _List_val
template<class _Ty,
	class _Alloc>
	class _List_val
		: public _List_nod<_Ty, _Alloc>
	{	// base class for list to initialize storage
public:
	typedef _List_nod<_Ty, _Alloc> _Mybase;
	typedef typename _Mybase::_Nodeptr _Nodeptr;
	typedef typename _Mybase::_Nodepref _Nodepref;
	typedef typename _Alloc::template rebind<_Ty>::other _Alty;

	typedef typename _Alty::size_type size_type;
	typedef typename _Alty::difference_type difference_type;
	typedef typename _Alty::pointer pointer;
	typedef typename _Alty::const_pointer const_pointer;
	typedef typename _Alty::reference reference;
	typedef typename _Alty::const_reference const_reference;
	typedef typename _Alty::value_type value_type;
	};
		// TEMPLATE CLASS list
template<class _Ty,
	class _Ax = allocator<_Ty> >
	class list
		: public _List_val<_Ty, _Ax>
	{	// bidirectional linked list
public:
	typedef list<_Ty, _Ax> _Myt;
	typedef _List_val<_Ty, _Ax> _Mybase;
	typedef typename _Mybase::_Alty _Alloc;
	typedef typename _Mybase::_Node _Node;
	typedef typename _Mybase::_Nodeptr _Nodeptr;

	typedef _Alloc allocator_type;
	typedef typename _Alloc::size_type size_type;
	typedef typename _Alloc::difference_type difference_type;
	typedef typename _Alloc::pointer pointer;
	typedef typename _Alloc::const_pointer const_pointer;
	typedef typename _Alloc::reference reference;
	typedef typename _Alloc::const_reference const_reference;
	typedef typename _Alloc::value_type value_type;

	typedef _List_const_iterator<_Mybase>
		const_iterator;
	typedef _List_iterator<_Mybase>
		iterator;

	typedef _STD reverse_iterator<iterator> reverse_iterator;
	typedef _STD reverse_iterator<const_iterator> const_reverse_iterator;
};

迭代器

// TEMPLATE CLASS _List_unchecked_const_iterator
template<class _Mylist, class _Base = _Iterator_base0>
	class _List_unchecked_const_iterator
		: public _Iterator012<bidirectional_iterator_tag,
			typename _Mylist::value_type,
			typename _Mylist::difference_type,
			typename _Mylist::const_pointer,
			typename _Mylist::const_reference,
			_Base>
	{	// unchecked iterator for nonmutable list
public:
	typedef _List_unchecked_const_iterator<_Mylist, _Base> _Myiter;
	typedef bidirectional_iterator_tag iterator_category;

	typedef typename _Mylist::_Nodeptr _Nodeptr;
	typedef typename _Mylist::value_type value_type;
	typedef typename _Mylist::difference_type difference_type;
	typedef typename _Mylist::const_pointer pointer;
	typedef typename _Mylist::const_reference reference;

	_List_unchecked_const_iterator()
		: _Ptr(0)
		{	// construct with null node pointer
		}

	_List_unchecked_const_iterator(_Nodeptr _Pnode, const _Mylist *_Plist)
		: _Ptr(_Pnode)
		{	// construct with node pointer _Pnode
		this->_Adopt(_Plist);
		}

	reference operator*() const
		{	// return designated value
        //_List_val::_Myval返回list里保存的元素的引用
		return (_Mylist::_Myval(_Ptr));
		}

	pointer operator->() const
		{	// return pointer to class object
        //第一个*返回iterator,第二个*返回元素,在返回此元素地址
		return (&**this);
		}

	_Myiter& operator++()
		{	// preincrement
        //iterator不变,变的是_Ptr。_Ptr变为指向的下一个结点
		_Ptr = _Mylist::_Nextnode(_Ptr);
		return (*this);
		}


	bool operator==(const _Myiter& _Right) const
		{	// test for iterator equality
        //看看node结点是否相等
		return (_Ptr == _Right._Ptr);
		}

	_Nodeptr _Mynode() const
		{	// return node pointer
		return (_Ptr);
		}

	_Nodeptr _Ptr;	// pointer to node
	};
	// TEMPLATE CLASS _List_unchecked_iterator
template<class _Mylist>
	class _List_unchecked_iterator
		: public _List_unchecked_const_iterator<_Mylist>
	{	// unchecked iterator for mutable list
public:
	typedef _List_unchecked_iterator<_Mylist> _Myiter;
	typedef _List_unchecked_const_iterator<_Mylist> _Mybase;
	typedef bidirectional_iterator_tag iterator_category;

	typedef typename _Mylist::_Nodeptr _Nodeptr;
	typedef typename _Mylist::value_type value_type;
	typedef typename _Mylist::difference_type difference_type;
	typedef typename _Mylist::pointer pointer;
	typedef typename _Mylist::reference reference;
}

 

	// TEMPLATE CLASS _List_const_iterator
template<class _Mylist>
	class _List_const_iterator
		: public _List_unchecked_const_iterator<_Mylist, _Iterator_base>
	{	// iterator for nonmutable list
public:
	typedef _List_const_iterator<_Mylist> _Myiter;
	typedef _List_unchecked_const_iterator<_Mylist, _Iterator_base> _Mybase;
	typedef bidirectional_iterator_tag iterator_category;

	typedef typename _Mylist::_Nodeptr _Nodeptr;
	typedef typename _Mylist::value_type value_type;
	typedef typename _Mylist::difference_type difference_type;
	typedef typename _Mylist::const_pointer pointer;
	typedef typename _Mylist::const_reference reference;
    }

 

	// TEMPLATE CLASS _List_iterator
template<class _Mylist>
	class _List_iterator
		: public _List_const_iterator<_Mylist>
	{	// iterator for mutable list
public:
	typedef _List_iterator<_Mylist> _Myiter;
	typedef _List_const_iterator<_Mylist> _Mybase;
	typedef bidirectional_iterator_tag iterator_category;

	typedef typename _Mylist::_Nodeptr _Nodeptr;
	typedef typename _Mylist::value_type value_type;
	typedef typename _Mylist::difference_type difference_type;
	typedef typename _Mylist::pointer pointer;
	typedef typename _Mylist::reference reference;
    }

从list的构造说起

std::list<int> intList;
	list()
		: _Mybase()
		{	// construct empty list
		}
	_List_val(_Alloc _Al = _Alloc())
		: _Mybase(_Al)
		{	// construct base, and allocator from _Al
        //下面这两个变量是在_List_val的基类_List_nod里定义的。
		this->_Mysize = 0;
		this->_Myhead = this->_Alnod.allocate(1);//给头指针分配内存
		this->_Nextnode(this->_Myhead) = this->_Myhead;//前驱是自己
		this->_Prevnode(this->_Myhead) = this->_Myhead;//后继也是自己
		}

从push_back查看内部实现细节

intList.push_back(11);
    //参数为右值引用版本
	void push_back(_Ty&& _Val)
		{	// insert element at end
		_Insert_rv(end(), _STD forward<_Ty>(_Val));
		}
//前插法,把新结点插在_Where的前面。	
template<class _Valty>
		void _Insert_rv(const_iterator _Where,
		_Valty&& _Val)
		{	// insert _Val at _Where
 #if _ITERATOR_DEBUG_LEVEL == 2
		if (_Where._Getcont() != this)
			_DEBUG_ERROR("list insert iterator outside range");
 #endif /* _ITERATOR_DEBUG_LEVEL == 2 */

		_Nodeptr _Pnode = _Where._Mynode();
        //创建一个node,此node的前置结点是_Pnode的前继结点,后继是_Pnode,即_Where处结点
		_Nodeptr _Newnode =
			this->_Buynode(_Pnode, this->_Prevnode(_Pnode),
				_STD forward<_Valty>(_Val));//扩展1
        //元素个数加一
		_Incsize(1);//扩展2
        //_Where前继结点指向新结点
		this->_Prevnode(_Pnode) = _Newnode;
        //_Where前继结点的后继指向新结点 
		this->_Nextnode(this->_Prevnode(_Newnode)) = _Newnode;
		}

扩展1 

	template<class _Valty>
		_Nodeptr _Buynode(_Nodeptr _Next,
		_Nodeptr _Prev, _Valty&& _Val)
		{	// allocate a node and set links and value
		_Nodeptr _Pnode = this->_Alnod.allocate(1);

		_TRY_BEGIN
        //设置前继结点
		this->_Nextnode(_Pnode) = _Next;
		this->_Prevnode(_Pnode) = _Prev;
        //设置后继结点
		_Cons_val(this->_Alval, _STD addressof(this->_Myval(_Pnode)),
			_STD forward<_Valty>(_Val));
		_CATCH_ALL
		this->_Alnod.deallocate(_Pnode, 1);
		_RERAISE;
		_CATCH_END

		return (_Pnode);
		}

扩展2

	void _Incsize(size_type _Count)
		{	// alter element count, with checking
        //减一的原因是因为还有头结点
		if (max_size() - this->_Mysize - 1 < _Count)
			_Xlength_error("list<T> too long");
		this->_Mysize += _Count;
		}

删除机制

	iterator erase(const_iterator _Where)
		{	// erase element at _Where
 #if _ITERATOR_DEBUG_LEVEL == 2    //Debug版本
        //如果迭代器的Container不是this或者此为头指针 报错
		if (_Where._Getcont() != this || _Where._Ptr == this->_Myhead)
			_DEBUG_ERROR("list erase iterator outside range");
        //获取当前迭代器到副本_Pnode,并将_Where ++,最后返回的也是_Where++下一个iterator
		_Nodeptr _Pnode = (_Where++)._Mynode();
		_Orphan_ptr(*this, _Pnode);

 #else /* _ITERATOR_DEBUG_LEVEL == 2 */    //Release版本
		_Nodeptr _Pnode = (_Where++)._Mynode();
 #endif /* _ITERATOR_DEBUG_LEVEL == 2 */

		if (_Pnode != this->_Myhead)
			{	// not list head, safe to erase
			this->_Nextnode(this->_Prevnode(_Pnode)) =
				this->_Nextnode(_Pnode);
			this->_Prevnode(this->_Nextnode(_Pnode)) =
				this->_Prevnode(_Pnode);

			_Dest_val(this->_Alnod, _Pnode);
			this->_Alnod.deallocate(_Pnode, 1);

			--this->_Mysize;
			}
		return (_Make_iter(_Where));
		}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值