basic_string.h: Trivial formatting fixes and/or const-ification of some variables.
[gcc.git] / libstdc++-v3 / include / bits / stl_deque.h
1 // Deque implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004 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.
10
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.
15
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.
20
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.
29
30 /*
31 *
32 * Copyright (c) 1994
33 * Hewlett-Packard Company
34 *
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.
42 *
43 *
44 * Copyright (c) 1997
45 * Silicon Graphics Computer Systems, Inc.
46 *
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.
54 */
55
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.
59 */
60
61 #ifndef _DEQUE_H
62 #define _DEQUE_H 1
63
64 #include <bits/concept_check.h>
65 #include <bits/stl_iterator_base_types.h>
66 #include <bits/stl_iterator_base_funcs.h>
67
68 namespace _GLIBCXX_STD
69 {
70 /**
71 * @if maint
72 * @brief This function controls the size of memory nodes.
73 * @param size The size of an element.
74 * @return The number (not byte size) of elements per node.
75 *
76 * This function started off as a compiler kludge from SGI, but seems to
77 * be a useful wrapper around a repeated constant expression. The '512' is
78 * tuneable (and no other code needs to change), but no investigation has
79 * been done since inheriting the SGI code.
80 * @endif
81 */
82 inline size_t
83 __deque_buf_size(size_t __size)
84 { return __size < 512 ? size_t(512 / __size) : size_t(1); }
85
86
87 /**
88 * @brief A deque::iterator.
89 *
90 * Quite a bit of intelligence here. Much of the functionality of deque is
91 * actually passed off to this class. A deque holds two of these internally,
92 * marking its valid range. Access to elements is done as offsets of either
93 * of those two, relying on operator overloading in this class.
94 *
95 * @if maint
96 * All the functions are op overloads except for _M_set_node.
97 * @endif
98 */
99 template<typename _Tp, typename _Ref, typename _Ptr>
100 struct _Deque_iterator
101 {
102 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
103 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
104
105 static size_t _S_buffer_size()
106 { return __deque_buf_size(sizeof(_Tp)); }
107
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;
116
117 _Tp* _M_cur;
118 _Tp* _M_first;
119 _Tp* _M_last;
120 _Map_pointer _M_node;
121
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
126 _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
127
128 _Deque_iterator(const iterator& __x)
129 : _M_cur(__x._M_cur), _M_first(__x._M_first),
130 _M_last(__x._M_last), _M_node(__x._M_node) {}
131
132 reference
133 operator*() const
134 { return *_M_cur; }
135
136 pointer
137 operator->() const
138 { return _M_cur; }
139
140 _Self&
141 operator++()
142 {
143 ++_M_cur;
144 if (_M_cur == _M_last)
145 {
146 _M_set_node(_M_node + 1);
147 _M_cur = _M_first;
148 }
149 return *this;
150 }
151
152 _Self
153 operator++(int)
154 {
155 _Self __tmp = *this;
156 ++*this;
157 return __tmp;
158 }
159
160 _Self&
161 operator--()
162 {
163 if (_M_cur == _M_first)
164 {
165 _M_set_node(_M_node - 1);
166 _M_cur = _M_last;
167 }
168 --_M_cur;
169 return *this;
170 }
171
172 _Self
173 operator--(int)
174 {
175 _Self __tmp = *this;
176 --*this;
177 return __tmp;
178 }
179
180 _Self&
181 operator+=(difference_type __n)
182 {
183 const difference_type __offset = __n + (_M_cur - _M_first);
184 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
185 _M_cur += __n;
186 else
187 {
188 const difference_type __node_offset =
189 __offset > 0 ? __offset / difference_type(_S_buffer_size())
190 : -difference_type((-__offset - 1)
191 / _S_buffer_size()) - 1;
192 _M_set_node(_M_node + __node_offset);
193 _M_cur = _M_first + (__offset - __node_offset
194 * difference_type(_S_buffer_size()));
195 }
196 return *this;
197 }
198
199 _Self
200 operator+(difference_type __n) const
201 {
202 _Self __tmp = *this;
203 return __tmp += __n;
204 }
205
206 _Self&
207 operator-=(difference_type __n)
208 { return *this += -__n; }
209
210 _Self
211 operator-(difference_type __n) const
212 {
213 _Self __tmp = *this;
214 return __tmp -= __n;
215 }
216
217 reference
218 operator[](difference_type __n) const
219 { return *(*this + __n); }
220
221 /** @if maint
222 * Prepares to traverse new_node. Sets everything except _M_cur, which
223 * should therefore be set by the caller immediately afterwards, based on
224 * _M_first and _M_last.
225 * @endif
226 */
227 void
228 _M_set_node(_Map_pointer __new_node)
229 {
230 _M_node = __new_node;
231 _M_first = *__new_node;
232 _M_last = _M_first + difference_type(_S_buffer_size());
233 }
234 };
235
236 // Note: we also provide overloads whose operands are of the same type in
237 // order to avoid ambiguous overload resolution when std::rel_ops operators
238 // are in scope (for additional details, see libstdc++/3628)
239 template<typename _Tp, typename _Ref, typename _Ptr>
240 inline bool
241 operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
242 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
243 { return __x._M_cur == __y._M_cur; }
244
245 template<typename _Tp, typename _RefL, typename _PtrL,
246 typename _RefR, typename _PtrR>
247 inline bool
248 operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
249 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
250 { return __x._M_cur == __y._M_cur; }
251
252 template<typename _Tp, typename _Ref, typename _Ptr>
253 inline bool
254 operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
255 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
256 { return !(__x == __y); }
257
258 template<typename _Tp, typename _RefL, typename _PtrL,
259 typename _RefR, typename _PtrR>
260 inline bool
261 operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
262 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
263 { return !(__x == __y); }
264
265 template<typename _Tp, typename _Ref, typename _Ptr>
266 inline bool
267 operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
268 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
269 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
270 : (__x._M_node < __y._M_node); }
271
272 template<typename _Tp, typename _RefL, typename _PtrL,
273 typename _RefR, typename _PtrR>
274 inline bool
275 operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
276 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
277 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
278 : (__x._M_node < __y._M_node); }
279
280 template<typename _Tp, typename _Ref, typename _Ptr>
281 inline bool
282 operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
283 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
284 { return __y < __x; }
285
286 template<typename _Tp, typename _RefL, typename _PtrL,
287 typename _RefR, typename _PtrR>
288 inline bool
289 operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
290 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
291 { return __y < __x; }
292
293 template<typename _Tp, typename _Ref, typename _Ptr>
294 inline bool
295 operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
296 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
297 { return !(__y < __x); }
298
299 template<typename _Tp, typename _RefL, typename _PtrL,
300 typename _RefR, typename _PtrR>
301 inline bool
302 operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
303 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
304 { return !(__y < __x); }
305
306 template<typename _Tp, typename _Ref, typename _Ptr>
307 inline bool
308 operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
309 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
310 { return !(__x < __y); }
311
312 template<typename _Tp, typename _RefL, typename _PtrL,
313 typename _RefR, typename _PtrR>
314 inline bool
315 operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
316 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
317 { return !(__x < __y); }
318
319 // _GLIBCXX_RESOLVE_LIB_DEFECTS
320 // According to the resolution of DR179 not only the various comparison
321 // operators but also operator- must accept mixed iterator/const_iterator
322 // parameters.
323 template<typename _Tp, typename _RefL, typename _PtrL,
324 typename _RefR, typename _PtrR>
325 inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
326 operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
327 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
328 {
329 return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
330 (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
331 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
332 + (__y._M_last - __y._M_cur);
333 }
334
335 template<typename _Tp, typename _Ref, typename _Ptr>
336 inline _Deque_iterator<_Tp, _Ref, _Ptr>
337 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
338 { return __x + __n; }
339
340 /**
341 * @if maint
342 * Deque base class. This class provides the unified face for %deque's
343 * allocation. This class's constructor and destructor allocate and
344 * deallocate (but do not initialize) storage. This makes %exception
345 * safety easier.
346 *
347 * Nothing in this class ever constructs or destroys an actual Tp element.
348 * (Deque handles that itself.) Only/All memory management is performed
349 * here.
350 * @endif
351 */
352 template<typename _Tp, typename _Alloc>
353 class _Deque_base
354 {
355 public:
356 typedef _Alloc allocator_type;
357
358 allocator_type
359 get_allocator() const
360 { return *static_cast<const _Alloc*>(&this->_M_impl); }
361
362 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
363 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
364
365 _Deque_base(const allocator_type& __a, size_t __num_elements)
366 : _M_impl(__a)
367 { _M_initialize_map(__num_elements); }
368
369 _Deque_base(const allocator_type& __a)
370 : _M_impl(__a)
371 { }
372
373 ~_Deque_base();
374
375 protected:
376 //This struct encapsulates the implementation of the std::deque
377 //standard container and at the same time makes use of the EBO
378 //for empty allocators.
379 struct _Deque_impl
380 : public _Alloc
381 {
382 _Tp** _M_map;
383 size_t _M_map_size;
384 iterator _M_start;
385 iterator _M_finish;
386
387 _Deque_impl(const _Alloc& __a)
388 : _Alloc(__a), _M_map(0), _M_map_size(0), _M_start(), _M_finish()
389 { }
390 };
391
392 typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
393 _Map_alloc_type _M_get_map_allocator() const
394 { return _Map_alloc_type(this->get_allocator()); }
395
396 _Tp*
397 _M_allocate_node()
398 { return _M_impl._Alloc::allocate(__deque_buf_size(sizeof(_Tp))); }
399
400 void
401 _M_deallocate_node(_Tp* __p)
402 { _M_impl._Alloc::deallocate(__p, __deque_buf_size(sizeof(_Tp))); }
403
404 _Tp**
405 _M_allocate_map(size_t __n)
406 { return _M_get_map_allocator().allocate(__n); }
407
408 void
409 _M_deallocate_map(_Tp** __p, size_t __n)
410 { _M_get_map_allocator().deallocate(__p, __n); }
411
412 protected:
413 void _M_initialize_map(size_t);
414 void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
415 void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
416 enum { _S_initial_map_size = 8 };
417
418 _Deque_impl _M_impl;
419 };
420
421 template<typename _Tp, typename _Alloc>
422 _Deque_base<_Tp, _Alloc>::
423 ~_Deque_base()
424 {
425 if (this->_M_impl._M_map)
426 {
427 _M_destroy_nodes(this->_M_impl._M_start._M_node,
428 this->_M_impl._M_finish._M_node + 1);
429 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
430 }
431 }
432
433 /**
434 * @if maint
435 * @brief Layout storage.
436 * @param num_elements The count of T's for which to allocate space
437 * at first.
438 * @return Nothing.
439 *
440 * The initial underlying memory layout is a bit complicated...
441 * @endif
442 */
443 template<typename _Tp, typename _Alloc>
444 void
445 _Deque_base<_Tp, _Alloc>::
446 _M_initialize_map(size_t __num_elements)
447 {
448 const size_t __num_nodes = (__num_elements / __deque_buf_size(sizeof(_Tp))
449 + 1);
450
451 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
452 size_t(__num_nodes + 2));
453 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
454
455 // For "small" maps (needing less than _M_map_size nodes), allocation
456 // starts in the middle elements and grows outwards. So nstart may be
457 // the beginning of _M_map, but for small maps it may be as far in as
458 // _M_map+3.
459
460 _Tp** __nstart = (this->_M_impl._M_map
461 + (this->_M_impl._M_map_size - __num_nodes) / 2);
462 _Tp** __nfinish = __nstart + __num_nodes;
463
464 try
465 { _M_create_nodes(__nstart, __nfinish); }
466 catch(...)
467 {
468 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
469 this->_M_impl._M_map = 0;
470 this->_M_impl._M_map_size = 0;
471 __throw_exception_again;
472 }
473
474 this->_M_impl._M_start._M_set_node(__nstart);
475 this->_M_impl._M_finish._M_set_node(__nfinish - 1);
476 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
477 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
478 + __num_elements
479 % __deque_buf_size(sizeof(_Tp)));
480 }
481
482 template<typename _Tp, typename _Alloc>
483 void
484 _Deque_base<_Tp, _Alloc>::
485 _M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
486 {
487 _Tp** __cur;
488 try
489 {
490 for (__cur = __nstart; __cur < __nfinish; ++__cur)
491 *__cur = this->_M_allocate_node();
492 }
493 catch(...)
494 {
495 _M_destroy_nodes(__nstart, __cur);
496 __throw_exception_again;
497 }
498 }
499
500 template<typename _Tp, typename _Alloc>
501 void
502 _Deque_base<_Tp, _Alloc>::
503 _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
504 {
505 for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
506 _M_deallocate_node(*__n);
507 }
508
509 /**
510 * @brief A standard container using fixed-size memory allocation and
511 * constant-time manipulation of elements at either end.
512 *
513 * @ingroup Containers
514 * @ingroup Sequences
515 *
516 * Meets the requirements of a <a href="tables.html#65">container</a>, a
517 * <a href="tables.html#66">reversible container</a>, and a
518 * <a href="tables.html#67">sequence</a>, including the
519 * <a href="tables.html#68">optional sequence requirements</a>.
520 *
521 * In previous HP/SGI versions of deque, there was an extra template
522 * parameter so users could control the node size. This extension turned
523 * out to violate the C++ standard (it can be detected using template
524 * template parameters), and it was removed.
525 *
526 * @if maint
527 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
528 *
529 * - Tp** _M_map
530 * - size_t _M_map_size
531 * - iterator _M_start, _M_finish
532 *
533 * map_size is at least 8. %map is an array of map_size pointers-to-"nodes".
534 * (The name %map has nothing to do with the std::map class, and "nodes"
535 * should not be confused with std::list's usage of "node".)
536 *
537 * A "node" has no specific type name as such, but it is referred to as
538 * "node" in this file. It is a simple array-of-Tp. If Tp is very large,
539 * there will be one Tp element per node (i.e., an "array" of one).
540 * For non-huge Tp's, node size is inversely related to Tp size: the
541 * larger the Tp, the fewer Tp's will fit in a node. The goal here is to
542 * keep the total size of a node relatively small and constant over different
543 * Tp's, to improve allocator efficiency.
544 *
545 * **** As I write this, the nodes are /not/ allocated using the high-speed
546 * memory pool. There are 20 hours left in the year; perhaps I can fix
547 * this before 2002.
548 *
549 * Not every pointer in the %map array will point to a node. If the initial
550 * number of elements in the deque is small, the /middle/ %map pointers will
551 * be valid, and the ones at the edges will be unused. This same situation
552 * will arise as the %map grows: available %map pointers, if any, will be on
553 * the ends. As new nodes are created, only a subset of the %map's pointers
554 * need to be copied "outward".
555 *
556 * Class invariants:
557 * - For any nonsingular iterator i:
558 * - i.node points to a member of the %map array. (Yes, you read that
559 * correctly: i.node does not actually point to a node.) The member of
560 * the %map array is what actually points to the node.
561 * - i.first == *(i.node) (This points to the node (first Tp element).)
562 * - i.last == i.first + node_size
563 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
564 * the implication of this is that i.cur is always a dereferenceable
565 * pointer, even if i is a past-the-end iterator.
566 * - Start and Finish are always nonsingular iterators. NOTE: this means that
567 * an empty deque must have one node, a deque with <N elements (where N is
568 * the node buffer size) must have one node, a deque with N through (2N-1)
569 * elements must have two nodes, etc.
570 * - For every node other than start.node and finish.node, every element in
571 * the node is an initialized object. If start.node == finish.node, then
572 * [start.cur, finish.cur) are initialized objects, and the elements outside
573 * that range are uninitialized storage. Otherwise, [start.cur, start.last)
574 * and [finish.first, finish.cur) are initialized objects, and [start.first,
575 * start.cur) and [finish.cur, finish.last) are uninitialized storage.
576 * - [%map, %map + map_size) is a valid, non-empty range.
577 * - [start.node, finish.node] is a valid range contained within
578 * [%map, %map + map_size).
579 * - A pointer in the range [%map, %map + map_size) points to an allocated
580 * node if and only if the pointer is in the range
581 * [start.node, finish.node].
582 *
583 * Here's the magic: nothing in deque is "aware" of the discontiguous
584 * storage!
585 *
586 * The memory setup and layout occurs in the parent, _Base, and the iterator
587 * class is entirely responsible for "leaping" from one node to the next.
588 * All the implementation routines for deque itself work only through the
589 * start and finish iterators. This keeps the routines simple and sane,
590 * and we can use other standard algorithms as well.
591 * @endif
592 */
593 template<typename _Tp, typename _Alloc = allocator<_Tp> >
594 class deque : protected _Deque_base<_Tp, _Alloc>
595 {
596 // concept requirements
597 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
598
599 typedef _Deque_base<_Tp, _Alloc> _Base;
600
601 public:
602 typedef _Tp value_type;
603 typedef typename _Alloc::pointer pointer;
604 typedef typename _Alloc::const_pointer const_pointer;
605 typedef typename _Alloc::reference reference;
606 typedef typename _Alloc::const_reference const_reference;
607 typedef typename _Base::iterator iterator;
608 typedef typename _Base::const_iterator const_iterator;
609 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
610 typedef std::reverse_iterator<iterator> reverse_iterator;
611 typedef size_t size_type;
612 typedef ptrdiff_t difference_type;
613 typedef typename _Base::allocator_type allocator_type;
614
615 protected:
616 typedef pointer* _Map_pointer;
617
618 static size_t _S_buffer_size()
619 { return __deque_buf_size(sizeof(_Tp)); }
620
621 // Functions controlling memory layout, and nothing else.
622 using _Base::_M_initialize_map;
623 using _Base::_M_create_nodes;
624 using _Base::_M_destroy_nodes;
625 using _Base::_M_allocate_node;
626 using _Base::_M_deallocate_node;
627 using _Base::_M_allocate_map;
628 using _Base::_M_deallocate_map;
629
630 /** @if maint
631 * A total of four data members accumulated down the heirarchy.
632 * May be accessed via _M_impl.*
633 * @endif
634 */
635 using _Base::_M_impl;
636
637 public:
638 // [23.2.1.1] construct/copy/destroy
639 // (assign() and get_allocator() are also listed in this section)
640 /**
641 * @brief Default constructor creates no elements.
642 */
643 explicit
644 deque(const allocator_type& __a = allocator_type())
645 : _Base(__a, 0) {}
646
647 /**
648 * @brief Create a %deque with copies of an exemplar element.
649 * @param n The number of elements to initially create.
650 * @param value An element to copy.
651 *
652 * This constructor fills the %deque with @a n copies of @a value.
653 */
654 deque(size_type __n, const value_type& __value,
655 const allocator_type& __a = allocator_type())
656 : _Base(__a, __n)
657 { _M_fill_initialize(__value); }
658
659 /**
660 * @brief Create a %deque with default elements.
661 * @param n The number of elements to initially create.
662 *
663 * This constructor fills the %deque with @a n copies of a
664 * default-constructed element.
665 */
666 explicit
667 deque(size_type __n)
668 : _Base(allocator_type(), __n)
669 { _M_fill_initialize(value_type()); }
670
671 /**
672 * @brief %Deque copy constructor.
673 * @param x A %deque of identical element and allocator types.
674 *
675 * The newly-created %deque uses a copy of the allocation object used
676 * by @a x.
677 */
678 deque(const deque& __x)
679 : _Base(__x.get_allocator(), __x.size())
680 { std::uninitialized_copy(__x.begin(), __x.end(),
681 this->_M_impl._M_start); }
682
683 /**
684 * @brief Builds a %deque from a range.
685 * @param first An input iterator.
686 * @param last An input iterator.
687 *
688 * Create a %deque consisting of copies of the elements from [first,
689 * last).
690 *
691 * If the iterators are forward, bidirectional, or random-access, then
692 * this will call the elements' copy constructor N times (where N is
693 * distance(first,last)) and do no memory reallocation. But if only
694 * input iterators are used, then this will do at most 2N calls to the
695 * copy constructor, and logN memory reallocations.
696 */
697 template<typename _InputIterator>
698 deque(_InputIterator __first, _InputIterator __last,
699 const allocator_type& __a = allocator_type())
700 : _Base(__a)
701 {
702 // Check whether it's an integral type. If so, it's not an iterator.
703 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
704 _M_initialize_dispatch(__first, __last, _Integral());
705 }
706
707 /**
708 * The dtor only erases the elements, and note that if the elements
709 * themselves are pointers, the pointed-to memory is not touched in any
710 * way. Managing the pointer is the user's responsibilty.
711 */
712 ~deque()
713 { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish); }
714
715 /**
716 * @brief %Deque assignment operator.
717 * @param x A %deque of identical element and allocator types.
718 *
719 * All the elements of @a x are copied, but unlike the copy constructor,
720 * the allocator object is not copied.
721 */
722 deque&
723 operator=(const deque& __x);
724
725 /**
726 * @brief Assigns a given value to a %deque.
727 * @param n Number of elements to be assigned.
728 * @param val Value to be assigned.
729 *
730 * This function fills a %deque with @a n copies of the given value.
731 * Note that the assignment completely changes the %deque and that the
732 * resulting %deque's size is the same as the number of elements assigned.
733 * Old data may be lost.
734 */
735 void
736 assign(size_type __n, const value_type& __val)
737 { _M_fill_assign(__n, __val); }
738
739 /**
740 * @brief Assigns a range to a %deque.
741 * @param first An input iterator.
742 * @param last An input iterator.
743 *
744 * This function fills a %deque with copies of the elements in the
745 * range [first,last).
746 *
747 * Note that the assignment completely changes the %deque and that the
748 * resulting %deque's size is the same as the number of elements
749 * assigned. Old data may be lost.
750 */
751 template<typename _InputIterator>
752 void
753 assign(_InputIterator __first, _InputIterator __last)
754 {
755 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
756 _M_assign_dispatch(__first, __last, _Integral());
757 }
758
759 /// Get a copy of the memory allocation object.
760 allocator_type
761 get_allocator() const
762 { return _Base::get_allocator(); }
763
764 // iterators
765 /**
766 * Returns a read/write iterator that points to the first element in the
767 * %deque. Iteration is done in ordinary element order.
768 */
769 iterator
770 begin()
771 { return this->_M_impl._M_start; }
772
773 /**
774 * Returns a read-only (constant) iterator that points to the first
775 * element in the %deque. Iteration is done in ordinary element order.
776 */
777 const_iterator
778 begin() const
779 { return this->_M_impl._M_start; }
780
781 /**
782 * Returns a read/write iterator that points one past the last element in
783 * the %deque. Iteration is done in ordinary element order.
784 */
785 iterator
786 end()
787 { return this->_M_impl._M_finish; }
788
789 /**
790 * Returns a read-only (constant) iterator that points one past the last
791 * element in the %deque. Iteration is done in ordinary element order.
792 */
793 const_iterator
794 end() const
795 { return this->_M_impl._M_finish; }
796
797 /**
798 * Returns a read/write reverse iterator that points to the last element
799 * in the %deque. Iteration is done in reverse element order.
800 */
801 reverse_iterator
802 rbegin()
803 { return reverse_iterator(this->_M_impl._M_finish); }
804
805 /**
806 * Returns a read-only (constant) reverse iterator that points to the
807 * last element in the %deque. Iteration is done in reverse element
808 * order.
809 */
810 const_reverse_iterator
811 rbegin() const
812 { return const_reverse_iterator(this->_M_impl._M_finish); }
813
814 /**
815 * Returns a read/write reverse iterator that points to one before the
816 * first element in the %deque. Iteration is done in reverse element
817 * order.
818 */
819 reverse_iterator
820 rend() { return reverse_iterator(this->_M_impl._M_start); }
821
822 /**
823 * Returns a read-only (constant) reverse iterator that points to one
824 * before the first element in the %deque. Iteration is done in reverse
825 * element order.
826 */
827 const_reverse_iterator
828 rend() const
829 { return const_reverse_iterator(this->_M_impl._M_start); }
830
831 // [23.2.1.2] capacity
832 /** Returns the number of elements in the %deque. */
833 size_type
834 size() const
835 { return this->_M_impl._M_finish - this->_M_impl._M_start; }
836
837 /** Returns the size() of the largest possible %deque. */
838 size_type
839 max_size() const
840 { return size_type(-1); }
841
842 /**
843 * @brief Resizes the %deque to the specified number of elements.
844 * @param new_size Number of elements the %deque should contain.
845 * @param x Data with which new elements should be populated.
846 *
847 * This function will %resize the %deque to the specified number of
848 * elements. If the number is smaller than the %deque's current size the
849 * %deque is truncated, otherwise the %deque is extended and new elements
850 * are populated with given data.
851 */
852 void
853 resize(size_type __new_size, const value_type& __x)
854 {
855 const size_type __len = size();
856 if (__new_size < __len)
857 erase(this->_M_impl._M_start + __new_size, this->_M_impl._M_finish);
858 else
859 insert(this->_M_impl._M_finish, __new_size - __len, __x);
860 }
861
862 /**
863 * @brief Resizes the %deque to the specified number of elements.
864 * @param new_size Number of elements the %deque should contain.
865 *
866 * This function will resize the %deque to the specified number of
867 * elements. If the number is smaller than the %deque's current size the
868 * %deque is truncated, otherwise the %deque is extended and new elements
869 * are default-constructed.
870 */
871 void
872 resize(size_type new_size)
873 { resize(new_size, value_type()); }
874
875 /**
876 * Returns true if the %deque is empty. (Thus begin() would equal end().)
877 */
878 bool
879 empty() const
880 { return this->_M_impl._M_finish == this->_M_impl._M_start; }
881
882 // element access
883 /**
884 * @brief Subscript access to the data contained in the %deque.
885 * @param n The index of the element for which data should be accessed.
886 * @return Read/write reference to data.
887 *
888 * This operator allows for easy, array-style, data access.
889 * Note that data access with this operator is unchecked and out_of_range
890 * lookups are not defined. (For checked lookups see at().)
891 */
892 reference
893 operator[](size_type __n)
894 { return this->_M_impl._M_start[difference_type(__n)]; }
895
896 /**
897 * @brief Subscript access to the data contained in the %deque.
898 * @param n The index of the element for which data should be accessed.
899 * @return Read-only (constant) reference to data.
900 *
901 * This operator allows for easy, array-style, data access.
902 * Note that data access with this operator is unchecked and out_of_range
903 * lookups are not defined. (For checked lookups see at().)
904 */
905 const_reference
906 operator[](size_type __n) const
907 { return this->_M_impl._M_start[difference_type(__n)]; }
908
909 protected:
910 /// @if maint Safety check used only from at(). @endif
911 void
912 _M_range_check(size_type __n) const
913 {
914 if (__n >= this->size())
915 __throw_out_of_range(__N("deque::_M_range_check"));
916 }
917
918 public:
919 /**
920 * @brief Provides access to the data contained in the %deque.
921 * @param n The index of the element for which data should be accessed.
922 * @return Read/write reference to data.
923 * @throw std::out_of_range If @a n is an invalid index.
924 *
925 * This function provides for safer data access. The parameter is first
926 * checked that it is in the range of the deque. The function throws
927 * out_of_range if the check fails.
928 */
929 reference
930 at(size_type __n)
931 {
932 _M_range_check(__n);
933 return (*this)[__n];
934 }
935
936 /**
937 * @brief Provides access to the data contained in the %deque.
938 * @param n The index of the element for which data should be accessed.
939 * @return Read-only (constant) reference to data.
940 * @throw std::out_of_range If @a n is an invalid index.
941 *
942 * This function provides for safer data access. The parameter is first
943 * checked that it is in the range of the deque. The function throws
944 * out_of_range if the check fails.
945 */
946 const_reference
947 at(size_type __n) const
948 {
949 _M_range_check(__n);
950 return (*this)[__n];
951 }
952
953 /**
954 * Returns a read/write reference to the data at the first element of the
955 * %deque.
956 */
957 reference
958 front()
959 { return *this->_M_impl._M_start; }
960
961 /**
962 * Returns a read-only (constant) reference to the data at the first
963 * element of the %deque.
964 */
965 const_reference
966 front() const
967 { return *this->_M_impl._M_start; }
968
969 /**
970 * Returns a read/write reference to the data at the last element of the
971 * %deque.
972 */
973 reference
974 back()
975 {
976 iterator __tmp = this->_M_impl._M_finish;
977 --__tmp;
978 return *__tmp;
979 }
980
981 /**
982 * Returns a read-only (constant) reference to the data at the last
983 * element of the %deque.
984 */
985 const_reference
986 back() const
987 {
988 const_iterator __tmp = this->_M_impl._M_finish;
989 --__tmp;
990 return *__tmp;
991 }
992
993 // [23.2.1.2] modifiers
994 /**
995 * @brief Add data to the front of the %deque.
996 * @param x Data to be added.
997 *
998 * This is a typical stack operation. The function creates an element at
999 * the front of the %deque and assigns the given data to it. Due to the
1000 * nature of a %deque this operation can be done in constant time.
1001 */
1002 void
1003 push_front(const value_type& __x)
1004 {
1005 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1006 {
1007 std::_Construct(this->_M_impl._M_start._M_cur - 1, __x);
1008 --this->_M_impl._M_start._M_cur;
1009 }
1010 else
1011 _M_push_front_aux(__x);
1012 }
1013
1014 /**
1015 * @brief Add data to the end of the %deque.
1016 * @param x Data to be added.
1017 *
1018 * This is a typical stack operation. The function creates an element at
1019 * the end of the %deque and assigns the given data to it. Due to the
1020 * nature of a %deque this operation can be done in constant time.
1021 */
1022 void
1023 push_back(const value_type& __x)
1024 {
1025 if (this->_M_impl._M_finish._M_cur
1026 != this->_M_impl._M_finish._M_last - 1)
1027 {
1028 std::_Construct(this->_M_impl._M_finish._M_cur, __x);
1029 ++this->_M_impl._M_finish._M_cur;
1030 }
1031 else
1032 _M_push_back_aux(__x);
1033 }
1034
1035 /**
1036 * @brief Removes first element.
1037 *
1038 * This is a typical stack operation. It shrinks the %deque by one.
1039 *
1040 * Note that no data is returned, and if the first element's data is
1041 * needed, it should be retrieved before pop_front() is called.
1042 */
1043 void
1044 pop_front()
1045 {
1046 if (this->_M_impl._M_start._M_cur
1047 != this->_M_impl._M_start._M_last - 1)
1048 {
1049 std::_Destroy(this->_M_impl._M_start._M_cur);
1050 ++this->_M_impl._M_start._M_cur;
1051 }
1052 else
1053 _M_pop_front_aux();
1054 }
1055
1056 /**
1057 * @brief Removes last element.
1058 *
1059 * This is a typical stack operation. It shrinks the %deque by one.
1060 *
1061 * Note that no data is returned, and if the last element's data is
1062 * needed, it should be retrieved before pop_back() is called.
1063 */
1064 void
1065 pop_back()
1066 {
1067 if (this->_M_impl._M_finish._M_cur
1068 != this->_M_impl._M_finish._M_first)
1069 {
1070 --this->_M_impl._M_finish._M_cur;
1071 std::_Destroy(this->_M_impl._M_finish._M_cur);
1072 }
1073 else
1074 _M_pop_back_aux();
1075 }
1076
1077 /**
1078 * @brief Inserts given value into %deque before specified iterator.
1079 * @param position An iterator into the %deque.
1080 * @param x Data to be inserted.
1081 * @return An iterator that points to the inserted data.
1082 *
1083 * This function will insert a copy of the given value before the
1084 * specified location.
1085 */
1086 iterator
1087 insert(iterator position, const value_type& __x);
1088
1089 /**
1090 * @brief Inserts a number of copies of given data into the %deque.
1091 * @param position An iterator into the %deque.
1092 * @param n Number of elements to be inserted.
1093 * @param x Data to be inserted.
1094 *
1095 * This function will insert a specified number of copies of the given
1096 * data before the location specified by @a position.
1097 */
1098 void
1099 insert(iterator __position, size_type __n, const value_type& __x)
1100 { _M_fill_insert(__position, __n, __x); }
1101
1102 /**
1103 * @brief Inserts a range into the %deque.
1104 * @param position An iterator into the %deque.
1105 * @param first An input iterator.
1106 * @param last An input iterator.
1107 *
1108 * This function will insert copies of the data in the range [first,last)
1109 * into the %deque before the location specified by @a pos. This is
1110 * known as "range insert."
1111 */
1112 template<typename _InputIterator>
1113 void
1114 insert(iterator __position, _InputIterator __first,
1115 _InputIterator __last)
1116 {
1117 // Check whether it's an integral type. If so, it's not an iterator.
1118 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
1119 _M_insert_dispatch(__position, __first, __last, _Integral());
1120 }
1121
1122 /**
1123 * @brief Remove element at given position.
1124 * @param position Iterator pointing to element to be erased.
1125 * @return An iterator pointing to the next element (or end()).
1126 *
1127 * This function will erase the element at the given position and thus
1128 * shorten the %deque by one.
1129 *
1130 * The user is cautioned that
1131 * this function only erases the element, and that if the element is
1132 * itself a pointer, the pointed-to memory is not touched in any way.
1133 * Managing the pointer is the user's responsibilty.
1134 */
1135 iterator
1136 erase(iterator __position);
1137
1138 /**
1139 * @brief Remove a range of elements.
1140 * @param first Iterator pointing to the first element to be erased.
1141 * @param last Iterator pointing to one past the last element to be
1142 * erased.
1143 * @return An iterator pointing to the element pointed to by @a last
1144 * prior to erasing (or end()).
1145 *
1146 * This function will erase the elements in the range [first,last) and
1147 * shorten the %deque accordingly.
1148 *
1149 * The user is cautioned that
1150 * this function only erases the elements, and that if the elements
1151 * themselves are pointers, the pointed-to memory is not touched in any
1152 * way. Managing the pointer is the user's responsibilty.
1153 */
1154 iterator
1155 erase(iterator __first, iterator __last);
1156
1157 /**
1158 * @brief Swaps data with another %deque.
1159 * @param x A %deque of the same element and allocator types.
1160 *
1161 * This exchanges the elements between two deques in constant time.
1162 * (Four pointers, so it should be quite fast.)
1163 * Note that the global std::swap() function is specialized such that
1164 * std::swap(d1,d2) will feed to this function.
1165 */
1166 void
1167 swap(deque& __x)
1168 {
1169 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
1170 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
1171 std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
1172 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
1173 }
1174
1175 /**
1176 * Erases all the elements. Note that this function only erases the
1177 * elements, and that if the elements themselves are pointers, the
1178 * pointed-to memory is not touched in any way. Managing the pointer is
1179 * the user's responsibilty.
1180 */
1181 void clear();
1182
1183 protected:
1184 // Internal constructor functions follow.
1185
1186 // called by the range constructor to implement [23.1.1]/9
1187 template<typename _Integer>
1188 void
1189 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1190 {
1191 _M_initialize_map(__n);
1192 _M_fill_initialize(__x);
1193 }
1194
1195 // called by the range constructor to implement [23.1.1]/9
1196 template<typename _InputIterator>
1197 void
1198 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1199 __false_type)
1200 {
1201 typedef typename iterator_traits<_InputIterator>::iterator_category
1202 _IterCategory;
1203 _M_range_initialize(__first, __last, _IterCategory());
1204 }
1205
1206 // called by the second initialize_dispatch above
1207 //@{
1208 /**
1209 * @if maint
1210 * @brief Fills the deque with whatever is in [first,last).
1211 * @param first An input iterator.
1212 * @param last An input iterator.
1213 * @return Nothing.
1214 *
1215 * If the iterators are actually forward iterators (or better), then the
1216 * memory layout can be done all at once. Else we move forward using
1217 * push_back on each value from the iterator.
1218 * @endif
1219 */
1220 template<typename _InputIterator>
1221 void
1222 _M_range_initialize(_InputIterator __first, _InputIterator __last,
1223 input_iterator_tag);
1224
1225 // called by the second initialize_dispatch above
1226 template<typename _ForwardIterator>
1227 void
1228 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1229 forward_iterator_tag);
1230 //@}
1231
1232 /**
1233 * @if maint
1234 * @brief Fills the %deque with copies of value.
1235 * @param value Initial value.
1236 * @return Nothing.
1237 * @pre _M_start and _M_finish have already been initialized, but none of
1238 * the %deque's elements have yet been constructed.
1239 *
1240 * This function is called only when the user provides an explicit size
1241 * (with or without an explicit exemplar value).
1242 * @endif
1243 */
1244 void
1245 _M_fill_initialize(const value_type& __value);
1246
1247 // Internal assign functions follow. The *_aux functions do the actual
1248 // assignment work for the range versions.
1249
1250 // called by the range assign to implement [23.1.1]/9
1251 template<typename _Integer>
1252 void
1253 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1254 {
1255 _M_fill_assign(static_cast<size_type>(__n),
1256 static_cast<value_type>(__val));
1257 }
1258
1259 // called by the range assign to implement [23.1.1]/9
1260 template<typename _InputIterator>
1261 void
1262 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1263 __false_type)
1264 {
1265 typedef typename iterator_traits<_InputIterator>::iterator_category
1266 _IterCategory;
1267 _M_assign_aux(__first, __last, _IterCategory());
1268 }
1269
1270 // called by the second assign_dispatch above
1271 template<typename _InputIterator>
1272 void
1273 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1274 input_iterator_tag);
1275
1276 // called by the second assign_dispatch above
1277 template<typename _ForwardIterator>
1278 void
1279 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1280 forward_iterator_tag)
1281 {
1282 const size_type __len = std::distance(__first, __last);
1283 if (__len > size())
1284 {
1285 _ForwardIterator __mid = __first;
1286 std::advance(__mid, size());
1287 std::copy(__first, __mid, begin());
1288 insert(end(), __mid, __last);
1289 }
1290 else
1291 erase(std::copy(__first, __last, begin()), end());
1292 }
1293
1294 // Called by assign(n,t), and the range assign when it turns out to be the
1295 // same thing.
1296 void
1297 _M_fill_assign(size_type __n, const value_type& __val)
1298 {
1299 if (__n > size())
1300 {
1301 std::fill(begin(), end(), __val);
1302 insert(end(), __n - size(), __val);
1303 }
1304 else
1305 {
1306 erase(begin() + __n, end());
1307 std::fill(begin(), end(), __val);
1308 }
1309 }
1310
1311 //@{
1312 /**
1313 * @if maint
1314 * @brief Helper functions for push_* and pop_*.
1315 * @endif
1316 */
1317 void _M_push_back_aux(const value_type&);
1318 void _M_push_front_aux(const value_type&);
1319 void _M_pop_back_aux();
1320 void _M_pop_front_aux();
1321 //@}
1322
1323 // Internal insert functions follow. The *_aux functions do the actual
1324 // insertion work when all shortcuts fail.
1325
1326 // called by the range insert to implement [23.1.1]/9
1327 template<typename _Integer>
1328 void
1329 _M_insert_dispatch(iterator __pos,
1330 _Integer __n, _Integer __x, __true_type)
1331 {
1332 _M_fill_insert(__pos, static_cast<size_type>(__n),
1333 static_cast<value_type>(__x));
1334 }
1335
1336 // called by the range insert to implement [23.1.1]/9
1337 template<typename _InputIterator>
1338 void
1339 _M_insert_dispatch(iterator __pos,
1340 _InputIterator __first, _InputIterator __last,
1341 __false_type)
1342 {
1343 typedef typename iterator_traits<_InputIterator>::iterator_category
1344 _IterCategory;
1345 _M_range_insert_aux(__pos, __first, __last, _IterCategory());
1346 }
1347
1348 // called by the second insert_dispatch above
1349 template<typename _InputIterator>
1350 void
1351 _M_range_insert_aux(iterator __pos, _InputIterator __first,
1352 _InputIterator __last, input_iterator_tag);
1353
1354 // called by the second insert_dispatch above
1355 template<typename _ForwardIterator>
1356 void
1357 _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
1358 _ForwardIterator __last, forward_iterator_tag);
1359
1360 // Called by insert(p,n,x), and the range insert when it turns out to be
1361 // the same thing. Can use fill functions in optimal situations,
1362 // otherwise passes off to insert_aux(p,n,x).
1363 void
1364 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1365
1366 // called by insert(p,x)
1367 iterator
1368 _M_insert_aux(iterator __pos, const value_type& __x);
1369
1370 // called by insert(p,n,x) via fill_insert
1371 void
1372 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
1373
1374 // called by range_insert_aux for forward iterators
1375 template<typename _ForwardIterator>
1376 void
1377 _M_insert_aux(iterator __pos,
1378 _ForwardIterator __first, _ForwardIterator __last,
1379 size_type __n);
1380
1381 //@{
1382 /**
1383 * @if maint
1384 * @brief Memory-handling helpers for the previous internal insert
1385 * functions.
1386 * @endif
1387 */
1388 iterator
1389 _M_reserve_elements_at_front(size_type __n)
1390 {
1391 const size_type __vacancies = this->_M_impl._M_start._M_cur
1392 - this->_M_impl._M_start._M_first;
1393 if (__n > __vacancies)
1394 _M_new_elements_at_front(__n - __vacancies);
1395 return this->_M_impl._M_start - difference_type(__n);
1396 }
1397
1398 iterator
1399 _M_reserve_elements_at_back(size_type __n)
1400 {
1401 const size_type __vacancies = (this->_M_impl._M_finish._M_last
1402 - this->_M_impl._M_finish._M_cur) - 1;
1403 if (__n > __vacancies)
1404 _M_new_elements_at_back(__n - __vacancies);
1405 return this->_M_impl._M_finish + difference_type(__n);
1406 }
1407
1408 void
1409 _M_new_elements_at_front(size_type __new_elements);
1410
1411 void
1412 _M_new_elements_at_back(size_type __new_elements);
1413 //@}
1414
1415
1416 //@{
1417 /**
1418 * @if maint
1419 * @brief Memory-handling helpers for the major %map.
1420 *
1421 * Makes sure the _M_map has space for new nodes. Does not actually add
1422 * the nodes. Can invalidate _M_map pointers. (And consequently, %deque
1423 * iterators.)
1424 * @endif
1425 */
1426 void
1427 _M_reserve_map_at_back (size_type __nodes_to_add = 1)
1428 {
1429 if (__nodes_to_add + 1 > this->_M_impl._M_map_size
1430 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
1431 _M_reallocate_map(__nodes_to_add, false);
1432 }
1433
1434 void
1435 _M_reserve_map_at_front (size_type __nodes_to_add = 1)
1436 {
1437 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
1438 - this->_M_impl._M_map))
1439 _M_reallocate_map(__nodes_to_add, true);
1440 }
1441
1442 void
1443 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
1444 //@}
1445 };
1446
1447
1448 /**
1449 * @brief Deque equality comparison.
1450 * @param x A %deque.
1451 * @param y A %deque of the same type as @a x.
1452 * @return True iff the size and elements of the deques are equal.
1453 *
1454 * This is an equivalence relation. It is linear in the size of the
1455 * deques. Deques are considered equivalent if their sizes are equal,
1456 * and if corresponding elements compare equal.
1457 */
1458 template<typename _Tp, typename _Alloc>
1459 inline bool
1460 operator==(const deque<_Tp, _Alloc>& __x,
1461 const deque<_Tp, _Alloc>& __y)
1462 { return __x.size() == __y.size()
1463 && std::equal(__x.begin(), __x.end(), __y.begin()); }
1464
1465 /**
1466 * @brief Deque ordering relation.
1467 * @param x A %deque.
1468 * @param y A %deque of the same type as @a x.
1469 * @return True iff @a x is lexicographically less than @a y.
1470 *
1471 * This is a total ordering relation. It is linear in the size of the
1472 * deques. The elements must be comparable with @c <.
1473 *
1474 * See std::lexicographical_compare() for how the determination is made.
1475 */
1476 template<typename _Tp, typename _Alloc>
1477 inline bool
1478 operator<(const deque<_Tp, _Alloc>& __x,
1479 const deque<_Tp, _Alloc>& __y)
1480 { return lexicographical_compare(__x.begin(), __x.end(),
1481 __y.begin(), __y.end()); }
1482
1483 /// Based on operator==
1484 template<typename _Tp, typename _Alloc>
1485 inline bool
1486 operator!=(const deque<_Tp, _Alloc>& __x,
1487 const deque<_Tp, _Alloc>& __y)
1488 { return !(__x == __y); }
1489
1490 /// Based on operator<
1491 template<typename _Tp, typename _Alloc>
1492 inline bool
1493 operator>(const deque<_Tp, _Alloc>& __x,
1494 const deque<_Tp, _Alloc>& __y)
1495 { return __y < __x; }
1496
1497 /// Based on operator<
1498 template<typename _Tp, typename _Alloc>
1499 inline bool
1500 operator<=(const deque<_Tp, _Alloc>& __x,
1501 const deque<_Tp, _Alloc>& __y)
1502 { return !(__y < __x); }
1503
1504 /// Based on operator<
1505 template<typename _Tp, typename _Alloc>
1506 inline bool
1507 operator>=(const deque<_Tp, _Alloc>& __x,
1508 const deque<_Tp, _Alloc>& __y)
1509 { return !(__x < __y); }
1510
1511 /// See std::deque::swap().
1512 template<typename _Tp, typename _Alloc>
1513 inline void
1514 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
1515 { __x.swap(__y); }
1516 } // namespace std
1517
1518 #endif /* _DEQUE_H */