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