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