头文件
c++/5/bits/stl_vector.h
源代码解析
/// See bits/stl_deque.h's _Deque_base for an explanation.
// 数据类型,构造器类型
template<typename _Tp, typename _Alloc>
struct _Vector_base
{
// 构造器类型
typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
rebind<_Tp>::other _Tp_alloc_type;
// 数据类型指针
typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer
pointer;
// vector 实现,是继承构造器的
struct _Vector_impl
: public _Tp_alloc_type
{
//开始位置
pointer _M_start;
//结束位置
pointer _M_finish;
//数组容量
pointer _M_end_of_storage;
//默认构造函数,_M_start=0;_M_finish=0;_M_end_of_storage
//调用 构造器默认构造函数
_Vector_impl()
: _Tp_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage()
{ }
//构造,_M_start=0;_M_finish=0;_M_end_of_storage
//调用 构造器 拷贝构造
_Vector_impl(_Tp_alloc_type const& __a) _GLIBCXX_NOEXCEPT
: _Tp_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage()
{ }
#if __cplusplus >= 201103L
//移动构造,_M_start=0;_M_finish=0;_M_end_of_storage
//
_Vector_impl(_Tp_alloc_type&& __a) noexcept
: _Tp_alloc_type(std::move(__a)),
_M_start(), _M_finish(), _M_end_of_storage()
{ }
#endif
//交换数据
void _M_swap_data(_Vector_impl& __x) _GLIBCXX_NOEXCEPT
{
std::swap(_M_start, __x._M_start);
std::swap(_M_finish, __x._M_finish);
std::swap(_M_end_of_storage, __x._M_end_of_storage);
}
};
public:
typedef _Alloc allocator_type;
//获取构造器 引用
_Tp_alloc_type&
_M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
{ return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
//获取构造器 常量引用
const _Tp_alloc_type&
_M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
{ return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
//获取构造器 拷贝
allocator_type
get_allocator() const _GLIBCXX_NOEXCEPT
{ return allocator_type(_M_get_Tp_allocator()); }
//默认构造
_Vector_base()
: _M_impl() { }
//构造 型参为构造器
_Vector_base(const allocator_type& __a) _GLIBCXX_NOEXCEPT
: _M_impl(__a) { }
//构造 型参为 数据类型数组的大小,采用 默认构造器
_Vector_base(size_t __n)
: _M_impl()
{ _M_create_storage(__n); }
//构造 型参为 数据类型数组的大小,构造器
_Vector_base(size_t __n, const allocator_type& __a)
: _M_impl(__a)
{ _M_create_storage(__n); }
#if __cplusplus >= 201103L
//构造,型参为 构造器的右值
_Vector_base(_Tp_alloc_type&& __a) noexcept
: _M_impl(std::move(__a)) { }
//移动构造
_Vector_base(_Vector_base&& __x) noexcept
: _M_impl(std::move(__x._M_get_Tp_allocator()))
{ this->_M_impl._M_swap_data(__x._M_impl); }
_Vector_base(_Vector_base&& __x, const allocator_type& __a)
: _M_impl(__a)
{
if (__x.get_allocator() == __a)
this->_M_impl._M_swap_data(__x._M_impl);
else
{
size_t __n = __x._M_impl._M_finish - __x._M_impl._M_start;
_M_create_storage(__n);
}
}
#endif
//析构
~_Vector_base() _GLIBCXX_NOEXCEPT
{ _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
- this->_M_impl._M_start); }
public:
//成员 变量
_Vector_impl _M_impl;
// 申请 数组大小
pointer
_M_allocate(size_t __n)
{
//
typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr;
return __n != 0 ? _Tr::allocate(_M_impl, __n) : pointer();
}
//释放 数组内存
void
_M_deallocate(pointer __p, size_t __n)
{
typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr;
if (__p)
_Tr::deallocate(_M_impl, __p, __n);
}
private:
//申请数组内存,
//_M_impl._M_start为数组 首地址
//_M_impl._M_finish为数组 首地址
//_M_end_of_storage为数组的 首地址+n
void
_M_create_storage(size_t __n)
{
this->_M_impl._M_start = this->_M_allocate(__n);
this->_M_impl._M_finish = this->_M_impl._M_start;
this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
}
};