1 // Deque implementation -*- C++ -*- 2 3 // Copyright (C) 2001-2014 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 { } 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 typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type; 491 492 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type; 493 494 //This struct encapsulates the implementation of the std::deque 495 //standard container and at the same time makes use of the EBO 496 //for empty allocators. 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 /** 786 * @brief Creates a %deque with no elements. 787 */ 788 deque() : _Base() { } 789 790 /** 791 * @brief Creates a %deque with no elements. 792 * @param __a An allocator object. 793 */ 794 explicit 795 deque(const allocator_type& __a) 796 : _Base(__a, 0) { } 797 798 #if __cplusplus >= 201103L 799 /** 800 * @brief Creates a %deque with default constructed elements. 801 * @param __n The number of elements to initially create. 802 * 803 * This constructor fills the %deque with @a n default 804 * constructed elements. 805 */ 806 explicit 807 deque(size_type __n) 808 : _Base(__n) 809 { _M_default_initialize(); } 810 811 /** 812 * @brief Creates a %deque with copies of an exemplar element. 813 * @param __n The number of elements to initially create. 814 * @param __value An element to copy. 815 * @param __a An allocator. 816 * 817 * This constructor fills the %deque with @a __n copies of @a __value. 818 */ 819 deque(size_type __n, const value_type& __value, 820 const allocator_type& __a = allocator_type()) 821 : _Base(__a, __n) 822 { _M_fill_initialize(__value); } 823 #else 824 /** 825 * @brief Creates a %deque with copies of an exemplar element. 826 * @param __n The number of elements to initially create. 827 * @param __value An element to copy. 828 * @param __a An allocator. 829 * 830 * This constructor fills the %deque with @a __n copies of @a __value. 831 */ 832 explicit 833 deque(size_type __n, const value_type& __value = value_type(), 834 const allocator_type& __a = allocator_type()) 835 : _Base(__a, __n) 836 { _M_fill_initialize(__value); } 837 #endif 838 839 /** 840 * @brief %Deque copy constructor. 841 * @param __x A %deque of identical element and allocator types. 842 * 843 * The newly-created %deque uses a copy of the allocation object used 844 * by @a __x. 845 */ 846 deque(const deque& __x) 847 : _Base(__x._M_get_Tp_allocator(), __x.size()) 848 { std::__uninitialized_copy_a(__x.begin(), __x.end(), 849 this->_M_impl._M_start, 850 _M_get_Tp_allocator()); } 851 852 #if __cplusplus >= 201103L 853 /** 854 * @brief %Deque move constructor. 855 * @param __x A %deque of identical element and allocator types. 856 * 857 * The newly-created %deque contains the exact contents of @a __x. 858 * The contents of @a __x are a valid, but unspecified %deque. 859 */ 860 deque(deque&& __x) 861 : _Base(std::move(__x)) { } 862 863 /** 864 * @brief Builds a %deque from an initializer list. 865 * @param __l An initializer_list. 866 * @param __a An allocator object. 867 * 868 * Create a %deque consisting of copies of the elements in the 869 * initializer_list @a __l. 870 * 871 * This will call the element type's copy constructor N times 872 * (where N is __l.size()) and do no memory reallocation. 873 */ 874 deque(initializer_list<value_type> __l, 875 const allocator_type& __a = allocator_type()) 876 : _Base(__a) 877 { 878 _M_range_initialize(__l.begin(), __l.end(), 879 random_access_iterator_tag()); 880 } 881 #endif 882 883 /** 884 * @brief Builds a %deque from a range. 885 * @param __first An input iterator. 886 * @param __last An input iterator. 887 * @param __a An allocator object. 888 * 889 * Create a %deque consisting of copies of the elements from [__first, 890 * __last). 891 * 892 * If the iterators are forward, bidirectional, or random-access, then 893 * this will call the elements' copy constructor N times (where N is 894 * distance(__first,__last)) and do no memory reallocation. But if only 895 * input iterators are used, then this will do at most 2N calls to the 896 * copy constructor, and logN memory reallocations. 897 */ 898 #if __cplusplus >= 201103L 899 template<typename _InputIterator, 900 typename = std::_RequireInputIter<_InputIterator>> 901 deque(_InputIterator __first, _InputIterator __last, 902 const allocator_type& __a = allocator_type()) 903 : _Base(__a) 904 { _M_initialize_dispatch(__first, __last, __false_type()); } 905 #else 906 template<typename _InputIterator> 907 deque(_InputIterator __first, _InputIterator __last, 908 const allocator_type& __a = allocator_type()) 909 : _Base(__a) 910 { 911 // Check whether it's an integral type. If so, it's not an iterator. 912 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 913 _M_initialize_dispatch(__first, __last, _Integral()); 914 } 915 #endif 916 917 /** 918 * The dtor only erases the elements, and note that if the elements 919 * themselves are pointers, the pointed-to memory is not touched in any 920 * way. Managing the pointer is the user's responsibility. 921 */ 922 ~deque() _GLIBCXX_NOEXCEPT 923 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); } 924 925 /** 926 * @brief %Deque assignment operator. 927 * @param __x A %deque of identical element and allocator types. 928 * 929 * All the elements of @a x are copied, but unlike the copy constructor, 930 * the allocator object is not copied. 931 */ 932 deque& 933 operator=(const deque& __x); 934 935 #if __cplusplus >= 201103L 936 /** 937 * @brief %Deque move assignment operator. 938 * @param __x A %deque of identical element and allocator types. 939 * 940 * The contents of @a __x are moved into this deque (without copying). 941 * @a __x is a valid, but unspecified %deque. 942 */ 943 deque& 944 operator=(deque&& __x) noexcept 945 { 946 // NB: DR 1204. 947 // NB: DR 675. 948 this->clear(); 949 this->swap(__x); 950 return *this; 951 } 952 953 /** 954 * @brief Assigns an initializer list to a %deque. 955 * @param __l An initializer_list. 956 * 957 * This function fills a %deque with copies of the elements in the 958 * initializer_list @a __l. 959 * 960 * Note that the assignment completely changes the %deque and that the 961 * resulting %deque's size is the same as the number of elements 962 * assigned. Old data may be lost. 963 */ 964 deque& 965 operator=(initializer_list<value_type> __l) 966 { 967 this->assign(__l.begin(), __l.end()); 968 return *this; 969 } 970 #endif 971 972 /** 973 * @brief Assigns a given value to a %deque. 974 * @param __n Number of elements to be assigned. 975 * @param __val Value to be assigned. 976 * 977 * This function fills a %deque with @a n copies of the given 978 * value. Note that the assignment completely changes the 979 * %deque and that the resulting %deque's size is the same as 980 * the number of elements assigned. Old data may be lost. 981 */ 982 void 983 assign(size_type __n, const value_type& __val) 984 { _M_fill_assign(__n, __val); } 985 986 /** 987 * @brief Assigns a range to a %deque. 988 * @param __first An input iterator. 989 * @param __last An input iterator. 990 * 991 * This function fills a %deque with copies of the elements in the 992 * range [__first,__last). 993 * 994 * Note that the assignment completely changes the %deque and that the 995 * resulting %deque's size is the same as the number of elements 996 * assigned. Old data may be lost. 997 */ 998 #if __cplusplus >= 201103L 999 template<typename _InputIterator, 1000 typename = std::_RequireInputIter<_InputIterator>> 1001 void 1002 assign(_InputIterator __first, _InputIterator __last) 1003 { _M_assign_dispatch(__first, __last, __false_type()); } 1004 #else 1005 template<typename _InputIterator> 1006 void 1007 assign(_InputIterator __first, _InputIterator __last) 1008 { 1009 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 1010 _M_assign_dispatch(__first, __last, _Integral()); 1011 } 1012 #endif 1013 1014 #if __cplusplus >= 201103L 1015 /** 1016 * @brief Assigns an initializer list to a %deque. 1017 * @param __l An initializer_list. 1018 * 1019 * This function fills a %deque with copies of the elements in the 1020 * initializer_list @a __l. 1021 * 1022 * Note that the assignment completely changes the %deque and that the 1023 * resulting %deque's size is the same as the number of elements 1024 * assigned. Old data may be lost. 1025 */ 1026 void 1027 assign(initializer_list<value_type> __l) 1028 { this->assign(__l.begin(), __l.end()); } 1029 #endif 1030 1031 /// Get a copy of the memory allocation object. 1032 allocator_type 1033 get_allocator() const _GLIBCXX_NOEXCEPT 1034 { return _Base::get_allocator(); } 1035 1036 // iterators 1037 /** 1038 * Returns a read/write iterator that points to the first element in the 1039 * %deque. Iteration is done in ordinary element order. 1040 */ 1041 iterator 1042 begin() _GLIBCXX_NOEXCEPT 1043 { return this->_M_impl._M_start; } 1044 1045 /** 1046 * Returns a read-only (constant) iterator that points to the first 1047 * element in the %deque. Iteration is done in ordinary element order. 1048 */ 1049 const_iterator 1050 begin() const _GLIBCXX_NOEXCEPT 1051 { return this->_M_impl._M_start; } 1052 1053 /** 1054 * Returns a read/write iterator that points one past the last 1055 * element in the %deque. Iteration is done in ordinary 1056 * element order. 1057 */ 1058 iterator 1059 end() _GLIBCXX_NOEXCEPT 1060 { return this->_M_impl._M_finish; } 1061 1062 /** 1063 * Returns a read-only (constant) iterator that points one past 1064 * the last element in the %deque. Iteration is done in 1065 * ordinary element order. 1066 */ 1067 const_iterator 1068 end() const _GLIBCXX_NOEXCEPT 1069 { return this->_M_impl._M_finish; } 1070 1071 /** 1072 * Returns a read/write reverse iterator that points to the 1073 * last element in the %deque. Iteration is done in reverse 1074 * element order. 1075 */ 1076 reverse_iterator 1077 rbegin() _GLIBCXX_NOEXCEPT 1078 { return reverse_iterator(this->_M_impl._M_finish); } 1079 1080 /** 1081 * Returns a read-only (constant) reverse iterator that points 1082 * to the last element in the %deque. Iteration is done in 1083 * reverse element order. 1084 */ 1085 const_reverse_iterator 1086 rbegin() const _GLIBCXX_NOEXCEPT 1087 { return const_reverse_iterator(this->_M_impl._M_finish); } 1088 1089 /** 1090 * Returns a read/write reverse iterator that points to one 1091 * before the first element in the %deque. Iteration is done 1092 * in reverse element order. 1093 */ 1094 reverse_iterator 1095 rend() _GLIBCXX_NOEXCEPT 1096 { return reverse_iterator(this->_M_impl._M_start); } 1097 1098 /** 1099 * Returns a read-only (constant) reverse iterator that points 1100 * to one before the first element in the %deque. Iteration is 1101 * done in reverse element order. 1102 */ 1103 const_reverse_iterator 1104 rend() const _GLIBCXX_NOEXCEPT 1105 { return const_reverse_iterator(this->_M_impl._M_start); } 1106 1107 #if __cplusplus >= 201103L 1108 /** 1109 * Returns a read-only (constant) iterator that points to the first 1110 * element in the %deque. Iteration is done in ordinary element order. 1111 */ 1112 const_iterator 1113 cbegin() const noexcept 1114 { return this->_M_impl._M_start; } 1115 1116 /** 1117 * Returns a read-only (constant) iterator that points one past 1118 * the last element in the %deque. Iteration is done in 1119 * ordinary element order. 1120 */ 1121 const_iterator 1122 cend() const noexcept 1123 { return this->_M_impl._M_finish; } 1124 1125 /** 1126 * Returns a read-only (constant) reverse iterator that points 1127 * to the last element in the %deque. Iteration is done in 1128 * reverse element order. 1129 */ 1130 const_reverse_iterator 1131 crbegin() const noexcept 1132 { return const_reverse_iterator(this->_M_impl._M_finish); } 1133 1134 /** 1135 * Returns a read-only (constant) reverse iterator that points 1136 * to one before the first element in the %deque. Iteration is 1137 * done in reverse element order. 1138 */ 1139 const_reverse_iterator 1140 crend() const noexcept 1141 { return const_reverse_iterator(this->_M_impl._M_start); } 1142 #endif 1143 1144 // [23.2.1.2] capacity 1145 /** Returns the number of elements in the %deque. */ 1146 size_type 1147 size() const _GLIBCXX_NOEXCEPT 1148 { return this->_M_impl._M_finish - this->_M_impl._M_start; } 1149 1150 /** Returns the size() of the largest possible %deque. */ 1151 size_type 1152 max_size() const _GLIBCXX_NOEXCEPT 1153 { return _M_get_Tp_allocator().max_size(); } 1154 1155 #if __cplusplus >= 201103L 1156 /** 1157 * @brief Resizes the %deque to the specified number of elements. 1158 * @param __new_size Number of elements the %deque should contain. 1159 * 1160 * This function will %resize the %deque to the specified 1161 * number of elements. If the number is smaller than the 1162 * %deque's current size the %deque is truncated, otherwise 1163 * default constructed elements are appended. 1164 */ 1165 void 1166 resize(size_type __new_size) 1167 { 1168 const size_type __len = size(); 1169 if (__new_size > __len) 1170 _M_default_append(__new_size - __len); 1171 else if (__new_size < __len) 1172 _M_erase_at_end(this->_M_impl._M_start 1173 + difference_type(__new_size)); 1174 } 1175 1176 /** 1177 * @brief Resizes the %deque to the specified number of elements. 1178 * @param __new_size Number of elements the %deque should contain. 1179 * @param __x Data with which new elements should be populated. 1180 * 1181 * This function will %resize the %deque to the specified 1182 * number of elements. If the number is smaller than the 1183 * %deque's current size the %deque is truncated, otherwise the 1184 * %deque is extended and new elements are populated with given 1185 * data. 1186 */ 1187 void 1188 resize(size_type __new_size, const value_type& __x) 1189 { 1190 const size_type __len = size(); 1191 if (__new_size > __len) 1192 insert(this->_M_impl._M_finish, __new_size - __len, __x); 1193 else if (__new_size < __len) 1194 _M_erase_at_end(this->_M_impl._M_start 1195 + difference_type(__new_size)); 1196 } 1197 #else 1198 /** 1199 * @brief Resizes the %deque to the specified number of elements. 1200 * @param __new_size Number of elements the %deque should contain. 1201 * @param __x Data with which new elements should be populated. 1202 * 1203 * This function will %resize the %deque to the specified 1204 * number of elements. If the number is smaller than the 1205 * %deque's current size the %deque is truncated, otherwise the 1206 * %deque is extended and new elements are populated with given 1207 * data. 1208 */ 1209 void 1210 resize(size_type __new_size, value_type __x = value_type()) 1211 { 1212 const size_type __len = size(); 1213 if (__new_size > __len) 1214 insert(this->_M_impl._M_finish, __new_size - __len, __x); 1215 else if (__new_size < __len) 1216 _M_erase_at_end(this->_M_impl._M_start 1217 + difference_type(__new_size)); 1218 } 1219 #endif 1220 1221 #if __cplusplus >= 201103L 1222 /** A non-binding request to reduce memory use. */ 1223 void 1224 shrink_to_fit() noexcept 1225 { _M_shrink_to_fit(); } 1226 #endif 1227 1228 /** 1229 * Returns true if the %deque is empty. (Thus begin() would 1230 * equal end().) 1231 */ 1232 bool 1233 empty() const _GLIBCXX_NOEXCEPT 1234 { return this->_M_impl._M_finish == this->_M_impl._M_start; } 1235 1236 // element access 1237 /** 1238 * @brief Subscript access to the data contained in the %deque. 1239 * @param __n The index of the element for which data should be 1240 * accessed. 1241 * @return Read/write reference to data. 1242 * 1243 * This operator allows for easy, array-style, data access. 1244 * Note that data access with this operator is unchecked and 1245 * out_of_range lookups are not defined. (For checked lookups 1246 * see at().) 1247 */ 1248 reference 1249 operator[](size_type __n) _GLIBCXX_NOEXCEPT 1250 { 1251 #if __google_stl_debug_deque 1252 _M_range_check(__n); 1253 #endif 1254 return this->_M_impl._M_start[difference_type(__n)]; 1255 } 1256 1257 /** 1258 * @brief Subscript access to the data contained in the %deque. 1259 * @param __n The index of the element for which data should be 1260 * accessed. 1261 * @return Read-only (constant) reference to data. 1262 * 1263 * This operator allows for easy, array-style, data access. 1264 * Note that data access with this operator is unchecked and 1265 * out_of_range lookups are not defined. (For checked lookups 1266 * see at().) 1267 */ 1268 const_reference 1269 operator[](size_type __n) const _GLIBCXX_NOEXCEPT 1270 { 1271 #if __google_stl_debug_deque 1272 _M_range_check(__n); 1273 #endif 1274 return this->_M_impl._M_start[difference_type(__n)]; 1275 } 1276 1277 protected: 1278 /// Safety check used only from at(). 1279 void 1280 _M_range_check(size_type __n) const 1281 { 1282 if (__n >= this->size()) 1283 __throw_out_of_range_fmt(__N("deque::_M_range_check: __n " 1284 "(which is %zu)>= this->size() " 1285 "(which is %zu)"), 1286 __n, this->size()); 1287 } 1288 1289 public: 1290 /** 1291 * @brief Provides access to the data contained in the %deque. 1292 * @param __n The index of the element for which data should be 1293 * accessed. 1294 * @return Read/write reference to data. 1295 * @throw std::out_of_range If @a __n is an invalid index. 1296 * 1297 * This function provides for safer data access. The parameter 1298 * is first checked that it is in the range of the deque. The 1299 * function throws out_of_range if the check fails. 1300 */ 1301 reference 1302 at(size_type __n) 1303 { 1304 _M_range_check(__n); 1305 return (*this)[__n]; 1306 } 1307 1308 /** 1309 * @brief Provides access to the data contained in the %deque. 1310 * @param __n The index of the element for which data should be 1311 * accessed. 1312 * @return Read-only (constant) reference to data. 1313 * @throw std::out_of_range If @a __n is an invalid index. 1314 * 1315 * This function provides for safer data access. The parameter is first 1316 * checked that it is in the range of the deque. The function throws 1317 * out_of_range if the check fails. 1318 */ 1319 const_reference 1320 at(size_type __n) const 1321 { 1322 _M_range_check(__n); 1323 return (*this)[__n]; 1324 } 1325 1326 /** 1327 * Returns a read/write reference to the data at the first 1328 * element of the %deque. 1329 */ 1330 reference 1331 front() _GLIBCXX_NOEXCEPT 1332 { 1333 #if __google_stl_debug_deque 1334 if (empty()) __throw_logic_error("front() on empty deque"); 1335 #endif 1336 return *begin(); 1337 } 1338 1339 /** 1340 * Returns a read-only (constant) reference to the data at the first 1341 * element of the %deque. 1342 */ 1343 const_reference 1344 front() const _GLIBCXX_NOEXCEPT 1345 { 1346 #if __google_stl_debug_deque 1347 if (empty()) __throw_logic_error("front() on empty deque"); 1348 #endif 1349 return *begin(); 1350 } 1351 1352 /** 1353 * Returns a read/write reference to the data at the last element of the 1354 * %deque. 1355 */ 1356 reference 1357 back() _GLIBCXX_NOEXCEPT 1358 { 1359 #if __google_stl_debug_deque 1360 if (empty()) __throw_logic_error("back() on empty deque"); 1361 #endif 1362 iterator __tmp = end(); 1363 --__tmp; 1364 return *__tmp; 1365 } 1366 1367 /** 1368 * Returns a read-only (constant) reference to the data at the last 1369 * element of the %deque. 1370 */ 1371 const_reference 1372 back() const _GLIBCXX_NOEXCEPT 1373 { 1374 #if __google_stl_debug_deque 1375 if (empty()) __throw_logic_error("back() on empty deque"); 1376 #endif 1377 const_iterator __tmp = end(); 1378 --__tmp; 1379 return *__tmp; 1380 } 1381 1382 // [23.2.1.2] modifiers 1383 /** 1384 * @brief Add data to the front of the %deque. 1385 * @param __x Data to be added. 1386 * 1387 * This is a typical stack operation. The function creates an 1388 * element at the front of the %deque and assigns the given 1389 * data to it. Due to the nature of a %deque this operation 1390 * can be done in constant time. 1391 */ 1392 void 1393 push_front(const value_type& __x) 1394 { 1395 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first) 1396 { 1397 this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1, __x); 1398 --this->_M_impl._M_start._M_cur; 1399 } 1400 else 1401 _M_push_front_aux(__x); 1402 } 1403 1404 #if __cplusplus >= 201103L 1405 void 1406 push_front(value_type&& __x) 1407 { emplace_front(std::move(__x)); } 1408 1409 template<typename... _Args> 1410 void 1411 emplace_front(_Args&&... __args); 1412 #endif 1413 1414 /** 1415 * @brief Add data to the end of the %deque. 1416 * @param __x Data to be added. 1417 * 1418 * This is a typical stack operation. The function creates an 1419 * element at the end of the %deque and assigns the given data 1420 * to it. Due to the nature of a %deque this operation can be 1421 * done in constant time. 1422 */ 1423 void 1424 push_back(const value_type& __x) 1425 { 1426 if (this->_M_impl._M_finish._M_cur 1427 != this->_M_impl._M_finish._M_last - 1) 1428 { 1429 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x); 1430 ++this->_M_impl._M_finish._M_cur; 1431 } 1432 else 1433 _M_push_back_aux(__x); 1434 } 1435 1436 #if __cplusplus >= 201103L 1437 void 1438 push_back(value_type&& __x) 1439 { emplace_back(std::move(__x)); } 1440 1441 template<typename... _Args> 1442 void 1443 emplace_back(_Args&&... __args); 1444 #endif 1445 1446 /** 1447 * @brief Removes first element. 1448 * 1449 * This is a typical stack operation. It shrinks the %deque by one. 1450 * 1451 * Note that no data is returned, and if the first element's data is 1452 * needed, it should be retrieved before pop_front() is called. 1453 */ 1454 void 1455 pop_front() _GLIBCXX_NOEXCEPT 1456 { 1457 #if __google_stl_debug_deque 1458 if (empty()) __throw_logic_error("pop_front() on empty deque"); 1459 #endif 1460 if (this->_M_impl._M_start._M_cur 1461 != this->_M_impl._M_start._M_last - 1) 1462 { 1463 this->_M_impl.destroy(this->_M_impl._M_start._M_cur); 1464 ++this->_M_impl._M_start._M_cur; 1465 } 1466 else 1467 _M_pop_front_aux(); 1468 } 1469 1470 /** 1471 * @brief Removes last element. 1472 * 1473 * This is a typical stack operation. It shrinks the %deque by one. 1474 * 1475 * Note that no data is returned, and if the last element's data is 1476 * needed, it should be retrieved before pop_back() is called. 1477 */ 1478 void 1479 pop_back() _GLIBCXX_NOEXCEPT 1480 { 1481 #if __google_stl_debug_deque 1482 if (empty()) __throw_logic_error("pop_back() on empty deque"); 1483 #endif 1484 if (this->_M_impl._M_finish._M_cur 1485 != this->_M_impl._M_finish._M_first) 1486 { 1487 --this->_M_impl._M_finish._M_cur; 1488 this->_M_impl.destroy(this->_M_impl._M_finish._M_cur); 1489 } 1490 else 1491 _M_pop_back_aux(); 1492 } 1493 1494 #if __cplusplus >= 201103L 1495 /** 1496 * @brief Inserts an object in %deque before specified iterator. 1497 * @param __position A const_iterator into the %deque. 1498 * @param __args Arguments. 1499 * @return An iterator that points to the inserted data. 1500 * 1501 * This function will insert an object of type T constructed 1502 * with T(std::forward<Args>(args)...) before the specified location. 1503 */ 1504 template<typename... _Args> 1505 iterator 1506 emplace(const_iterator __position, _Args&&... __args); 1507 1508 /** 1509 * @brief Inserts given value into %deque before specified iterator. 1510 * @param __position A const_iterator into the %deque. 1511 * @param __x Data to be inserted. 1512 * @return An iterator that points to the inserted data. 1513 * 1514 * This function will insert a copy of the given value before the 1515 * specified location. 1516 */ 1517 iterator 1518 insert(const_iterator __position, const value_type& __x); 1519 #else 1520 /** 1521 * @brief Inserts given value into %deque before specified iterator. 1522 * @param __position An iterator into the %deque. 1523 * @param __x Data to be inserted. 1524 * @return An iterator that points to the inserted data. 1525 * 1526 * This function will insert a copy of the given value before the 1527 * specified location. 1528 */ 1529 iterator 1530 insert(iterator __position, const value_type& __x); 1531 #endif 1532 1533 #if __cplusplus >= 201103L 1534 /** 1535 * @brief Inserts given rvalue into %deque before specified iterator. 1536 * @param __position A const_iterator into the %deque. 1537 * @param __x Data to be inserted. 1538 * @return An iterator that points to the inserted data. 1539 * 1540 * This function will insert a copy of the given rvalue before the 1541 * specified location. 1542 */ 1543 iterator 1544 insert(const_iterator __position, value_type&& __x) 1545 { return emplace(__position, std::move(__x)); } 1546 1547 /** 1548 * @brief Inserts an initializer list into the %deque. 1549 * @param __p An iterator into the %deque. 1550 * @param __l An initializer_list. 1551 * 1552 * This function will insert copies of the data in the 1553 * initializer_list @a __l into the %deque before the location 1554 * specified by @a __p. This is known as <em>list insert</em>. 1555 */ 1556 iterator 1557 insert(const_iterator __p, initializer_list<value_type> __l) 1558 { return this->insert(__p, __l.begin(), __l.end()); } 1559 #endif 1560 1561 #if __cplusplus >= 201103L 1562 /** 1563 * @brief Inserts a number of copies of given data into the %deque. 1564 * @param __position A const_iterator into the %deque. 1565 * @param __n Number of elements to be inserted. 1566 * @param __x Data to be inserted. 1567 * @return An iterator that points to the inserted data. 1568 * 1569 * This function will insert a specified number of copies of the given 1570 * data before the location specified by @a __position. 1571 */ 1572 iterator 1573 insert(const_iterator __position, size_type __n, const value_type& __x) 1574 { 1575 #if __google_stl_debug_deque 1576 if (__position < this->begin() || __position > this->end()) 1577 __throw_logic_error("insert() at invalid position"); 1578 #endif 1579 difference_type __offset = __position - cbegin(); 1580 _M_fill_insert(__position._M_const_cast(), __n, __x); 1581 return begin() + __offset; 1582 } 1583 #else 1584 /** 1585 * @brief Inserts a number of copies of given data into the %deque. 1586 * @param __position An iterator into the %deque. 1587 * @param __n Number of elements to be inserted. 1588 * @param __x Data to be inserted. 1589 * 1590 * This function will insert a specified number of copies of the given 1591 * data before the location specified by @a __position. 1592 */ 1593 void 1594 insert(iterator __position, size_type __n, const value_type& __x) 1595 { 1596 #if __google_stl_debug_deque 1597 if (__position < this->begin() || __position > this->end()) 1598 __throw_logic_error("insert() at invalid position"); 1599 #endif 1600 _M_fill_insert(__position, __n, __x); 1601 } 1602 #endif 1603 1604 #if __cplusplus >= 201103L 1605 /** 1606 * @brief Inserts a range into the %deque. 1607 * @param __position A const_iterator into the %deque. 1608 * @param __first An input iterator. 1609 * @param __last An input iterator. 1610 * @return An iterator that points to the inserted data. 1611 * 1612 * This function will insert copies of the data in the range 1613 * [__first,__last) into the %deque before the location specified 1614 * by @a __position. This is known as <em>range insert</em>. 1615 */ 1616 template<typename _InputIterator, 1617 typename = std::_RequireInputIter<_InputIterator>> 1618 iterator 1619 insert(const_iterator __position, _InputIterator __first, 1620 _InputIterator __last) 1621 { 1622 #if __google_stl_debug_vector 1623 if (__position < this->begin() || __position > this->end()) 1624 __throw_out_of_range(__N("insert() at invalid position")); 1625 #endif 1626 difference_type __offset = __position - cbegin(); 1627 _M_insert_dispatch(__position._M_const_cast(), 1628 __first, __last, __false_type()); 1629 return begin() + __offset; 1630 } 1631 #else 1632 /** 1633 * @brief Inserts a range into the %deque. 1634 * @param __position An iterator into the %deque. 1635 * @param __first An input iterator. 1636 * @param __last An input iterator. 1637 * 1638 * This function will insert copies of the data in the range 1639 * [__first,__last) into the %deque before the location specified 1640 * by @a __position. This is known as <em>range insert</em>. 1641 */ 1642 template<typename _InputIterator> 1643 void 1644 insert(iterator __position, _InputIterator __first, 1645 _InputIterator __last) 1646 { 1647 // Check whether it's an integral type. If so, it's not an iterator. 1648 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 1649 _M_insert_dispatch(__position, __first, __last, _Integral()); 1650 } 1651 #endif 1652 1653 /** 1654 * @brief Remove element at given position. 1655 * @param __position Iterator pointing to element to be erased. 1656 * @return An iterator pointing to the next element (or end()). 1657 * 1658 * This function will erase the element at the given position and thus 1659 * shorten the %deque by one. 1660 * 1661 * The user is cautioned that 1662 * this function only erases the element, and that if the element is 1663 * itself a pointer, the pointed-to memory is not touched in any way. 1664 * Managing the pointer is the user's responsibility. 1665 */ 1666 iterator 1667 #if __cplusplus >= 201103L 1668 erase(const_iterator __position) 1669 #else 1670 erase(iterator __position) 1671 #endif 1672 { return _M_erase(__position._M_const_cast()); } 1673 1674 /** 1675 * @brief Remove a range of elements. 1676 * @param __first Iterator pointing to the first element to be erased. 1677 * @param __last Iterator pointing to one past the last element to be 1678 * erased. 1679 * @return An iterator pointing to the element pointed to by @a last 1680 * prior to erasing (or end()). 1681 * 1682 * This function will erase the elements in the range 1683 * [__first,__last) and shorten the %deque accordingly. 1684 * 1685 * The user is cautioned that 1686 * this function only erases the elements, and that if the elements 1687 * themselves are pointers, the pointed-to memory is not touched in any 1688 * way. Managing the pointer is the user's responsibility. 1689 */ 1690 iterator 1691 #if __cplusplus >= 201103L 1692 erase(const_iterator __first, const_iterator __last) 1693 #else 1694 erase(iterator __first, iterator __last) 1695 #endif 1696 { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); } 1697 1698 /** 1699 * @brief Swaps data with another %deque. 1700 * @param __x A %deque of the same element and allocator types. 1701 * 1702 * This exchanges the elements between two deques in constant time. 1703 * (Four pointers, so it should be quite fast.) 1704 * Note that the global std::swap() function is specialized such that 1705 * std::swap(d1,d2) will feed to this function. 1706 */ 1707 void 1708 swap(deque& __x) _GLIBCXX_NOEXCEPT 1709 { 1710 std::swap(this->_M_impl._M_start, __x._M_impl._M_start); 1711 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish); 1712 std::swap(this->_M_impl._M_map, __x._M_impl._M_map); 1713 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size); 1714 1715 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1716 // 431. Swapping containers with unequal allocators. 1717 std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(), 1718 __x._M_get_Tp_allocator()); 1719 } 1720 1721 /** 1722 * Erases all the elements. Note that this function only erases the 1723 * elements, and that if the elements themselves are pointers, the 1724 * pointed-to memory is not touched in any way. Managing the pointer is 1725 * the user's responsibility. 1726 */ 1727 void 1728 clear() _GLIBCXX_NOEXCEPT 1729 { _M_erase_at_end(begin()); } 1730 1731 protected: 1732 // Internal constructor functions follow. 1733 1734 // called by the range constructor to implement [23.1.1]/9 1735 1736 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1737 // 438. Ambiguity in the "do the right thing" clause 1738 template<typename _Integer> 1739 void 1740 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) 1741 { 1742 _M_initialize_map(static_cast<size_type>(__n)); 1743 _M_fill_initialize(__x); 1744 } 1745 1746 // called by the range constructor to implement [23.1.1]/9 1747 template<typename _InputIterator> 1748 void 1749 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last, 1750 __false_type) 1751 { 1752 typedef typename std::iterator_traits<_InputIterator>:: 1753 iterator_category _IterCategory; 1754 _M_range_initialize(__first, __last, _IterCategory()); 1755 } 1756 1757 // called by the second initialize_dispatch above 1758 //@{ 1759 /** 1760 * @brief Fills the deque with whatever is in [first,last). 1761 * @param __first An input iterator. 1762 * @param __last An input iterator. 1763 * @return Nothing. 1764 * 1765 * If the iterators are actually forward iterators (or better), then the 1766 * memory layout can be done all at once. Else we move forward using 1767 * push_back on each value from the iterator. 1768 */ 1769 template<typename _InputIterator> 1770 void 1771 _M_range_initialize(_InputIterator __first, _InputIterator __last, 1772 std::input_iterator_tag); 1773 1774 // called by the second initialize_dispatch above 1775 template<typename _ForwardIterator> 1776 void 1777 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last, 1778 std::forward_iterator_tag); 1779 //@} 1780 1781 /** 1782 * @brief Fills the %deque with copies of value. 1783 * @param __value Initial value. 1784 * @return Nothing. 1785 * @pre _M_start and _M_finish have already been initialized, 1786 * but none of the %deque's elements have yet been constructed. 1787 * 1788 * This function is called only when the user provides an explicit size 1789 * (with or without an explicit exemplar value). 1790 */ 1791 void 1792 _M_fill_initialize(const value_type& __value); 1793 1794 #if __cplusplus >= 201103L 1795 // called by deque(n). 1796 void 1797 _M_default_initialize(); 1798 #endif 1799 1800 // Internal assign functions follow. The *_aux functions do the actual 1801 // assignment work for the range versions. 1802 1803 // called by the range assign to implement [23.1.1]/9 1804 1805 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1806 // 438. Ambiguity in the "do the right thing" clause 1807 template<typename _Integer> 1808 void 1809 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 1810 { _M_fill_assign(__n, __val); } 1811 1812 // called by the range assign to implement [23.1.1]/9 1813 template<typename _InputIterator> 1814 void 1815 _M_assign_dispatch(_InputIterator __first, _InputIterator __last, 1816 __false_type) 1817 { 1818 typedef typename std::iterator_traits<_InputIterator>:: 1819 iterator_category _IterCategory; 1820 _M_assign_aux(__first, __last, _IterCategory()); 1821 } 1822 1823 // called by the second assign_dispatch above 1824 template<typename _InputIterator> 1825 void 1826 _M_assign_aux(_InputIterator __first, _InputIterator __last, 1827 std::input_iterator_tag); 1828 1829 // called by the second assign_dispatch above 1830 template<typename _ForwardIterator> 1831 void 1832 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, 1833 std::forward_iterator_tag) 1834 { 1835 const size_type __len = std::distance(__first, __last); 1836 if (__len > size()) 1837 { 1838 _ForwardIterator __mid = __first; 1839 std::advance(__mid, size()); 1840 std::copy(__first, __mid, begin()); 1841 insert(end(), __mid, __last); 1842 } 1843 else 1844 _M_erase_at_end(std::copy(__first, __last, begin())); 1845 } 1846 1847 // Called by assign(n,t), and the range assign when it turns out 1848 // to be the same thing. 1849 void 1850 _M_fill_assign(size_type __n, const value_type& __val) 1851 { 1852 if (__n > size()) 1853 { 1854 std::fill(begin(), end(), __val); 1855 insert(end(), __n - size(), __val); 1856 } 1857 else 1858 { 1859 _M_erase_at_end(begin() + difference_type(__n)); 1860 std::fill(begin(), end(), __val); 1861 } 1862 } 1863 1864 //@{ 1865 /// Helper functions for push_* and pop_*. 1866 #if __cplusplus < 201103L 1867 void _M_push_back_aux(const value_type&); 1868 1869 void _M_push_front_aux(const value_type&); 1870 #else 1871 template<typename... _Args> 1872 void _M_push_back_aux(_Args&&... __args); 1873 1874 template<typename... _Args> 1875 void _M_push_front_aux(_Args&&... __args); 1876 #endif 1877 1878 void _M_pop_back_aux(); 1879 1880 void _M_pop_front_aux(); 1881 //@} 1882 1883 // Internal insert functions follow. The *_aux functions do the actual 1884 // insertion work when all shortcuts fail. 1885 1886 // called by the range insert to implement [23.1.1]/9 1887 1888 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1889 // 438. Ambiguity in the "do the right thing" clause 1890 template<typename _Integer> 1891 void 1892 _M_insert_dispatch(iterator __pos, 1893 _Integer __n, _Integer __x, __true_type) 1894 { _M_fill_insert(__pos, __n, __x); } 1895 1896 // called by the range insert to implement [23.1.1]/9 1897 template<typename _InputIterator> 1898 void 1899 _M_insert_dispatch(iterator __pos, 1900 _InputIterator __first, _InputIterator __last, 1901 __false_type) 1902 { 1903 typedef typename std::iterator_traits<_InputIterator>:: 1904 iterator_category _IterCategory; 1905 _M_range_insert_aux(__pos, __first, __last, _IterCategory()); 1906 } 1907 1908 // called by the second insert_dispatch above 1909 template<typename _InputIterator> 1910 void 1911 _M_range_insert_aux(iterator __pos, _InputIterator __first, 1912 _InputIterator __last, std::input_iterator_tag); 1913 1914 // called by the second insert_dispatch above 1915 template<typename _ForwardIterator> 1916 void 1917 _M_range_insert_aux(iterator __pos, _ForwardIterator __first, 1918 _ForwardIterator __last, std::forward_iterator_tag); 1919 1920 // Called by insert(p,n,x), and the range insert when it turns out to be 1921 // the same thing. Can use fill functions in optimal situations, 1922 // otherwise passes off to insert_aux(p,n,x). 1923 void 1924 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x); 1925 1926 // called by insert(p,x) 1927 #if __cplusplus < 201103L 1928 iterator 1929 _M_insert_aux(iterator __pos, const value_type& __x); 1930 #else 1931 template<typename... _Args> 1932 iterator 1933 _M_insert_aux(iterator __pos, _Args&&... __args); 1934 #endif 1935 1936 // called by insert(p,n,x) via fill_insert 1937 void 1938 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x); 1939 1940 // called by range_insert_aux for forward iterators 1941 template<typename _ForwardIterator> 1942 void 1943 _M_insert_aux(iterator __pos, 1944 _ForwardIterator __first, _ForwardIterator __last, 1945 size_type __n); 1946 1947 1948 // Internal erase functions follow. 1949 1950 void 1951 _M_destroy_data_aux(iterator __first, iterator __last); 1952 1953 // Called by ~deque(). 1954 // NB: Doesn't deallocate the nodes. 1955 template<typename _Alloc1> 1956 void 1957 _M_destroy_data(iterator __first, iterator __last, const _Alloc1&) 1958 { _M_destroy_data_aux(__first, __last); } 1959 1960 void 1961 _M_destroy_data(iterator __first, iterator __last, 1962 const std::allocator<_Tp>&) 1963 { 1964 if (!__has_trivial_destructor(value_type)) 1965 _M_destroy_data_aux(__first, __last); 1966 } 1967 1968 // Called by erase(q1, q2). 1969 void 1970 _M_erase_at_begin(iterator __pos) 1971 { 1972 _M_destroy_data(begin(), __pos, _M_get_Tp_allocator()); 1973 _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node); 1974 this->_M_impl._M_start = __pos; 1975 } 1976 1977 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux, 1978 // _M_fill_assign, operator=. 1979 void 1980 _M_erase_at_end(iterator __pos) 1981 { 1982 _M_destroy_data(__pos, end(), _M_get_Tp_allocator()); 1983 _M_destroy_nodes(__pos._M_node + 1, 1984 this->_M_impl._M_finish._M_node + 1); 1985 this->_M_impl._M_finish = __pos; 1986 } 1987 1988 iterator 1989 _M_erase(iterator __pos); 1990 1991 iterator 1992 _M_erase(iterator __first, iterator __last); 1993 1994 #if __cplusplus >= 201103L 1995 // Called by resize(sz). 1996 void 1997 _M_default_append(size_type __n); 1998 1999 bool 2000 _M_shrink_to_fit(); 2001 #endif 2002 2003 //@{ 2004 /// Memory-handling helpers for the previous internal insert functions. 2005 iterator 2006 _M_reserve_elements_at_front(size_type __n) 2007 { 2008 const size_type __vacancies = this->_M_impl._M_start._M_cur 2009 - this->_M_impl._M_start._M_first; 2010 if (__n > __vacancies) 2011 _M_new_elements_at_front(__n - __vacancies); 2012 return this->_M_impl._M_start - difference_type(__n); 2013 } 2014 2015 iterator 2016 _M_reserve_elements_at_back(size_type __n) 2017 { 2018 const size_type __vacancies = (this->_M_impl._M_finish._M_last 2019 - this->_M_impl._M_finish._M_cur) - 1; 2020 if (__n > __vacancies) 2021 _M_new_elements_at_back(__n - __vacancies); 2022 return this->_M_impl._M_finish + difference_type(__n); 2023 } 2024 2025 void 2026 _M_new_elements_at_front(size_type __new_elements); 2027 2028 void 2029 _M_new_elements_at_back(size_type __new_elements); 2030 //@} 2031 2032 2033 //@{ 2034 /** 2035 * @brief Memory-handling helpers for the major %map. 2036 * 2037 * Makes sure the _M_map has space for new nodes. Does not 2038 * actually add the nodes. Can invalidate _M_map pointers. 2039 * (And consequently, %deque iterators.) 2040 */ 2041 void 2042 _M_reserve_map_at_back(size_type __nodes_to_add = 1) 2043 { 2044 if (__nodes_to_add + 1 > this->_M_impl._M_map_size 2045 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map)) 2046 _M_reallocate_map(__nodes_to_add, false); 2047 } 2048 2049 void 2050 _M_reserve_map_at_front(size_type __nodes_to_add = 1) 2051 { 2052 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node 2053 - this->_M_impl._M_map)) 2054 _M_reallocate_map(__nodes_to_add, true); 2055 } 2056 2057 void 2058 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front); 2059 //@} 2060 }; 2061 2062 2063 /** 2064 * @brief Deque equality comparison. 2065 * @param __x A %deque. 2066 * @param __y A %deque of the same type as @a __x. 2067 * @return True iff the size and elements of the deques are equal. 2068 * 2069 * This is an equivalence relation. It is linear in the size of the 2070 * deques. Deques are considered equivalent if their sizes are equal, 2071 * and if corresponding elements compare equal. 2072 */ 2073 template<typename _Tp, typename _Alloc> 2074 inline bool 2075 operator==(const deque<_Tp, _Alloc>& __x, 2076 const deque<_Tp, _Alloc>& __y) 2077 { return __x.size() == __y.size() 2078 && std::equal(__x.begin(), __x.end(), __y.begin()); } 2079 2080 /** 2081 * @brief Deque ordering relation. 2082 * @param __x A %deque. 2083 * @param __y A %deque of the same type as @a __x. 2084 * @return True iff @a x is lexicographically less than @a __y. 2085 * 2086 * This is a total ordering relation. It is linear in the size of the 2087 * deques. The elements must be comparable with @c <. 2088 * 2089 * See std::lexicographical_compare() for how the determination is made. 2090 */ 2091 template<typename _Tp, typename _Alloc> 2092 inline bool 2093 operator<(const deque<_Tp, _Alloc>& __x, 2094 const deque<_Tp, _Alloc>& __y) 2095 { return std::lexicographical_compare(__x.begin(), __x.end(), 2096 __y.begin(), __y.end()); } 2097 2098 /// Based on operator== 2099 template<typename _Tp, typename _Alloc> 2100 inline bool 2101 operator!=(const deque<_Tp, _Alloc>& __x, 2102 const deque<_Tp, _Alloc>& __y) 2103 { return !(__x == __y); } 2104 2105 /// Based on operator< 2106 template<typename _Tp, typename _Alloc> 2107 inline bool 2108 operator>(const deque<_Tp, _Alloc>& __x, 2109 const deque<_Tp, _Alloc>& __y) 2110 { return __y < __x; } 2111 2112 /// Based on operator< 2113 template<typename _Tp, typename _Alloc> 2114 inline bool 2115 operator<=(const deque<_Tp, _Alloc>& __x, 2116 const deque<_Tp, _Alloc>& __y) 2117 { return !(__y < __x); } 2118 2119 /// Based on operator< 2120 template<typename _Tp, typename _Alloc> 2121 inline bool 2122 operator>=(const deque<_Tp, _Alloc>& __x, 2123 const deque<_Tp, _Alloc>& __y) 2124 { return !(__x < __y); } 2125 2126 /// See std::deque::swap(). 2127 template<typename _Tp, typename _Alloc> 2128 inline void 2129 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y) 2130 { __x.swap(__y); } 2131 2132 #undef _GLIBCXX_DEQUE_BUF_SIZE 2133 2134 _GLIBCXX_END_NAMESPACE_CONTAINER 2135 } // namespace std 2136 2137 #endif /* _STL_DEQUE_H */ 2138