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