1 // Deque implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 namespace _GLIBCXX_STD
66 template <typename _Tp, typename _Alloc>
69 operator=(const deque& __x)
71 const size_type __len = size();
74 if (__len >= __x.size())
75 erase(std::copy(__x.begin(), __x.end(), this->_M_impl._M_start),
76 this->_M_impl._M_finish);
79 const_iterator __mid = __x.begin() + difference_type(__len);
80 std::copy(__x.begin(), __mid, this->_M_impl._M_start);
81 insert(this->_M_impl._M_finish, __mid, __x.end());
87 template <typename _Tp, typename _Alloc>
88 typename deque<_Tp,_Alloc>::iterator
90 insert(iterator position, const value_type& __x)
92 if (position._M_cur == this->_M_impl._M_start._M_cur)
95 return this->_M_impl._M_start;
97 else if (position._M_cur == this->_M_impl._M_finish._M_cur)
100 iterator __tmp = this->_M_impl._M_finish;
105 return _M_insert_aux(position, __x);
108 template <typename _Tp, typename _Alloc>
109 typename deque<_Tp,_Alloc>::iterator
111 erase(iterator __position)
113 iterator __next = __position;
115 const size_type __index = __position - this->_M_impl._M_start;
116 if (__index < (size() >> 1))
118 std::copy_backward(this->_M_impl._M_start, __position, __next);
123 std::copy(__next, this->_M_impl._M_finish, __position);
126 return this->_M_impl._M_start + __index;
129 template <typename _Tp, typename _Alloc>
130 typename deque<_Tp,_Alloc>::iterator
132 erase(iterator __first, iterator __last)
134 if (__first == this->_M_impl._M_start
135 && __last == this->_M_impl._M_finish)
138 return this->_M_impl._M_finish;
142 const difference_type __n = __last - __first;
143 const difference_type __elems_before = (__first
144 - this->_M_impl._M_start);
145 if (static_cast<size_type>(__elems_before) < (size() - __n) / 2)
147 std::copy_backward(this->_M_impl._M_start, __first, __last);
148 iterator __new_start = this->_M_impl._M_start + __n;
149 std::_Destroy(this->_M_impl._M_start, __new_start);
150 _M_destroy_nodes(this->_M_impl._M_start._M_node,
151 __new_start._M_node);
152 this->_M_impl._M_start = __new_start;
156 std::copy(__last, this->_M_impl._M_finish, __first);
157 iterator __new_finish = this->_M_impl._M_finish - __n;
158 std::_Destroy(__new_finish, this->_M_impl._M_finish);
159 _M_destroy_nodes(__new_finish._M_node + 1,
160 this->_M_impl._M_finish._M_node + 1);
161 this->_M_impl._M_finish = __new_finish;
163 return this->_M_impl._M_start + __elems_before;
167 template <typename _Tp, typename _Alloc>
172 for (_Map_pointer __node = this->_M_impl._M_start._M_node + 1;
173 __node < this->_M_impl._M_finish._M_node;
176 std::_Destroy(*__node, *__node + _S_buffer_size());
177 _M_deallocate_node(*__node);
180 if (this->_M_impl._M_start._M_node != this->_M_impl._M_finish._M_node)
182 std::_Destroy(this->_M_impl._M_start._M_cur,
183 this->_M_impl._M_start._M_last);
184 std::_Destroy(this->_M_impl._M_finish._M_first,
185 this->_M_impl._M_finish._M_cur);
186 _M_deallocate_node(this->_M_impl._M_finish._M_first);
189 std::_Destroy(this->_M_impl._M_start._M_cur,
190 this->_M_impl._M_finish._M_cur);
192 this->_M_impl._M_finish = this->_M_impl._M_start;
195 template <typename _Tp, class _Alloc>
196 template <typename _InputIterator>
199 ::_M_assign_aux(_InputIterator __first, _InputIterator __last,
202 iterator __cur = begin();
203 for (; __first != __last && __cur != end(); ++__cur, ++__first)
205 if (__first == __last)
208 insert(end(), __first, __last);
211 template <typename _Tp, typename _Alloc>
214 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
216 if (__pos._M_cur == this->_M_impl._M_start._M_cur)
218 iterator __new_start = _M_reserve_elements_at_front(__n);
221 std::uninitialized_fill(__new_start, this->_M_impl._M_start, __x);
222 this->_M_impl._M_start = __new_start;
226 _M_destroy_nodes(__new_start._M_node,
227 this->_M_impl._M_start._M_node);
228 __throw_exception_again;
231 else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
233 iterator __new_finish = _M_reserve_elements_at_back(__n);
236 std::uninitialized_fill(this->_M_impl._M_finish,
238 this->_M_impl._M_finish = __new_finish;
242 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
243 __new_finish._M_node + 1);
244 __throw_exception_again;
248 _M_insert_aux(__pos, __n, __x);
251 template <typename _Tp, typename _Alloc>
254 _M_fill_initialize(const value_type& __value)
259 for (__cur = this->_M_impl._M_start._M_node;
260 __cur < this->_M_impl._M_finish._M_node;
262 std::uninitialized_fill(*__cur, *__cur + _S_buffer_size(), __value);
263 std::uninitialized_fill(this->_M_impl._M_finish._M_first,
264 this->_M_impl._M_finish._M_cur,
269 std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur));
270 __throw_exception_again;
274 template <typename _Tp, typename _Alloc>
275 template <typename _InputIterator>
278 _M_range_initialize(_InputIterator __first, _InputIterator __last,
281 this->_M_initialize_map(0);
284 for (; __first != __last; ++__first)
290 __throw_exception_again;
294 template <typename _Tp, typename _Alloc>
295 template <typename _ForwardIterator>
298 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
299 forward_iterator_tag)
301 const size_type __n = std::distance(__first, __last);
302 this->_M_initialize_map(__n);
304 _Map_pointer __cur_node;
307 for (__cur_node = this->_M_impl._M_start._M_node;
308 __cur_node < this->_M_impl._M_finish._M_node;
311 _ForwardIterator __mid = __first;
312 std::advance(__mid, _S_buffer_size());
313 std::uninitialized_copy(__first, __mid, *__cur_node);
316 std::uninitialized_copy(__first, __last,
317 this->_M_impl._M_finish._M_first);
321 std::_Destroy(this->_M_impl._M_start,
322 iterator(*__cur_node, __cur_node));
323 __throw_exception_again;
327 // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_last - 1.
328 template <typename _Tp, typename _Alloc>
331 _M_push_back_aux(const value_type& __t)
333 value_type __t_copy = __t;
334 _M_reserve_map_at_back();
335 *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node();
338 std::_Construct(this->_M_impl._M_finish._M_cur, __t_copy);
339 this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node
341 this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_first;
345 _M_deallocate_node(*(this->_M_impl._M_finish._M_node + 1));
346 __throw_exception_again;
350 // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_first.
351 template <typename _Tp, typename _Alloc>
354 _M_push_front_aux(const value_type& __t)
356 value_type __t_copy = __t;
357 _M_reserve_map_at_front();
358 *(this->_M_impl._M_start._M_node - 1) = this->_M_allocate_node();
361 this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node
363 this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_last - 1;
364 std::_Construct(this->_M_impl._M_start._M_cur, __t_copy);
368 ++this->_M_impl._M_start;
369 _M_deallocate_node(*(this->_M_impl._M_start._M_node - 1));
370 __throw_exception_again;
374 // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_first.
375 template <typename _Tp, typename _Alloc>
376 void deque<_Tp, _Alloc>::
379 _M_deallocate_node(this->_M_impl._M_finish._M_first);
380 this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node - 1);
381 this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_last - 1;
382 std::_Destroy(this->_M_impl._M_finish._M_cur);
385 // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_last - 1.
386 // Note that if the deque has at least one element (a precondition for this
387 // member function), and if
388 // _M_impl._M_start._M_cur == _M_impl._M_start._M_last,
389 // then the deque must have at least two nodes.
390 template <typename _Tp, typename _Alloc>
391 void deque<_Tp, _Alloc>::
394 std::_Destroy(this->_M_impl._M_start._M_cur);
395 _M_deallocate_node(this->_M_impl._M_start._M_first);
396 this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node + 1);
397 this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_first;
400 template <typename _Tp, typename _Alloc>
401 template <typename _InputIterator>
404 _M_range_insert_aux(iterator __pos,
405 _InputIterator __first, _InputIterator __last,
407 { std::copy(__first, __last, std::inserter(*this, __pos)); }
409 template <typename _Tp, typename _Alloc>
410 template <typename _ForwardIterator>
413 _M_range_insert_aux(iterator __pos,
414 _ForwardIterator __first, _ForwardIterator __last,
415 forward_iterator_tag)
417 const size_type __n = std::distance(__first, __last);
418 if (__pos._M_cur == this->_M_impl._M_start._M_cur)
420 iterator __new_start = _M_reserve_elements_at_front(__n);
423 std::uninitialized_copy(__first, __last, __new_start);
424 this->_M_impl._M_start = __new_start;
428 _M_destroy_nodes(__new_start._M_node,
429 this->_M_impl._M_start._M_node);
430 __throw_exception_again;
433 else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
435 iterator __new_finish = _M_reserve_elements_at_back(__n);
438 std::uninitialized_copy(__first, __last,
439 this->_M_impl._M_finish);
440 this->_M_impl._M_finish = __new_finish;
444 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
445 __new_finish._M_node + 1);
446 __throw_exception_again;
450 _M_insert_aux(__pos, __first, __last, __n);
453 template <typename _Tp, typename _Alloc>
454 typename deque<_Tp, _Alloc>::iterator
456 _M_insert_aux(iterator __pos, const value_type& __x)
458 difference_type __index = __pos - this->_M_impl._M_start;
459 value_type __x_copy = __x; // XXX copy
460 if (static_cast<size_type>(__index) < size() / 2)
463 iterator __front1 = this->_M_impl._M_start;
465 iterator __front2 = __front1;
467 __pos = this->_M_impl._M_start + __index;
468 iterator __pos1 = __pos;
470 std::copy(__front2, __pos1, __front1);
475 iterator __back1 = this->_M_impl._M_finish;
477 iterator __back2 = __back1;
479 __pos = this->_M_impl._M_start + __index;
480 std::copy_backward(__pos, __back2, __back1);
486 template <typename _Tp, typename _Alloc>
489 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x)
491 const difference_type __elems_before = __pos - this->_M_impl._M_start;
492 const size_type __length = this->size();
493 value_type __x_copy = __x;
494 if (__elems_before < difference_type(__length / 2))
496 iterator __new_start = _M_reserve_elements_at_front(__n);
497 iterator __old_start = this->_M_impl._M_start;
498 __pos = this->_M_impl._M_start + __elems_before;
501 if (__elems_before >= difference_type(__n))
503 iterator __start_n = (this->_M_impl._M_start
504 + difference_type(__n));
505 std::uninitialized_copy(this->_M_impl._M_start, __start_n,
507 this->_M_impl._M_start = __new_start;
508 std::copy(__start_n, __pos, __old_start);
509 fill(__pos - difference_type(__n), __pos, __x_copy);
513 std::__uninitialized_copy_fill(this->_M_impl._M_start,
515 this->_M_impl._M_start,
517 this->_M_impl._M_start = __new_start;
518 std::fill(__old_start, __pos, __x_copy);
523 _M_destroy_nodes(__new_start._M_node,
524 this->_M_impl._M_start._M_node);
525 __throw_exception_again;
530 iterator __new_finish = _M_reserve_elements_at_back(__n);
531 iterator __old_finish = this->_M_impl._M_finish;
532 const difference_type __elems_after =
533 difference_type(__length) - __elems_before;
534 __pos = this->_M_impl._M_finish - __elems_after;
537 if (__elems_after > difference_type(__n))
539 iterator __finish_n = (this->_M_impl._M_finish
540 - difference_type(__n));
541 std::uninitialized_copy(__finish_n, this->_M_impl._M_finish,
542 this->_M_impl._M_finish);
543 this->_M_impl._M_finish = __new_finish;
544 std::copy_backward(__pos, __finish_n, __old_finish);
545 std::fill(__pos, __pos + difference_type(__n), __x_copy);
549 std::__uninitialized_fill_copy(this->_M_impl._M_finish,
550 __pos + difference_type(__n),
552 this->_M_impl._M_finish);
553 this->_M_impl._M_finish = __new_finish;
554 std::fill(__pos, __old_finish, __x_copy);
559 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
560 __new_finish._M_node + 1);
561 __throw_exception_again;
566 template <typename _Tp, typename _Alloc>
567 template <typename _ForwardIterator>
570 _M_insert_aux(iterator __pos,
571 _ForwardIterator __first, _ForwardIterator __last,
574 const difference_type __elemsbefore = __pos - this->_M_impl._M_start;
575 const size_type __length = size();
576 if (static_cast<size_type>(__elemsbefore) < __length / 2)
578 iterator __new_start = _M_reserve_elements_at_front(__n);
579 iterator __old_start = this->_M_impl._M_start;
580 __pos = this->_M_impl._M_start + __elemsbefore;
583 if (__elemsbefore >= difference_type(__n))
585 iterator __start_n = (this->_M_impl._M_start
586 + difference_type(__n));
587 std::uninitialized_copy(this->_M_impl._M_start, __start_n,
589 this->_M_impl._M_start = __new_start;
590 std::copy(__start_n, __pos, __old_start);
591 std::copy(__first, __last, __pos - difference_type(__n));
595 _ForwardIterator __mid = __first;
596 std::advance(__mid, difference_type(__n) - __elemsbefore);
597 std::__uninitialized_copy_copy(this->_M_impl._M_start,
598 __pos, __first, __mid,
600 this->_M_impl._M_start = __new_start;
601 std::copy(__mid, __last, __old_start);
606 _M_destroy_nodes(__new_start._M_node,
607 this->_M_impl._M_start._M_node);
608 __throw_exception_again;
613 iterator __new_finish = _M_reserve_elements_at_back(__n);
614 iterator __old_finish = this->_M_impl._M_finish;
615 const difference_type __elemsafter =
616 difference_type(__length) - __elemsbefore;
617 __pos = this->_M_impl._M_finish - __elemsafter;
620 if (__elemsafter > difference_type(__n))
622 iterator __finish_n = (this->_M_impl._M_finish
623 - difference_type(__n));
624 std::uninitialized_copy(__finish_n,
625 this->_M_impl._M_finish,
626 this->_M_impl._M_finish);
627 this->_M_impl._M_finish = __new_finish;
628 std::copy_backward(__pos, __finish_n, __old_finish);
629 std::copy(__first, __last, __pos);
633 _ForwardIterator __mid = __first;
634 std::advance(__mid, __elemsafter);
635 std::__uninitialized_copy_copy(__mid, __last, __pos,
636 this->_M_impl._M_finish,
637 this->_M_impl._M_finish);
638 this->_M_impl._M_finish = __new_finish;
639 std::copy(__first, __mid, __pos);
644 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
645 __new_finish._M_node + 1);
646 __throw_exception_again;
651 template <typename _Tp, typename _Alloc>
654 _M_new_elements_at_front(size_type __new_elems)
656 const size_type __new_nodes
657 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
658 _M_reserve_map_at_front(__new_nodes);
662 for (__i = 1; __i <= __new_nodes; ++__i)
663 *(this->_M_impl._M_start._M_node - __i) = this->_M_allocate_node();
667 for (size_type __j = 1; __j < __i; ++__j)
668 _M_deallocate_node(*(this->_M_impl._M_start._M_node - __j));
669 __throw_exception_again;
673 template <typename _Tp, typename _Alloc>
676 _M_new_elements_at_back(size_type __new_elems)
678 const size_type __new_nodes
679 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
680 _M_reserve_map_at_back(__new_nodes);
684 for (__i = 1; __i <= __new_nodes; ++__i)
685 *(this->_M_impl._M_finish._M_node + __i) = this->_M_allocate_node();
689 for (size_type __j = 1; __j < __i; ++__j)
690 _M_deallocate_node(*(this->_M_impl._M_finish._M_node + __j));
691 __throw_exception_again;
695 template <typename _Tp, typename _Alloc>
698 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front)
700 const size_type __old_num_nodes
701 = this->_M_impl._M_finish._M_node - this->_M_impl._M_start._M_node + 1;
702 const size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
704 _Map_pointer __new_nstart;
705 if (this->_M_impl._M_map_size > 2 * __new_num_nodes)
707 __new_nstart = this->_M_impl._M_map + (this->_M_impl._M_map_size
708 - __new_num_nodes) / 2
709 + (__add_at_front ? __nodes_to_add : 0);
710 if (__new_nstart < this->_M_impl._M_start._M_node)
711 std::copy(this->_M_impl._M_start._M_node,
712 this->_M_impl._M_finish._M_node + 1,
715 std::copy_backward(this->_M_impl._M_start._M_node,
716 this->_M_impl._M_finish._M_node + 1,
717 __new_nstart + __old_num_nodes);
721 size_type __new_map_size = this->_M_impl._M_map_size
722 + std::max(this->_M_impl._M_map_size,
725 _Map_pointer __new_map = this->_M_allocate_map(__new_map_size);
726 __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
727 + (__add_at_front ? __nodes_to_add : 0);
728 std::copy(this->_M_impl._M_start._M_node,
729 this->_M_impl._M_finish._M_node + 1,
731 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
733 this->_M_impl._M_map = __new_map;
734 this->_M_impl._M_map_size = __new_map_size;
737 this->_M_impl._M_start._M_set_node(__new_nstart);
738 this->_M_impl._M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);