stl_bvector.h: Change calls to 3-argument distance() into standard 2-argument version.
[official-gcc.git] / libstdc++-v3 / include / bits / stl_deque.h
blobb2d58d8f0a69fedfd7a0ce1c95ab086c4bd77bbc
1 // deque implementation -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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.
32 * Copyright (c) 1994
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.
44 * Copyright (c) 1997
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 /** @file stl_deque.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
61 #include <bits/concept_check.h>
62 #include <bits/stl_iterator_base_types.h>
63 #include <bits/stl_iterator_base_funcs.h>
65 #ifndef __GLIBCPP_INTERNAL_DEQUE_H
66 #define __GLIBCPP_INTERNAL_DEQUE_H
69 // Since this entire file is within namespace std, there's no reason to
70 // waste two spaces along the left column. Thus the leading indentation is
71 // slightly violated from here on.
72 namespace std
75 /**
76 * @maint
77 * @brief This function controls the size of memory nodes.
78 * @param size The size of an element.
79 * @return The number (not bytesize) of elements per node.
81 * This function started off as a compiler kludge from SGI, but seems to
82 * be a useful wrapper around a repeated constant expression.
83 * @endmaint
85 inline size_t
86 __deque_buf_size(size_t __size)
87 { return __size < 512 ? size_t(512 / __size) : size_t(1); }
90 /// A deque::iterator.
91 /**
92 * Quite a bit of intelligence here. Much of the functionality of deque is
93 * actually passed off to this class. A deque holds two of these internally,
94 * marking its valid range. Access to elements is done as offsets of either
95 * of those two, relying on operator overloading in this class.
97 * @maint
98 * All the functions are op overloads except for _M_set_node.
99 * @endmaint
101 template <class _Tp, class _Ref, class _Ptr>
102 struct _Deque_iterator
104 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
105 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
106 static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); }
108 typedef random_access_iterator_tag iterator_category;
109 typedef _Tp value_type;
110 typedef _Ptr pointer;
111 typedef _Ref reference;
112 typedef size_t size_type;
113 typedef ptrdiff_t difference_type;
114 typedef _Tp** _Map_pointer;
115 typedef _Deque_iterator _Self;
117 _Tp* _M_cur;
118 _Tp* _M_first;
119 _Tp* _M_last;
120 _Map_pointer _M_node;
122 _Deque_iterator(_Tp* __x, _Map_pointer __y)
123 : _M_cur(__x), _M_first(*__y),
124 _M_last(*__y + _S_buffer_size()), _M_node(__y) {}
125 _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
126 _Deque_iterator(const iterator& __x)
127 : _M_cur(__x._M_cur), _M_first(__x._M_first),
128 _M_last(__x._M_last), _M_node(__x._M_node) {}
130 reference operator*() const { return *_M_cur; }
131 pointer operator->() const { return _M_cur; }
133 difference_type operator-(const _Self& __x) const {
134 return difference_type(_S_buffer_size()) * (_M_node - __x._M_node - 1) +
135 (_M_cur - _M_first) + (__x._M_last - __x._M_cur);
138 _Self& operator++() {
139 ++_M_cur;
140 if (_M_cur == _M_last) {
141 _M_set_node(_M_node + 1);
142 _M_cur = _M_first;
144 return *this;
146 _Self operator++(int) {
147 _Self __tmp = *this;
148 ++*this;
149 return __tmp;
152 _Self& operator--() {
153 if (_M_cur == _M_first) {
154 _M_set_node(_M_node - 1);
155 _M_cur = _M_last;
157 --_M_cur;
158 return *this;
160 _Self operator--(int) {
161 _Self __tmp = *this;
162 --*this;
163 return __tmp;
166 _Self& operator+=(difference_type __n)
168 difference_type __offset = __n + (_M_cur - _M_first);
169 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
170 _M_cur += __n;
171 else {
172 difference_type __node_offset =
173 __offset > 0 ? __offset / difference_type(_S_buffer_size())
174 : -difference_type((-__offset - 1) / _S_buffer_size()) - 1;
175 _M_set_node(_M_node + __node_offset);
176 _M_cur = _M_first +
177 (__offset - __node_offset * difference_type(_S_buffer_size()));
179 return *this;
182 _Self operator+(difference_type __n) const
184 _Self __tmp = *this;
185 return __tmp += __n;
188 _Self& operator-=(difference_type __n) { return *this += -__n; }
190 _Self operator-(difference_type __n) const {
191 _Self __tmp = *this;
192 return __tmp -= __n;
195 reference operator[](difference_type __n) const { return *(*this + __n); }
197 bool operator==(const _Self& __x) const { return _M_cur == __x._M_cur; }
198 bool operator!=(const _Self& __x) const { return !(*this == __x); }
199 bool operator<(const _Self& __x) const {
200 return (_M_node == __x._M_node) ?
201 (_M_cur < __x._M_cur) : (_M_node < __x._M_node);
203 bool operator>(const _Self& __x) const { return __x < *this; }
204 bool operator<=(const _Self& __x) const { return !(__x < *this); }
205 bool operator>=(const _Self& __x) const { return !(*this < __x); }
207 /** @maint
208 * Prepares to traverse new_node. Sets everything except _M_cur, which
209 * should therefore be set by the caller immediately afterwards, based on
210 * _M_first and _M_last.
211 * @endmaint
213 void _M_set_node(_Map_pointer __new_node) {
214 _M_node = __new_node;
215 _M_first = *__new_node;
216 _M_last = _M_first + difference_type(_S_buffer_size());
220 template <class _Tp, class _Ref, class _Ptr>
221 inline _Deque_iterator<_Tp, _Ref, _Ptr>
222 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
224 return __x + __n;
228 /// @maint Primary default version. @endmaint
230 * @maint
231 * Deque base class. It has two purposes. First, its constructor
232 * and destructor allocate (but don't initialize) storage. This makes
233 * exception safety easier. Second, the base class encapsulates all of
234 * the differences between SGI-style allocators and standard-conforming
235 * allocators. There are two versions: this ordinary one, and the
236 * space-saving specialization for instanceless allocators.
237 * @endmaint
239 template <class _Tp, class _Alloc, bool __is_static>
240 class _Deque_alloc_base
242 public:
243 typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type;
244 allocator_type get_allocator() const { return _M_node_allocator; }
246 _Deque_alloc_base(const allocator_type& __a)
247 : _M_node_allocator(__a), _M_map_allocator(__a),
248 _M_map(0), _M_map_size(0)
251 protected:
252 typedef typename _Alloc_traits<_Tp*, _Alloc>::allocator_type
253 _Map_allocator_type;
255 allocator_type _M_node_allocator;
256 _Map_allocator_type _M_map_allocator;
258 _Tp* _M_allocate_node() {
259 return _M_node_allocator.allocate(__deque_buf_size(sizeof(_Tp)));
261 void _M_deallocate_node(_Tp* __p) {
262 _M_node_allocator.deallocate(__p, __deque_buf_size(sizeof(_Tp)));
264 _Tp** _M_allocate_map(size_t __n)
265 { return _M_map_allocator.allocate(__n); }
266 void _M_deallocate_map(_Tp** __p, size_t __n)
267 { _M_map_allocator.deallocate(__p, __n); }
269 _Tp** _M_map;
270 size_t _M_map_size;
273 /// Specialization for instanceless allocators.
274 template <class _Tp, class _Alloc>
275 class _Deque_alloc_base<_Tp, _Alloc, true>
277 public:
278 typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type;
279 allocator_type get_allocator() const { return allocator_type(); }
281 _Deque_alloc_base(const allocator_type&) : _M_map(0), _M_map_size(0) {}
283 protected:
284 typedef typename _Alloc_traits<_Tp, _Alloc>::_Alloc_type _Node_alloc_type;
285 typedef typename _Alloc_traits<_Tp*, _Alloc>::_Alloc_type _Map_alloc_type;
287 _Tp* _M_allocate_node() {
288 return _Node_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
290 void _M_deallocate_node(_Tp* __p) {
291 _Node_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
293 _Tp** _M_allocate_map(size_t __n)
294 { return _Map_alloc_type::allocate(__n); }
295 void _M_deallocate_map(_Tp** __p, size_t __n)
296 { _Map_alloc_type::deallocate(__p, __n); }
298 _Tp** _M_map;
299 size_t _M_map_size;
304 * @maint
305 * Deque base class. Using _Alloc_traits in the instantiation of the parent
306 * class provides the compile-time dispatching mentioned in the parent's docs.
307 * This class provides the unified face for deque's allocation.
309 * Nothing in this class ever constructs or destroys an actual Tp element.
310 * (Deque handles that itself.) Only/All memory management is performed here.
311 * @endmaint
313 template <class _Tp, class _Alloc>
314 class _Deque_base
315 : public _Deque_alloc_base<_Tp,_Alloc,
316 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
318 public:
319 typedef _Deque_alloc_base<_Tp,_Alloc,
320 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
321 _Base;
322 typedef typename _Base::allocator_type allocator_type;
323 typedef _Deque_iterator<_Tp,_Tp&,_Tp*> iterator;
324 typedef _Deque_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
326 _Deque_base(const allocator_type& __a, size_t __num_elements)
327 : _Base(__a), _M_start(), _M_finish()
328 { _M_initialize_map(__num_elements); }
329 _Deque_base(const allocator_type& __a)
330 : _Base(__a), _M_start(), _M_finish() {}
331 ~_Deque_base();
333 protected:
334 void _M_initialize_map(size_t);
335 void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
336 void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
337 enum { _S_initial_map_size = 8 };
339 protected:
340 iterator _M_start;
341 iterator _M_finish;
345 template <class _Tp, class _Alloc>
346 _Deque_base<_Tp,_Alloc>::~_Deque_base()
348 if (_M_map) {
349 _M_destroy_nodes(_M_start._M_node, _M_finish._M_node + 1);
350 _M_deallocate_map(_M_map, _M_map_size);
355 * @maint
356 * @brief Layout storage.
357 * @param num_elements The count of T's for which to allocate space at first.
358 * @return Nothing.
360 * The initial underlying memory layout is a bit complicated...
361 * @endmaint
363 template <class _Tp, class _Alloc>
364 void
365 _Deque_base<_Tp,_Alloc>::_M_initialize_map(size_t __num_elements)
367 size_t __num_nodes =
368 __num_elements / __deque_buf_size(sizeof(_Tp)) + 1;
370 _M_map_size = max((size_t) _S_initial_map_size, __num_nodes + 2);
371 _M_map = _M_allocate_map(_M_map_size);
373 _Tp** __nstart = _M_map + (_M_map_size - __num_nodes) / 2;
374 _Tp** __nfinish = __nstart + __num_nodes;
376 try
377 { _M_create_nodes(__nstart, __nfinish); }
378 catch(...)
380 _M_deallocate_map(_M_map, _M_map_size);
381 _M_map = 0;
382 _M_map_size = 0;
383 __throw_exception_again;
386 _M_start._M_set_node(__nstart);
387 _M_finish._M_set_node(__nfinish - 1);
388 _M_start._M_cur = _M_start._M_first;
389 _M_finish._M_cur = _M_finish._M_first +
390 __num_elements % __deque_buf_size(sizeof(_Tp));
393 template <class _Tp, class _Alloc>
394 void _Deque_base<_Tp,_Alloc>::_M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
396 _Tp** __cur;
397 try {
398 for (__cur = __nstart; __cur < __nfinish; ++__cur)
399 *__cur = _M_allocate_node();
401 catch(...)
403 _M_destroy_nodes(__nstart, __cur);
404 __throw_exception_again;
408 template <class _Tp, class _Alloc>
409 void
410 _Deque_base<_Tp,_Alloc>::_M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
412 for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
413 _M_deallocate_node(*__n);
418 * Placeholder: see http://www.sgi.com/tech/stl/Deque.html for now.
420 * In previous HP/SGI versions of deque, there was an extra template parameter
421 * so users could control the node size. This extension turned out to violate
422 * the C++ standard (it can be detected using template template parameters),
423 * and it was removed.
425 * @maint
426 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
428 * - Tp** _M_map
429 * - size_t _M_map_size
430 * - iterator _M_start, _M_finish
432 * map_size is at least 8. map is an array of map_size pointers-to-"nodes".
434 * A "node" has no specific type name as such, but it is referred to as
435 * "node" in this file. It is a simple array-of-Tp. If Tp is very large,
436 * there will be one Tp element per node (i.e., an "array" of one).
437 * For non-huge Tp's, node size is inversely related to Tp size: the
438 * larger the Tp, the fewer Tp's will fit in a node. The goal here is to
439 * keep the total size of a node relatively small and constant over different
440 * Tp's, to improve allocator efficiency.
442 * **** As I write this, the nodes are /not/ allocated using the high-speed
443 * memory pool. There are 20 hours left in the year; perhaps I can fix
444 * this before 2002.
446 * Not every pointer in the map array will point to a node. If the initial
447 * number of elements in the deque is small, the /middle/ map pointers will
448 * be valid, and the ones at the edges will be unused. This same situation
449 * will arise as the map grows: available map pointers, if any, will be on
450 * the ends. As new nodes are created, only a subset of the map's pointers
451 * need to be copied "outward".
453 * Class invariants:
454 * - For any nonsingular iterator i:
455 * - i.node points to a member of the map array. (Yes, you read that
456 * correctly: i.node does not actually point to a node.) The member of
457 * the map array is what actually points to the node.
458 * - i.first == *(i.node) (This points to the node (first Tp element).)
459 * - i.last == i.first + node_size
460 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
461 * the implication of this is that i.cur is always a dereferenceable
462 * pointer, even if i is a past-the-end iterator.
463 * - Start and Finish are always nonsingular iterators. NOTE: this means that
464 * an empty deque must have one node, a deque with <N elements (where N is
465 * the node buffer size) must have one node, a deque with N through (2N-1)
466 * elements must have two nodes, etc.
467 * - For every node other than start.node and finish.node, every element in the
468 * node is an initialized object. If start.node == finish.node, then
469 * [start.cur, finish.cur) are initialized objects, and the elements outside
470 * that range are uninitialized storage. Otherwise, [start.cur, start.last)
471 * and [finish.first, finish.cur) are initialized objects, and [start.first,
472 * start.cur) and [finish.cur, finish.last) are uninitialized storage.
473 * - [map, map + map_size) is a valid, non-empty range.
474 * - [start.node, finish.node] is a valid range contained within
475 * [map, map + map_size).
476 * - A pointer in the range [map, map + map_size) points to an allocated node
477 * if and only if the pointer is in the range [start.node, finish.node].
479 * Here's the magic: nothing in deque is "aware" of the discontiguous storage!
481 * The memory setup and layout occurs in the parent, _Base, and the iterator
482 * class is entirely responsible for "leaping" from one node to the next. All
483 * the implementation routines for deque itself work only through the start
484 * and finish iterators. This keeps the routines simple and sane, and we can
485 * use other standard algorithms as well.
487 * @endmaint
489 template <class _Tp, class _Alloc = allocator<_Tp> >
490 class deque : protected _Deque_base<_Tp, _Alloc>
492 // concept requirements
493 __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
495 typedef _Deque_base<_Tp, _Alloc> _Base;
497 public:
498 typedef _Tp value_type;
499 typedef value_type* pointer;
500 typedef const value_type* const_pointer;
501 typedef value_type& reference;
502 typedef const value_type& const_reference;
503 typedef size_t size_type;
504 typedef ptrdiff_t difference_type;
506 typedef typename _Base::allocator_type allocator_type;
507 allocator_type get_allocator() const { return _Base::get_allocator(); }
509 typedef typename _Base::iterator iterator;
510 typedef typename _Base::const_iterator const_iterator;
511 typedef reverse_iterator<const_iterator> const_reverse_iterator;
512 typedef reverse_iterator<iterator> reverse_iterator;
514 protected:
515 typedef pointer* _Map_pointer;
516 static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); }
518 // Functions controlling memory layout, and nothing else.
519 using _Base::_M_initialize_map;
520 using _Base::_M_create_nodes;
521 using _Base::_M_destroy_nodes;
522 using _Base::_M_allocate_node;
523 using _Base::_M_deallocate_node;
524 using _Base::_M_allocate_map;
525 using _Base::_M_deallocate_map;
527 /** @maint
528 * A total of four data members accumulated down the heirarchy. If the
529 * _Alloc type requires separate instances, then two of them will also be
530 * included in each deque.
531 * @endmaint
533 using _Base::_M_map;
534 using _Base::_M_map_size;
535 using _Base::_M_start;
536 using _Base::_M_finish;
538 public: // Basic accessors
539 iterator begin() { return _M_start; }
540 iterator end() { return _M_finish; }
541 const_iterator begin() const { return _M_start; }
542 const_iterator end() const { return _M_finish; }
544 reverse_iterator rbegin() { return reverse_iterator(_M_finish); }
545 reverse_iterator rend() { return reverse_iterator(_M_start); }
546 const_reverse_iterator rbegin() const
547 { return const_reverse_iterator(_M_finish); }
548 const_reverse_iterator rend() const
549 { return const_reverse_iterator(_M_start); }
551 reference operator[](size_type __n)
552 { return _M_start[difference_type(__n)]; }
553 const_reference operator[](size_type __n) const
554 { return _M_start[difference_type(__n)]; }
556 void _M_range_check(size_type __n) const {
557 if (__n >= this->size())
558 __throw_range_error("deque");
561 reference at(size_type __n)
562 { _M_range_check(__n); return (*this)[__n]; }
563 const_reference at(size_type __n) const
564 { _M_range_check(__n); return (*this)[__n]; }
566 reference front() { return *_M_start; }
567 reference back() {
568 iterator __tmp = _M_finish;
569 --__tmp;
570 return *__tmp;
572 const_reference front() const { return *_M_start; }
573 const_reference back() const {
574 const_iterator __tmp = _M_finish;
575 --__tmp;
576 return *__tmp;
579 size_type size() const { return _M_finish - _M_start; }
580 size_type max_size() const { return size_type(-1); }
581 bool empty() const { return _M_finish == _M_start; }
583 public: // Constructor, destructor.
584 explicit deque(const allocator_type& __a = allocator_type())
585 : _Base(__a, 0) {}
586 deque(const deque& __x) : _Base(__x.get_allocator(), __x.size())
587 { uninitialized_copy(__x.begin(), __x.end(), _M_start); }
588 deque(size_type __n, const value_type& __value,
589 const allocator_type& __a = allocator_type()) : _Base(__a, __n)
590 { _M_fill_initialize(__value); }
592 explicit
593 deque(size_type __n)
594 : _Base(allocator_type(), __n)
595 { _M_fill_initialize(value_type()); }
597 // Check whether it's an integral type. If so, it's not an iterator.
598 template<class _InputIterator>
599 deque(_InputIterator __first, _InputIterator __last,
600 const allocator_type& __a = allocator_type())
601 : _Base(__a)
603 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
604 _M_initialize_dispatch(__first, __last, _Integral());
607 template<class _Integer>
608 void
609 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
611 _M_initialize_map(__n);
612 _M_fill_initialize(__x);
615 template<class _InputIter>
616 void
617 _M_initialize_dispatch(_InputIter __first, _InputIter __last, __false_type)
619 typedef typename iterator_traits<_InputIter>::iterator_category _IterCategory;
620 _M_range_initialize(__first, __last, _IterCategory());
623 ~deque()
624 { _Destroy(_M_start, _M_finish); }
626 deque& operator= (const deque& __x) {
627 const size_type __len = size();
628 if (&__x != this) {
629 if (__len >= __x.size())
630 erase(copy(__x.begin(), __x.end(), _M_start), _M_finish);
631 else {
632 const_iterator __mid = __x.begin() + difference_type(__len);
633 copy(__x.begin(), __mid, _M_start);
634 insert(_M_finish, __mid, __x.end());
637 return *this;
640 void swap(deque& __x) {
641 std::swap(_M_start, __x._M_start);
642 std::swap(_M_finish, __x._M_finish);
643 std::swap(_M_map, __x._M_map);
644 std::swap(_M_map_size, __x._M_map_size);
647 public:
648 // assign(), a generalized assignment member function. Two
649 // versions: one that takes a count, and one that takes a range.
650 // The range version is a member template, so we dispatch on whether
651 // or not the type is an integer.
653 void _M_fill_assign(size_type __n, const _Tp& __val) {
654 if (__n > size()) {
655 fill(begin(), end(), __val);
656 insert(end(), __n - size(), __val);
658 else {
659 erase(begin() + __n, end());
660 fill(begin(), end(), __val);
664 void
665 assign(size_type __n, const _Tp& __val)
666 { _M_fill_assign(__n, __val); }
668 template<class _InputIterator>
669 void
670 assign(_InputIterator __first, _InputIterator __last)
672 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
673 _M_assign_dispatch(__first, __last, _Integral());
676 private: // helper functions for assign()
678 template<class _Integer>
679 void
680 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
681 { _M_fill_assign(static_cast<size_type>(__n), static_cast<_Tp>(__val)); }
683 template<class _InputIterator>
684 void
685 _M_assign_dispatch(_InputIterator __first, _InputIterator __last, __false_type)
687 typedef typename iterator_traits<_InputIterator>::iterator_category _IterCategory;
688 _M_assign_aux(__first, __last, _IterCategory());
691 template <class _InputIterator>
692 void _M_assign_aux(_InputIterator __first, _InputIterator __last,
693 input_iterator_tag);
695 template <class _ForwardIterator>
696 void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
697 forward_iterator_tag) {
698 size_type __len = distance(__first, __last);
699 if (__len > size()) {
700 _ForwardIterator __mid = __first;
701 advance(__mid, size());
702 copy(__first, __mid, begin());
703 insert(end(), __mid, __last);
705 else
706 erase(copy(__first, __last, begin()), end());
709 public: // push_* and pop_*
711 void
712 push_back(const value_type& __t)
714 if (_M_finish._M_cur != _M_finish._M_last - 1) {
715 _Construct(_M_finish._M_cur, __t);
716 ++_M_finish._M_cur;
718 else
719 _M_push_back_aux(__t);
722 void
723 push_back()
725 if (_M_finish._M_cur != _M_finish._M_last - 1) {
726 _Construct(_M_finish._M_cur);
727 ++_M_finish._M_cur;
729 else
730 _M_push_back_aux();
733 void
734 push_front(const value_type& __t)
736 if (_M_start._M_cur != _M_start._M_first) {
737 _Construct(_M_start._M_cur - 1, __t);
738 --_M_start._M_cur;
740 else
741 _M_push_front_aux(__t);
744 void
745 push_front()
747 if (_M_start._M_cur != _M_start._M_first) {
748 _Construct(_M_start._M_cur - 1);
749 --_M_start._M_cur;
751 else
752 _M_push_front_aux();
756 void
757 pop_back()
759 if (_M_finish._M_cur != _M_finish._M_first) {
760 --_M_finish._M_cur;
761 _Destroy(_M_finish._M_cur);
763 else
764 _M_pop_back_aux();
767 void
768 pop_front()
770 if (_M_start._M_cur != _M_start._M_last - 1) {
771 _Destroy(_M_start._M_cur);
772 ++_M_start._M_cur;
774 else
775 _M_pop_front_aux();
778 public: // Insert
780 iterator
781 insert(iterator position, const value_type& __x)
783 if (position._M_cur == _M_start._M_cur) {
784 push_front(__x);
785 return _M_start;
787 else if (position._M_cur == _M_finish._M_cur) {
788 push_back(__x);
789 iterator __tmp = _M_finish;
790 --__tmp;
791 return __tmp;
793 else {
794 return _M_insert_aux(position, __x);
798 iterator
799 insert(iterator __position)
800 { return insert(__position, value_type()); }
802 void
803 insert(iterator __pos, size_type __n, const value_type& __x)
804 { _M_fill_insert(__pos, __n, __x); }
806 void
807 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
809 // Check whether it's an integral type. If so, it's not an iterator.
810 template<class _InputIterator>
811 void
812 insert(iterator __pos, _InputIterator __first, _InputIterator __last)
814 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
815 _M_insert_dispatch(__pos, __first, __last, _Integral());
818 template<class _Integer>
819 void
820 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, __true_type)
821 { _M_fill_insert(__pos, static_cast<size_type>(__n), static_cast<value_type>(__x)); }
823 template<class _InputIterator>
824 void
825 _M_insert_dispatch(iterator __pos,
826 _InputIterator __first, _InputIterator __last,
827 __false_type)
829 typedef typename iterator_traits<_InputIterator>::iterator_category _IterCategory;
830 insert(__pos, __first, __last, _IterCategory());
833 void resize(size_type __new_size, const value_type& __x) {
834 const size_type __len = size();
835 if (__new_size < __len)
836 erase(_M_start + __new_size, _M_finish);
837 else
838 insert(_M_finish, __new_size - __len, __x);
841 void resize(size_type new_size) { resize(new_size, value_type()); }
843 public: // Erase
844 iterator erase(iterator __pos) {
845 iterator __next = __pos;
846 ++__next;
847 size_type __index = __pos - _M_start;
848 if (__index < (size() >> 1)) {
849 copy_backward(_M_start, __pos, __next);
850 pop_front();
852 else {
853 copy(__next, _M_finish, __pos);
854 pop_back();
856 return _M_start + __index;
859 iterator erase(iterator __first, iterator __last);
860 void clear();
862 protected: // Internal construction/destruction
864 void _M_fill_initialize(const value_type& __value);
866 template <class _InputIterator>
867 void _M_range_initialize(_InputIterator __first, _InputIterator __last,
868 input_iterator_tag);
870 template <class _ForwardIterator>
871 void _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
872 forward_iterator_tag);
874 protected: // Internal push_* and pop_*
876 void _M_push_back_aux(const value_type&);
877 void _M_push_back_aux();
878 void _M_push_front_aux(const value_type&);
879 void _M_push_front_aux();
880 void _M_pop_back_aux();
881 void _M_pop_front_aux();
883 protected: // Internal insert functions
885 template <class _InputIterator>
886 void insert(iterator __pos, _InputIterator __first, _InputIterator __last,
887 input_iterator_tag);
889 template <class _ForwardIterator>
890 void insert(iterator __pos,
891 _ForwardIterator __first, _ForwardIterator __last,
892 forward_iterator_tag);
894 iterator _M_insert_aux(iterator __pos, const value_type& __x);
895 iterator _M_insert_aux(iterator __pos);
896 void _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
898 template <class _ForwardIterator>
899 void _M_insert_aux(iterator __pos,
900 _ForwardIterator __first, _ForwardIterator __last,
901 size_type __n);
903 iterator _M_reserve_elements_at_front(size_type __n) {
904 size_type __vacancies = _M_start._M_cur - _M_start._M_first;
905 if (__n > __vacancies)
906 _M_new_elements_at_front(__n - __vacancies);
907 return _M_start - difference_type(__n);
910 iterator _M_reserve_elements_at_back(size_type __n) {
911 size_type __vacancies = (_M_finish._M_last - _M_finish._M_cur) - 1;
912 if (__n > __vacancies)
913 _M_new_elements_at_back(__n - __vacancies);
914 return _M_finish + difference_type(__n);
917 void _M_new_elements_at_front(size_type __new_elements);
918 void _M_new_elements_at_back(size_type __new_elements);
920 protected: // Allocation of _M_map and nodes
922 // Makes sure the _M_map has space for new nodes. Does not actually
923 // add the nodes. Can invalidate _M_map pointers. (And consequently,
924 // deque iterators.)
926 void _M_reserve_map_at_back (size_type __nodes_to_add = 1) {
927 if (__nodes_to_add + 1 > _M_map_size - (_M_finish._M_node - _M_map))
928 _M_reallocate_map(__nodes_to_add, false);
931 void _M_reserve_map_at_front (size_type __nodes_to_add = 1) {
932 if (__nodes_to_add > size_type(_M_start._M_node - _M_map))
933 _M_reallocate_map(__nodes_to_add, true);
936 void _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
939 // Non-inline member functions
941 template <class _Tp, class _Alloc>
942 template <class _InputIter>
943 void deque<_Tp, _Alloc>
944 ::_M_assign_aux(_InputIter __first, _InputIter __last, input_iterator_tag)
946 iterator __cur = begin();
947 for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
948 *__cur = *__first;
949 if (__first == __last)
950 erase(__cur, end());
951 else
952 insert(end(), __first, __last);
955 template <class _Tp, class _Alloc>
956 void deque<_Tp, _Alloc>::_M_fill_insert(iterator __pos,
957 size_type __n, const value_type& __x)
959 if (__pos._M_cur == _M_start._M_cur) {
960 iterator __new_start = _M_reserve_elements_at_front(__n);
961 try {
962 uninitialized_fill(__new_start, _M_start, __x);
963 _M_start = __new_start;
965 catch(...)
967 _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
968 __throw_exception_again;
971 else if (__pos._M_cur == _M_finish._M_cur) {
972 iterator __new_finish = _M_reserve_elements_at_back(__n);
973 try {
974 uninitialized_fill(_M_finish, __new_finish, __x);
975 _M_finish = __new_finish;
977 catch(...)
979 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);
980 __throw_exception_again;
983 else
984 _M_insert_aux(__pos, __n, __x);
987 template <class _Tp, class _Alloc>
988 typename deque<_Tp,_Alloc>::iterator
989 deque<_Tp,_Alloc>::erase(iterator __first, iterator __last)
991 if (__first == _M_start && __last == _M_finish) {
992 clear();
993 return _M_finish;
995 else {
996 difference_type __n = __last - __first;
997 difference_type __elems_before = __first - _M_start;
998 if (static_cast<size_type>(__elems_before) < (size() - __n) / 2) {
999 copy_backward(_M_start, __first, __last);
1000 iterator __new_start = _M_start + __n;
1001 _Destroy(_M_start, __new_start);
1002 _M_destroy_nodes(_M_start._M_node, __new_start._M_node);
1003 _M_start = __new_start;
1005 else {
1006 copy(__last, _M_finish, __first);
1007 iterator __new_finish = _M_finish - __n;
1008 _Destroy(__new_finish, _M_finish);
1009 _M_destroy_nodes(__new_finish._M_node + 1, _M_finish._M_node + 1);
1010 _M_finish = __new_finish;
1012 return _M_start + __elems_before;
1016 template <class _Tp, class _Alloc>
1017 void deque<_Tp,_Alloc>::clear()
1019 for (_Map_pointer __node = _M_start._M_node + 1;
1020 __node < _M_finish._M_node;
1021 ++__node) {
1022 _Destroy(*__node, *__node + _S_buffer_size());
1023 _M_deallocate_node(*__node);
1026 if (_M_start._M_node != _M_finish._M_node) {
1027 _Destroy(_M_start._M_cur, _M_start._M_last);
1028 _Destroy(_M_finish._M_first, _M_finish._M_cur);
1029 _M_deallocate_node(_M_finish._M_first);
1031 else
1032 _Destroy(_M_start._M_cur, _M_finish._M_cur);
1034 _M_finish = _M_start;
1038 * @maint
1039 * @brief Fills the deque with copies of value.
1040 * @param value Initial value.
1041 * @return Nothing.
1042 * @pre _M_start and _M_finish have already been initialized, but none of the
1043 * deque's elements have yet been constructed.
1045 * This function is called only when the user provides an explicit size (with
1046 * or without an explicit exemplar value).
1047 * @endmaint
1049 template <class _Tp, class _Alloc>
1050 void deque<_Tp,_Alloc>::_M_fill_initialize(const value_type& __value)
1052 _Map_pointer __cur;
1053 try {
1054 for (__cur = _M_start._M_node; __cur < _M_finish._M_node; ++__cur)
1055 uninitialized_fill(*__cur, *__cur + _S_buffer_size(), __value);
1056 uninitialized_fill(_M_finish._M_first, _M_finish._M_cur, __value);
1058 catch(...)
1060 _Destroy(_M_start, iterator(*__cur, __cur));
1061 __throw_exception_again;
1065 /** @{
1066 * @maint
1067 * @brief Fills the deque with whatever is in [first,last).
1068 * @param first An input iterator.
1069 * @param last An input iterator.
1070 * @return Nothing.
1072 * If the iterators are actually forward iterators (or better), then the
1073 * memory layout can be done all at once. Else we move forward using
1074 * push_back on each value from the iterator.
1075 * @endmaint
1077 template <class _Tp, class _Alloc> template <class _InputIterator>
1078 void deque<_Tp,_Alloc>::_M_range_initialize(_InputIterator __first,
1079 _InputIterator __last,
1080 input_iterator_tag)
1082 _M_initialize_map(0);
1083 try {
1084 for ( ; __first != __last; ++__first)
1085 push_back(*__first);
1087 catch(...)
1089 clear();
1090 __throw_exception_again;
1094 template <class _Tp, class _Alloc> template <class _ForwardIterator>
1095 void deque<_Tp,_Alloc>::_M_range_initialize(_ForwardIterator __first,
1096 _ForwardIterator __last,
1097 forward_iterator_tag)
1099 size_type __n = distance(__first, __last);
1100 _M_initialize_map(__n);
1102 _Map_pointer __cur_node;
1103 try {
1104 for (__cur_node = _M_start._M_node;
1105 __cur_node < _M_finish._M_node;
1106 ++__cur_node) {
1107 _ForwardIterator __mid = __first;
1108 advance(__mid, _S_buffer_size());
1109 uninitialized_copy(__first, __mid, *__cur_node);
1110 __first = __mid;
1112 uninitialized_copy(__first, __last, _M_finish._M_first);
1114 catch(...)
1116 _Destroy(_M_start, iterator(*__cur_node, __cur_node));
1117 __throw_exception_again;
1120 /** @} */
1122 // Called only if _M_finish._M_cur == _M_finish._M_last - 1.
1123 template <class _Tp, class _Alloc>
1124 void
1125 deque<_Tp,_Alloc>::_M_push_back_aux(const value_type& __t)
1127 value_type __t_copy = __t;
1128 _M_reserve_map_at_back();
1129 *(_M_finish._M_node + 1) = _M_allocate_node();
1130 try {
1131 _Construct(_M_finish._M_cur, __t_copy);
1132 _M_finish._M_set_node(_M_finish._M_node + 1);
1133 _M_finish._M_cur = _M_finish._M_first;
1135 catch(...)
1137 _M_deallocate_node(*(_M_finish._M_node + 1));
1138 __throw_exception_again;
1142 // Called only if _M_finish._M_cur == _M_finish._M_last - 1.
1143 template <class _Tp, class _Alloc>
1144 void
1145 deque<_Tp,_Alloc>::_M_push_back_aux()
1147 _M_reserve_map_at_back();
1148 *(_M_finish._M_node + 1) = _M_allocate_node();
1149 try {
1150 _Construct(_M_finish._M_cur);
1151 _M_finish._M_set_node(_M_finish._M_node + 1);
1152 _M_finish._M_cur = _M_finish._M_first;
1154 catch(...)
1156 _M_deallocate_node(*(_M_finish._M_node + 1));
1157 __throw_exception_again;
1161 // Called only if _M_start._M_cur == _M_start._M_first.
1162 template <class _Tp, class _Alloc>
1163 void
1164 deque<_Tp,_Alloc>::_M_push_front_aux(const value_type& __t)
1166 value_type __t_copy = __t;
1167 _M_reserve_map_at_front();
1168 *(_M_start._M_node - 1) = _M_allocate_node();
1169 try {
1170 _M_start._M_set_node(_M_start._M_node - 1);
1171 _M_start._M_cur = _M_start._M_last - 1;
1172 _Construct(_M_start._M_cur, __t_copy);
1174 catch(...)
1176 ++_M_start;
1177 _M_deallocate_node(*(_M_start._M_node - 1));
1178 __throw_exception_again;
1182 // Called only if _M_start._M_cur == _M_start._M_first.
1183 template <class _Tp, class _Alloc>
1184 void
1185 deque<_Tp,_Alloc>::_M_push_front_aux()
1187 _M_reserve_map_at_front();
1188 *(_M_start._M_node - 1) = _M_allocate_node();
1189 try {
1190 _M_start._M_set_node(_M_start._M_node - 1);
1191 _M_start._M_cur = _M_start._M_last - 1;
1192 _Construct(_M_start._M_cur);
1194 catch(...)
1196 ++_M_start;
1197 _M_deallocate_node(*(_M_start._M_node - 1));
1198 __throw_exception_again;
1202 // Called only if _M_finish._M_cur == _M_finish._M_first.
1203 template <class _Tp, class _Alloc>
1204 void deque<_Tp,_Alloc>::_M_pop_back_aux()
1206 _M_deallocate_node(_M_finish._M_first);
1207 _M_finish._M_set_node(_M_finish._M_node - 1);
1208 _M_finish._M_cur = _M_finish._M_last - 1;
1209 _Destroy(_M_finish._M_cur);
1212 // Called only if _M_start._M_cur == _M_start._M_last - 1. Note that
1213 // if the deque has at least one element (a precondition for this member
1214 // function), and if _M_start._M_cur == _M_start._M_last, then the deque
1215 // must have at least two nodes.
1216 template <class _Tp, class _Alloc>
1217 void deque<_Tp,_Alloc>::_M_pop_front_aux()
1219 _Destroy(_M_start._M_cur);
1220 _M_deallocate_node(_M_start._M_first);
1221 _M_start._M_set_node(_M_start._M_node + 1);
1222 _M_start._M_cur = _M_start._M_first;
1225 template <class _Tp, class _Alloc> template <class _InputIterator>
1226 void deque<_Tp,_Alloc>::insert(iterator __pos,
1227 _InputIterator __first, _InputIterator __last,
1228 input_iterator_tag)
1230 copy(__first, __last, inserter(*this, __pos));
1233 template <class _Tp, class _Alloc> template <class _ForwardIterator>
1234 void
1235 deque<_Tp,_Alloc>::insert(iterator __pos,
1236 _ForwardIterator __first, _ForwardIterator __last,
1237 forward_iterator_tag) {
1238 size_type __n = distance(__first, __last);
1239 if (__pos._M_cur == _M_start._M_cur) {
1240 iterator __new_start = _M_reserve_elements_at_front(__n);
1241 try {
1242 uninitialized_copy(__first, __last, __new_start);
1243 _M_start = __new_start;
1245 catch(...)
1247 _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
1248 __throw_exception_again;
1251 else if (__pos._M_cur == _M_finish._M_cur) {
1252 iterator __new_finish = _M_reserve_elements_at_back(__n);
1253 try {
1254 uninitialized_copy(__first, __last, _M_finish);
1255 _M_finish = __new_finish;
1257 catch(...)
1259 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);
1260 __throw_exception_again;
1263 else
1264 _M_insert_aux(__pos, __first, __last, __n);
1267 template <class _Tp, class _Alloc>
1268 typename deque<_Tp, _Alloc>::iterator
1269 deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos, const value_type& __x)
1271 difference_type __index = __pos - _M_start;
1272 value_type __x_copy = __x;
1273 if (static_cast<size_type>(__index) < size() / 2) {
1274 push_front(front());
1275 iterator __front1 = _M_start;
1276 ++__front1;
1277 iterator __front2 = __front1;
1278 ++__front2;
1279 __pos = _M_start + __index;
1280 iterator __pos1 = __pos;
1281 ++__pos1;
1282 copy(__front2, __pos1, __front1);
1284 else {
1285 push_back(back());
1286 iterator __back1 = _M_finish;
1287 --__back1;
1288 iterator __back2 = __back1;
1289 --__back2;
1290 __pos = _M_start + __index;
1291 copy_backward(__pos, __back2, __back1);
1293 *__pos = __x_copy;
1294 return __pos;
1297 template <class _Tp, class _Alloc>
1298 typename deque<_Tp,_Alloc>::iterator
1299 deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos)
1301 difference_type __index = __pos - _M_start;
1302 if (static_cast<size_type>(__index) < size() / 2) {
1303 push_front(front());
1304 iterator __front1 = _M_start;
1305 ++__front1;
1306 iterator __front2 = __front1;
1307 ++__front2;
1308 __pos = _M_start + __index;
1309 iterator __pos1 = __pos;
1310 ++__pos1;
1311 copy(__front2, __pos1, __front1);
1313 else {
1314 push_back(back());
1315 iterator __back1 = _M_finish;
1316 --__back1;
1317 iterator __back2 = __back1;
1318 --__back2;
1319 __pos = _M_start + __index;
1320 copy_backward(__pos, __back2, __back1);
1322 *__pos = value_type();
1323 return __pos;
1326 template <class _Tp, class _Alloc>
1327 void deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos,
1328 size_type __n,
1329 const value_type& __x)
1331 const difference_type __elems_before = __pos - _M_start;
1332 size_type __length = this->size();
1333 value_type __x_copy = __x;
1334 if (__elems_before < difference_type(__length / 2)) {
1335 iterator __new_start = _M_reserve_elements_at_front(__n);
1336 iterator __old_start = _M_start;
1337 __pos = _M_start + __elems_before;
1338 try {
1339 if (__elems_before >= difference_type(__n)) {
1340 iterator __start_n = _M_start + difference_type(__n);
1341 uninitialized_copy(_M_start, __start_n, __new_start);
1342 _M_start = __new_start;
1343 copy(__start_n, __pos, __old_start);
1344 fill(__pos - difference_type(__n), __pos, __x_copy);
1346 else {
1347 __uninitialized_copy_fill(_M_start, __pos, __new_start,
1348 _M_start, __x_copy);
1349 _M_start = __new_start;
1350 fill(__old_start, __pos, __x_copy);
1353 catch(...)
1355 _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
1356 __throw_exception_again;
1359 else {
1360 iterator __new_finish = _M_reserve_elements_at_back(__n);
1361 iterator __old_finish = _M_finish;
1362 const difference_type __elems_after =
1363 difference_type(__length) - __elems_before;
1364 __pos = _M_finish - __elems_after;
1365 try {
1366 if (__elems_after > difference_type(__n)) {
1367 iterator __finish_n = _M_finish - difference_type(__n);
1368 uninitialized_copy(__finish_n, _M_finish, _M_finish);
1369 _M_finish = __new_finish;
1370 copy_backward(__pos, __finish_n, __old_finish);
1371 fill(__pos, __pos + difference_type(__n), __x_copy);
1373 else {
1374 __uninitialized_fill_copy(_M_finish, __pos + difference_type(__n),
1375 __x_copy, __pos, _M_finish);
1376 _M_finish = __new_finish;
1377 fill(__pos, __old_finish, __x_copy);
1380 catch(...)
1382 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);
1383 __throw_exception_again;
1388 template <class _Tp, class _Alloc> template <class _ForwardIterator>
1389 void deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos,
1390 _ForwardIterator __first,
1391 _ForwardIterator __last,
1392 size_type __n)
1394 const difference_type __elemsbefore = __pos - _M_start;
1395 size_type __length = size();
1396 if (static_cast<size_type>(__elemsbefore) < __length / 2) {
1397 iterator __new_start = _M_reserve_elements_at_front(__n);
1398 iterator __old_start = _M_start;
1399 __pos = _M_start + __elemsbefore;
1400 try {
1401 if (__elemsbefore >= difference_type(__n)) {
1402 iterator __start_n = _M_start + difference_type(__n);
1403 uninitialized_copy(_M_start, __start_n, __new_start);
1404 _M_start = __new_start;
1405 copy(__start_n, __pos, __old_start);
1406 copy(__first, __last, __pos - difference_type(__n));
1408 else {
1409 _ForwardIterator __mid = __first;
1410 advance(__mid, difference_type(__n) - __elemsbefore);
1411 __uninitialized_copy_copy(_M_start, __pos, __first, __mid,
1412 __new_start);
1413 _M_start = __new_start;
1414 copy(__mid, __last, __old_start);
1417 catch(...)
1419 _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
1420 __throw_exception_again;
1423 else {
1424 iterator __new_finish = _M_reserve_elements_at_back(__n);
1425 iterator __old_finish = _M_finish;
1426 const difference_type __elemsafter =
1427 difference_type(__length) - __elemsbefore;
1428 __pos = _M_finish - __elemsafter;
1429 try {
1430 if (__elemsafter > difference_type(__n)) {
1431 iterator __finish_n = _M_finish - difference_type(__n);
1432 uninitialized_copy(__finish_n, _M_finish, _M_finish);
1433 _M_finish = __new_finish;
1434 copy_backward(__pos, __finish_n, __old_finish);
1435 copy(__first, __last, __pos);
1437 else {
1438 _ForwardIterator __mid = __first;
1439 advance(__mid, __elemsafter);
1440 __uninitialized_copy_copy(__mid, __last, __pos, _M_finish, _M_finish);
1441 _M_finish = __new_finish;
1442 copy(__first, __mid, __pos);
1445 catch(...)
1447 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);
1448 __throw_exception_again;
1453 template <class _Tp, class _Alloc>
1454 void deque<_Tp,_Alloc>::_M_new_elements_at_front(size_type __new_elems)
1456 size_type __new_nodes
1457 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
1458 _M_reserve_map_at_front(__new_nodes);
1459 size_type __i;
1460 try {
1461 for (__i = 1; __i <= __new_nodes; ++__i)
1462 *(_M_start._M_node - __i) = _M_allocate_node();
1464 catch(...) {
1465 for (size_type __j = 1; __j < __i; ++__j)
1466 _M_deallocate_node(*(_M_start._M_node - __j));
1467 __throw_exception_again;
1471 template <class _Tp, class _Alloc>
1472 void deque<_Tp,_Alloc>::_M_new_elements_at_back(size_type __new_elems)
1474 size_type __new_nodes
1475 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
1476 _M_reserve_map_at_back(__new_nodes);
1477 size_type __i;
1478 try {
1479 for (__i = 1; __i <= __new_nodes; ++__i)
1480 *(_M_finish._M_node + __i) = _M_allocate_node();
1482 catch(...) {
1483 for (size_type __j = 1; __j < __i; ++__j)
1484 _M_deallocate_node(*(_M_finish._M_node + __j));
1485 __throw_exception_again;
1489 template <class _Tp, class _Alloc>
1490 void deque<_Tp,_Alloc>::_M_reallocate_map(size_type __nodes_to_add,
1491 bool __add_at_front)
1493 size_type __old_num_nodes = _M_finish._M_node - _M_start._M_node + 1;
1494 size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
1496 _Map_pointer __new_nstart;
1497 if (_M_map_size > 2 * __new_num_nodes) {
1498 __new_nstart = _M_map + (_M_map_size - __new_num_nodes) / 2
1499 + (__add_at_front ? __nodes_to_add : 0);
1500 if (__new_nstart < _M_start._M_node)
1501 copy(_M_start._M_node, _M_finish._M_node + 1, __new_nstart);
1502 else
1503 copy_backward(_M_start._M_node, _M_finish._M_node + 1,
1504 __new_nstart + __old_num_nodes);
1506 else {
1507 size_type __new_map_size =
1508 _M_map_size + max(_M_map_size, __nodes_to_add) + 2;
1510 _Map_pointer __new_map = _M_allocate_map(__new_map_size);
1511 __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
1512 + (__add_at_front ? __nodes_to_add : 0);
1513 copy(_M_start._M_node, _M_finish._M_node + 1, __new_nstart);
1514 _M_deallocate_map(_M_map, _M_map_size);
1516 _M_map = __new_map;
1517 _M_map_size = __new_map_size;
1520 _M_start._M_set_node(__new_nstart);
1521 _M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
1525 // Nonmember functions.
1527 template <class _Tp, class _Alloc>
1528 inline bool operator==(const deque<_Tp, _Alloc>& __x,
1529 const deque<_Tp, _Alloc>& __y) {
1530 return __x.size() == __y.size() &&
1531 equal(__x.begin(), __x.end(), __y.begin());
1534 template <class _Tp, class _Alloc>
1535 inline bool operator<(const deque<_Tp, _Alloc>& __x,
1536 const deque<_Tp, _Alloc>& __y) {
1537 return lexicographical_compare(__x.begin(), __x.end(),
1538 __y.begin(), __y.end());
1541 template <class _Tp, class _Alloc>
1542 inline bool operator!=(const deque<_Tp, _Alloc>& __x,
1543 const deque<_Tp, _Alloc>& __y) {
1544 return !(__x == __y);
1547 template <class _Tp, class _Alloc>
1548 inline bool operator>(const deque<_Tp, _Alloc>& __x,
1549 const deque<_Tp, _Alloc>& __y) {
1550 return __y < __x;
1553 template <class _Tp, class _Alloc>
1554 inline bool operator<=(const deque<_Tp, _Alloc>& __x,
1555 const deque<_Tp, _Alloc>& __y) {
1556 return !(__y < __x);
1558 template <class _Tp, class _Alloc>
1559 inline bool operator>=(const deque<_Tp, _Alloc>& __x,
1560 const deque<_Tp, _Alloc>& __y) {
1561 return !(__x < __y);
1564 template <class _Tp, class _Alloc>
1565 inline void swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y) {
1566 __x.swap(__y);
1569 } // namespace std
1571 #endif /* __GLIBCPP_INTERNAL_DEQUE_H */
1573 // Local Variables:
1574 // mode:C++
1575 // End: