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