_vector.h
来自「stl的源码」· C头文件 代码 · 共 774 行 · 第 1/2 页
H
774 行
}#if !defined(_STLP_DONT_SUP_DFLT_PARAM) && !defined(_STLP_NO_ANACHRONISMS) iterator insert(iterator __pos, const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp));#else iterator insert(iterator __pos, const _Tp& __x);#endif#if defined(_STLP_DONT_SUP_DFLT_PARAM) && !defined(_STLP_NO_ANACHRONISMS) void push_back() { push_back(_STLP_DEFAULT_CONSTRUCTED(_Tp)); } iterator insert(iterator __pos) { return insert(__pos, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }#endif void swap(_Self& __x) { _STLP_STD::swap(this->_M_start, __x._M_start); _STLP_STD::swap(this->_M_finish, __x._M_finish); this->_M_end_of_storage.swap(__x._M_end_of_storage); }#if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER) void _M_swap_workaround(_Self& __x) { swap(__x); }#endifprivate: void _M_fill_insert_aux (iterator __pos, size_type __n, const _Tp& __x, const __true_type& /*_Movable*/); void _M_fill_insert_aux (iterator __pos, size_type __n, const _Tp& __x, const __false_type& /*_Movable*/); void _M_fill_insert (iterator __pos, size_type __n, const _Tp& __x); bool _M_is_inside(const value_type& __x) const { return (&__x >= this->_M_start && &__x < this->_M_finish); }#if defined (_STLP_MEMBER_TEMPLATES) template <class _ForwardIterator> void _M_range_insert_realloc(iterator __pos, _ForwardIterator __first, _ForwardIterator __last,#else void _M_range_insert_realloc(iterator __pos, const_iterator __first, const_iterator __last,#endif size_type __n) { typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;#if !defined (_STLP_NO_MOVE_SEMANTIC) typedef typename __move_traits<_Tp>::implemented _Movable;#endif size_type __len = _M_compute_next_size(__n); pointer __new_start = this->_M_end_of_storage.allocate(__len, __len); pointer __new_finish = __new_start; _STLP_TRY { __new_finish = _STLP_PRIV __uninitialized_move(this->_M_start, __pos, __new_start, _TrivialUCopy(), _Movable()); __new_finish = uninitialized_copy(__first, __last, __new_finish); __new_finish = _STLP_PRIV __uninitialized_move(__pos, this->_M_finish, __new_finish, _TrivialUCopy(), _Movable()); } _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start,__new_finish), this->_M_end_of_storage.deallocate(__new_start,__len))) _M_clear_after_move(); _M_set(__new_start, __new_finish, __new_start + __len); }#if defined (_STLP_MEMBER_TEMPLATES) template <class _ForwardIterator> void _M_range_insert_aux(iterator __pos, _ForwardIterator __first, _ForwardIterator __last,#else void _M_range_insert_aux(iterator __pos, const_iterator __first, const_iterator __last,#endif size_type __n, const __true_type& /*_Movable*/) { iterator __src = this->_M_finish - 1; iterator __dst = __src + __n; for (; __src >= __pos; --__dst, --__src) { _STLP_STD::_Move_Construct(__dst, *__src); _STLP_STD::_Destroy_Moved(__src); } uninitialized_copy(__first, __last, __pos); this->_M_finish += __n; }#if defined (_STLP_MEMBER_TEMPLATES) template <class _ForwardIterator> void _M_range_insert_aux(iterator __pos, _ForwardIterator __first, _ForwardIterator __last,#else void _M_range_insert_aux(iterator __pos, const_iterator __first, const_iterator __last,#endif size_type __n, const __false_type& /*_Movable*/) { typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy; typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy; const size_type __elems_after = this->_M_finish - __pos; pointer __old_finish = this->_M_finish; if (__elems_after > __n) { _STLP_PRIV __ucopy_ptrs(this->_M_finish - __n, this->_M_finish, this->_M_finish, _TrivialUCopy()); this->_M_finish += __n; _STLP_PRIV __copy_backward_ptrs(__pos, __old_finish - __n, __old_finish, _TrivialCopy()); copy(__first, __last, __pos); } else {#if defined ( _STLP_MEMBER_TEMPLATES ) _ForwardIterator __mid = __first; _STLP_STD::advance(__mid, __elems_after);#else const_pointer __mid = __first + __elems_after;#endif uninitialized_copy(__mid, __last, this->_M_finish); this->_M_finish += __n - __elems_after; _STLP_PRIV __ucopy_ptrs(__pos, __old_finish, this->_M_finish, _TrivialUCopy()); this->_M_finish += __elems_after; copy(__first, __mid, __pos); } /* elems_after */ }#if defined (_STLP_MEMBER_TEMPLATES) template <class _Integer> void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val, const __true_type&) { _M_fill_insert(__pos, (size_type) __n, (_Tp) __val); } template <class _InputIterator> void _M_insert_dispatch(iterator __pos, _InputIterator __first, _InputIterator __last, const __false_type&) { _M_range_insert(__pos, __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> void insert(iterator __pos, _InputIterator __first, _InputIterator __last) { typedef typename _IsIntegral<_InputIterator>::_Ret _Integral; _M_insert_dispatch(__pos, __first, __last, _Integral()); }private: template <class _InputIterator> void _M_range_insert(iterator __pos, _InputIterator __first, _InputIterator __last, const input_iterator_tag &) { for ( ; __first != __last; ++__first) { __pos = insert(__pos, *__first); ++__pos; } } template <class _ForwardIterator> void _M_range_insert(iterator __pos, _ForwardIterator __first, _ForwardIterator __last, const forward_iterator_tag &) {#elsepublic: void insert(iterator __pos, const_iterator __first, const_iterator __last) {#endif#if !defined (_STLP_NO_MOVE_SEMANTIC) typedef typename __move_traits<_Tp>::implemented _Movable;#endif /* This method do not check self referencing. * Standard forbids it, checked by the debug mode. */ if (__first != __last) { size_type __n = _STLP_STD::distance(__first, __last); if (size_type(this->_M_end_of_storage._M_data - this->_M_finish) >= __n) { _M_range_insert_aux(__pos, __first, __last, __n, _Movable()); } else { _M_range_insert_realloc(__pos, __first, __last, __n); } } }public: void insert (iterator __pos, size_type __n, const _Tp& __x) { _M_fill_insert(__pos, __n, __x); } void pop_back() { --this->_M_finish; _STLP_STD::_Destroy(this->_M_finish); }private: iterator _M_erase(iterator __pos, const __true_type& /*_Movable*/) { _STLP_STD::_Destroy(__pos); iterator __dst = __pos, __src = __dst + 1; iterator __end = end(); for (; __src != __end; ++__dst, ++__src) { _STLP_STD::_Move_Construct(__dst, *__src); _STLP_STD::_Destroy_Moved(__src); } this->_M_finish = __dst; return __pos; } iterator _M_erase(iterator __pos, const __false_type& /*_Movable*/) { if (__pos + 1 != end()) { typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy; _STLP_PRIV __copy_ptrs(__pos + 1, this->_M_finish, __pos, _TrivialCopy()); } --this->_M_finish; _STLP_STD::_Destroy(this->_M_finish); return __pos; } iterator _M_erase(iterator __first, iterator __last, const __true_type& /*_Movable*/) { iterator __dst = __first, __src = __last; iterator __end = end(); for (; __dst != __last && __src != __end; ++__dst, ++__src) { _STLP_STD::_Destroy(__dst); _STLP_STD::_Move_Construct(__dst, *__src); } if (__dst != __last) { //There is more elements to erase than element to move: _STLP_STD::_Destroy_Range(__dst, __last); _STLP_STD::_Destroy_Moved_Range(__last, __end); } else { //There is more element to move than element to erase: for (; __src != __end; ++__dst, ++__src) { _STLP_STD::_Destroy_Moved(__dst); _STLP_STD::_Move_Construct(__dst, *__src); } _STLP_STD::_Destroy_Moved_Range(__dst, __end); } this->_M_finish = __dst; return __first; } iterator _M_erase(iterator __first, iterator __last, const __false_type& /*_Movable*/) { typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy; pointer __i = _STLP_PRIV __copy_ptrs(__last, this->_M_finish, __first, _TrivialCopy()); _STLP_STD::_Destroy_Range(__i, this->_M_finish); this->_M_finish = __i; return __first; }public: iterator erase(iterator __pos) {#if !defined (_STLP_NO_MOVE_SEMANTIC) typedef typename __move_traits<_Tp>::implemented _Movable;#endif return _M_erase(__pos, _Movable()); } iterator erase(iterator __first, iterator __last) {#if !defined (_STLP_NO_MOVE_SEMANTIC) typedef typename __move_traits<_Tp>::implemented _Movable;#endif if (__first == __last) return __first; return _M_erase(__first, __last, _Movable()); }#if !defined (_STLP_DONT_SUP_DFLT_PARAM) void resize(size_type __new_size, const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp)) {#else void resize(size_type __new_size, const _Tp& __x) {#endif /*_STLP_DONT_SUP_DFLT_PARAM*/ if (__new_size < size()) erase(begin() + __new_size, end()); else insert(end(), __new_size - size(), __x); }#if defined (_STLP_DONT_SUP_DFLT_PARAM) void resize(size_type __new_size) { resize(__new_size, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }#endif /*_STLP_DONT_SUP_DFLT_PARAM*/ void clear() { erase(begin(), end()); }private: void _M_clear() { _STLP_STD::_Destroy_Range(rbegin(), rend()); this->_M_end_of_storage.deallocate(this->_M_start, this->_M_end_of_storage._M_data - this->_M_start); } void _M_clear_after_move() { _STLP_STD::_Destroy_Moved_Range(rbegin(), rend()); this->_M_end_of_storage.deallocate(this->_M_start, this->_M_end_of_storage._M_data - this->_M_start); } void _M_set(pointer __s, pointer __f, pointer __e) { this->_M_start = __s; this->_M_finish = __f; this->_M_end_of_storage._M_data = __e; }#if defined (_STLP_MEMBER_TEMPLATES) template <class _ForwardIterator> pointer _M_allocate_and_copy(size_type& __n, _ForwardIterator __first, _ForwardIterator __last)#else /* _STLP_MEMBER_TEMPLATES */ pointer _M_allocate_and_copy(size_type& __n, const_pointer __first, const_pointer __last)#endif /* _STLP_MEMBER_TEMPLATES */ { pointer __result = this->_M_end_of_storage.allocate(__n, __n); _STLP_TRY { uninitialized_copy(__first, __last, __result); return __result; } _STLP_UNWIND(this->_M_end_of_storage.deallocate(__result, __n)) _STLP_RET_AFTER_THROW(__result) }#if defined (_STLP_MEMBER_TEMPLATES) template <class _InputIterator> void _M_range_initialize(_InputIterator __first, _InputIterator __last, const input_iterator_tag &) { for ( ; __first != __last; ++__first) push_back(*__first); } // This function is only called by the constructor. template <class _ForwardIterator> void _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last, const forward_iterator_tag &) { size_type __n = _STLP_STD::distance(__first, __last); this->_M_start = this->_M_end_of_storage.allocate(__n, __n); this->_M_end_of_storage._M_data = this->_M_start + __n; this->_M_finish = uninitialized_copy(__first, __last, this->_M_start); }#endif /* _STLP_MEMBER_TEMPLATES */};#if defined (vector)# undef vector_STLP_MOVE_TO_STD_NAMESPACE#endif_STLP_END_NAMESPACE#if !defined (_STLP_LINK_TIME_INSTANTIATION)# include <stl/_vector.c>#endif#if defined (_STLP_USE_PTR_SPECIALIZATIONS)# include <stl/pointers/_vector.h>#endif//We define the bool specialization before the debug interfave//to benefit of the debug version of vector even for the bool//specialization.#if !defined (_STLP_NO_BOOL) || !defined (_STLP_NO_EXTENSIONS)# if !defined (_STLP_INTERNAL_BVECTOR_H)# include <stl/_bvector.h># endif#endif#if defined (_STLP_DEBUG)# include <stl/debug/_vector.h>#endif_STLP_BEGIN_NAMESPACE#if !defined (_STLP_NO_BOOL) && !defined (_STLP_NO_EXTENSIONS)// This typedef is non-standard. It is provided for backward compatibility.typedef vector<bool, allocator<bool> > bit_vector;#endif#define _STLP_TEMPLATE_HEADER template <class _Tp, class _Alloc>#define _STLP_TEMPLATE_CONTAINER vector<_Tp, _Alloc>#include <stl/_relops_cont.h>#undef _STLP_TEMPLATE_CONTAINER#undef _STLP_TEMPLATE_HEADER#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)# if !defined (_STLP_NO_MOVE_SEMANTIC)template <class _Tp, class _Alloc>struct __move_traits<vector<_Tp, _Alloc> > { typedef __true_type implemented; typedef typename __move_traits<_Alloc>::complete complete;};# endif# if !defined (_STLP_DEBUG)template <class _Tp, class _Alloc>struct _DefaultZeroValue<vector<_Tp, _Alloc> >{ typedef typename __type_traits<_Alloc>::has_trivial_default_constructor _Ret; };# endif#endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */_STLP_END_NAMESPACE#endif /* _STLP_VECTOR_H */// Local Variables:// mode:C++// End:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?