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 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _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 difference_type __index = __position - begin();
116 if (static_cast<size_type>(__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,
188 __x, _M_get_Tp_allocator());
189 this->_M_impl._M_start = __new_start;
193 _M_destroy_nodes(__new_start._M_node,
194 this->_M_impl._M_start._M_node);
195 __throw_exception_again;
198 else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
200 iterator __new_finish = _M_reserve_elements_at_back(__n);
203 std::__uninitialized_fill_a(this->_M_impl._M_finish,
205 _M_get_Tp_allocator());
206 this->_M_impl._M_finish = __new_finish;
210 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
211 __new_finish._M_node + 1);
212 __throw_exception_again;
216 _M_insert_aux(__pos, __n, __x);
219 template <typename _Tp, typename _Alloc>
222 _M_fill_initialize(const value_type& __value)
227 for (__cur = this->_M_impl._M_start._M_node;
228 __cur < this->_M_impl._M_finish._M_node;
230 std::__uninitialized_fill_a(*__cur, *__cur + _S_buffer_size(),
231 __value, _M_get_Tp_allocator());
232 std::__uninitialized_fill_a(this->_M_impl._M_finish._M_first,
233 this->_M_impl._M_finish._M_cur,
234 __value, _M_get_Tp_allocator());
238 std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
239 _M_get_Tp_allocator());
240 __throw_exception_again;
244 template <typename _Tp, typename _Alloc>
245 template <typename _InputIterator>
248 _M_range_initialize(_InputIterator __first, _InputIterator __last,
249 std::input_iterator_tag)
251 this->_M_initialize_map(0);
254 for (; __first != __last; ++__first)
260 __throw_exception_again;
264 template <typename _Tp, typename _Alloc>
265 template <typename _ForwardIterator>
268 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
269 std::forward_iterator_tag)
271 const size_type __n = std::distance(__first, __last);
272 this->_M_initialize_map(__n);
274 _Map_pointer __cur_node;
277 for (__cur_node = this->_M_impl._M_start._M_node;
278 __cur_node < this->_M_impl._M_finish._M_node;
281 _ForwardIterator __mid = __first;
282 std::advance(__mid, _S_buffer_size());
283 std::__uninitialized_copy_a(__first, __mid, *__cur_node,
284 _M_get_Tp_allocator());
287 std::__uninitialized_copy_a(__first, __last,
288 this->_M_impl._M_finish._M_first,
289 _M_get_Tp_allocator());
293 std::_Destroy(this->_M_impl._M_start,
294 iterator(*__cur_node, __cur_node),
295 _M_get_Tp_allocator());
296 __throw_exception_again;
300 // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_last - 1.
301 template <typename _Tp, typename _Alloc>
304 _M_push_back_aux(const value_type& __t)
306 value_type __t_copy = __t;
307 _M_reserve_map_at_back();
308 *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node();
311 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __t_copy);
312 this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node
314 this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_first;
318 _M_deallocate_node(*(this->_M_impl._M_finish._M_node + 1));
319 __throw_exception_again;
323 // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_first.
324 template <typename _Tp, typename _Alloc>
327 _M_push_front_aux(const value_type& __t)
329 value_type __t_copy = __t;
330 _M_reserve_map_at_front();
331 *(this->_M_impl._M_start._M_node - 1) = this->_M_allocate_node();
334 this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node
336 this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_last - 1;
337 this->_M_impl.construct(this->_M_impl._M_start._M_cur, __t_copy);
341 ++this->_M_impl._M_start;
342 _M_deallocate_node(*(this->_M_impl._M_start._M_node - 1));
343 __throw_exception_again;
347 // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_first.
348 template <typename _Tp, typename _Alloc>
349 void deque<_Tp, _Alloc>::
352 _M_deallocate_node(this->_M_impl._M_finish._M_first);
353 this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node - 1);
354 this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_last - 1;
355 this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
358 // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_last - 1.
359 // Note that if the deque has at least one element (a precondition for this
360 // member function), and if
361 // _M_impl._M_start._M_cur == _M_impl._M_start._M_last,
362 // then the deque must have at least two nodes.
363 template <typename _Tp, typename _Alloc>
364 void deque<_Tp, _Alloc>::
367 this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
368 _M_deallocate_node(this->_M_impl._M_start._M_first);
369 this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node + 1);
370 this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_first;
373 template <typename _Tp, typename _Alloc>
374 template <typename _InputIterator>
377 _M_range_insert_aux(iterator __pos,
378 _InputIterator __first, _InputIterator __last,
379 std::input_iterator_tag)
380 { std::copy(__first, __last, std::inserter(*this, __pos)); }
382 template <typename _Tp, typename _Alloc>
383 template <typename _ForwardIterator>
386 _M_range_insert_aux(iterator __pos,
387 _ForwardIterator __first, _ForwardIterator __last,
388 std::forward_iterator_tag)
390 const size_type __n = std::distance(__first, __last);
391 if (__pos._M_cur == this->_M_impl._M_start._M_cur)
393 iterator __new_start = _M_reserve_elements_at_front(__n);
396 std::__uninitialized_copy_a(__first, __last, __new_start,
397 _M_get_Tp_allocator());
398 this->_M_impl._M_start = __new_start;
402 _M_destroy_nodes(__new_start._M_node,
403 this->_M_impl._M_start._M_node);
404 __throw_exception_again;
407 else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
409 iterator __new_finish = _M_reserve_elements_at_back(__n);
412 std::__uninitialized_copy_a(__first, __last,
413 this->_M_impl._M_finish,
414 _M_get_Tp_allocator());
415 this->_M_impl._M_finish = __new_finish;
419 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
420 __new_finish._M_node + 1);
421 __throw_exception_again;
425 _M_insert_aux(__pos, __first, __last, __n);
428 template <typename _Tp, typename _Alloc>
429 typename deque<_Tp, _Alloc>::iterator
431 _M_insert_aux(iterator __pos, const value_type& __x)
433 difference_type __index = __pos - this->_M_impl._M_start;
434 value_type __x_copy = __x; // XXX copy
435 if (static_cast<size_type>(__index) < size() / 2)
438 iterator __front1 = this->_M_impl._M_start;
440 iterator __front2 = __front1;
442 __pos = this->_M_impl._M_start + __index;
443 iterator __pos1 = __pos;
445 std::copy(__front2, __pos1, __front1);
450 iterator __back1 = this->_M_impl._M_finish;
452 iterator __back2 = __back1;
454 __pos = this->_M_impl._M_start + __index;
455 std::copy_backward(__pos, __back2, __back1);
461 template <typename _Tp, typename _Alloc>
464 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x)
466 const difference_type __elems_before = __pos - this->_M_impl._M_start;
467 const size_type __length = this->size();
468 value_type __x_copy = __x;
469 if (__elems_before < difference_type(__length / 2))
471 iterator __new_start = _M_reserve_elements_at_front(__n);
472 iterator __old_start = this->_M_impl._M_start;
473 __pos = this->_M_impl._M_start + __elems_before;
476 if (__elems_before >= difference_type(__n))
478 iterator __start_n = (this->_M_impl._M_start
479 + difference_type(__n));
480 std::__uninitialized_copy_a(this->_M_impl._M_start,
481 __start_n, __new_start,
482 _M_get_Tp_allocator());
483 this->_M_impl._M_start = __new_start;
484 std::copy(__start_n, __pos, __old_start);
485 std::fill(__pos - difference_type(__n), __pos, __x_copy);
489 std::__uninitialized_copy_fill(this->_M_impl._M_start,
491 this->_M_impl._M_start,
493 _M_get_Tp_allocator());
494 this->_M_impl._M_start = __new_start;
495 std::fill(__old_start, __pos, __x_copy);
500 _M_destroy_nodes(__new_start._M_node,
501 this->_M_impl._M_start._M_node);
502 __throw_exception_again;
507 iterator __new_finish = _M_reserve_elements_at_back(__n);
508 iterator __old_finish = this->_M_impl._M_finish;
509 const difference_type __elems_after =
510 difference_type(__length) - __elems_before;
511 __pos = this->_M_impl._M_finish - __elems_after;
514 if (__elems_after > difference_type(__n))
516 iterator __finish_n = (this->_M_impl._M_finish
517 - difference_type(__n));
518 std::__uninitialized_copy_a(__finish_n,
519 this->_M_impl._M_finish,
520 this->_M_impl._M_finish,
521 _M_get_Tp_allocator());
522 this->_M_impl._M_finish = __new_finish;
523 std::copy_backward(__pos, __finish_n, __old_finish);
524 std::fill(__pos, __pos + difference_type(__n), __x_copy);
528 std::__uninitialized_fill_copy(this->_M_impl._M_finish,
529 __pos + difference_type(__n),
531 this->_M_impl._M_finish,
532 _M_get_Tp_allocator());
533 this->_M_impl._M_finish = __new_finish;
534 std::fill(__pos, __old_finish, __x_copy);
539 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
540 __new_finish._M_node + 1);
541 __throw_exception_again;
546 template <typename _Tp, typename _Alloc>
547 template <typename _ForwardIterator>
550 _M_insert_aux(iterator __pos,
551 _ForwardIterator __first, _ForwardIterator __last,
554 const difference_type __elemsbefore = __pos - this->_M_impl._M_start;
555 const size_type __length = size();
556 if (static_cast<size_type>(__elemsbefore) < __length / 2)
558 iterator __new_start = _M_reserve_elements_at_front(__n);
559 iterator __old_start = this->_M_impl._M_start;
560 __pos = this->_M_impl._M_start + __elemsbefore;
563 if (__elemsbefore >= difference_type(__n))
565 iterator __start_n = (this->_M_impl._M_start
566 + difference_type(__n));
567 std::__uninitialized_copy_a(this->_M_impl._M_start,
568 __start_n, __new_start,
569 _M_get_Tp_allocator());
570 this->_M_impl._M_start = __new_start;
571 std::copy(__start_n, __pos, __old_start);
572 std::copy(__first, __last, __pos - difference_type(__n));
576 _ForwardIterator __mid = __first;
577 std::advance(__mid, difference_type(__n) - __elemsbefore);
578 std::__uninitialized_copy_copy(this->_M_impl._M_start,
579 __pos, __first, __mid,
581 _M_get_Tp_allocator());
582 this->_M_impl._M_start = __new_start;
583 std::copy(__mid, __last, __old_start);
588 _M_destroy_nodes(__new_start._M_node,
589 this->_M_impl._M_start._M_node);
590 __throw_exception_again;
595 iterator __new_finish = _M_reserve_elements_at_back(__n);
596 iterator __old_finish = this->_M_impl._M_finish;
597 const difference_type __elemsafter =
598 difference_type(__length) - __elemsbefore;
599 __pos = this->_M_impl._M_finish - __elemsafter;
602 if (__elemsafter > difference_type(__n))
604 iterator __finish_n = (this->_M_impl._M_finish
605 - difference_type(__n));
606 std::__uninitialized_copy_a(__finish_n,
607 this->_M_impl._M_finish,
608 this->_M_impl._M_finish,
609 _M_get_Tp_allocator());
610 this->_M_impl._M_finish = __new_finish;
611 std::copy_backward(__pos, __finish_n, __old_finish);
612 std::copy(__first, __last, __pos);
616 _ForwardIterator __mid = __first;
617 std::advance(__mid, __elemsafter);
618 std::__uninitialized_copy_copy(__mid, __last, __pos,
619 this->_M_impl._M_finish,
620 this->_M_impl._M_finish,
621 _M_get_Tp_allocator());
622 this->_M_impl._M_finish = __new_finish;
623 std::copy(__first, __mid, __pos);
628 _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
629 __new_finish._M_node + 1);
630 __throw_exception_again;
635 template<typename _Tp, typename _Alloc>
638 _M_destroy_data_aux(iterator __first, iterator __last)
640 for (_Map_pointer __node = __first._M_node + 1;
641 __node < __last._M_node; ++__node)
642 std::_Destroy(*__node, *__node + _S_buffer_size(),
643 _M_get_Tp_allocator());
645 if (__first._M_node != __last._M_node)
647 std::_Destroy(__first._M_cur, __first._M_last,
648 _M_get_Tp_allocator());
649 std::_Destroy(__last._M_first, __last._M_cur,
650 _M_get_Tp_allocator());
653 std::_Destroy(__first._M_cur, __last._M_cur,
654 _M_get_Tp_allocator());
657 template <typename _Tp, typename _Alloc>
660 _M_new_elements_at_front(size_type __new_elems)
662 const size_type __new_nodes
663 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
664 _M_reserve_map_at_front(__new_nodes);
668 for (__i = 1; __i <= __new_nodes; ++__i)
669 *(this->_M_impl._M_start._M_node - __i) = this->_M_allocate_node();
673 for (size_type __j = 1; __j < __i; ++__j)
674 _M_deallocate_node(*(this->_M_impl._M_start._M_node - __j));
675 __throw_exception_again;
679 template <typename _Tp, typename _Alloc>
682 _M_new_elements_at_back(size_type __new_elems)
684 const size_type __new_nodes
685 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
686 _M_reserve_map_at_back(__new_nodes);
690 for (__i = 1; __i <= __new_nodes; ++__i)
691 *(this->_M_impl._M_finish._M_node + __i) = this->_M_allocate_node();
695 for (size_type __j = 1; __j < __i; ++__j)
696 _M_deallocate_node(*(this->_M_impl._M_finish._M_node + __j));
697 __throw_exception_again;
701 template <typename _Tp, typename _Alloc>
704 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front)
706 const size_type __old_num_nodes
707 = this->_M_impl._M_finish._M_node - this->_M_impl._M_start._M_node + 1;
708 const size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
710 _Map_pointer __new_nstart;
711 if (this->_M_impl._M_map_size > 2 * __new_num_nodes)
713 __new_nstart = this->_M_impl._M_map + (this->_M_impl._M_map_size
714 - __new_num_nodes) / 2
715 + (__add_at_front ? __nodes_to_add : 0);
716 if (__new_nstart < this->_M_impl._M_start._M_node)
717 std::copy(this->_M_impl._M_start._M_node,
718 this->_M_impl._M_finish._M_node + 1,
721 std::copy_backward(this->_M_impl._M_start._M_node,
722 this->_M_impl._M_finish._M_node + 1,
723 __new_nstart + __old_num_nodes);
727 size_type __new_map_size = this->_M_impl._M_map_size
728 + std::max(this->_M_impl._M_map_size,
731 _Map_pointer __new_map = this->_M_allocate_map(__new_map_size);
732 __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
733 + (__add_at_front ? __nodes_to_add : 0);
734 std::copy(this->_M_impl._M_start._M_node,
735 this->_M_impl._M_finish._M_node + 1,
737 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
739 this->_M_impl._M_map = __new_map;
740 this->_M_impl._M_map_size = __new_map_size;
743 this->_M_impl._M_start._M_set_node(__new_nstart);
744 this->_M_impl._M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
747 _GLIBCXX_END_NESTED_NAMESPACE