_vector.h
来自「stl的源码」· C头文件 代码 · 共 774 行 · 第 1/2 页
H
774 行
/* * * Copyright (c) 1994 * Hewlett-Packard Company * * Copyright (c) 1996,1997 * Silicon Graphics Computer Systems, Inc. * * Copyright (c) 1997 * Moscow Center for SPARC Technology * * Copyright (c) 1999 * Boris Fomitchev * * This material is provided "as is", with absolutely no warranty expressed * or implied. Any use is at your own risk. * * Permission to use or copy this software for any purpose is hereby granted * without fee, provided the above notices are retained on all copies. * Permission to modify the code and to distribute modified code is granted, * provided the above notices are retained, and a notice that the code was * modified is included with the above copyright notice. * *//* NOTE: This is an internal header file, included by other STL headers. * You should not attempt to use it directly. */#ifndef _STLP_INTERNAL_VECTOR_H#define _STLP_INTERNAL_VECTOR_H#ifndef _STLP_INTERNAL_ALGOBASE_H# include <stl/_algobase.h>#endif#ifndef _STLP_INTERNAL_ALLOC_H# include <stl/_alloc.h>#endif#ifndef _STLP_INTERNAL_ITERATOR_H# include <stl/_iterator.h>#endif#ifndef _STLP_INTERNAL_UNINITIALIZED_H# include <stl/_uninitialized.h>#endif_STLP_BEGIN_NAMESPACE// The vector base class serves one purpose, its constructor and// destructor allocate (but don't initialize) storage. This makes// exception safety easier._STLP_MOVE_TO_PRIV_NAMESPACEtemplate <class _Tp, class _Alloc>class _Vector_base {public: typedef _Vector_base<_Tp, _Alloc> _Self; _STLP_FORCE_ALLOCATORS(_Tp, _Alloc) typedef _Alloc allocator_type; typedef _Tp* pointer; typedef _STLP_alloc_proxy<pointer, _Tp, allocator_type> _AllocProxy; _Vector_base(const _Alloc& __a) : _M_start(0), _M_finish(0), _M_end_of_storage(__a, 0) {} _Vector_base(size_t __n, const _Alloc& __a) : _M_start(0), _M_finish(0), _M_end_of_storage(__a, 0) { _M_start = _M_end_of_storage.allocate(__n, __n); _M_finish = _M_start; _M_end_of_storage._M_data = _M_start + __n; _STLP_MPWFIX_TRY _STLP_MPWFIX_CATCH }#if !defined (_STLP_NO_MOVE_SEMANTIC) _Vector_base(__move_source<_Self> src) : _M_start(src.get()._M_start), _M_finish(src.get()._M_finish), _M_end_of_storage(__move_source<_AllocProxy>(src.get()._M_end_of_storage)) { //Set the source as empty: src.get()._M_finish = src.get()._M_end_of_storage._M_data = src.get()._M_start = 0; }#endif ~_Vector_base() { if (_M_start != _STLP_DEFAULT_CONSTRUCTED(pointer)) _M_end_of_storage.deallocate(_M_start, _M_end_of_storage._M_data - _M_start); }protected: void _STLP_FUNCTION_THROWS _M_throw_length_error() const; void _STLP_FUNCTION_THROWS _M_throw_out_of_range() const; pointer _M_start; pointer _M_finish; _AllocProxy _M_end_of_storage;};#if defined (_STLP_USE_PTR_SPECIALIZATIONS)# define vector _STLP_PTR_IMPL_NAME(vector)#elif defined (_STLP_DEBUG)# define vector _STLP_NON_DBG_NAME(vector)#else_STLP_MOVE_TO_STD_NAMESPACE#endiftemplate <class _Tp, _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Tp>) >class vector : protected _STLP_PRIV _Vector_base<_Tp, _Alloc>#if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (vector) , public __stlport_class<vector<_Tp, _Alloc> >#endif{private: typedef _STLP_PRIV _Vector_base<_Tp, _Alloc> _Base; typedef vector<_Tp, _Alloc> _Self;public: _STLP_FORCE_ALLOCATORS(_Tp, _Alloc) typedef typename _Base::allocator_type allocator_type; typedef _Tp value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef value_type* iterator; typedef const value_type* const_iterator; typedef value_type& reference; typedef const value_type& const_reference; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef random_access_iterator_tag _Iterator_category; _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS; allocator_type get_allocator() const { return _STLP_CONVERT_ALLOCATOR((const allocator_type&)this->_M_end_of_storage, _Tp); }private:#if defined (_STLP_NO_MOVE_SEMANTIC) typedef __false_type _Movable;#endif // handles insertions on overflow void _M_insert_overflow_aux(pointer __pos, const _Tp& __x, const __false_type& /*_Movable*/, size_type __fill_len, bool __atend); void _M_insert_overflow_aux(pointer __pos, const _Tp& __x, const __true_type& /*_Movable*/, size_type __fill_len, bool __atend) { //We need to take care of self referencing here: if (_M_is_inside(__x)) { value_type __x_copy = __x; _M_insert_overflow_aux(__pos, __x_copy, __false_type(), __fill_len, __atend); return; } _M_insert_overflow_aux(__pos, __x, __false_type(), __fill_len, __atend); } void _M_insert_overflow(pointer __pos, const _Tp& __x, const __false_type& /*_TrivialCopy*/, size_type __fill_len, bool __atend = false) {#if !defined (_STLP_NO_MOVE_SEMANTIC) typedef typename __move_traits<_Tp>::implemented _Movable;#endif _M_insert_overflow_aux(__pos, __x, _Movable(), __fill_len, __atend); } void _M_insert_overflow(pointer __pos, const _Tp& __x, const __true_type& /*_TrivialCopy*/, size_type __fill_len, bool __atend = false); void _M_range_check(size_type __n) const { if (__n >= size_type(this->_M_finish - this->_M_start)) this->_M_throw_out_of_range(); } size_type _M_compute_next_size(size_type __n) { const size_type __size = size(); if (__n > max_size() - __size) this->_M_throw_length_error(); size_type __len = __size + (max)(__n, __size); if (__len > max_size() || __len < __size) __len = max_size(); // overflow return __len; }public: iterator begin() { return this->_M_start; } const_iterator begin() const { return this->_M_start; } iterator end() { return this->_M_finish; } const_iterator end() const { return this->_M_finish; } 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()); } size_type size() const { return size_type(this->_M_finish - this->_M_start); } size_type max_size() const { size_type __vector_max_size = size_type(-1) / sizeof(_Tp); typename allocator_type::size_type __alloc_max_size = this->_M_end_of_storage.max_size(); return (__alloc_max_size < __vector_max_size)?__alloc_max_size:__vector_max_size; } size_type capacity() const { return size_type(this->_M_end_of_storage._M_data - this->_M_start); } bool empty() const { return this->_M_start == this->_M_finish; } reference operator[](size_type __n) { return *(begin() + __n); } const_reference operator[](size_type __n) const { return *(begin() + __n); } reference front() { return *begin(); } const_reference front() const { return *begin(); } reference back() { return *(end() - 1); } const_reference back() const { return *(end() - 1); } reference at(size_type __n) { _M_range_check(__n); return (*this)[__n]; } const_reference at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; }#if !defined (_STLP_DONT_SUP_DFLT_PARAM) explicit vector(const allocator_type& __a = allocator_type())#else vector() : _STLP_PRIV _Vector_base<_Tp, _Alloc>(allocator_type()) {} vector(const allocator_type& __a)#endif : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__a) {}#if !defined (_STLP_DONT_SUP_DFLT_PARAM)private: //We always call _M_initialize with only 1 parameter. Default parameter //is used to allow explicit instanciation of vector with types with no //default constructor. void _M_initialize(size_type __n, const _Tp& __val = _STLP_DEFAULT_CONSTRUCTED(_Tp)) { this->_M_finish = _STLP_PRIV __uninitialized_init(this->_M_start, __n, __val); }public: explicit vector(size_type __n) : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, allocator_type()) { _M_initialize(__n); } vector(size_type __n, const _Tp& __val, const allocator_type& __a = allocator_type())#else explicit vector(size_type __n) : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, allocator_type()) { this->_M_finish = _STLP_PRIV __uninitialized_init(this->_M_start, __n, _STLP_DEFAULT_CONSTRUCTED(_Tp)); } vector(size_type __n, const _Tp& __val) : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, allocator_type()) { this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_start, __n, __val); } vector(size_type __n, const _Tp& __val, const allocator_type& __a)#endif : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, __a) { this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_start, __n, __val); } vector(const _Self& __x) : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__x.size(), __x.get_allocator()) { typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy; this->_M_finish = _STLP_PRIV __ucopy_ptrs(__x.begin(), __x.end(), this->_M_start, _TrivialUCopy()); }#if !defined (_STLP_NO_MOVE_SEMANTIC) vector(__move_source<_Self> src) : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__move_source<_Base>(src.get())) {}#endif#if defined (_STLP_MEMBER_TEMPLATES)private: template <class _Integer> void _M_initialize_aux(_Integer __n, _Integer __val, const __true_type& /*_IsIntegral*/) { size_type __real_n = __n; this->_M_start = this->_M_end_of_storage.allocate(__n, __real_n); this->_M_end_of_storage._M_data = this->_M_start + __real_n; this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_start, __n, __val); } template <class _InputIterator> void _M_initialize_aux(_InputIterator __first, _InputIterator __last, const __false_type& /*_IsIntegral*/) { _M_range_initialize(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIterator)); }public: // Check whether it's an integral type. If so, it's not an iterator. template <class _InputIterator> vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL ) : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__a) { typedef typename _IsIntegral<_InputIterator>::_Ret _Integral; _M_initialize_aux(__first, __last, _Integral()); }# if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS) template <class _InputIterator> vector(_InputIterator __first, _InputIterator __last) : _STLP_PRIV _Vector_base<_Tp, _Alloc>(allocator_type()) { typedef typename _IsIntegral<_InputIterator>::_Ret _Integral; _M_initialize_aux(__first, __last, _Integral()); }# endif /* _STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS */#else /* _STLP_MEMBER_TEMPLATES */ vector(const _Tp* __first, const _Tp* __last, const allocator_type& __a = allocator_type()) : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__last - __first, __a) { typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy; this->_M_finish = _STLP_PRIV __ucopy_ptrs(__first, __last, this->_M_start, _TrivialUCopy()); }#endif /* _STLP_MEMBER_TEMPLATES */ //As the vector container is a back insert oriented container it //seems rather logical to destroy elements in reverse order. ~vector() { _STLP_STD::_Destroy_Range(rbegin(), rend()); } _Self& operator=(const _Self& __x); void reserve(size_type __n); // 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. void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); } void _M_fill_assign(size_type __n, const _Tp& __val);#if defined (_STLP_MEMBER_TEMPLATES) template <class _ForwardIter> void _M_assign_aux(_ForwardIter __first, _ForwardIter __last, const forward_iterator_tag &) {#else void assign(const_iterator __first, const_iterator __last) { typedef const_iterator _ForwardIter;#endif const size_type __len = _STLP_STD::distance(__first, __last); if (__len > capacity()) { size_type __n = __len; iterator __tmp = _M_allocate_and_copy(__n, __first, __last); _M_clear(); _M_set(__tmp, __tmp + __len, __tmp + __n); } else if (size() >= __len) { iterator __new_finish = copy(__first, __last, this->_M_start); _STLP_STD::_Destroy_Range(__new_finish, this->_M_finish); this->_M_finish = __new_finish; } else { _ForwardIter __mid = __first; _STLP_STD::advance(__mid, size()); _STLP_STD::copy(__first, __mid, this->_M_start); this->_M_finish = _STLP_STD::uninitialized_copy(__mid, __last, this->_M_finish); } }#if defined (_STLP_MEMBER_TEMPLATES) template <class _InputIter> void _M_assign_aux(_InputIter __first, _InputIter __last, const input_iterator_tag &) { iterator __cur = begin(); for ( ; __first != __last && __cur != end(); ++__cur, ++__first) *__cur = *__first; if (__first == __last) erase(__cur, end()); else insert(end(), __first, __last); } template <class _Integer> void _M_assign_dispatch(_Integer __n, _Integer __val, const __true_type& /*_IsIntegral*/) { _M_fill_assign(__n, __val); } template <class _InputIter> void _M_assign_dispatch(_InputIter __first, _InputIter __last, const __false_type& /*_IsIntegral*/) { _M_assign_aux(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIter)); } template <class _InputIterator> void assign(_InputIterator __first, _InputIterator __last) { typedef typename _IsIntegral<_InputIterator>::_Ret _Integral; _M_assign_dispatch(__first, __last, _Integral()); }#endif#if !defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS) void push_back(const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp)) {#else void push_back(const _Tp& __x) {#endif if (this->_M_finish != this->_M_end_of_storage._M_data) { _Copy_Construct(this->_M_finish, __x); ++this->_M_finish; } else { typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy; _M_insert_overflow(this->_M_finish, __x, _TrivialCopy(), 1, true); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?