1 // Deque implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001-2016 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/deque.tcc
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{deque}
59 namespace std _GLIBCXX_VISIBILITY(default)
61 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
63 #if __cplusplus >= 201103L
64 template <typename _Tp, typename _Alloc>
67 _M_default_initialize()
72 for (__cur = this->_M_impl._M_start._M_node;
73 __cur < this->_M_impl._M_finish._M_node;
75 std::__uninitialized_default_a(*__cur, *__cur + _S_buffer_size(),
76 _M_get_Tp_allocator());
77 std::__uninitialized_default_a(this->_M_impl._M_finish._M_first,
78 this->_M_impl._M_finish._M_cur,
79 _M_get_Tp_allocator());
83 std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
84 _M_get_Tp_allocator());
85 __throw_exception_again;
90 template <typename _Tp, typename _Alloc>
93 operator=(const deque& __x)
97 #if __cplusplus >= 201103L
98 if (_Alloc_traits::_S_propagate_on_copy_assign())
100 if (!_Alloc_traits::_S_always_equal()
101 && _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
103 // Replacement allocator cannot free existing storage,
104 // so deallocate everything and take copy of __x's data.
105 _M_replace_map(__x, __x.get_allocator());
106 std::__alloc_on_copy(_M_get_Tp_allocator(),
107 __x._M_get_Tp_allocator());
110 std::__alloc_on_copy(_M_get_Tp_allocator(),
111 __x._M_get_Tp_allocator());
114 const size_type __len = size();
115 if (__len >= __x.size())
116 _M_erase_at_end(std::copy(__x.begin(), __x.end(),
117 this->_M_impl._M_start));
120 const_iterator __mid = __x.begin() + difference_type(__len);
121 std::copy(__x.begin(), __mid, this->_M_impl._M_start);
122 _M_range_insert_aux(this->_M_impl._M_finish, __mid, __x.end(),
123 std::random_access_iterator_tag());
129 #if __cplusplus >= 201103L
130 template<typename _Tp, typename _Alloc>
131 template<typename... _Args>
132 #if __cplusplus > 201402L
133 typename deque<_Tp, _Alloc>::reference
138 emplace_front(_Args&&... __args)
140 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
142 _Alloc_traits::construct(this->_M_impl,
143 this->_M_impl._M_start._M_cur - 1,
144 std::forward<_Args>(__args)...);
145 --this->_M_impl._M_start._M_cur;
148 _M_push_front_aux(std::forward<_Args>(__args)...);
149 #if __cplusplus > 201402L
154 template<typename _Tp, typename _Alloc>
155 template<typename... _Args>
156 #if __cplusplus > 201402L
157 typename deque<_Tp, _Alloc>::reference
162 emplace_back(_Args&&... __args)
164 if (this->_M_impl._M_finish._M_cur
165 != this->_M_impl._M_finish._M_last - 1)
167 _Alloc_traits::construct(this->_M_impl,
168 this->_M_impl._M_finish._M_cur,
169 std::forward<_Args>(__args)...);
170 ++this->_M_impl._M_finish._M_cur;
173 _M_push_back_aux(std::forward<_Args>(__args)...);
174 #if __cplusplus > 201402L
180 #if __cplusplus >= 201103L
181 template<typename _Tp, typename _Alloc>
182 template<typename... _Args>
183 typename deque<_Tp, _Alloc>::iterator
185 emplace(const_iterator __position, _Args&&... __args)
187 if (__position._M_cur == this->_M_impl._M_start._M_cur)
189 emplace_front(std::forward<_Args>(__args)...);
190 return this->_M_impl._M_start;
192 else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
194 emplace_back(std::forward<_Args>(__args)...);
195 iterator __tmp = this->_M_impl._M_finish;
200 return _M_insert_aux(__position._M_const_cast(),
201 std::forward<_Args>(__args)...);
205 template <typename _Tp, typename _Alloc>
206 typename deque<_Tp, _Alloc>::iterator
208 #if __cplusplus >= 201103L
209 insert(const_iterator __position, const value_type& __x)
211 insert(iterator __position, const value_type& __x)
214 if (__position._M_cur == this->_M_impl._M_start._M_cur)
217 return this->_M_impl._M_start;
219 else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
222 iterator __tmp = this->_M_impl._M_finish;
227 return _M_insert_aux(__position._M_const_cast(), __x);
230 template <typename _Tp, typename _Alloc>
231 typename deque<_Tp, _Alloc>::iterator
233 _M_erase(iterator __position)
235 iterator __next = __position;
237 const difference_type __index = __position - begin();
238 if (static_cast<size_type>(__index) < (size() >> 1))
240 if (__position != begin())
241 _GLIBCXX_MOVE_BACKWARD3(begin(), __position, __next);
247 _GLIBCXX_MOVE3(__next, end(), __position);
250 return begin() + __index;
253 template <typename _Tp, typename _Alloc>
254 typename deque<_Tp, _Alloc>::iterator
256 _M_erase(iterator __first, iterator __last)
258 if (__first == __last)
260 else if (__first == begin() && __last == end())
267 const difference_type __n = __last - __first;
268 const difference_type __elems_before = __first - begin();
269 if (static_cast<size_type>(__elems_before) <= (size() - __n) / 2)
271 if (__first != begin())
272 _GLIBCXX_MOVE_BACKWARD3(begin(), __first, __last);
273 _M_erase_at_begin(begin() + __n);
278 _GLIBCXX_MOVE3(__last, end(), __first);
279 _M_erase_at_end(end() - __n);
281 return begin() + __elems_before;
285 template <typename _Tp, class _Alloc>
286 template <typename _InputIterator>
289 _M_assign_aux(_InputIterator __first, _InputIterator __last,
290 std::input_iterator_tag)
292 iterator __cur = begin();
293 for (; __first != __last && __cur != end(); ++__cur, ++__first)
295 if (__first == __last)
296 _M_erase_at_end(__cur);
298 _M_range_insert_aux(end(), __first, __last,
299 std::__iterator_category(__first));
302 template <typename _Tp, typename _Alloc>
305 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
307 if (__pos._M_cur == this->_M_impl._M_start._M_cur)
309 iterator __new_start = _M_reserve_elements_at_front(__n);
312 std::__uninitialized_fill_a(__new_start, this->_M_impl._M_start,
313 __x, _M_get_Tp_allocator());
314 this->_M_impl._M_start = __new_start;
318 _M_destroy_nodes(__new_start._M_node,
319 this->_M_impl._M_start._M_node);
320 __throw_exception_again;
323 else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
325 iterator __new_finish = _M_reserve_elements_at_back(__n);
328 std::__uninitialized_fill_a(this->_M_impl._M_finish,
330 _M_get_Tp_allocator());
331 this->_M_impl._M_finish = __new_finish;
335 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
336 __new_finish._M_node + 1);
337 __throw_exception_again;
341 _M_insert_aux(__pos, __n, __x);
344 #if __cplusplus >= 201103L
345 template <typename _Tp, typename _Alloc>
348 _M_default_append(size_type __n)
352 iterator __new_finish = _M_reserve_elements_at_back(__n);
355 std::__uninitialized_default_a(this->_M_impl._M_finish,
357 _M_get_Tp_allocator());
358 this->_M_impl._M_finish = __new_finish;
362 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
363 __new_finish._M_node + 1);
364 __throw_exception_again;
369 template <typename _Tp, typename _Alloc>
374 const difference_type __front_capacity
375 = (this->_M_impl._M_start._M_cur - this->_M_impl._M_start._M_first);
376 if (__front_capacity == 0)
379 const difference_type __back_capacity
380 = (this->_M_impl._M_finish._M_last - this->_M_impl._M_finish._M_cur);
381 if (__front_capacity + __back_capacity < _S_buffer_size())
384 return std::__shrink_to_fit_aux<deque>::_S_do_it(*this);
388 template <typename _Tp, typename _Alloc>
391 _M_fill_initialize(const value_type& __value)
396 for (__cur = this->_M_impl._M_start._M_node;
397 __cur < this->_M_impl._M_finish._M_node;
399 std::__uninitialized_fill_a(*__cur, *__cur + _S_buffer_size(),
400 __value, _M_get_Tp_allocator());
401 std::__uninitialized_fill_a(this->_M_impl._M_finish._M_first,
402 this->_M_impl._M_finish._M_cur,
403 __value, _M_get_Tp_allocator());
407 std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
408 _M_get_Tp_allocator());
409 __throw_exception_again;
413 template <typename _Tp, typename _Alloc>
414 template <typename _InputIterator>
417 _M_range_initialize(_InputIterator __first, _InputIterator __last,
418 std::input_iterator_tag)
420 this->_M_initialize_map(0);
423 for (; __first != __last; ++__first)
424 #if __cplusplus >= 201103L
425 emplace_back(*__first);
433 __throw_exception_again;
437 template <typename _Tp, typename _Alloc>
438 template <typename _ForwardIterator>
441 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
442 std::forward_iterator_tag)
444 const size_type __n = std::distance(__first, __last);
445 this->_M_initialize_map(__n);
447 _Map_pointer __cur_node;
450 for (__cur_node = this->_M_impl._M_start._M_node;
451 __cur_node < this->_M_impl._M_finish._M_node;
454 _ForwardIterator __mid = __first;
455 std::advance(__mid, _S_buffer_size());
456 std::__uninitialized_copy_a(__first, __mid, *__cur_node,
457 _M_get_Tp_allocator());
460 std::__uninitialized_copy_a(__first, __last,
461 this->_M_impl._M_finish._M_first,
462 _M_get_Tp_allocator());
466 std::_Destroy(this->_M_impl._M_start,
467 iterator(*__cur_node, __cur_node),
468 _M_get_Tp_allocator());
469 __throw_exception_again;
473 // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_last - 1.
474 template<typename _Tp, typename _Alloc>
475 #if __cplusplus >= 201103L
476 template<typename... _Args>
479 _M_push_back_aux(_Args&&... __args)
483 _M_push_back_aux(const value_type& __t)
486 _M_reserve_map_at_back();
487 *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node();
490 #if __cplusplus >= 201103L
491 _Alloc_traits::construct(this->_M_impl,
492 this->_M_impl._M_finish._M_cur,
493 std::forward<_Args>(__args)...);
495 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __t);
497 this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node
499 this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_first;
503 _M_deallocate_node(*(this->_M_impl._M_finish._M_node + 1));
504 __throw_exception_again;
508 // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_first.
509 template<typename _Tp, typename _Alloc>
510 #if __cplusplus >= 201103L
511 template<typename... _Args>
514 _M_push_front_aux(_Args&&... __args)
518 _M_push_front_aux(const value_type& __t)
521 _M_reserve_map_at_front();
522 *(this->_M_impl._M_start._M_node - 1) = this->_M_allocate_node();
525 this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node
527 this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_last - 1;
528 #if __cplusplus >= 201103L
529 _Alloc_traits::construct(this->_M_impl,
530 this->_M_impl._M_start._M_cur,
531 std::forward<_Args>(__args)...);
533 this->_M_impl.construct(this->_M_impl._M_start._M_cur, __t);
538 ++this->_M_impl._M_start;
539 _M_deallocate_node(*(this->_M_impl._M_start._M_node - 1));
540 __throw_exception_again;
544 // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_first.
545 template <typename _Tp, typename _Alloc>
546 void deque<_Tp, _Alloc>::
549 _M_deallocate_node(this->_M_impl._M_finish._M_first);
550 this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node - 1);
551 this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_last - 1;
552 _Alloc_traits::destroy(_M_get_Tp_allocator(),
553 this->_M_impl._M_finish._M_cur);
556 // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_last - 1.
557 // Note that if the deque has at least one element (a precondition for this
558 // member function), and if
559 // _M_impl._M_start._M_cur == _M_impl._M_start._M_last,
560 // then the deque must have at least two nodes.
561 template <typename _Tp, typename _Alloc>
562 void deque<_Tp, _Alloc>::
565 _Alloc_traits::destroy(_M_get_Tp_allocator(),
566 this->_M_impl._M_start._M_cur);
567 _M_deallocate_node(this->_M_impl._M_start._M_first);
568 this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node + 1);
569 this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_first;
572 template <typename _Tp, typename _Alloc>
573 template <typename _InputIterator>
576 _M_range_insert_aux(iterator __pos,
577 _InputIterator __first, _InputIterator __last,
578 std::input_iterator_tag)
579 { std::copy(__first, __last, std::inserter(*this, __pos)); }
581 template <typename _Tp, typename _Alloc>
582 template <typename _ForwardIterator>
585 _M_range_insert_aux(iterator __pos,
586 _ForwardIterator __first, _ForwardIterator __last,
587 std::forward_iterator_tag)
589 const size_type __n = std::distance(__first, __last);
590 if (__pos._M_cur == this->_M_impl._M_start._M_cur)
592 iterator __new_start = _M_reserve_elements_at_front(__n);
595 std::__uninitialized_copy_a(__first, __last, __new_start,
596 _M_get_Tp_allocator());
597 this->_M_impl._M_start = __new_start;
601 _M_destroy_nodes(__new_start._M_node,
602 this->_M_impl._M_start._M_node);
603 __throw_exception_again;
606 else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
608 iterator __new_finish = _M_reserve_elements_at_back(__n);
611 std::__uninitialized_copy_a(__first, __last,
612 this->_M_impl._M_finish,
613 _M_get_Tp_allocator());
614 this->_M_impl._M_finish = __new_finish;
618 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
619 __new_finish._M_node + 1);
620 __throw_exception_again;
624 _M_insert_aux(__pos, __first, __last, __n);
627 template<typename _Tp, typename _Alloc>
628 #if __cplusplus >= 201103L
629 template<typename... _Args>
630 typename deque<_Tp, _Alloc>::iterator
632 _M_insert_aux(iterator __pos, _Args&&... __args)
634 value_type __x_copy(std::forward<_Args>(__args)...); // XXX copy
636 typename deque<_Tp, _Alloc>::iterator
638 _M_insert_aux(iterator __pos, const value_type& __x)
640 value_type __x_copy = __x; // XXX copy
642 difference_type __index = __pos - this->_M_impl._M_start;
643 if (static_cast<size_type>(__index) < size() / 2)
645 push_front(_GLIBCXX_MOVE(front()));
646 iterator __front1 = this->_M_impl._M_start;
648 iterator __front2 = __front1;
650 __pos = this->_M_impl._M_start + __index;
651 iterator __pos1 = __pos;
653 _GLIBCXX_MOVE3(__front2, __pos1, __front1);
657 push_back(_GLIBCXX_MOVE(back()));
658 iterator __back1 = this->_M_impl._M_finish;
660 iterator __back2 = __back1;
662 __pos = this->_M_impl._M_start + __index;
663 _GLIBCXX_MOVE_BACKWARD3(__pos, __back2, __back1);
665 *__pos = _GLIBCXX_MOVE(__x_copy);
669 template <typename _Tp, typename _Alloc>
672 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x)
674 const difference_type __elems_before = __pos - this->_M_impl._M_start;
675 const size_type __length = this->size();
676 value_type __x_copy = __x;
677 if (__elems_before < difference_type(__length / 2))
679 iterator __new_start = _M_reserve_elements_at_front(__n);
680 iterator __old_start = this->_M_impl._M_start;
681 __pos = this->_M_impl._M_start + __elems_before;
684 if (__elems_before >= difference_type(__n))
686 iterator __start_n = (this->_M_impl._M_start
687 + difference_type(__n));
688 std::__uninitialized_move_a(this->_M_impl._M_start,
689 __start_n, __new_start,
690 _M_get_Tp_allocator());
691 this->_M_impl._M_start = __new_start;
692 _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
693 std::fill(__pos - difference_type(__n), __pos, __x_copy);
697 std::__uninitialized_move_fill(this->_M_impl._M_start,
699 this->_M_impl._M_start,
701 _M_get_Tp_allocator());
702 this->_M_impl._M_start = __new_start;
703 std::fill(__old_start, __pos, __x_copy);
708 _M_destroy_nodes(__new_start._M_node,
709 this->_M_impl._M_start._M_node);
710 __throw_exception_again;
715 iterator __new_finish = _M_reserve_elements_at_back(__n);
716 iterator __old_finish = this->_M_impl._M_finish;
717 const difference_type __elems_after =
718 difference_type(__length) - __elems_before;
719 __pos = this->_M_impl._M_finish - __elems_after;
722 if (__elems_after > difference_type(__n))
724 iterator __finish_n = (this->_M_impl._M_finish
725 - difference_type(__n));
726 std::__uninitialized_move_a(__finish_n,
727 this->_M_impl._M_finish,
728 this->_M_impl._M_finish,
729 _M_get_Tp_allocator());
730 this->_M_impl._M_finish = __new_finish;
731 _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
732 std::fill(__pos, __pos + difference_type(__n), __x_copy);
736 std::__uninitialized_fill_move(this->_M_impl._M_finish,
737 __pos + difference_type(__n),
739 this->_M_impl._M_finish,
740 _M_get_Tp_allocator());
741 this->_M_impl._M_finish = __new_finish;
742 std::fill(__pos, __old_finish, __x_copy);
747 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
748 __new_finish._M_node + 1);
749 __throw_exception_again;
754 template <typename _Tp, typename _Alloc>
755 template <typename _ForwardIterator>
758 _M_insert_aux(iterator __pos,
759 _ForwardIterator __first, _ForwardIterator __last,
762 const difference_type __elemsbefore = __pos - this->_M_impl._M_start;
763 const size_type __length = size();
764 if (static_cast<size_type>(__elemsbefore) < __length / 2)
766 iterator __new_start = _M_reserve_elements_at_front(__n);
767 iterator __old_start = this->_M_impl._M_start;
768 __pos = this->_M_impl._M_start + __elemsbefore;
771 if (__elemsbefore >= difference_type(__n))
773 iterator __start_n = (this->_M_impl._M_start
774 + difference_type(__n));
775 std::__uninitialized_move_a(this->_M_impl._M_start,
776 __start_n, __new_start,
777 _M_get_Tp_allocator());
778 this->_M_impl._M_start = __new_start;
779 _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
780 std::copy(__first, __last, __pos - difference_type(__n));
784 _ForwardIterator __mid = __first;
785 std::advance(__mid, difference_type(__n) - __elemsbefore);
786 std::__uninitialized_move_copy(this->_M_impl._M_start,
787 __pos, __first, __mid,
789 _M_get_Tp_allocator());
790 this->_M_impl._M_start = __new_start;
791 std::copy(__mid, __last, __old_start);
796 _M_destroy_nodes(__new_start._M_node,
797 this->_M_impl._M_start._M_node);
798 __throw_exception_again;
803 iterator __new_finish = _M_reserve_elements_at_back(__n);
804 iterator __old_finish = this->_M_impl._M_finish;
805 const difference_type __elemsafter =
806 difference_type(__length) - __elemsbefore;
807 __pos = this->_M_impl._M_finish - __elemsafter;
810 if (__elemsafter > difference_type(__n))
812 iterator __finish_n = (this->_M_impl._M_finish
813 - difference_type(__n));
814 std::__uninitialized_move_a(__finish_n,
815 this->_M_impl._M_finish,
816 this->_M_impl._M_finish,
817 _M_get_Tp_allocator());
818 this->_M_impl._M_finish = __new_finish;
819 _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
820 std::copy(__first, __last, __pos);
824 _ForwardIterator __mid = __first;
825 std::advance(__mid, __elemsafter);
826 std::__uninitialized_copy_move(__mid, __last, __pos,
827 this->_M_impl._M_finish,
828 this->_M_impl._M_finish,
829 _M_get_Tp_allocator());
830 this->_M_impl._M_finish = __new_finish;
831 std::copy(__first, __mid, __pos);
836 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
837 __new_finish._M_node + 1);
838 __throw_exception_again;
843 template<typename _Tp, typename _Alloc>
846 _M_destroy_data_aux(iterator __first, iterator __last)
848 for (_Map_pointer __node = __first._M_node + 1;
849 __node < __last._M_node; ++__node)
850 std::_Destroy(*__node, *__node + _S_buffer_size(),
851 _M_get_Tp_allocator());
853 if (__first._M_node != __last._M_node)
855 std::_Destroy(__first._M_cur, __first._M_last,
856 _M_get_Tp_allocator());
857 std::_Destroy(__last._M_first, __last._M_cur,
858 _M_get_Tp_allocator());
861 std::_Destroy(__first._M_cur, __last._M_cur,
862 _M_get_Tp_allocator());
865 template <typename _Tp, typename _Alloc>
868 _M_new_elements_at_front(size_type __new_elems)
870 if (this->max_size() - this->size() < __new_elems)
871 __throw_length_error(__N("deque::_M_new_elements_at_front"));
873 const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
875 _M_reserve_map_at_front(__new_nodes);
879 for (__i = 1; __i <= __new_nodes; ++__i)
880 *(this->_M_impl._M_start._M_node - __i) = this->_M_allocate_node();
884 for (size_type __j = 1; __j < __i; ++__j)
885 _M_deallocate_node(*(this->_M_impl._M_start._M_node - __j));
886 __throw_exception_again;
890 template <typename _Tp, typename _Alloc>
893 _M_new_elements_at_back(size_type __new_elems)
895 if (this->max_size() - this->size() < __new_elems)
896 __throw_length_error(__N("deque::_M_new_elements_at_back"));
898 const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
900 _M_reserve_map_at_back(__new_nodes);
904 for (__i = 1; __i <= __new_nodes; ++__i)
905 *(this->_M_impl._M_finish._M_node + __i) = this->_M_allocate_node();
909 for (size_type __j = 1; __j < __i; ++__j)
910 _M_deallocate_node(*(this->_M_impl._M_finish._M_node + __j));
911 __throw_exception_again;
915 template <typename _Tp, typename _Alloc>
918 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front)
920 const size_type __old_num_nodes
921 = this->_M_impl._M_finish._M_node - this->_M_impl._M_start._M_node + 1;
922 const size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
924 _Map_pointer __new_nstart;
925 if (this->_M_impl._M_map_size > 2 * __new_num_nodes)
927 __new_nstart = this->_M_impl._M_map + (this->_M_impl._M_map_size
928 - __new_num_nodes) / 2
929 + (__add_at_front ? __nodes_to_add : 0);
930 if (__new_nstart < this->_M_impl._M_start._M_node)
931 std::copy(this->_M_impl._M_start._M_node,
932 this->_M_impl._M_finish._M_node + 1,
935 std::copy_backward(this->_M_impl._M_start._M_node,
936 this->_M_impl._M_finish._M_node + 1,
937 __new_nstart + __old_num_nodes);
941 size_type __new_map_size = this->_M_impl._M_map_size
942 + std::max(this->_M_impl._M_map_size,
945 _Map_pointer __new_map = this->_M_allocate_map(__new_map_size);
946 __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
947 + (__add_at_front ? __nodes_to_add : 0);
948 std::copy(this->_M_impl._M_start._M_node,
949 this->_M_impl._M_finish._M_node + 1,
951 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
953 this->_M_impl._M_map = __new_map;
954 this->_M_impl._M_map_size = __new_map_size;
957 this->_M_impl._M_start._M_set_node(__new_nstart);
958 this->_M_impl._M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
961 // Overload for deque::iterators, exploiting the "segmented-iterator
963 template<typename _Tp>
965 fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
966 const _Deque_iterator<_Tp, _Tp&, _Tp*>& __last, const _Tp& __value)
968 typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
970 for (typename _Self::_Map_pointer __node = __first._M_node + 1;
971 __node < __last._M_node; ++__node)
972 std::fill(*__node, *__node + _Self::_S_buffer_size(), __value);
974 if (__first._M_node != __last._M_node)
976 std::fill(__first._M_cur, __first._M_last, __value);
977 std::fill(__last._M_first, __last._M_cur, __value);
980 std::fill(__first._M_cur, __last._M_cur, __value);
983 template<typename _Tp>
984 _Deque_iterator<_Tp, _Tp&, _Tp*>
985 copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
986 _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
987 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
989 typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
990 typedef typename _Self::difference_type difference_type;
992 difference_type __len = __last - __first;
995 const difference_type __clen
996 = std::min(__len, std::min(__first._M_last - __first._M_cur,
997 __result._M_last - __result._M_cur));
998 std::copy(__first._M_cur, __first._M_cur + __clen, __result._M_cur);
1006 template<typename _Tp>
1007 _Deque_iterator<_Tp, _Tp&, _Tp*>
1008 copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
1009 _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
1010 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
1012 typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
1013 typedef typename _Self::difference_type difference_type;
1015 difference_type __len = __last - __first;
1018 difference_type __llen = __last._M_cur - __last._M_first;
1019 _Tp* __lend = __last._M_cur;
1021 difference_type __rlen = __result._M_cur - __result._M_first;
1022 _Tp* __rend = __result._M_cur;
1026 __llen = _Self::_S_buffer_size();
1027 __lend = *(__last._M_node - 1) + __llen;
1031 __rlen = _Self::_S_buffer_size();
1032 __rend = *(__result._M_node - 1) + __rlen;
1035 const difference_type __clen = std::min(__len,
1036 std::min(__llen, __rlen));
1037 std::copy_backward(__lend - __clen, __lend, __rend);
1045 #if __cplusplus >= 201103L
1046 template<typename _Tp>
1047 _Deque_iterator<_Tp, _Tp&, _Tp*>
1048 move(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
1049 _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
1050 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
1052 typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
1053 typedef typename _Self::difference_type difference_type;
1055 difference_type __len = __last - __first;
1058 const difference_type __clen
1059 = std::min(__len, std::min(__first._M_last - __first._M_cur,
1060 __result._M_last - __result._M_cur));
1061 std::move(__first._M_cur, __first._M_cur + __clen, __result._M_cur);
1069 template<typename _Tp>
1070 _Deque_iterator<_Tp, _Tp&, _Tp*>
1071 move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
1072 _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
1073 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
1075 typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
1076 typedef typename _Self::difference_type difference_type;
1078 difference_type __len = __last - __first;
1081 difference_type __llen = __last._M_cur - __last._M_first;
1082 _Tp* __lend = __last._M_cur;
1084 difference_type __rlen = __result._M_cur - __result._M_first;
1085 _Tp* __rend = __result._M_cur;
1089 __llen = _Self::_S_buffer_size();
1090 __lend = *(__last._M_node - 1) + __llen;
1094 __rlen = _Self::_S_buffer_size();
1095 __rend = *(__result._M_node - 1) + __rlen;
1098 const difference_type __clen = std::min(__len,
1099 std::min(__llen, __rlen));
1100 std::move_backward(__lend - __clen, __lend, __rend);
1109 _GLIBCXX_END_NAMESPACE_CONTAINER