📄 stl_deque.h
字号:
#ifdef __STL_MEMBER_TEMPLATES template <class _InputIterator> void insert(iterator __pos, _InputIterator __first, _InputIterator __last, input_iterator_tag); template <class _ForwardIterator> void insert(iterator __pos, _ForwardIterator __first, _ForwardIterator __last, forward_iterator_tag);#endif /* __STL_MEMBER_TEMPLATES */ iterator _M_insert_aux(iterator __pos, const value_type& __x); iterator _M_insert_aux(iterator __pos); void _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);#ifdef __STL_MEMBER_TEMPLATES template <class _ForwardIterator> void _M_insert_aux(iterator __pos, _ForwardIterator __first, _ForwardIterator __last, size_type __n);#else /* __STL_MEMBER_TEMPLATES */ void _M_insert_aux(iterator __pos, const value_type* __first, const value_type* __last, size_type __n); void _M_insert_aux(iterator __pos, const_iterator __first, const_iterator __last, size_type __n); #endif /* __STL_MEMBER_TEMPLATES */ iterator _M_reserve_elements_at_front(size_type __n) { size_type __vacancies = _M_start._M_cur - _M_start._M_first; if (__n > __vacancies) _M_new_elements_at_front(__n - __vacancies); return _M_start - difference_type(__n); } iterator _M_reserve_elements_at_back(size_type __n) { size_type __vacancies = (_M_finish._M_last - _M_finish._M_cur) - 1; if (__n > __vacancies) _M_new_elements_at_back(__n - __vacancies); return _M_finish + difference_type(__n); } void _M_new_elements_at_front(size_type __new_elements); void _M_new_elements_at_back(size_type __new_elements);protected: // Allocation of _M_map and nodes // Makes sure the _M_map has space for new nodes. Does not actually // add the nodes. Can invalidate _M_map pointers. (And consequently, // deque iterators.) void _M_reserve_map_at_back (size_type __nodes_to_add = 1) { if (__nodes_to_add + 1 > _M_map_size - (_M_finish._M_node - _M_map)) _M_reallocate_map(__nodes_to_add, false); } void _M_reserve_map_at_front (size_type __nodes_to_add = 1) { if (__nodes_to_add > size_type(_M_start._M_node - _M_map)) _M_reallocate_map(__nodes_to_add, true); } void _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);};// Non-inline member functions#ifdef __STL_MEMBER_TEMPLATEStemplate <class _Tp, class _Alloc>template <class _InputIter>void deque<_Tp, _Alloc> ::_M_assign_aux(_InputIter __first, _InputIter __last, 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);}#endif /* __STL_MEMBER_TEMPLATES */template <class _Tp, class _Alloc>void deque<_Tp, _Alloc>::_M_fill_insert(iterator __pos, size_type __n, const value_type& __x){ if (__pos._M_cur == _M_start._M_cur) { iterator __new_start = _M_reserve_elements_at_front(__n); __STL_TRY { uninitialized_fill(__new_start, _M_start, __x); _M_start = __new_start; } __STL_UNWIND(_M_destroy_nodes(__new_start._M_node, _M_start._M_node)); } else if (__pos._M_cur == _M_finish._M_cur) { iterator __new_finish = _M_reserve_elements_at_back(__n); __STL_TRY { uninitialized_fill(_M_finish, __new_finish, __x); _M_finish = __new_finish; } __STL_UNWIND(_M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1)); } else _M_insert_aux(__pos, __n, __x);}#ifndef __STL_MEMBER_TEMPLATES template <class _Tp, class _Alloc>void deque<_Tp, _Alloc>::insert(iterator __pos, const value_type* __first, const value_type* __last) { size_type __n = __last - __first; if (__pos._M_cur == _M_start._M_cur) { iterator __new_start = _M_reserve_elements_at_front(__n); __STL_TRY { uninitialized_copy(__first, __last, __new_start); _M_start = __new_start; } __STL_UNWIND(_M_destroy_nodes(__new_start._M_node, _M_start._M_node)); } else if (__pos._M_cur == _M_finish._M_cur) { iterator __new_finish = _M_reserve_elements_at_back(__n); __STL_TRY { uninitialized_copy(__first, __last, _M_finish); _M_finish = __new_finish; } __STL_UNWIND(_M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1)); } else _M_insert_aux(__pos, __first, __last, __n);}template <class _Tp, class _Alloc>void deque<_Tp,_Alloc>::insert(iterator __pos, const_iterator __first, const_iterator __last){ size_type __n = __last - __first; if (__pos._M_cur == _M_start._M_cur) { iterator __new_start = _M_reserve_elements_at_front(__n); __STL_TRY { uninitialized_copy(__first, __last, __new_start); _M_start = __new_start; } __STL_UNWIND(_M_destroy_nodes(__new_start._M_node, _M_start._M_node)); } else if (__pos._M_cur == _M_finish._M_cur) { iterator __new_finish = _M_reserve_elements_at_back(__n); __STL_TRY { uninitialized_copy(__first, __last, _M_finish); _M_finish = __new_finish; } __STL_UNWIND(_M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1)); } else _M_insert_aux(__pos, __first, __last, __n);}#endif /* __STL_MEMBER_TEMPLATES */template <class _Tp, class _Alloc>typename deque<_Tp,_Alloc>::iterator deque<_Tp,_Alloc>::erase(iterator __first, iterator __last){ if (__first == _M_start && __last == _M_finish) { clear(); return _M_finish; } else { difference_type __n = __last - __first; difference_type __elems_before = __first - _M_start; if (__elems_before < difference_type((this->size() - __n) / 2)) { copy_backward(_M_start, __first, __last); iterator __new_start = _M_start + __n; destroy(_M_start, __new_start); _M_destroy_nodes(__new_start._M_node, _M_start._M_node); _M_start = __new_start; } else { copy(__last, _M_finish, __first); iterator __new_finish = _M_finish - __n; destroy(__new_finish, _M_finish); _M_destroy_nodes(__new_finish._M_node + 1, _M_finish._M_node + 1); _M_finish = __new_finish; } return _M_start + __elems_before; }}template <class _Tp, class _Alloc> void deque<_Tp,_Alloc>::clear(){ for (_Map_pointer __node = _M_start._M_node + 1; __node < _M_finish._M_node; ++__node) { destroy(*__node, *__node + _S_buffer_size()); _M_deallocate_node(*__node); } if (_M_start._M_node != _M_finish._M_node) { destroy(_M_start._M_cur, _M_start._M_last); destroy(_M_finish._M_first, _M_finish._M_cur); _M_deallocate_node(_M_finish._M_first); } else destroy(_M_start._M_cur, _M_finish._M_cur); _M_finish = _M_start;}// Precondition: _M_start and _M_finish have already been initialized,// but none of the deque's elements have yet been constructed.template <class _Tp, class _Alloc>void deque<_Tp,_Alloc>::_M_fill_initialize(const value_type& __value) { _Map_pointer __cur; __STL_TRY { for (__cur = _M_start._M_node; __cur < _M_finish._M_node; ++__cur) uninitialized_fill(*__cur, *__cur + _S_buffer_size(), __value); uninitialized_fill(_M_finish._M_first, _M_finish._M_cur, __value); } __STL_UNWIND(destroy(_M_start, iterator(*__cur, __cur)));}#ifdef __STL_MEMBER_TEMPLATES template <class _Tp, class _Alloc> template <class _InputIterator>void deque<_Tp,_Alloc>::_M_range_initialize(_InputIterator __first, _InputIterator __last, input_iterator_tag){ _M_initialize_map(0); __STL_TRY { for ( ; __first != __last; ++__first) push_back(*__first); } __STL_UNWIND(clear());}template <class _Tp, class _Alloc> template <class _ForwardIterator>void deque<_Tp,_Alloc>::_M_range_initialize(_ForwardIterator __first, _ForwardIterator __last, forward_iterator_tag){ size_type __n = 0; distance(__first, __last, __n); _M_initialize_map(__n); _Map_pointer __cur_node; __STL_TRY { for (__cur_node = _M_start._M_node; __cur_node < _M_finish._M_node; ++__cur_node) { _ForwardIterator __mid = __first; advance(__mid, _S_buffer_size()); uninitialized_copy(__first, __mid, *__cur_node); __first = __mid; } uninitialized_copy(__first, __last, _M_finish._M_first); } __STL_UNWIND(destroy(_M_start, iterator(*__cur_node, __cur_node)));}#endif /* __STL_MEMBER_TEMPLATES */// Called only if _M_finish._M_cur == _M_finish._M_last - 1.template <class _Tp, class _Alloc>void deque<_Tp,_Alloc>::_M_push_back_aux(const value_type& __t){ value_type __t_copy = __t; _M_reserve_map_at_back(); *(_M_finish._M_node + 1) = _M_allocate_node(); __STL_TRY { construct(_M_finish._M_cur, __t_copy); _M_finish._M_set_node(_M_finish._M_node + 1); _M_finish._M_cur = _M_finish._M_first; } __STL_UNWIND(_M_deallocate_node(*(_M_finish._M_node + 1)));}// Called only if _M_finish._M_cur == _M_finish._M_last - 1.template <class _Tp, class _Alloc>void deque<_Tp,_Alloc>::_M_push_back_aux(){ _M_reserve_map_at_back(); *(_M_finish._M_node + 1) = _M_allocate_node(); __STL_TRY { construct(_M_finish._M_cur); _M_finish._M_set_node(_M_finish._M_node + 1); _M_finish._M_cur = _M_finish._M_first; } __STL_UNWIND(_M_deallocate_node(*(_M_finish._M_node + 1)));}// Called only if _M_start._M_cur == _M_start._M_first.template <class _Tp, class _Alloc>void deque<_Tp,_Alloc>::_M_push_front_aux(const value_type& __t){ value_type __t_copy = __t; _M_reserve_map_at_front(); *(_M_start._M_node - 1) = _M_allocate_node(); __STL_TRY { _M_start._M_set_node(_M_start._M_node - 1); _M_start._M_cur = _M_start._M_last - 1; construct(_M_start._M_cur, __t_copy); } __STL_UNWIND((++_M_start, _M_deallocate_node(*(_M_start._M_node - 1))));} // Called only if _M_start._M_cur == _M_start._M_first.template <class _Tp, class _Alloc>void deque<_Tp,_Alloc>::_M_push_front_aux(){ _M_reserve_map_at_front(); *(_M_start._M_node - 1) = _M_allocate_node(); __STL_TRY { _M_start._M_set_node(_M_start._M_node - 1); _M_start._M_cur = _M_start._M_last - 1; construct(_M_start._M_cur); } __STL_UNWIND((++_M_start, _M_deallocate_node(*(_M_start._M_node - 1))));} // Called only if _M_finish._M_cur == _M_finish._M_first.template <class _Tp, class _Alloc>void deque<_Tp,_Alloc>::_M_pop_back_aux(){ _M_deallocate_node(_M_finish._M_first); _M_finish._M_set_node(_M_finish._M_node - 1); _M_finish._M_cur = _M_finish._M_last - 1; destroy(_M_finish._M_cur);}// Called only if _M_start._M_cur == _M_start._M_last - 1. Note that // if the deque has at least one element (a precondition for this member // function), and if _M_start._M_cur == _M_start._M_last, then the deque // must have at least two nodes.template <class _Tp, class _Alloc>void deque<_Tp,_Alloc>::_M_pop_front_aux(){ destroy(_M_start._M_cur); _M_deallocate_node(_M_start._M_first); _M_start._M_set_node(_M_start._M_node + 1); _M_start._M_cur = _M_start._M_first;} #ifdef __STL_MEMBER_TEMPLATES template <class _Tp, class _Alloc> template <class _InputIterator>void deque<_Tp,_Alloc>::insert(iterator __pos, _InputIterator __first, _InputIterator __last, input_iterator_tag){ copy(__first, __last, inserter(*this, __pos));}template <class _Tp, class _Alloc> template <class _ForwardIterator>voiddeque<_Tp,_Alloc>::insert(iterator __pos, _ForwardIterator __first, _ForwardIterator __last, forward_iterator_tag) { size_type __n = 0; distance(__first, __last, __n); if (__pos._M_cur == _M_start._M_cur) { iterator __new_start = _M_reserve_elements_at_front(__n); __STL_TRY { uninitialized_copy(__first, __last, __new_start); _M_start = __new_start; } __STL_UNWIND(_M_destroy_nodes(__new_start._M_node, _M_start._M_node)); } else if (__pos._M_cur == _M_finish._M_cur) { iterator __new_finish = _M_reserve_elements_at_back(__n); __STL_TRY { uninitialized_copy(__first, __last, _M_finish); _M_finish = __new_finish; } __STL_UNWIND(_M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1)); } else _M_insert_aux(__pos, __first, __last, __n);}#endif /* __STL_MEMBER_TEMPLATES */template <class _Tp, class _Alloc>typename deque<_Tp, _Alloc>::iteratordeque<_Tp,_Alloc>::_M_insert_aux(iterator __pos, const value_type& __x){ difference_type __index = __pos - _M_start; value_type __x_copy = __x; if (size_type(__index) < this->size() / 2) { push_front(front()); iterator __front1 = _M_start; ++__front1; iterator __front2 = __front1; ++__front2; __pos = _M_start + __index; iterator __pos1 = __pos; ++__pos1; copy(__front2, __pos1, __front1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -