1 // Deque implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
30 * Hewlett-Packard Company
32 * Permission to use, copy, modify, distribute and sell this software
33 * and its documentation for any purpose is hereby granted without fee,
34 * provided that the above copyright notice appear in all copies and
35 * that both that copyright notice and this permission notice appear
36 * in supporting documentation. Hewlett-Packard Company makes no
37 * representations about the suitability of this software for any
38 * purpose. It is provided "as is" without express or implied warranty.
42 * Silicon Graphics Computer Systems, Inc.
44 * Permission to use, copy, modify, distribute and sell this software
45 * and its documentation for any purpose is hereby granted without fee,
46 * provided that the above copyright notice appear in all copies and
47 * that both that copyright notice and this permission notice appear
48 * in supporting documentation. Silicon Graphics makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
54 * This is an internal header file, included by other library headers.
55 * You should not attempt to use it directly.
61 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
63 #ifdef __GXX_EXPERIMENTAL_CXX0X__
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)
95 const size_type __len = size();
98 if (__len >= __x.size())
99 _M_erase_at_end(std::copy(__x.begin(), __x.end(),
100 this->_M_impl._M_start));
103 const_iterator __mid = __x.begin() + difference_type(__len);
104 std::copy(__x.begin(), __mid, this->_M_impl._M_start);
105 insert(this->_M_impl._M_finish, __mid, __x.end());
111 #ifdef __GXX_EXPERIMENTAL_CXX0X__
112 template<typename _Tp, typename _Alloc>
113 template<typename... _Args>
116 emplace_front(_Args&&... __args)
118 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
120 this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1,
121 std::forward<_Args>(__args)...);
122 --this->_M_impl._M_start._M_cur;
125 _M_push_front_aux(std::forward<_Args>(__args)...);
128 template<typename _Tp, typename _Alloc>
129 template<typename... _Args>
132 emplace_back(_Args&&... __args)
134 if (this->_M_impl._M_finish._M_cur
135 != this->_M_impl._M_finish._M_last - 1)
137 this->_M_impl.construct(this->_M_impl._M_finish._M_cur,
138 std::forward<_Args>(__args)...);
139 ++this->_M_impl._M_finish._M_cur;
142 _M_push_back_aux(std::forward<_Args>(__args)...);
146 template <typename _Tp, typename _Alloc>
147 typename deque<_Tp, _Alloc>::iterator
149 insert(iterator __position, const value_type& __x)
151 if (__position._M_cur == this->_M_impl._M_start._M_cur)
154 return this->_M_impl._M_start;
156 else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
159 iterator __tmp = this->_M_impl._M_finish;
164 return _M_insert_aux(__position, __x);
167 #ifdef __GXX_EXPERIMENTAL_CXX0X__
168 template<typename _Tp, typename _Alloc>
169 template<typename... _Args>
170 typename deque<_Tp, _Alloc>::iterator
172 emplace(iterator __position, _Args&&... __args)
174 if (__position._M_cur == this->_M_impl._M_start._M_cur)
176 push_front(std::forward<_Args>(__args)...);
177 return this->_M_impl._M_start;
179 else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
181 push_back(std::forward<_Args>(__args)...);
182 iterator __tmp = this->_M_impl._M_finish;
187 return _M_insert_aux(__position, std::forward<_Args>(__args)...);
191 template <typename _Tp, typename _Alloc>
192 typename deque<_Tp, _Alloc>::iterator
194 erase(iterator __position)
196 iterator __next = __position;
198 const difference_type __index = __position - begin();
199 if (static_cast<size_type>(__index) < (size() >> 1))
201 if (__position != begin())
202 _GLIBCXX_MOVE_BACKWARD3(begin(), __position, __next);
208 _GLIBCXX_MOVE3(__next, end(), __position);
211 return begin() + __index;
214 template <typename _Tp, typename _Alloc>
215 typename deque<_Tp, _Alloc>::iterator
217 erase(iterator __first, iterator __last)
219 if (__first == begin() && __last == end())
226 const difference_type __n = __last - __first;
227 const difference_type __elems_before = __first - begin();
228 if (static_cast<size_type>(__elems_before) <= (size() - __n) / 2)
230 if (__first != begin())
231 _GLIBCXX_MOVE_BACKWARD3(begin(), __first, __last);
232 _M_erase_at_begin(begin() + __n);
237 _GLIBCXX_MOVE3(__last, end(), __first);
238 _M_erase_at_end(end() - __n);
240 return begin() + __elems_before;
244 template <typename _Tp, class _Alloc>
245 template <typename _InputIterator>
248 _M_assign_aux(_InputIterator __first, _InputIterator __last,
249 std::input_iterator_tag)
251 iterator __cur = begin();
252 for (; __first != __last && __cur != end(); ++__cur, ++__first)
254 if (__first == __last)
255 _M_erase_at_end(__cur);
257 insert(end(), __first, __last);
260 template <typename _Tp, typename _Alloc>
263 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
265 if (__pos._M_cur == this->_M_impl._M_start._M_cur)
267 iterator __new_start = _M_reserve_elements_at_front(__n);
270 std::__uninitialized_fill_a(__new_start, this->_M_impl._M_start,
271 __x, _M_get_Tp_allocator());
272 this->_M_impl._M_start = __new_start;
276 _M_destroy_nodes(__new_start._M_node,
277 this->_M_impl._M_start._M_node);
278 __throw_exception_again;
281 else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
283 iterator __new_finish = _M_reserve_elements_at_back(__n);
286 std::__uninitialized_fill_a(this->_M_impl._M_finish,
288 _M_get_Tp_allocator());
289 this->_M_impl._M_finish = __new_finish;
293 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
294 __new_finish._M_node + 1);
295 __throw_exception_again;
299 _M_insert_aux(__pos, __n, __x);
302 #ifdef __GXX_EXPERIMENTAL_CXX0X__
303 template <typename _Tp, typename _Alloc>
306 _M_default_append(size_type __n)
310 iterator __new_finish = _M_reserve_elements_at_back(__n);
313 std::__uninitialized_default_a(this->_M_impl._M_finish,
315 _M_get_Tp_allocator());
316 this->_M_impl._M_finish = __new_finish;
320 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
321 __new_finish._M_node + 1);
322 __throw_exception_again;
328 template <typename _Tp, typename _Alloc>
331 _M_fill_initialize(const value_type& __value)
336 for (__cur = this->_M_impl._M_start._M_node;
337 __cur < this->_M_impl._M_finish._M_node;
339 std::__uninitialized_fill_a(*__cur, *__cur + _S_buffer_size(),
340 __value, _M_get_Tp_allocator());
341 std::__uninitialized_fill_a(this->_M_impl._M_finish._M_first,
342 this->_M_impl._M_finish._M_cur,
343 __value, _M_get_Tp_allocator());
347 std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
348 _M_get_Tp_allocator());
349 __throw_exception_again;
353 template <typename _Tp, typename _Alloc>
354 template <typename _InputIterator>
357 _M_range_initialize(_InputIterator __first, _InputIterator __last,
358 std::input_iterator_tag)
360 this->_M_initialize_map(0);
363 for (; __first != __last; ++__first)
369 __throw_exception_again;
373 template <typename _Tp, typename _Alloc>
374 template <typename _ForwardIterator>
377 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
378 std::forward_iterator_tag)
380 const size_type __n = std::distance(__first, __last);
381 this->_M_initialize_map(__n);
383 _Map_pointer __cur_node;
386 for (__cur_node = this->_M_impl._M_start._M_node;
387 __cur_node < this->_M_impl._M_finish._M_node;
390 _ForwardIterator __mid = __first;
391 std::advance(__mid, _S_buffer_size());
392 std::__uninitialized_copy_a(__first, __mid, *__cur_node,
393 _M_get_Tp_allocator());
396 std::__uninitialized_copy_a(__first, __last,
397 this->_M_impl._M_finish._M_first,
398 _M_get_Tp_allocator());
402 std::_Destroy(this->_M_impl._M_start,
403 iterator(*__cur_node, __cur_node),
404 _M_get_Tp_allocator());
405 __throw_exception_again;
409 // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_last - 1.
410 template<typename _Tp, typename _Alloc>
411 #ifdef __GXX_EXPERIMENTAL_CXX0X__
412 template<typename... _Args>
415 _M_push_back_aux(_Args&&... __args)
419 _M_push_back_aux(const value_type& __t)
422 _M_reserve_map_at_back();
423 *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node();
426 #ifdef __GXX_EXPERIMENTAL_CXX0X__
427 this->_M_impl.construct(this->_M_impl._M_finish._M_cur,
428 std::forward<_Args>(__args)...);
430 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __t);
432 this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node
434 this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_first;
438 _M_deallocate_node(*(this->_M_impl._M_finish._M_node + 1));
439 __throw_exception_again;
443 // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_first.
444 template<typename _Tp, typename _Alloc>
445 #ifdef __GXX_EXPERIMENTAL_CXX0X__
446 template<typename... _Args>
449 _M_push_front_aux(_Args&&... __args)
453 _M_push_front_aux(const value_type& __t)
456 _M_reserve_map_at_front();
457 *(this->_M_impl._M_start._M_node - 1) = this->_M_allocate_node();
460 this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node
462 this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_last - 1;
463 #ifdef __GXX_EXPERIMENTAL_CXX0X__
464 this->_M_impl.construct(this->_M_impl._M_start._M_cur,
465 std::forward<_Args>(__args)...);
467 this->_M_impl.construct(this->_M_impl._M_start._M_cur, __t);
472 ++this->_M_impl._M_start;
473 _M_deallocate_node(*(this->_M_impl._M_start._M_node - 1));
474 __throw_exception_again;
478 // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_first.
479 template <typename _Tp, typename _Alloc>
480 void deque<_Tp, _Alloc>::
483 _M_deallocate_node(this->_M_impl._M_finish._M_first);
484 this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node - 1);
485 this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_last - 1;
486 this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
489 // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_last - 1.
490 // Note that if the deque has at least one element (a precondition for this
491 // member function), and if
492 // _M_impl._M_start._M_cur == _M_impl._M_start._M_last,
493 // then the deque must have at least two nodes.
494 template <typename _Tp, typename _Alloc>
495 void deque<_Tp, _Alloc>::
498 this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
499 _M_deallocate_node(this->_M_impl._M_start._M_first);
500 this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node + 1);
501 this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_first;
504 template <typename _Tp, typename _Alloc>
505 template <typename _InputIterator>
508 _M_range_insert_aux(iterator __pos,
509 _InputIterator __first, _InputIterator __last,
510 std::input_iterator_tag)
511 { std::copy(__first, __last, std::inserter(*this, __pos)); }
513 template <typename _Tp, typename _Alloc>
514 template <typename _ForwardIterator>
517 _M_range_insert_aux(iterator __pos,
518 _ForwardIterator __first, _ForwardIterator __last,
519 std::forward_iterator_tag)
521 const size_type __n = std::distance(__first, __last);
522 if (__pos._M_cur == this->_M_impl._M_start._M_cur)
524 iterator __new_start = _M_reserve_elements_at_front(__n);
527 std::__uninitialized_copy_a(__first, __last, __new_start,
528 _M_get_Tp_allocator());
529 this->_M_impl._M_start = __new_start;
533 _M_destroy_nodes(__new_start._M_node,
534 this->_M_impl._M_start._M_node);
535 __throw_exception_again;
538 else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
540 iterator __new_finish = _M_reserve_elements_at_back(__n);
543 std::__uninitialized_copy_a(__first, __last,
544 this->_M_impl._M_finish,
545 _M_get_Tp_allocator());
546 this->_M_impl._M_finish = __new_finish;
550 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
551 __new_finish._M_node + 1);
552 __throw_exception_again;
556 _M_insert_aux(__pos, __first, __last, __n);
559 template<typename _Tp, typename _Alloc>
560 #ifdef __GXX_EXPERIMENTAL_CXX0X__
561 template<typename... _Args>
562 typename deque<_Tp, _Alloc>::iterator
564 _M_insert_aux(iterator __pos, _Args&&... __args)
566 value_type __x_copy(std::forward<_Args>(__args)...); // XXX copy
568 typename deque<_Tp, _Alloc>::iterator
570 _M_insert_aux(iterator __pos, const value_type& __x)
572 value_type __x_copy = __x; // XXX copy
574 difference_type __index = __pos - this->_M_impl._M_start;
575 if (static_cast<size_type>(__index) < size() / 2)
577 push_front(_GLIBCXX_MOVE(front()));
578 iterator __front1 = this->_M_impl._M_start;
580 iterator __front2 = __front1;
582 __pos = this->_M_impl._M_start + __index;
583 iterator __pos1 = __pos;
585 _GLIBCXX_MOVE3(__front2, __pos1, __front1);
589 push_back(_GLIBCXX_MOVE(back()));
590 iterator __back1 = this->_M_impl._M_finish;
592 iterator __back2 = __back1;
594 __pos = this->_M_impl._M_start + __index;
595 _GLIBCXX_MOVE_BACKWARD3(__pos, __back2, __back1);
597 *__pos = _GLIBCXX_MOVE(__x_copy);
601 template <typename _Tp, typename _Alloc>
604 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x)
606 const difference_type __elems_before = __pos - this->_M_impl._M_start;
607 const size_type __length = this->size();
608 value_type __x_copy = __x;
609 if (__elems_before < difference_type(__length / 2))
611 iterator __new_start = _M_reserve_elements_at_front(__n);
612 iterator __old_start = this->_M_impl._M_start;
613 __pos = this->_M_impl._M_start + __elems_before;
616 if (__elems_before >= difference_type(__n))
618 iterator __start_n = (this->_M_impl._M_start
619 + difference_type(__n));
620 std::__uninitialized_move_a(this->_M_impl._M_start,
621 __start_n, __new_start,
622 _M_get_Tp_allocator());
623 this->_M_impl._M_start = __new_start;
624 _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
625 std::fill(__pos - difference_type(__n), __pos, __x_copy);
629 std::__uninitialized_move_fill(this->_M_impl._M_start,
631 this->_M_impl._M_start,
633 _M_get_Tp_allocator());
634 this->_M_impl._M_start = __new_start;
635 std::fill(__old_start, __pos, __x_copy);
640 _M_destroy_nodes(__new_start._M_node,
641 this->_M_impl._M_start._M_node);
642 __throw_exception_again;
647 iterator __new_finish = _M_reserve_elements_at_back(__n);
648 iterator __old_finish = this->_M_impl._M_finish;
649 const difference_type __elems_after =
650 difference_type(__length) - __elems_before;
651 __pos = this->_M_impl._M_finish - __elems_after;
654 if (__elems_after > difference_type(__n))
656 iterator __finish_n = (this->_M_impl._M_finish
657 - difference_type(__n));
658 std::__uninitialized_move_a(__finish_n,
659 this->_M_impl._M_finish,
660 this->_M_impl._M_finish,
661 _M_get_Tp_allocator());
662 this->_M_impl._M_finish = __new_finish;
663 _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
664 std::fill(__pos, __pos + difference_type(__n), __x_copy);
668 std::__uninitialized_fill_move(this->_M_impl._M_finish,
669 __pos + difference_type(__n),
671 this->_M_impl._M_finish,
672 _M_get_Tp_allocator());
673 this->_M_impl._M_finish = __new_finish;
674 std::fill(__pos, __old_finish, __x_copy);
679 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
680 __new_finish._M_node + 1);
681 __throw_exception_again;
686 template <typename _Tp, typename _Alloc>
687 template <typename _ForwardIterator>
690 _M_insert_aux(iterator __pos,
691 _ForwardIterator __first, _ForwardIterator __last,
694 const difference_type __elemsbefore = __pos - this->_M_impl._M_start;
695 const size_type __length = size();
696 if (static_cast<size_type>(__elemsbefore) < __length / 2)
698 iterator __new_start = _M_reserve_elements_at_front(__n);
699 iterator __old_start = this->_M_impl._M_start;
700 __pos = this->_M_impl._M_start + __elemsbefore;
703 if (__elemsbefore >= difference_type(__n))
705 iterator __start_n = (this->_M_impl._M_start
706 + difference_type(__n));
707 std::__uninitialized_move_a(this->_M_impl._M_start,
708 __start_n, __new_start,
709 _M_get_Tp_allocator());
710 this->_M_impl._M_start = __new_start;
711 _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
712 std::copy(__first, __last, __pos - difference_type(__n));
716 _ForwardIterator __mid = __first;
717 std::advance(__mid, difference_type(__n) - __elemsbefore);
718 std::__uninitialized_move_copy(this->_M_impl._M_start,
719 __pos, __first, __mid,
721 _M_get_Tp_allocator());
722 this->_M_impl._M_start = __new_start;
723 std::copy(__mid, __last, __old_start);
728 _M_destroy_nodes(__new_start._M_node,
729 this->_M_impl._M_start._M_node);
730 __throw_exception_again;
735 iterator __new_finish = _M_reserve_elements_at_back(__n);
736 iterator __old_finish = this->_M_impl._M_finish;
737 const difference_type __elemsafter =
738 difference_type(__length) - __elemsbefore;
739 __pos = this->_M_impl._M_finish - __elemsafter;
742 if (__elemsafter > difference_type(__n))
744 iterator __finish_n = (this->_M_impl._M_finish
745 - difference_type(__n));
746 std::__uninitialized_move_a(__finish_n,
747 this->_M_impl._M_finish,
748 this->_M_impl._M_finish,
749 _M_get_Tp_allocator());
750 this->_M_impl._M_finish = __new_finish;
751 _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
752 std::copy(__first, __last, __pos);
756 _ForwardIterator __mid = __first;
757 std::advance(__mid, __elemsafter);
758 std::__uninitialized_copy_move(__mid, __last, __pos,
759 this->_M_impl._M_finish,
760 this->_M_impl._M_finish,
761 _M_get_Tp_allocator());
762 this->_M_impl._M_finish = __new_finish;
763 std::copy(__first, __mid, __pos);
768 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
769 __new_finish._M_node + 1);
770 __throw_exception_again;
775 template<typename _Tp, typename _Alloc>
778 _M_destroy_data_aux(iterator __first, iterator __last)
780 for (_Map_pointer __node = __first._M_node + 1;
781 __node < __last._M_node; ++__node)
782 std::_Destroy(*__node, *__node + _S_buffer_size(),
783 _M_get_Tp_allocator());
785 if (__first._M_node != __last._M_node)
787 std::_Destroy(__first._M_cur, __first._M_last,
788 _M_get_Tp_allocator());
789 std::_Destroy(__last._M_first, __last._M_cur,
790 _M_get_Tp_allocator());
793 std::_Destroy(__first._M_cur, __last._M_cur,
794 _M_get_Tp_allocator());
797 template <typename _Tp, typename _Alloc>
800 _M_new_elements_at_front(size_type __new_elems)
802 if (this->max_size() - this->size() < __new_elems)
803 __throw_length_error(__N("deque::_M_new_elements_at_front"));
805 const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
807 _M_reserve_map_at_front(__new_nodes);
811 for (__i = 1; __i <= __new_nodes; ++__i)
812 *(this->_M_impl._M_start._M_node - __i) = this->_M_allocate_node();
816 for (size_type __j = 1; __j < __i; ++__j)
817 _M_deallocate_node(*(this->_M_impl._M_start._M_node - __j));
818 __throw_exception_again;
822 template <typename _Tp, typename _Alloc>
825 _M_new_elements_at_back(size_type __new_elems)
827 if (this->max_size() - this->size() < __new_elems)
828 __throw_length_error(__N("deque::_M_new_elements_at_back"));
830 const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
832 _M_reserve_map_at_back(__new_nodes);
836 for (__i = 1; __i <= __new_nodes; ++__i)
837 *(this->_M_impl._M_finish._M_node + __i) = this->_M_allocate_node();
841 for (size_type __j = 1; __j < __i; ++__j)
842 _M_deallocate_node(*(this->_M_impl._M_finish._M_node + __j));
843 __throw_exception_again;
847 template <typename _Tp, typename _Alloc>
850 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front)
852 const size_type __old_num_nodes
853 = this->_M_impl._M_finish._M_node - this->_M_impl._M_start._M_node + 1;
854 const size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
856 _Map_pointer __new_nstart;
857 if (this->_M_impl._M_map_size > 2 * __new_num_nodes)
859 __new_nstart = this->_M_impl._M_map + (this->_M_impl._M_map_size
860 - __new_num_nodes) / 2
861 + (__add_at_front ? __nodes_to_add : 0);
862 if (__new_nstart < this->_M_impl._M_start._M_node)
863 std::copy(this->_M_impl._M_start._M_node,
864 this->_M_impl._M_finish._M_node + 1,
867 std::copy_backward(this->_M_impl._M_start._M_node,
868 this->_M_impl._M_finish._M_node + 1,
869 __new_nstart + __old_num_nodes);
873 size_type __new_map_size = this->_M_impl._M_map_size
874 + std::max(this->_M_impl._M_map_size,
877 _Map_pointer __new_map = this->_M_allocate_map(__new_map_size);
878 __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
879 + (__add_at_front ? __nodes_to_add : 0);
880 std::copy(this->_M_impl._M_start._M_node,
881 this->_M_impl._M_finish._M_node + 1,
883 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
885 this->_M_impl._M_map = __new_map;
886 this->_M_impl._M_map_size = __new_map_size;
889 this->_M_impl._M_start._M_set_node(__new_nstart);
890 this->_M_impl._M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
893 // Overload for deque::iterators, exploiting the "segmented-iterator
895 template<typename _Tp>
897 fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
898 const _Deque_iterator<_Tp, _Tp&, _Tp*>& __last, const _Tp& __value)
900 typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
902 for (typename _Self::_Map_pointer __node = __first._M_node + 1;
903 __node < __last._M_node; ++__node)
904 std::fill(*__node, *__node + _Self::_S_buffer_size(), __value);
906 if (__first._M_node != __last._M_node)
908 std::fill(__first._M_cur, __first._M_last, __value);
909 std::fill(__last._M_first, __last._M_cur, __value);
912 std::fill(__first._M_cur, __last._M_cur, __value);
915 template<typename _Tp>
916 _Deque_iterator<_Tp, _Tp&, _Tp*>
917 copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
918 _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
919 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
921 typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
922 typedef typename _Self::difference_type difference_type;
924 difference_type __len = __last - __first;
927 const difference_type __clen
928 = std::min(__len, std::min(__first._M_last - __first._M_cur,
929 __result._M_last - __result._M_cur));
930 std::copy(__first._M_cur, __first._M_cur + __clen, __result._M_cur);
938 template<typename _Tp>
939 _Deque_iterator<_Tp, _Tp&, _Tp*>
940 copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
941 _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
942 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
944 typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
945 typedef typename _Self::difference_type difference_type;
947 difference_type __len = __last - __first;
950 difference_type __llen = __last._M_cur - __last._M_first;
951 _Tp* __lend = __last._M_cur;
953 difference_type __rlen = __result._M_cur - __result._M_first;
954 _Tp* __rend = __result._M_cur;
958 __llen = _Self::_S_buffer_size();
959 __lend = *(__last._M_node - 1) + __llen;
963 __rlen = _Self::_S_buffer_size();
964 __rend = *(__result._M_node - 1) + __rlen;
967 const difference_type __clen = std::min(__len,
968 std::min(__llen, __rlen));
969 std::copy_backward(__lend - __clen, __lend, __rend);
977 #ifdef __GXX_EXPERIMENTAL_CXX0X__
978 template<typename _Tp>
979 _Deque_iterator<_Tp, _Tp&, _Tp*>
980 move(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
981 _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
982 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
984 typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
985 typedef typename _Self::difference_type difference_type;
987 difference_type __len = __last - __first;
990 const difference_type __clen
991 = std::min(__len, std::min(__first._M_last - __first._M_cur,
992 __result._M_last - __result._M_cur));
993 std::move(__first._M_cur, __first._M_cur + __clen, __result._M_cur);
1001 template<typename _Tp>
1002 _Deque_iterator<_Tp, _Tp&, _Tp*>
1003 move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
1004 _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
1005 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
1007 typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
1008 typedef typename _Self::difference_type difference_type;
1010 difference_type __len = __last - __first;
1013 difference_type __llen = __last._M_cur - __last._M_first;
1014 _Tp* __lend = __last._M_cur;
1016 difference_type __rlen = __result._M_cur - __result._M_first;
1017 _Tp* __rend = __result._M_cur;
1021 __llen = _Self::_S_buffer_size();
1022 __lend = *(__last._M_node - 1) + __llen;
1026 __rlen = _Self::_S_buffer_size();
1027 __rend = *(__result._M_node - 1) + __rlen;
1030 const difference_type __clen = std::min(__len,
1031 std::min(__llen, __rlen));
1032 std::move_backward(__lend - __clen, __lend, __rend);
1041 _GLIBCXX_END_NESTED_NAMESPACE