1 // Deque implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 _M_erase_at_end(std::copy(__x.begin(), __x.end(),
76 this->_M_impl._M_start));
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 - begin();
116 if (__index < (size() >> 1))
118 if (__position != begin())
119 std::copy_backward(begin(), __position, __next);
125 std::copy(__next, end(), __position);
128 return begin() + __index;
131 template <typename _Tp, typename _Alloc>
132 typename deque<_Tp, _Alloc>::iterator
134 erase(iterator __first, iterator __last)
136 if (__first == begin() && __last == end())
143 const difference_type __n = __last - __first;
144 const difference_type __elems_before = __first - begin();
145 if (static_cast<size_type>(__elems_before) < (size() - __n) / 2)
147 if (__first != begin())
148 std::copy_backward(begin(), __first, __last);
149 _M_erase_at_begin(begin() + __n);
154 std::copy(__last, end(), __first);
155 _M_erase_at_end(end() - __n);
157 return begin() + __elems_before;
161 template <typename _Tp, class _Alloc>
162 template <typename _InputIterator>
165 _M_assign_aux(_InputIterator __first, _InputIterator __last,
166 std::input_iterator_tag)
168 iterator __cur = begin();
169 for (; __first != __last && __cur != end(); ++__cur, ++__first)
171 if (__first == __last)
172 _M_erase_at_end(__cur);
174 insert(end(), __first, __last);
177 template <typename _Tp, typename _Alloc>
180 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
182 if (__pos._M_cur == this->_M_impl._M_start._M_cur)
184 iterator __new_start = _M_reserve_elements_at_front(__n);
187 std::__uninitialized_fill_a(__new_start, this->_M_impl._M_start,
189 _M_get_Tp_allocator());
190 this->_M_impl._M_start = __new_start;
194 _M_destroy_nodes(__new_start._M_node,
195 this->_M_impl._M_start._M_node);
196 __throw_exception_again;
199 else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
201 iterator __new_finish = _M_reserve_elements_at_back(__n);
204 std::__uninitialized_fill_a(this->_M_impl._M_finish,
206 _M_get_Tp_allocator());
207 this->_M_impl._M_finish = __new_finish;
211 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
212 __new_finish._M_node + 1);
213 __throw_exception_again;
217 _M_insert_aux(__pos, __n, __x);
220 template <typename _Tp, typename _Alloc>
223 _M_fill_initialize(const value_type& __value)
228 for (__cur = this->_M_impl._M_start._M_node;
229 __cur < this->_M_impl._M_finish._M_node;
231 std::__uninitialized_fill_a(*__cur, *__cur + _S_buffer_size(),
232 __value, _M_get_Tp_allocator());
233 std::__uninitialized_fill_a(this->_M_impl._M_finish._M_first,
234 this->_M_impl._M_finish._M_cur,
235 __value, _M_get_Tp_allocator());
239 std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
240 _M_get_Tp_allocator());
241 __throw_exception_again;
245 template <typename _Tp, typename _Alloc>
246 template <typename _InputIterator>
249 _M_range_initialize(_InputIterator __first, _InputIterator __last,
250 std::input_iterator_tag)
252 this->_M_initialize_map(0);
255 for (; __first != __last; ++__first)
261 __throw_exception_again;
265 template <typename _Tp, typename _Alloc>
266 template <typename _ForwardIterator>
269 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
270 std::forward_iterator_tag)
272 const size_type __n = std::distance(__first, __last);
273 this->_M_initialize_map(__n);
275 _Map_pointer __cur_node;
278 for (__cur_node = this->_M_impl._M_start._M_node;
279 __cur_node < this->_M_impl._M_finish._M_node;
282 _ForwardIterator __mid = __first;
283 std::advance(__mid, _S_buffer_size());
284 std::__uninitialized_copy_a(__first, __mid, *__cur_node,
285 _M_get_Tp_allocator());
288 std::__uninitialized_copy_a(__first, __last,
289 this->_M_impl._M_finish._M_first,
290 _M_get_Tp_allocator());
294 std::_Destroy(this->_M_impl._M_start,
295 iterator(*__cur_node, __cur_node),
296 _M_get_Tp_allocator());
297 __throw_exception_again;
301 // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_last - 1.
302 template <typename _Tp, typename _Alloc>
305 _M_push_back_aux(const value_type& __t)
307 value_type __t_copy = __t;
308 _M_reserve_map_at_back();
309 *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node();
312 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __t_copy);
313 this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node
315 this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_first;
319 _M_deallocate_node(*(this->_M_impl._M_finish._M_node + 1));
320 __throw_exception_again;
324 // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_first.
325 template <typename _Tp, typename _Alloc>
328 _M_push_front_aux(const value_type& __t)
330 value_type __t_copy = __t;
331 _M_reserve_map_at_front();
332 *(this->_M_impl._M_start._M_node - 1) = this->_M_allocate_node();
335 this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node
337 this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_last - 1;
338 this->_M_impl.construct(this->_M_impl._M_start._M_cur, __t_copy);
342 ++this->_M_impl._M_start;
343 _M_deallocate_node(*(this->_M_impl._M_start._M_node - 1));
344 __throw_exception_again;
348 // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_first.
349 template <typename _Tp, typename _Alloc>
350 void deque<_Tp, _Alloc>::
353 _M_deallocate_node(this->_M_impl._M_finish._M_first);
354 this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node - 1);
355 this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_last - 1;
356 this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
359 // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_last - 1.
360 // Note that if the deque has at least one element (a precondition for this
361 // member function), and if
362 // _M_impl._M_start._M_cur == _M_impl._M_start._M_last,
363 // then the deque must have at least two nodes.
364 template <typename _Tp, typename _Alloc>
365 void deque<_Tp, _Alloc>::
368 this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
369 _M_deallocate_node(this->_M_impl._M_start._M_first);
370 this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node + 1);
371 this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_first;
374 template <typename _Tp, typename _Alloc>
375 template <typename _InputIterator>
378 _M_range_insert_aux(iterator __pos,
379 _InputIterator __first, _InputIterator __last,
380 std::input_iterator_tag)
381 { std::copy(__first, __last, std::inserter(*this, __pos)); }
383 template <typename _Tp, typename _Alloc>
384 template <typename _ForwardIterator>
387 _M_range_insert_aux(iterator __pos,
388 _ForwardIterator __first, _ForwardIterator __last,
389 std::forward_iterator_tag)
391 const size_type __n = std::distance(__first, __last);
392 if (__pos._M_cur == this->_M_impl._M_start._M_cur)
394 iterator __new_start = _M_reserve_elements_at_front(__n);
397 std::__uninitialized_copy_a(__first, __last, __new_start,
398 _M_get_Tp_allocator());
399 this->_M_impl._M_start = __new_start;
403 _M_destroy_nodes(__new_start._M_node,
404 this->_M_impl._M_start._M_node);
405 __throw_exception_again;
408 else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
410 iterator __new_finish = _M_reserve_elements_at_back(__n);
413 std::__uninitialized_copy_a(__first, __last,
414 this->_M_impl._M_finish,
415 _M_get_Tp_allocator());
416 this->_M_impl._M_finish = __new_finish;
420 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
421 __new_finish._M_node + 1);
422 __throw_exception_again;
426 _M_insert_aux(__pos, __first, __last, __n);
429 template <typename _Tp, typename _Alloc>
430 typename deque<_Tp, _Alloc>::iterator
432 _M_insert_aux(iterator __pos, const value_type& __x)
434 difference_type __index = __pos - this->_M_impl._M_start;
435 value_type __x_copy = __x; // XXX copy
436 if (static_cast<size_type>(__index) < size() / 2)
439 iterator __front1 = this->_M_impl._M_start;
441 iterator __front2 = __front1;
443 __pos = this->_M_impl._M_start + __index;
444 iterator __pos1 = __pos;
446 std::copy(__front2, __pos1, __front1);
451 iterator __back1 = this->_M_impl._M_finish;
453 iterator __back2 = __back1;
455 __pos = this->_M_impl._M_start + __index;
456 std::copy_backward(__pos, __back2, __back1);
462 template <typename _Tp, typename _Alloc>
465 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x)
467 const difference_type __elems_before = __pos - this->_M_impl._M_start;
468 const size_type __length = this->size();
469 value_type __x_copy = __x;
470 if (__elems_before < difference_type(__length / 2))
472 iterator __new_start = _M_reserve_elements_at_front(__n);
473 iterator __old_start = this->_M_impl._M_start;
474 __pos = this->_M_impl._M_start + __elems_before;
477 if (__elems_before >= difference_type(__n))
479 iterator __start_n = (this->_M_impl._M_start
480 + difference_type(__n));
481 std::__uninitialized_copy_a(this->_M_impl._M_start,
482 __start_n, __new_start,
483 _M_get_Tp_allocator());
484 this->_M_impl._M_start = __new_start;
485 std::copy(__start_n, __pos, __old_start);
486 fill(__pos - difference_type(__n), __pos, __x_copy);
490 std::__uninitialized_copy_fill(this->_M_impl._M_start,
492 this->_M_impl._M_start,
494 _M_get_Tp_allocator());
495 this->_M_impl._M_start = __new_start;
496 std::fill(__old_start, __pos, __x_copy);
501 _M_destroy_nodes(__new_start._M_node,
502 this->_M_impl._M_start._M_node);
503 __throw_exception_again;
508 iterator __new_finish = _M_reserve_elements_at_back(__n);
509 iterator __old_finish = this->_M_impl._M_finish;
510 const difference_type __elems_after =
511 difference_type(__length) - __elems_before;
512 __pos = this->_M_impl._M_finish - __elems_after;
515 if (__elems_after > difference_type(__n))
517 iterator __finish_n = (this->_M_impl._M_finish
518 - difference_type(__n));
519 std::__uninitialized_copy_a(__finish_n,
520 this->_M_impl._M_finish,
521 this->_M_impl._M_finish,
522 _M_get_Tp_allocator());
523 this->_M_impl._M_finish = __new_finish;
524 std::copy_backward(__pos, __finish_n, __old_finish);
525 std::fill(__pos, __pos + difference_type(__n), __x_copy);
529 std::__uninitialized_fill_copy(this->_M_impl._M_finish,
530 __pos + difference_type(__n),
532 this->_M_impl._M_finish,
533 _M_get_Tp_allocator());
534 this->_M_impl._M_finish = __new_finish;
535 std::fill(__pos, __old_finish, __x_copy);
540 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
541 __new_finish._M_node + 1);
542 __throw_exception_again;
547 template <typename _Tp, typename _Alloc>
548 template <typename _ForwardIterator>
551 _M_insert_aux(iterator __pos,
552 _ForwardIterator __first, _ForwardIterator __last,
555 const difference_type __elemsbefore = __pos - this->_M_impl._M_start;
556 const size_type __length = size();
557 if (static_cast<size_type>(__elemsbefore) < __length / 2)
559 iterator __new_start = _M_reserve_elements_at_front(__n);
560 iterator __old_start = this->_M_impl._M_start;
561 __pos = this->_M_impl._M_start + __elemsbefore;
564 if (__elemsbefore >= difference_type(__n))
566 iterator __start_n = (this->_M_impl._M_start
567 + difference_type(__n));
568 std::__uninitialized_copy_a(this->_M_impl._M_start,
569 __start_n, __new_start,
570 _M_get_Tp_allocator());
571 this->_M_impl._M_start = __new_start;
572 std::copy(__start_n, __pos, __old_start);
573 std::copy(__first, __last, __pos - difference_type(__n));
577 _ForwardIterator __mid = __first;
578 std::advance(__mid, difference_type(__n) - __elemsbefore);
579 std::__uninitialized_copy_copy(this->_M_impl._M_start,
580 __pos, __first, __mid,
582 _M_get_Tp_allocator());
583 this->_M_impl._M_start = __new_start;
584 std::copy(__mid, __last, __old_start);
589 _M_destroy_nodes(__new_start._M_node,
590 this->_M_impl._M_start._M_node);
591 __throw_exception_again;
596 iterator __new_finish = _M_reserve_elements_at_back(__n);
597 iterator __old_finish = this->_M_impl._M_finish;
598 const difference_type __elemsafter =
599 difference_type(__length) - __elemsbefore;
600 __pos = this->_M_impl._M_finish - __elemsafter;
603 if (__elemsafter > difference_type(__n))
605 iterator __finish_n = (this->_M_impl._M_finish
606 - difference_type(__n));
607 std::__uninitialized_copy_a(__finish_n,
608 this->_M_impl._M_finish,
609 this->_M_impl._M_finish,
610 _M_get_Tp_allocator());
611 this->_M_impl._M_finish = __new_finish;
612 std::copy_backward(__pos, __finish_n, __old_finish);
613 std::copy(__first, __last, __pos);
617 _ForwardIterator __mid = __first;
618 std::advance(__mid, __elemsafter);
619 std::__uninitialized_copy_copy(__mid, __last, __pos,
620 this->_M_impl._M_finish,
621 this->_M_impl._M_finish,
622 _M_get_Tp_allocator());
623 this->_M_impl._M_finish = __new_finish;
624 std::copy(__first, __mid, __pos);
629 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
630 __new_finish._M_node + 1);
631 __throw_exception_again;
636 template<typename _Tp, typename _Alloc>
639 _M_destroy_data_aux(iterator __first, iterator __last)
641 for (_Map_pointer __node = __first._M_node + 1;
642 __node < __last._M_node; ++__node)
643 std::_Destroy(*__node, *__node + _S_buffer_size(),
644 _M_get_Tp_allocator());
646 if (__first._M_node != __last._M_node)
648 std::_Destroy(__first._M_cur, __first._M_last,
649 _M_get_Tp_allocator());
650 std::_Destroy(__last._M_first, __last._M_cur,
651 _M_get_Tp_allocator());
654 std::_Destroy(__first._M_cur, __last._M_cur,
655 _M_get_Tp_allocator());
658 template <typename _Tp, typename _Alloc>
661 _M_new_elements_at_front(size_type __new_elems)
663 const size_type __new_nodes
664 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
665 _M_reserve_map_at_front(__new_nodes);
669 for (__i = 1; __i <= __new_nodes; ++__i)
670 *(this->_M_impl._M_start._M_node - __i) = this->_M_allocate_node();
674 for (size_type __j = 1; __j < __i; ++__j)
675 _M_deallocate_node(*(this->_M_impl._M_start._M_node - __j));
676 __throw_exception_again;
680 template <typename _Tp, typename _Alloc>
683 _M_new_elements_at_back(size_type __new_elems)
685 const size_type __new_nodes
686 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
687 _M_reserve_map_at_back(__new_nodes);
691 for (__i = 1; __i <= __new_nodes; ++__i)
692 *(this->_M_impl._M_finish._M_node + __i) = this->_M_allocate_node();
696 for (size_type __j = 1; __j < __i; ++__j)
697 _M_deallocate_node(*(this->_M_impl._M_finish._M_node + __j));
698 __throw_exception_again;
702 template <typename _Tp, typename _Alloc>
705 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front)
707 const size_type __old_num_nodes
708 = this->_M_impl._M_finish._M_node - this->_M_impl._M_start._M_node + 1;
709 const size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
711 _Map_pointer __new_nstart;
712 if (this->_M_impl._M_map_size > 2 * __new_num_nodes)
714 __new_nstart = this->_M_impl._M_map + (this->_M_impl._M_map_size
715 - __new_num_nodes) / 2
716 + (__add_at_front ? __nodes_to_add : 0);
717 if (__new_nstart < this->_M_impl._M_start._M_node)
718 std::copy(this->_M_impl._M_start._M_node,
719 this->_M_impl._M_finish._M_node + 1,
722 std::copy_backward(this->_M_impl._M_start._M_node,
723 this->_M_impl._M_finish._M_node + 1,
724 __new_nstart + __old_num_nodes);
728 size_type __new_map_size = this->_M_impl._M_map_size
729 + std::max(this->_M_impl._M_map_size,
732 _Map_pointer __new_map = this->_M_allocate_map(__new_map_size);
733 __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
734 + (__add_at_front ? __nodes_to_add : 0);
735 std::copy(this->_M_impl._M_start._M_node,
736 this->_M_impl._M_finish._M_node + 1,
738 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
740 this->_M_impl._M_map = __new_map;
741 this->_M_impl._M_map_size = __new_map_size;
744 this->_M_impl._M_start._M_set_node(__new_nstart);
745 this->_M_impl._M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);