1 // deque implementation -*- C++ -*-
3 // Copyright (C) 2001 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.
56 /* NOTE: This is an internal header file, included by other STL headers.
57 * You should not attempt to use it directly.
60 #include <bits/concept_check.h>
61 #include <bits/stl_iterator_base_types.h>
62 #include <bits/stl_iterator_base_funcs.h>
64 #ifndef __SGI_STL_INTERNAL_DEQUE_H
65 #define __SGI_STL_INTERNAL_DEQUE_H
68 * For any nonsingular iterator i:
69 * i.node is the address of an element in the map array. The
70 * contents of i.node is a pointer to the beginning of a node.
71 * i.first == *(i.node)
72 * i.last == i.first + node_size
73 * i.cur is a pointer in the range [i.first, i.last). NOTE:
74 * the implication of this is that i.cur is always a dereferenceable
75 * pointer, even if i is a past-the-end iterator.
76 * Start and Finish are always nonsingular iterators. NOTE: this means
77 * that an empty deque must have one node, and that a deque
78 * with N elements, where N is the buffer size, must have two nodes.
79 * For every node other than start.node and finish.node, every element
80 * in the node is an initialized object. If start.node == finish.node,
81 * then [start.cur, finish.cur) are initialized objects, and
82 * the elements outside that range are uninitialized storage. Otherwise,
83 * [start.cur, start.last) and [finish.first, finish.cur) are initialized
84 * objects, and [start.first, start.cur) and [finish.cur, finish.last)
85 * are uninitialized storage.
86 * [map, map + map_size) is a valid, non-empty range.
87 * [start.node, finish.node] is a valid range contained within
88 * [map, map + map_size).
89 * A pointer in the range [map, map + map_size) points to an allocated node
90 * if and only if the pointer is in the range [start.node, finish.node].
95 * In previous versions of deque, there was an extra template
96 * parameter so users could control the node size. This extension
97 * turns out to violate the C++ standard (it can be detected using
98 * template template parameters), and it has been removed.
103 // Note: this function is simply a kludge to work around several compilers'
104 // bugs in handling constant expressions.
106 __deque_buf_size(size_t __size)
107 { return __size < 512 ? size_t(512 / __size) : size_t(1); }
109 template <class _Tp, class _Ref, class _Ptr>
110 struct _Deque_iterator {
111 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
112 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
113 static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); }
115 typedef random_access_iterator_tag iterator_category;
116 typedef _Tp value_type;
117 typedef _Ptr pointer;
118 typedef _Ref reference;
119 typedef size_t size_type;
120 typedef ptrdiff_t difference_type;
121 typedef _Tp** _Map_pointer;
123 typedef _Deque_iterator _Self;
128 _Map_pointer _M_node;
130 _Deque_iterator(_Tp* __x, _Map_pointer __y)
131 : _M_cur(__x), _M_first(*__y),
132 _M_last(*__y + _S_buffer_size()), _M_node(__y) {}
133 _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
134 _Deque_iterator(const iterator& __x)
135 : _M_cur(__x._M_cur), _M_first(__x._M_first),
136 _M_last(__x._M_last), _M_node(__x._M_node) {}
138 reference operator*() const { return *_M_cur; }
139 pointer operator->() const { return _M_cur; }
141 difference_type operator-(const _Self& __x) const {
142 return difference_type(_S_buffer_size()) * (_M_node - __x._M_node - 1) +
143 (_M_cur - _M_first) + (__x._M_last - __x._M_cur);
146 _Self& operator++() {
148 if (_M_cur == _M_last) {
149 _M_set_node(_M_node + 1);
154 _Self operator++(int) {
160 _Self& operator--() {
161 if (_M_cur == _M_first) {
162 _M_set_node(_M_node - 1);
168 _Self operator--(int) {
174 _Self& operator+=(difference_type __n)
176 difference_type __offset = __n + (_M_cur - _M_first);
177 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
180 difference_type __node_offset =
181 __offset > 0 ? __offset / difference_type(_S_buffer_size())
182 : -difference_type((-__offset - 1) / _S_buffer_size()) - 1;
183 _M_set_node(_M_node + __node_offset);
185 (__offset - __node_offset * difference_type(_S_buffer_size()));
190 _Self operator+(difference_type __n) const
196 _Self& operator-=(difference_type __n) { return *this += -__n; }
198 _Self operator-(difference_type __n) const {
203 reference operator[](difference_type __n) const { return *(*this + __n); }
205 bool operator==(const _Self& __x) const { return _M_cur == __x._M_cur; }
206 bool operator!=(const _Self& __x) const { return !(*this == __x); }
207 bool operator<(const _Self& __x) const {
208 return (_M_node == __x._M_node) ?
209 (_M_cur < __x._M_cur) : (_M_node < __x._M_node);
211 bool operator>(const _Self& __x) const { return __x < *this; }
212 bool operator<=(const _Self& __x) const { return !(__x < *this); }
213 bool operator>=(const _Self& __x) const { return !(*this < __x); }
215 void _M_set_node(_Map_pointer __new_node) {
216 _M_node = __new_node;
217 _M_first = *__new_node;
218 _M_last = _M_first + difference_type(_S_buffer_size());
222 template <class _Tp, class _Ref, class _Ptr>
223 inline _Deque_iterator<_Tp, _Ref, _Ptr>
224 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
230 // Deque base class. It has two purposes. First, its constructor
231 // and destructor allocate (but don't initialize) storage. This makes
232 // exception safety easier. Second, the base class encapsulates all of
233 // the differences between SGI-style allocators and standard-conforming
236 // Base class for ordinary allocators.
237 template <class _Tp, class _Alloc, bool __is_static>
238 class _Deque_alloc_base {
240 typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type;
241 allocator_type get_allocator() const { return _M_node_allocator; }
243 _Deque_alloc_base(const allocator_type& __a)
244 : _M_node_allocator(__a), _M_map_allocator(__a),
245 _M_map(0), _M_map_size(0)
249 typedef typename _Alloc_traits<_Tp*, _Alloc>::allocator_type
252 allocator_type _M_node_allocator;
253 _Map_allocator_type _M_map_allocator;
255 _Tp* _M_allocate_node() {
256 return _M_node_allocator.allocate(__deque_buf_size(sizeof(_Tp)));
258 void _M_deallocate_node(_Tp* __p) {
259 _M_node_allocator.deallocate(__p, __deque_buf_size(sizeof(_Tp)));
261 _Tp** _M_allocate_map(size_t __n)
262 { return _M_map_allocator.allocate(__n); }
263 void _M_deallocate_map(_Tp** __p, size_t __n)
264 { _M_map_allocator.deallocate(__p, __n); }
270 // Specialization for instanceless allocators.
271 template <class _Tp, class _Alloc>
272 class _Deque_alloc_base<_Tp, _Alloc, true>
275 typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type;
276 allocator_type get_allocator() const { return allocator_type(); }
278 _Deque_alloc_base(const allocator_type&) : _M_map(0), _M_map_size(0) {}
281 typedef typename _Alloc_traits<_Tp, _Alloc>::_Alloc_type _Node_alloc_type;
282 typedef typename _Alloc_traits<_Tp*, _Alloc>::_Alloc_type _Map_alloc_type;
284 _Tp* _M_allocate_node() {
285 return _Node_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
287 void _M_deallocate_node(_Tp* __p) {
288 _Node_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
290 _Tp** _M_allocate_map(size_t __n)
291 { return _Map_alloc_type::allocate(__n); }
292 void _M_deallocate_map(_Tp** __p, size_t __n)
293 { _Map_alloc_type::deallocate(__p, __n); }
299 template <class _Tp, class _Alloc>
301 : public _Deque_alloc_base<_Tp,_Alloc,
302 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
305 typedef _Deque_alloc_base<_Tp,_Alloc,
306 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
308 typedef typename _Base::allocator_type allocator_type;
309 typedef _Deque_iterator<_Tp,_Tp&,_Tp*> iterator;
310 typedef _Deque_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
312 _Deque_base(const allocator_type& __a, size_t __num_elements)
313 : _Base(__a), _M_start(), _M_finish()
314 { _M_initialize_map(__num_elements); }
315 _Deque_base(const allocator_type& __a)
316 : _Base(__a), _M_start(), _M_finish() {}
320 void _M_initialize_map(size_t);
321 void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
322 void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
323 enum { _S_initial_map_size = 8 };
330 // Non-inline member functions from _Deque_base.
332 template <class _Tp, class _Alloc>
333 _Deque_base<_Tp,_Alloc>::~_Deque_base() {
335 _M_destroy_nodes(_M_start._M_node, _M_finish._M_node + 1);
336 _M_deallocate_map(_M_map, _M_map_size);
340 template <class _Tp, class _Alloc>
342 _Deque_base<_Tp,_Alloc>::_M_initialize_map(size_t __num_elements)
345 __num_elements / __deque_buf_size(sizeof(_Tp)) + 1;
347 _M_map_size = max((size_t) _S_initial_map_size, __num_nodes + 2);
348 _M_map = _M_allocate_map(_M_map_size);
350 _Tp** __nstart = _M_map + (_M_map_size - __num_nodes) / 2;
351 _Tp** __nfinish = __nstart + __num_nodes;
354 { _M_create_nodes(__nstart, __nfinish); }
357 _M_deallocate_map(_M_map, _M_map_size);
360 __throw_exception_again;
363 _M_start._M_set_node(__nstart);
364 _M_finish._M_set_node(__nfinish - 1);
365 _M_start._M_cur = _M_start._M_first;
366 _M_finish._M_cur = _M_finish._M_first +
367 __num_elements % __deque_buf_size(sizeof(_Tp));
370 template <class _Tp, class _Alloc>
371 void _Deque_base<_Tp,_Alloc>::_M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
375 for (__cur = __nstart; __cur < __nfinish; ++__cur)
376 *__cur = _M_allocate_node();
380 _M_destroy_nodes(__nstart, __cur);
381 __throw_exception_again;
385 template <class _Tp, class _Alloc>
387 _Deque_base<_Tp,_Alloc>::_M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
389 for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
390 _M_deallocate_node(*__n);
393 template <class _Tp, class _Alloc = allocator<_Tp> >
394 class deque : protected _Deque_base<_Tp, _Alloc> {
396 // concept requirements
397 __glibcpp_class_requires(_Tp, _SGIAssignableConcept);
399 typedef _Deque_base<_Tp, _Alloc> _Base;
400 public: // Basic types
401 typedef _Tp value_type;
402 typedef value_type* pointer;
403 typedef const value_type* const_pointer;
404 typedef value_type& reference;
405 typedef const value_type& const_reference;
406 typedef size_t size_type;
407 typedef ptrdiff_t difference_type;
409 typedef typename _Base::allocator_type allocator_type;
410 allocator_type get_allocator() const { return _Base::get_allocator(); }
413 typedef typename _Base::iterator iterator;
414 typedef typename _Base::const_iterator const_iterator;
416 typedef reverse_iterator<const_iterator> const_reverse_iterator;
417 typedef reverse_iterator<iterator> reverse_iterator;
419 protected: // Internal typedefs
420 typedef pointer* _Map_pointer;
421 static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); }
424 using _Base::_M_initialize_map;
425 using _Base::_M_create_nodes;
426 using _Base::_M_destroy_nodes;
427 using _Base::_M_allocate_node;
428 using _Base::_M_deallocate_node;
429 using _Base::_M_allocate_map;
430 using _Base::_M_deallocate_map;
433 using _Base::_M_map_size;
434 using _Base::_M_start;
435 using _Base::_M_finish;
437 public: // Basic accessors
438 iterator begin() { return _M_start; }
439 iterator end() { return _M_finish; }
440 const_iterator begin() const { return _M_start; }
441 const_iterator end() const { return _M_finish; }
443 reverse_iterator rbegin() { return reverse_iterator(_M_finish); }
444 reverse_iterator rend() { return reverse_iterator(_M_start); }
445 const_reverse_iterator rbegin() const
446 { return const_reverse_iterator(_M_finish); }
447 const_reverse_iterator rend() const
448 { return const_reverse_iterator(_M_start); }
450 reference operator[](size_type __n)
451 { return _M_start[difference_type(__n)]; }
452 const_reference operator[](size_type __n) const
453 { return _M_start[difference_type(__n)]; }
455 void _M_range_check(size_type __n) const {
456 if (__n >= this->size())
457 __throw_range_error("deque");
460 reference at(size_type __n)
461 { _M_range_check(__n); return (*this)[__n]; }
462 const_reference at(size_type __n) const
463 { _M_range_check(__n); return (*this)[__n]; }
465 reference front() { return *_M_start; }
467 iterator __tmp = _M_finish;
471 const_reference front() const { return *_M_start; }
472 const_reference back() const {
473 const_iterator __tmp = _M_finish;
478 size_type size() const { return _M_finish - _M_start; }
479 size_type max_size() const { return size_type(-1); }
480 bool empty() const { return _M_finish == _M_start; }
482 public: // Constructor, destructor.
483 explicit deque(const allocator_type& __a = allocator_type())
485 deque(const deque& __x) : _Base(__x.get_allocator(), __x.size())
486 { uninitialized_copy(__x.begin(), __x.end(), _M_start); }
487 deque(size_type __n, const value_type& __value,
488 const allocator_type& __a = allocator_type()) : _Base(__a, __n)
489 { _M_fill_initialize(__value); }
493 : _Base(allocator_type(), __n)
494 { _M_fill_initialize(value_type()); }
496 // Check whether it's an integral type. If so, it's not an iterator.
497 template<class _InputIterator>
498 deque(_InputIterator __first, _InputIterator __last,
499 const allocator_type& __a = allocator_type())
502 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
503 _M_initialize_dispatch(__first, __last, _Integral());
506 template<class _Integer>
508 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
510 _M_initialize_map(__n);
511 _M_fill_initialize(__x);
514 template<class _InputIter>
516 _M_initialize_dispatch(_InputIter __first, _InputIter __last, __false_type)
518 typedef typename iterator_traits<_InputIter>::iterator_category _IterCategory;
519 _M_range_initialize(__first, __last, _IterCategory());
523 { _Destroy(_M_start, _M_finish); }
525 deque& operator= (const deque& __x) {
526 const size_type __len = size();
528 if (__len >= __x.size())
529 erase(copy(__x.begin(), __x.end(), _M_start), _M_finish);
531 const_iterator __mid = __x.begin() + difference_type(__len);
532 copy(__x.begin(), __mid, _M_start);
533 insert(_M_finish, __mid, __x.end());
539 void swap(deque& __x) {
540 std::swap(_M_start, __x._M_start);
541 std::swap(_M_finish, __x._M_finish);
542 std::swap(_M_map, __x._M_map);
543 std::swap(_M_map_size, __x._M_map_size);
547 // assign(), a generalized assignment member function. Two
548 // versions: one that takes a count, and one that takes a range.
549 // The range version is a member template, so we dispatch on whether
550 // or not the type is an integer.
552 void _M_fill_assign(size_type __n, const _Tp& __val) {
554 fill(begin(), end(), __val);
555 insert(end(), __n - size(), __val);
558 erase(begin() + __n, end());
559 fill(begin(), end(), __val);
564 assign(size_type __n, const _Tp& __val)
565 { _M_fill_assign(__n, __val); }
567 template<class _InputIterator>
569 assign(_InputIterator __first, _InputIterator __last)
571 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
572 _M_assign_dispatch(__first, __last, _Integral());
575 private: // helper functions for assign()
577 template<class _Integer>
579 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
580 { _M_fill_assign(static_cast<size_type>(__n), static_cast<_Tp>(__val)); }
582 template<class _InputIterator>
584 _M_assign_dispatch(_InputIterator __first, _InputIterator __last, __false_type)
586 typedef typename iterator_traits<_InputIterator>::iterator_category _IterCategory;
587 _M_assign_aux(__first, __last, _IterCategory());
590 template <class _InputIterator>
591 void _M_assign_aux(_InputIterator __first, _InputIterator __last,
594 template <class _ForwardIterator>
595 void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
596 forward_iterator_tag) {
598 distance(__first, __last, __len);
599 if (__len > size()) {
600 _ForwardIterator __mid = __first;
601 advance(__mid, size());
602 copy(__first, __mid, begin());
603 insert(end(), __mid, __last);
606 erase(copy(__first, __last, begin()), end());
609 public: // push_* and pop_*
612 push_back(const value_type& __t)
614 if (_M_finish._M_cur != _M_finish._M_last - 1) {
615 _Construct(_M_finish._M_cur, __t);
619 _M_push_back_aux(__t);
625 if (_M_finish._M_cur != _M_finish._M_last - 1) {
626 _Construct(_M_finish._M_cur);
634 push_front(const value_type& __t)
636 if (_M_start._M_cur != _M_start._M_first) {
637 _Construct(_M_start._M_cur - 1, __t);
641 _M_push_front_aux(__t);
647 if (_M_start._M_cur != _M_start._M_first) {
648 _Construct(_M_start._M_cur - 1);
659 if (_M_finish._M_cur != _M_finish._M_first) {
661 _Destroy(_M_finish._M_cur);
670 if (_M_start._M_cur != _M_start._M_last - 1) {
671 _Destroy(_M_start._M_cur);
681 insert(iterator position, const value_type& __x)
683 if (position._M_cur == _M_start._M_cur) {
687 else if (position._M_cur == _M_finish._M_cur) {
689 iterator __tmp = _M_finish;
694 return _M_insert_aux(position, __x);
699 insert(iterator __position)
700 { return insert(__position, value_type()); }
703 insert(iterator __pos, size_type __n, const value_type& __x)
704 { _M_fill_insert(__pos, __n, __x); }
707 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
709 // Check whether it's an integral type. If so, it's not an iterator.
710 template<class _InputIterator>
712 insert(iterator __pos, _InputIterator __first, _InputIterator __last)
714 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
715 _M_insert_dispatch(__pos, __first, __last, _Integral());
718 template<class _Integer>
720 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, __true_type)
721 { _M_fill_insert(__pos, static_cast<size_type>(__n), static_cast<value_type>(__x)); }
723 template<class _InputIterator>
725 _M_insert_dispatch(iterator __pos,
726 _InputIterator __first, _InputIterator __last,
729 typedef typename iterator_traits<_InputIterator>::iterator_category _IterCategory;
730 insert(__pos, __first, __last, _IterCategory());
733 void resize(size_type __new_size, const value_type& __x) {
734 const size_type __len = size();
735 if (__new_size < __len)
736 erase(_M_start + __new_size, _M_finish);
738 insert(_M_finish, __new_size - __len, __x);
741 void resize(size_type new_size) { resize(new_size, value_type()); }
744 iterator erase(iterator __pos) {
745 iterator __next = __pos;
747 size_type __index = __pos - _M_start;
748 if (__index < (size() >> 1)) {
749 copy_backward(_M_start, __pos, __next);
753 copy(__next, _M_finish, __pos);
756 return _M_start + __index;
759 iterator erase(iterator __first, iterator __last);
762 protected: // Internal construction/destruction
764 void _M_fill_initialize(const value_type& __value);
766 template <class _InputIterator>
767 void _M_range_initialize(_InputIterator __first, _InputIterator __last,
770 template <class _ForwardIterator>
771 void _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
772 forward_iterator_tag);
774 protected: // Internal push_* and pop_*
776 void _M_push_back_aux(const value_type&);
777 void _M_push_back_aux();
778 void _M_push_front_aux(const value_type&);
779 void _M_push_front_aux();
780 void _M_pop_back_aux();
781 void _M_pop_front_aux();
783 protected: // Internal insert functions
785 template <class _InputIterator>
786 void insert(iterator __pos, _InputIterator __first, _InputIterator __last,
789 template <class _ForwardIterator>
790 void insert(iterator __pos,
791 _ForwardIterator __first, _ForwardIterator __last,
792 forward_iterator_tag);
794 iterator _M_insert_aux(iterator __pos, const value_type& __x);
795 iterator _M_insert_aux(iterator __pos);
796 void _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
798 template <class _ForwardIterator>
799 void _M_insert_aux(iterator __pos,
800 _ForwardIterator __first, _ForwardIterator __last,
803 iterator _M_reserve_elements_at_front(size_type __n) {
804 size_type __vacancies = _M_start._M_cur - _M_start._M_first;
805 if (__n > __vacancies)
806 _M_new_elements_at_front(__n - __vacancies);
807 return _M_start - difference_type(__n);
810 iterator _M_reserve_elements_at_back(size_type __n) {
811 size_type __vacancies = (_M_finish._M_last - _M_finish._M_cur) - 1;
812 if (__n > __vacancies)
813 _M_new_elements_at_back(__n - __vacancies);
814 return _M_finish + difference_type(__n);
817 void _M_new_elements_at_front(size_type __new_elements);
818 void _M_new_elements_at_back(size_type __new_elements);
820 protected: // Allocation of _M_map and nodes
822 // Makes sure the _M_map has space for new nodes. Does not actually
823 // add the nodes. Can invalidate _M_map pointers. (And consequently,
826 void _M_reserve_map_at_back (size_type __nodes_to_add = 1) {
827 if (__nodes_to_add + 1 > _M_map_size - (_M_finish._M_node - _M_map))
828 _M_reallocate_map(__nodes_to_add, false);
831 void _M_reserve_map_at_front (size_type __nodes_to_add = 1) {
832 if (__nodes_to_add > size_type(_M_start._M_node - _M_map))
833 _M_reallocate_map(__nodes_to_add, true);
836 void _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
839 // Non-inline member functions
841 template <class _Tp, class _Alloc>
842 template <class _InputIter>
843 void deque<_Tp, _Alloc>
844 ::_M_assign_aux(_InputIter __first, _InputIter __last, input_iterator_tag)
846 iterator __cur = begin();
847 for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
849 if (__first == __last)
852 insert(end(), __first, __last);
855 template <class _Tp, class _Alloc>
856 void deque<_Tp, _Alloc>::_M_fill_insert(iterator __pos,
857 size_type __n, const value_type& __x)
859 if (__pos._M_cur == _M_start._M_cur) {
860 iterator __new_start = _M_reserve_elements_at_front(__n);
862 uninitialized_fill(__new_start, _M_start, __x);
863 _M_start = __new_start;
867 _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
868 __throw_exception_again;
871 else if (__pos._M_cur == _M_finish._M_cur) {
872 iterator __new_finish = _M_reserve_elements_at_back(__n);
874 uninitialized_fill(_M_finish, __new_finish, __x);
875 _M_finish = __new_finish;
879 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);
880 __throw_exception_again;
884 _M_insert_aux(__pos, __n, __x);
887 template <class _Tp, class _Alloc>
888 typename deque<_Tp,_Alloc>::iterator
889 deque<_Tp,_Alloc>::erase(iterator __first, iterator __last)
891 if (__first == _M_start && __last == _M_finish) {
896 difference_type __n = __last - __first;
897 difference_type __elems_before = __first - _M_start;
898 if (static_cast<size_type>(__elems_before) < (size() - __n) / 2) {
899 copy_backward(_M_start, __first, __last);
900 iterator __new_start = _M_start + __n;
901 _Destroy(_M_start, __new_start);
902 _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
903 _M_start = __new_start;
906 copy(__last, _M_finish, __first);
907 iterator __new_finish = _M_finish - __n;
908 _Destroy(__new_finish, _M_finish);
909 _M_destroy_nodes(__new_finish._M_node + 1, _M_finish._M_node + 1);
910 _M_finish = __new_finish;
912 return _M_start + __elems_before;
916 template <class _Tp, class _Alloc>
917 void deque<_Tp,_Alloc>::clear()
919 for (_Map_pointer __node = _M_start._M_node + 1;
920 __node < _M_finish._M_node;
922 _Destroy(*__node, *__node + _S_buffer_size());
923 _M_deallocate_node(*__node);
926 if (_M_start._M_node != _M_finish._M_node) {
927 _Destroy(_M_start._M_cur, _M_start._M_last);
928 _Destroy(_M_finish._M_first, _M_finish._M_cur);
929 _M_deallocate_node(_M_finish._M_first);
932 _Destroy(_M_start._M_cur, _M_finish._M_cur);
934 _M_finish = _M_start;
937 // Precondition: _M_start and _M_finish have already been initialized,
938 // but none of the deque's elements have yet been constructed.
939 template <class _Tp, class _Alloc>
940 void deque<_Tp,_Alloc>::_M_fill_initialize(const value_type& __value) {
943 for (__cur = _M_start._M_node; __cur < _M_finish._M_node; ++__cur)
944 uninitialized_fill(*__cur, *__cur + _S_buffer_size(), __value);
945 uninitialized_fill(_M_finish._M_first, _M_finish._M_cur, __value);
949 _Destroy(_M_start, iterator(*__cur, __cur));
950 __throw_exception_again;
954 template <class _Tp, class _Alloc> template <class _InputIterator>
955 void deque<_Tp,_Alloc>::_M_range_initialize(_InputIterator __first,
956 _InputIterator __last,
959 _M_initialize_map(0);
961 for ( ; __first != __last; ++__first)
967 __throw_exception_again;
971 template <class _Tp, class _Alloc> template <class _ForwardIterator>
972 void deque<_Tp,_Alloc>::_M_range_initialize(_ForwardIterator __first,
973 _ForwardIterator __last,
974 forward_iterator_tag)
977 distance(__first, __last, __n);
978 _M_initialize_map(__n);
980 _Map_pointer __cur_node;
982 for (__cur_node = _M_start._M_node;
983 __cur_node < _M_finish._M_node;
985 _ForwardIterator __mid = __first;
986 advance(__mid, _S_buffer_size());
987 uninitialized_copy(__first, __mid, *__cur_node);
990 uninitialized_copy(__first, __last, _M_finish._M_first);
994 _Destroy(_M_start, iterator(*__cur_node, __cur_node));
995 __throw_exception_again;
999 // Called only if _M_finish._M_cur == _M_finish._M_last - 1.
1000 template <class _Tp, class _Alloc>
1002 deque<_Tp,_Alloc>::_M_push_back_aux(const value_type& __t)
1004 value_type __t_copy = __t;
1005 _M_reserve_map_at_back();
1006 *(_M_finish._M_node + 1) = _M_allocate_node();
1008 _Construct(_M_finish._M_cur, __t_copy);
1009 _M_finish._M_set_node(_M_finish._M_node + 1);
1010 _M_finish._M_cur = _M_finish._M_first;
1014 _M_deallocate_node(*(_M_finish._M_node + 1));
1015 __throw_exception_again;
1019 // Called only if _M_finish._M_cur == _M_finish._M_last - 1.
1020 template <class _Tp, class _Alloc>
1022 deque<_Tp,_Alloc>::_M_push_back_aux()
1024 _M_reserve_map_at_back();
1025 *(_M_finish._M_node + 1) = _M_allocate_node();
1027 _Construct(_M_finish._M_cur);
1028 _M_finish._M_set_node(_M_finish._M_node + 1);
1029 _M_finish._M_cur = _M_finish._M_first;
1033 _M_deallocate_node(*(_M_finish._M_node + 1));
1034 __throw_exception_again;
1038 // Called only if _M_start._M_cur == _M_start._M_first.
1039 template <class _Tp, class _Alloc>
1041 deque<_Tp,_Alloc>::_M_push_front_aux(const value_type& __t)
1043 value_type __t_copy = __t;
1044 _M_reserve_map_at_front();
1045 *(_M_start._M_node - 1) = _M_allocate_node();
1047 _M_start._M_set_node(_M_start._M_node - 1);
1048 _M_start._M_cur = _M_start._M_last - 1;
1049 _Construct(_M_start._M_cur, __t_copy);
1054 _M_deallocate_node(*(_M_start._M_node - 1));
1055 __throw_exception_again;
1059 // Called only if _M_start._M_cur == _M_start._M_first.
1060 template <class _Tp, class _Alloc>
1062 deque<_Tp,_Alloc>::_M_push_front_aux()
1064 _M_reserve_map_at_front();
1065 *(_M_start._M_node - 1) = _M_allocate_node();
1067 _M_start._M_set_node(_M_start._M_node - 1);
1068 _M_start._M_cur = _M_start._M_last - 1;
1069 _Construct(_M_start._M_cur);
1074 _M_deallocate_node(*(_M_start._M_node - 1));
1075 __throw_exception_again;
1079 // Called only if _M_finish._M_cur == _M_finish._M_first.
1080 template <class _Tp, class _Alloc>
1081 void deque<_Tp,_Alloc>::_M_pop_back_aux()
1083 _M_deallocate_node(_M_finish._M_first);
1084 _M_finish._M_set_node(_M_finish._M_node - 1);
1085 _M_finish._M_cur = _M_finish._M_last - 1;
1086 _Destroy(_M_finish._M_cur);
1089 // Called only if _M_start._M_cur == _M_start._M_last - 1. Note that
1090 // if the deque has at least one element (a precondition for this member
1091 // function), and if _M_start._M_cur == _M_start._M_last, then the deque
1092 // must have at least two nodes.
1093 template <class _Tp, class _Alloc>
1094 void deque<_Tp,_Alloc>::_M_pop_front_aux()
1096 _Destroy(_M_start._M_cur);
1097 _M_deallocate_node(_M_start._M_first);
1098 _M_start._M_set_node(_M_start._M_node + 1);
1099 _M_start._M_cur = _M_start._M_first;
1102 template <class _Tp, class _Alloc> template <class _InputIterator>
1103 void deque<_Tp,_Alloc>::insert(iterator __pos,
1104 _InputIterator __first, _InputIterator __last,
1107 copy(__first, __last, inserter(*this, __pos));
1110 template <class _Tp, class _Alloc> template <class _ForwardIterator>
1112 deque<_Tp,_Alloc>::insert(iterator __pos,
1113 _ForwardIterator __first, _ForwardIterator __last,
1114 forward_iterator_tag) {
1116 distance(__first, __last, __n);
1117 if (__pos._M_cur == _M_start._M_cur) {
1118 iterator __new_start = _M_reserve_elements_at_front(__n);
1120 uninitialized_copy(__first, __last, __new_start);
1121 _M_start = __new_start;
1125 _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
1126 __throw_exception_again;
1129 else if (__pos._M_cur == _M_finish._M_cur) {
1130 iterator __new_finish = _M_reserve_elements_at_back(__n);
1132 uninitialized_copy(__first, __last, _M_finish);
1133 _M_finish = __new_finish;
1137 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);
1138 __throw_exception_again;
1142 _M_insert_aux(__pos, __first, __last, __n);
1145 template <class _Tp, class _Alloc>
1146 typename deque<_Tp, _Alloc>::iterator
1147 deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos, const value_type& __x)
1149 difference_type __index = __pos - _M_start;
1150 value_type __x_copy = __x;
1151 if (static_cast<size_type>(__index) < size() / 2) {
1152 push_front(front());
1153 iterator __front1 = _M_start;
1155 iterator __front2 = __front1;
1157 __pos = _M_start + __index;
1158 iterator __pos1 = __pos;
1160 copy(__front2, __pos1, __front1);
1164 iterator __back1 = _M_finish;
1166 iterator __back2 = __back1;
1168 __pos = _M_start + __index;
1169 copy_backward(__pos, __back2, __back1);
1175 template <class _Tp, class _Alloc>
1176 typename deque<_Tp,_Alloc>::iterator
1177 deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos)
1179 difference_type __index = __pos - _M_start;
1180 if (static_cast<size_type>(__index) < size() / 2) {
1181 push_front(front());
1182 iterator __front1 = _M_start;
1184 iterator __front2 = __front1;
1186 __pos = _M_start + __index;
1187 iterator __pos1 = __pos;
1189 copy(__front2, __pos1, __front1);
1193 iterator __back1 = _M_finish;
1195 iterator __back2 = __back1;
1197 __pos = _M_start + __index;
1198 copy_backward(__pos, __back2, __back1);
1200 *__pos = value_type();
1204 template <class _Tp, class _Alloc>
1205 void deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos,
1207 const value_type& __x)
1209 const difference_type __elems_before = __pos - _M_start;
1210 size_type __length = this->size();
1211 value_type __x_copy = __x;
1212 if (__elems_before < difference_type(__length / 2)) {
1213 iterator __new_start = _M_reserve_elements_at_front(__n);
1214 iterator __old_start = _M_start;
1215 __pos = _M_start + __elems_before;
1217 if (__elems_before >= difference_type(__n)) {
1218 iterator __start_n = _M_start + difference_type(__n);
1219 uninitialized_copy(_M_start, __start_n, __new_start);
1220 _M_start = __new_start;
1221 copy(__start_n, __pos, __old_start);
1222 fill(__pos - difference_type(__n), __pos, __x_copy);
1225 __uninitialized_copy_fill(_M_start, __pos, __new_start,
1226 _M_start, __x_copy);
1227 _M_start = __new_start;
1228 fill(__old_start, __pos, __x_copy);
1233 _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
1234 __throw_exception_again;
1238 iterator __new_finish = _M_reserve_elements_at_back(__n);
1239 iterator __old_finish = _M_finish;
1240 const difference_type __elems_after =
1241 difference_type(__length) - __elems_before;
1242 __pos = _M_finish - __elems_after;
1244 if (__elems_after > difference_type(__n)) {
1245 iterator __finish_n = _M_finish - difference_type(__n);
1246 uninitialized_copy(__finish_n, _M_finish, _M_finish);
1247 _M_finish = __new_finish;
1248 copy_backward(__pos, __finish_n, __old_finish);
1249 fill(__pos, __pos + difference_type(__n), __x_copy);
1252 __uninitialized_fill_copy(_M_finish, __pos + difference_type(__n),
1253 __x_copy, __pos, _M_finish);
1254 _M_finish = __new_finish;
1255 fill(__pos, __old_finish, __x_copy);
1260 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);
1261 __throw_exception_again;
1266 template <class _Tp, class _Alloc> template <class _ForwardIterator>
1267 void deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos,
1268 _ForwardIterator __first,
1269 _ForwardIterator __last,
1272 const difference_type __elemsbefore = __pos - _M_start;
1273 size_type __length = size();
1274 if (static_cast<size_type>(__elemsbefore) < __length / 2) {
1275 iterator __new_start = _M_reserve_elements_at_front(__n);
1276 iterator __old_start = _M_start;
1277 __pos = _M_start + __elemsbefore;
1279 if (__elemsbefore >= difference_type(__n)) {
1280 iterator __start_n = _M_start + difference_type(__n);
1281 uninitialized_copy(_M_start, __start_n, __new_start);
1282 _M_start = __new_start;
1283 copy(__start_n, __pos, __old_start);
1284 copy(__first, __last, __pos - difference_type(__n));
1287 _ForwardIterator __mid = __first;
1288 advance(__mid, difference_type(__n) - __elemsbefore);
1289 __uninitialized_copy_copy(_M_start, __pos, __first, __mid,
1291 _M_start = __new_start;
1292 copy(__mid, __last, __old_start);
1297 _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
1298 __throw_exception_again;
1302 iterator __new_finish = _M_reserve_elements_at_back(__n);
1303 iterator __old_finish = _M_finish;
1304 const difference_type __elemsafter =
1305 difference_type(__length) - __elemsbefore;
1306 __pos = _M_finish - __elemsafter;
1308 if (__elemsafter > difference_type(__n)) {
1309 iterator __finish_n = _M_finish - difference_type(__n);
1310 uninitialized_copy(__finish_n, _M_finish, _M_finish);
1311 _M_finish = __new_finish;
1312 copy_backward(__pos, __finish_n, __old_finish);
1313 copy(__first, __last, __pos);
1316 _ForwardIterator __mid = __first;
1317 advance(__mid, __elemsafter);
1318 __uninitialized_copy_copy(__mid, __last, __pos, _M_finish, _M_finish);
1319 _M_finish = __new_finish;
1320 copy(__first, __mid, __pos);
1325 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);
1326 __throw_exception_again;
1331 template <class _Tp, class _Alloc>
1332 void deque<_Tp,_Alloc>::_M_new_elements_at_front(size_type __new_elems)
1334 size_type __new_nodes
1335 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
1336 _M_reserve_map_at_front(__new_nodes);
1339 for (__i = 1; __i <= __new_nodes; ++__i)
1340 *(_M_start._M_node - __i) = _M_allocate_node();
1343 for (size_type __j = 1; __j < __i; ++__j)
1344 _M_deallocate_node(*(_M_start._M_node - __j));
1349 template <class _Tp, class _Alloc>
1350 void deque<_Tp,_Alloc>::_M_new_elements_at_back(size_type __new_elems)
1352 size_type __new_nodes
1353 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
1354 _M_reserve_map_at_back(__new_nodes);
1357 for (__i = 1; __i <= __new_nodes; ++__i)
1358 *(_M_finish._M_node + __i) = _M_allocate_node();
1361 for (size_type __j = 1; __j < __i; ++__j)
1362 _M_deallocate_node(*(_M_finish._M_node + __j));
1367 template <class _Tp, class _Alloc>
1368 void deque<_Tp,_Alloc>::_M_reallocate_map(size_type __nodes_to_add,
1369 bool __add_at_front)
1371 size_type __old_num_nodes = _M_finish._M_node - _M_start._M_node + 1;
1372 size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
1374 _Map_pointer __new_nstart;
1375 if (_M_map_size > 2 * __new_num_nodes) {
1376 __new_nstart = _M_map + (_M_map_size - __new_num_nodes) / 2
1377 + (__add_at_front ? __nodes_to_add : 0);
1378 if (__new_nstart < _M_start._M_node)
1379 copy(_M_start._M_node, _M_finish._M_node + 1, __new_nstart);
1381 copy_backward(_M_start._M_node, _M_finish._M_node + 1,
1382 __new_nstart + __old_num_nodes);
1385 size_type __new_map_size =
1386 _M_map_size + max(_M_map_size, __nodes_to_add) + 2;
1388 _Map_pointer __new_map = _M_allocate_map(__new_map_size);
1389 __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
1390 + (__add_at_front ? __nodes_to_add : 0);
1391 copy(_M_start._M_node, _M_finish._M_node + 1, __new_nstart);
1392 _M_deallocate_map(_M_map, _M_map_size);
1395 _M_map_size = __new_map_size;
1398 _M_start._M_set_node(__new_nstart);
1399 _M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
1403 // Nonmember functions.
1405 template <class _Tp, class _Alloc>
1406 inline bool operator==(const deque<_Tp, _Alloc>& __x,
1407 const deque<_Tp, _Alloc>& __y) {
1408 return __x.size() == __y.size() &&
1409 equal(__x.begin(), __x.end(), __y.begin());
1412 template <class _Tp, class _Alloc>
1413 inline bool operator<(const deque<_Tp, _Alloc>& __x,
1414 const deque<_Tp, _Alloc>& __y) {
1415 return lexicographical_compare(__x.begin(), __x.end(),
1416 __y.begin(), __y.end());
1419 template <class _Tp, class _Alloc>
1420 inline bool operator!=(const deque<_Tp, _Alloc>& __x,
1421 const deque<_Tp, _Alloc>& __y) {
1422 return !(__x == __y);
1425 template <class _Tp, class _Alloc>
1426 inline bool operator>(const deque<_Tp, _Alloc>& __x,
1427 const deque<_Tp, _Alloc>& __y) {
1431 template <class _Tp, class _Alloc>
1432 inline bool operator<=(const deque<_Tp, _Alloc>& __x,
1433 const deque<_Tp, _Alloc>& __y) {
1434 return !(__y < __x);
1436 template <class _Tp, class _Alloc>
1437 inline bool operator>=(const deque<_Tp, _Alloc>& __x,
1438 const deque<_Tp, _Alloc>& __y) {
1439 return !(__x < __y);
1442 template <class _Tp, class _Alloc>
1443 inline void swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y) {
1449 #endif /* __SGI_STL_INTERNAL_DEQUE_H */