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