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