|
libstdc++
|
00001 // Deque implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001-2019 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * 00027 * Copyright (c) 1994 00028 * Hewlett-Packard Company 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Hewlett-Packard Company makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 * 00038 * 00039 * Copyright (c) 1997 00040 * Silicon Graphics Computer Systems, Inc. 00041 * 00042 * Permission to use, copy, modify, distribute and sell this software 00043 * and its documentation for any purpose is hereby granted without fee, 00044 * provided that the above copyright notice appear in all copies and 00045 * that both that copyright notice and this permission notice appear 00046 * in supporting documentation. Silicon Graphics makes no 00047 * representations about the suitability of this software for any 00048 * purpose. It is provided "as is" without express or implied warranty. 00049 */ 00050 00051 /** @file bits/stl_deque.h 00052 * This is an internal header file, included by other library headers. 00053 * Do not attempt to use it directly. @headername{deque} 00054 */ 00055 00056 #ifndef _STL_DEQUE_H 00057 #define _STL_DEQUE_H 1 00058 00059 #include <bits/concept_check.h> 00060 #include <bits/stl_iterator_base_types.h> 00061 #include <bits/stl_iterator_base_funcs.h> 00062 #if __cplusplus >= 201103L 00063 #include <initializer_list> 00064 #include <bits/stl_uninitialized.h> // for __is_bitwise_relocatable 00065 #endif 00066 00067 #include <debug/assertions.h> 00068 00069 namespace std _GLIBCXX_VISIBILITY(default) 00070 { 00071 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00072 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER 00073 00074 /** 00075 * @brief This function controls the size of memory nodes. 00076 * @param __size The size of an element. 00077 * @return The number (not byte size) of elements per node. 00078 * 00079 * This function started off as a compiler kludge from SGI, but 00080 * seems to be a useful wrapper around a repeated constant 00081 * expression. The @b 512 is tunable (and no other code needs to 00082 * change), but no investigation has been done since inheriting the 00083 * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what 00084 * you are doing, however: changing it breaks the binary 00085 * compatibility!! 00086 */ 00087 00088 #ifndef _GLIBCXX_DEQUE_BUF_SIZE 00089 #define _GLIBCXX_DEQUE_BUF_SIZE 512 00090 #endif 00091 00092 _GLIBCXX_CONSTEXPR inline size_t 00093 __deque_buf_size(size_t __size) 00094 { return (__size < _GLIBCXX_DEQUE_BUF_SIZE 00095 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) : size_t(1)); } 00096 00097 00098 /** 00099 * @brief A deque::iterator. 00100 * 00101 * Quite a bit of intelligence here. Much of the functionality of 00102 * deque is actually passed off to this class. A deque holds two 00103 * of these internally, marking its valid range. Access to 00104 * elements is done as offsets of either of those two, relying on 00105 * operator overloading in this class. 00106 * 00107 * All the functions are op overloads except for _M_set_node. 00108 */ 00109 template<typename _Tp, typename _Ref, typename _Ptr> 00110 struct _Deque_iterator 00111 { 00112 #if __cplusplus < 201103L 00113 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator; 00114 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator; 00115 typedef _Tp* _Elt_pointer; 00116 typedef _Tp** _Map_pointer; 00117 #else 00118 private: 00119 template<typename _Up> 00120 using __ptr_to = typename pointer_traits<_Ptr>::template rebind<_Up>; 00121 template<typename _CvTp> 00122 using __iter = _Deque_iterator<_Tp, _CvTp&, __ptr_to<_CvTp>>; 00123 public: 00124 typedef __iter<_Tp> iterator; 00125 typedef __iter<const _Tp> const_iterator; 00126 typedef __ptr_to<_Tp> _Elt_pointer; 00127 typedef __ptr_to<_Elt_pointer> _Map_pointer; 00128 #endif 00129 00130 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT 00131 { return __deque_buf_size(sizeof(_Tp)); } 00132 00133 typedef std::random_access_iterator_tag iterator_category; 00134 typedef _Tp value_type; 00135 typedef _Ptr pointer; 00136 typedef _Ref reference; 00137 typedef size_t size_type; 00138 typedef ptrdiff_t difference_type; 00139 typedef _Deque_iterator _Self; 00140 00141 _Elt_pointer _M_cur; 00142 _Elt_pointer _M_first; 00143 _Elt_pointer _M_last; 00144 _Map_pointer _M_node; 00145 00146 _Deque_iterator(_Elt_pointer __x, _Map_pointer __y) _GLIBCXX_NOEXCEPT 00147 : _M_cur(__x), _M_first(*__y), 00148 _M_last(*__y + _S_buffer_size()), _M_node(__y) { } 00149 00150 _Deque_iterator() _GLIBCXX_NOEXCEPT 00151 : _M_cur(), _M_first(), _M_last(), _M_node() { } 00152 00153 #if __cplusplus < 201103L 00154 // Conversion from iterator to const_iterator. 00155 _Deque_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT 00156 : _M_cur(__x._M_cur), _M_first(__x._M_first), 00157 _M_last(__x._M_last), _M_node(__x._M_node) { } 00158 #else 00159 // Conversion from iterator to const_iterator. 00160 template<typename _Iter, 00161 typename = _Require<is_same<_Self, const_iterator>, 00162 is_same<_Iter, iterator>>> 00163 _Deque_iterator(const _Iter& __x) noexcept 00164 : _M_cur(__x._M_cur), _M_first(__x._M_first), 00165 _M_last(__x._M_last), _M_node(__x._M_node) { } 00166 00167 _Deque_iterator(const _Deque_iterator& __x) noexcept 00168 : _M_cur(__x._M_cur), _M_first(__x._M_first), 00169 _M_last(__x._M_last), _M_node(__x._M_node) { } 00170 00171 _Deque_iterator& operator=(const _Deque_iterator&) = default; 00172 #endif 00173 00174 iterator 00175 _M_const_cast() const _GLIBCXX_NOEXCEPT 00176 { return iterator(_M_cur, _M_node); } 00177 00178 reference 00179 operator*() const _GLIBCXX_NOEXCEPT 00180 { return *_M_cur; } 00181 00182 pointer 00183 operator->() const _GLIBCXX_NOEXCEPT 00184 { return _M_cur; } 00185 00186 _Self& 00187 operator++() _GLIBCXX_NOEXCEPT 00188 { 00189 ++_M_cur; 00190 if (_M_cur == _M_last) 00191 { 00192 _M_set_node(_M_node + 1); 00193 _M_cur = _M_first; 00194 } 00195 return *this; 00196 } 00197 00198 _Self 00199 operator++(int) _GLIBCXX_NOEXCEPT 00200 { 00201 _Self __tmp = *this; 00202 ++*this; 00203 return __tmp; 00204 } 00205 00206 _Self& 00207 operator--() _GLIBCXX_NOEXCEPT 00208 { 00209 if (_M_cur == _M_first) 00210 { 00211 _M_set_node(_M_node - 1); 00212 _M_cur = _M_last; 00213 } 00214 --_M_cur; 00215 return *this; 00216 } 00217 00218 _Self 00219 operator--(int) _GLIBCXX_NOEXCEPT 00220 { 00221 _Self __tmp = *this; 00222 --*this; 00223 return __tmp; 00224 } 00225 00226 _Self& 00227 operator+=(difference_type __n) _GLIBCXX_NOEXCEPT 00228 { 00229 const difference_type __offset = __n + (_M_cur - _M_first); 00230 if (__offset >= 0 && __offset < difference_type(_S_buffer_size())) 00231 _M_cur += __n; 00232 else 00233 { 00234 const difference_type __node_offset = 00235 __offset > 0 ? __offset / difference_type(_S_buffer_size()) 00236 : -difference_type((-__offset - 1) 00237 / _S_buffer_size()) - 1; 00238 _M_set_node(_M_node + __node_offset); 00239 _M_cur = _M_first + (__offset - __node_offset 00240 * difference_type(_S_buffer_size())); 00241 } 00242 return *this; 00243 } 00244 00245 _Self 00246 operator+(difference_type __n) const _GLIBCXX_NOEXCEPT 00247 { 00248 _Self __tmp = *this; 00249 return __tmp += __n; 00250 } 00251 00252 _Self& 00253 operator-=(difference_type __n) _GLIBCXX_NOEXCEPT 00254 { return *this += -__n; } 00255 00256 _Self 00257 operator-(difference_type __n) const _GLIBCXX_NOEXCEPT 00258 { 00259 _Self __tmp = *this; 00260 return __tmp -= __n; 00261 } 00262 00263 reference 00264 operator[](difference_type __n) const _GLIBCXX_NOEXCEPT 00265 { return *(*this + __n); } 00266 00267 /** 00268 * Prepares to traverse new_node. Sets everything except 00269 * _M_cur, which should therefore be set by the caller 00270 * immediately afterwards, based on _M_first and _M_last. 00271 */ 00272 void 00273 _M_set_node(_Map_pointer __new_node) _GLIBCXX_NOEXCEPT 00274 { 00275 _M_node = __new_node; 00276 _M_first = *__new_node; 00277 _M_last = _M_first + difference_type(_S_buffer_size()); 00278 } 00279 }; 00280 00281 // Note: we also provide overloads whose operands are of the same type in 00282 // order to avoid ambiguous overload resolution when std::rel_ops operators 00283 // are in scope (for additional details, see libstdc++/3628) 00284 template<typename _Tp, typename _Ref, typename _Ptr> 00285 inline bool 00286 operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00287 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT 00288 { return __x._M_cur == __y._M_cur; } 00289 00290 template<typename _Tp, typename _RefL, typename _PtrL, 00291 typename _RefR, typename _PtrR> 00292 inline bool 00293 operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00294 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT 00295 { return __x._M_cur == __y._M_cur; } 00296 00297 template<typename _Tp, typename _Ref, typename _Ptr> 00298 inline bool 00299 operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00300 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT 00301 { return !(__x == __y); } 00302 00303 template<typename _Tp, typename _RefL, typename _PtrL, 00304 typename _RefR, typename _PtrR> 00305 inline bool 00306 operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00307 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT 00308 { return !(__x == __y); } 00309 00310 template<typename _Tp, typename _Ref, typename _Ptr> 00311 inline bool 00312 operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00313 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT 00314 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur) 00315 : (__x._M_node < __y._M_node); } 00316 00317 template<typename _Tp, typename _RefL, typename _PtrL, 00318 typename _RefR, typename _PtrR> 00319 inline bool 00320 operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00321 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT 00322 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur) 00323 : (__x._M_node < __y._M_node); } 00324 00325 template<typename _Tp, typename _Ref, typename _Ptr> 00326 inline bool 00327 operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00328 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT 00329 { return __y < __x; } 00330 00331 template<typename _Tp, typename _RefL, typename _PtrL, 00332 typename _RefR, typename _PtrR> 00333 inline bool 00334 operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00335 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT 00336 { return __y < __x; } 00337 00338 template<typename _Tp, typename _Ref, typename _Ptr> 00339 inline bool 00340 operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00341 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT 00342 { return !(__y < __x); } 00343 00344 template<typename _Tp, typename _RefL, typename _PtrL, 00345 typename _RefR, typename _PtrR> 00346 inline bool 00347 operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00348 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT 00349 { return !(__y < __x); } 00350 00351 template<typename _Tp, typename _Ref, typename _Ptr> 00352 inline bool 00353 operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00354 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT 00355 { return !(__x < __y); } 00356 00357 template<typename _Tp, typename _RefL, typename _PtrL, 00358 typename _RefR, typename _PtrR> 00359 inline bool 00360 operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00361 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT 00362 { return !(__x < __y); } 00363 00364 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00365 // According to the resolution of DR179 not only the various comparison 00366 // operators but also operator- must accept mixed iterator/const_iterator 00367 // parameters. 00368 template<typename _Tp, typename _Ref, typename _Ptr> 00369 inline typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type 00370 operator-(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00371 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT 00372 { 00373 return typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type 00374 (_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size()) 00375 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first) 00376 + (__y._M_last - __y._M_cur); 00377 } 00378 00379 template<typename _Tp, typename _RefL, typename _PtrL, 00380 typename _RefR, typename _PtrR> 00381 inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type 00382 operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00383 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT 00384 { 00385 return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type 00386 (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size()) 00387 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first) 00388 + (__y._M_last - __y._M_cur); 00389 } 00390 00391 template<typename _Tp, typename _Ref, typename _Ptr> 00392 inline _Deque_iterator<_Tp, _Ref, _Ptr> 00393 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x) 00394 _GLIBCXX_NOEXCEPT 00395 { return __x + __n; } 00396 00397 template<typename _Tp> 00398 void 00399 fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>&, 00400 const _Deque_iterator<_Tp, _Tp&, _Tp*>&, const _Tp&); 00401 00402 template<typename _Tp> 00403 _Deque_iterator<_Tp, _Tp&, _Tp*> 00404 copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>, 00405 _Deque_iterator<_Tp, const _Tp&, const _Tp*>, 00406 _Deque_iterator<_Tp, _Tp&, _Tp*>); 00407 00408 template<typename _Tp> 00409 inline _Deque_iterator<_Tp, _Tp&, _Tp*> 00410 copy(_Deque_iterator<_Tp, _Tp&, _Tp*> __first, 00411 _Deque_iterator<_Tp, _Tp&, _Tp*> __last, 00412 _Deque_iterator<_Tp, _Tp&, _Tp*> __result) 00413 { return std::copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first), 00414 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last), 00415 __result); } 00416 00417 template<typename _Tp> 00418 _Deque_iterator<_Tp, _Tp&, _Tp*> 00419 copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>, 00420 _Deque_iterator<_Tp, const _Tp&, const _Tp*>, 00421 _Deque_iterator<_Tp, _Tp&, _Tp*>); 00422 00423 template<typename _Tp> 00424 inline _Deque_iterator<_Tp, _Tp&, _Tp*> 00425 copy_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first, 00426 _Deque_iterator<_Tp, _Tp&, _Tp*> __last, 00427 _Deque_iterator<_Tp, _Tp&, _Tp*> __result) 00428 { return std::copy_backward(_Deque_iterator<_Tp, 00429 const _Tp&, const _Tp*>(__first), 00430 _Deque_iterator<_Tp, 00431 const _Tp&, const _Tp*>(__last), 00432 __result); } 00433 00434 #if __cplusplus >= 201103L 00435 template<typename _Tp> 00436 _Deque_iterator<_Tp, _Tp&, _Tp*> 00437 move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>, 00438 _Deque_iterator<_Tp, const _Tp&, const _Tp*>, 00439 _Deque_iterator<_Tp, _Tp&, _Tp*>); 00440 00441 template<typename _Tp> 00442 inline _Deque_iterator<_Tp, _Tp&, _Tp*> 00443 move(_Deque_iterator<_Tp, _Tp&, _Tp*> __first, 00444 _Deque_iterator<_Tp, _Tp&, _Tp*> __last, 00445 _Deque_iterator<_Tp, _Tp&, _Tp*> __result) 00446 { return std::move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first), 00447 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last), 00448 __result); } 00449 00450 template<typename _Tp> 00451 _Deque_iterator<_Tp, _Tp&, _Tp*> 00452 move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>, 00453 _Deque_iterator<_Tp, const _Tp&, const _Tp*>, 00454 _Deque_iterator<_Tp, _Tp&, _Tp*>); 00455 00456 template<typename _Tp> 00457 inline _Deque_iterator<_Tp, _Tp&, _Tp*> 00458 move_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first, 00459 _Deque_iterator<_Tp, _Tp&, _Tp*> __last, 00460 _Deque_iterator<_Tp, _Tp&, _Tp*> __result) 00461 { return std::move_backward(_Deque_iterator<_Tp, 00462 const _Tp&, const _Tp*>(__first), 00463 _Deque_iterator<_Tp, 00464 const _Tp&, const _Tp*>(__last), 00465 __result); } 00466 #endif 00467 00468 /** 00469 * Deque base class. This class provides the unified face for %deque's 00470 * allocation. This class's constructor and destructor allocate and 00471 * deallocate (but do not initialize) storage. This makes %exception 00472 * safety easier. 00473 * 00474 * Nothing in this class ever constructs or destroys an actual Tp element. 00475 * (Deque handles that itself.) Only/All memory management is performed 00476 * here. 00477 */ 00478 template<typename _Tp, typename _Alloc> 00479 class _Deque_base 00480 { 00481 protected: 00482 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template 00483 rebind<_Tp>::other _Tp_alloc_type; 00484 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits; 00485 00486 #if __cplusplus < 201103L 00487 typedef _Tp* _Ptr; 00488 typedef const _Tp* _Ptr_const; 00489 #else 00490 typedef typename _Alloc_traits::pointer _Ptr; 00491 typedef typename _Alloc_traits::const_pointer _Ptr_const; 00492 #endif 00493 00494 typedef typename _Alloc_traits::template rebind<_Ptr>::other 00495 _Map_alloc_type; 00496 typedef __gnu_cxx::__alloc_traits<_Map_alloc_type> _Map_alloc_traits; 00497 00498 public: 00499 typedef _Alloc allocator_type; 00500 00501 allocator_type 00502 get_allocator() const _GLIBCXX_NOEXCEPT 00503 { return allocator_type(_M_get_Tp_allocator()); } 00504 00505 typedef _Deque_iterator<_Tp, _Tp&, _Ptr> iterator; 00506 typedef _Deque_iterator<_Tp, const _Tp&, _Ptr_const> const_iterator; 00507 00508 _Deque_base() 00509 : _M_impl() 00510 { _M_initialize_map(0); } 00511 00512 _Deque_base(size_t __num_elements) 00513 : _M_impl() 00514 { _M_initialize_map(__num_elements); } 00515 00516 _Deque_base(const allocator_type& __a, size_t __num_elements) 00517 : _M_impl(__a) 00518 { _M_initialize_map(__num_elements); } 00519 00520 _Deque_base(const allocator_type& __a) 00521 : _M_impl(__a) 00522 { /* Caller must initialize map. */ } 00523 00524 #if __cplusplus >= 201103L 00525 _Deque_base(_Deque_base&& __x, false_type) 00526 : _M_impl(__x._M_move_impl()) 00527 { } 00528 00529 _Deque_base(_Deque_base&& __x, true_type) 00530 : _M_impl(std::move(__x._M_get_Tp_allocator())) 00531 { 00532 _M_initialize_map(0); 00533 if (__x._M_impl._M_map) 00534 this->_M_impl._M_swap_data(__x._M_impl); 00535 } 00536 00537 _Deque_base(_Deque_base&& __x) 00538 : _Deque_base(std::move(__x), typename _Alloc_traits::is_always_equal{}) 00539 { } 00540 00541 _Deque_base(_Deque_base&& __x, const allocator_type& __a, size_t __n) 00542 : _M_impl(__a) 00543 { 00544 if (__x.get_allocator() == __a) 00545 { 00546 if (__x._M_impl._M_map) 00547 { 00548 _M_initialize_map(0); 00549 this->_M_impl._M_swap_data(__x._M_impl); 00550 } 00551 } 00552 else 00553 { 00554 _M_initialize_map(__n); 00555 } 00556 } 00557 #endif 00558 00559 ~_Deque_base() _GLIBCXX_NOEXCEPT; 00560 00561 protected: 00562 typedef typename iterator::_Map_pointer _Map_pointer; 00563 00564 //This struct encapsulates the implementation of the std::deque 00565 //standard container and at the same time makes use of the EBO 00566 //for empty allocators. 00567 struct _Deque_impl 00568 : public _Tp_alloc_type 00569 { 00570 _Map_pointer _M_map; 00571 size_t _M_map_size; 00572 iterator _M_start; 00573 iterator _M_finish; 00574 00575 _Deque_impl() 00576 : _Tp_alloc_type(), _M_map(), _M_map_size(0), 00577 _M_start(), _M_finish() 00578 { } 00579 00580 _Deque_impl(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT 00581 : _Tp_alloc_type(__a), _M_map(), _M_map_size(0), 00582 _M_start(), _M_finish() 00583 { } 00584 00585 #if __cplusplus >= 201103L 00586 _Deque_impl(_Deque_impl&&) = default; 00587 00588 _Deque_impl(_Tp_alloc_type&& __a) noexcept 00589 : _Tp_alloc_type(std::move(__a)), _M_map(), _M_map_size(0), 00590 _M_start(), _M_finish() 00591 { } 00592 #endif 00593 00594 void _M_swap_data(_Deque_impl& __x) _GLIBCXX_NOEXCEPT 00595 { 00596 using std::swap; 00597 swap(this->_M_start, __x._M_start); 00598 swap(this->_M_finish, __x._M_finish); 00599 swap(this->_M_map, __x._M_map); 00600 swap(this->_M_map_size, __x._M_map_size); 00601 } 00602 }; 00603 00604 _Tp_alloc_type& 00605 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT 00606 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); } 00607 00608 const _Tp_alloc_type& 00609 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT 00610 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); } 00611 00612 _Map_alloc_type 00613 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT 00614 { return _Map_alloc_type(_M_get_Tp_allocator()); } 00615 00616 _Ptr 00617 _M_allocate_node() 00618 { 00619 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits; 00620 return _Traits::allocate(_M_impl, __deque_buf_size(sizeof(_Tp))); 00621 } 00622 00623 void 00624 _M_deallocate_node(_Ptr __p) _GLIBCXX_NOEXCEPT 00625 { 00626 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits; 00627 _Traits::deallocate(_M_impl, __p, __deque_buf_size(sizeof(_Tp))); 00628 } 00629 00630 _Map_pointer 00631 _M_allocate_map(size_t __n) 00632 { 00633 _Map_alloc_type __map_alloc = _M_get_map_allocator(); 00634 return _Map_alloc_traits::allocate(__map_alloc, __n); 00635 } 00636 00637 void 00638 _M_deallocate_map(_Map_pointer __p, size_t __n) _GLIBCXX_NOEXCEPT 00639 { 00640 _Map_alloc_type __map_alloc = _M_get_map_allocator(); 00641 _Map_alloc_traits::deallocate(__map_alloc, __p, __n); 00642 } 00643 00644 protected: 00645 void _M_initialize_map(size_t); 00646 void _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish); 00647 void _M_destroy_nodes(_Map_pointer __nstart, 00648 _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT; 00649 enum { _S_initial_map_size = 8 }; 00650 00651 _Deque_impl _M_impl; 00652 00653 #if __cplusplus >= 201103L 00654 private: 00655 _Deque_impl 00656 _M_move_impl() 00657 { 00658 if (!_M_impl._M_map) 00659 return std::move(_M_impl); 00660 00661 // Create a copy of the current allocator. 00662 _Tp_alloc_type __alloc{_M_get_Tp_allocator()}; 00663 // Put that copy in a moved-from state. 00664 _Tp_alloc_type __sink __attribute((__unused__)) {std::move(__alloc)}; 00665 // Create an empty map that allocates using the moved-from allocator. 00666 _Deque_base __empty{__alloc}; 00667 __empty._M_initialize_map(0); 00668 // Now safe to modify current allocator and perform non-throwing swaps. 00669 _Deque_impl __ret{std::move(_M_get_Tp_allocator())}; 00670 _M_impl._M_swap_data(__ret); 00671 _M_impl._M_swap_data(__empty._M_impl); 00672 return __ret; 00673 } 00674 #endif 00675 }; 00676 00677 template<typename _Tp, typename _Alloc> 00678 _Deque_base<_Tp, _Alloc>:: 00679 ~_Deque_base() _GLIBCXX_NOEXCEPT 00680 { 00681 if (this->_M_impl._M_map) 00682 { 00683 _M_destroy_nodes(this->_M_impl._M_start._M_node, 00684 this->_M_impl._M_finish._M_node + 1); 00685 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size); 00686 } 00687 } 00688 00689 /** 00690 * @brief Layout storage. 00691 * @param __num_elements The count of T's for which to allocate space 00692 * at first. 00693 * @return Nothing. 00694 * 00695 * The initial underlying memory layout is a bit complicated... 00696 */ 00697 template<typename _Tp, typename _Alloc> 00698 void 00699 _Deque_base<_Tp, _Alloc>:: 00700 _M_initialize_map(size_t __num_elements) 00701 { 00702 const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp)) 00703 + 1); 00704 00705 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size, 00706 size_t(__num_nodes + 2)); 00707 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size); 00708 00709 // For "small" maps (needing less than _M_map_size nodes), allocation 00710 // starts in the middle elements and grows outwards. So nstart may be 00711 // the beginning of _M_map, but for small maps it may be as far in as 00712 // _M_map+3. 00713 00714 _Map_pointer __nstart = (this->_M_impl._M_map 00715 + (this->_M_impl._M_map_size - __num_nodes) / 2); 00716 _Map_pointer __nfinish = __nstart + __num_nodes; 00717 00718 __try 00719 { _M_create_nodes(__nstart, __nfinish); } 00720 __catch(...) 00721 { 00722 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size); 00723 this->_M_impl._M_map = _Map_pointer(); 00724 this->_M_impl._M_map_size = 0; 00725 __throw_exception_again; 00726 } 00727 00728 this->_M_impl._M_start._M_set_node(__nstart); 00729 this->_M_impl._M_finish._M_set_node(__nfinish - 1); 00730 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first; 00731 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first 00732 + __num_elements 00733 % __deque_buf_size(sizeof(_Tp))); 00734 } 00735 00736 template<typename _Tp, typename _Alloc> 00737 void 00738 _Deque_base<_Tp, _Alloc>:: 00739 _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish) 00740 { 00741 _Map_pointer __cur; 00742 __try 00743 { 00744 for (__cur = __nstart; __cur < __nfinish; ++__cur) 00745 *__cur = this->_M_allocate_node(); 00746 } 00747 __catch(...) 00748 { 00749 _M_destroy_nodes(__nstart, __cur); 00750 __throw_exception_again; 00751 } 00752 } 00753 00754 template<typename _Tp, typename _Alloc> 00755 void 00756 _Deque_base<_Tp, _Alloc>:: 00757 _M_destroy_nodes(_Map_pointer __nstart, 00758 _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT 00759 { 00760 for (_Map_pointer __n = __nstart; __n < __nfinish; ++__n) 00761 _M_deallocate_node(*__n); 00762 } 00763 00764 /** 00765 * @brief A standard container using fixed-size memory allocation and 00766 * constant-time manipulation of elements at either end. 00767 * 00768 * @ingroup sequences 00769 * 00770 * @tparam _Tp Type of element. 00771 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>. 00772 * 00773 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00774 * <a href="tables.html#66">reversible container</a>, and a 00775 * <a href="tables.html#67">sequence</a>, including the 00776 * <a href="tables.html#68">optional sequence requirements</a>. 00777 * 00778 * In previous HP/SGI versions of deque, there was an extra template 00779 * parameter so users could control the node size. This extension turned 00780 * out to violate the C++ standard (it can be detected using template 00781 * template parameters), and it was removed. 00782 * 00783 * Here's how a deque<Tp> manages memory. Each deque has 4 members: 00784 * 00785 * - Tp** _M_map 00786 * - size_t _M_map_size 00787 * - iterator _M_start, _M_finish 00788 * 00789 * map_size is at least 8. %map is an array of map_size 00790 * pointers-to-@a nodes. (The name %map has nothing to do with the 00791 * std::map class, and @b nodes should not be confused with 00792 * std::list's usage of @a node.) 00793 * 00794 * A @a node has no specific type name as such, but it is referred 00795 * to as @a node in this file. It is a simple array-of-Tp. If Tp 00796 * is very large, there will be one Tp element per node (i.e., an 00797 * @a array of one). For non-huge Tp's, node size is inversely 00798 * related to Tp size: the larger the Tp, the fewer Tp's will fit 00799 * in a node. The goal here is to keep the total size of a node 00800 * relatively small and constant over different Tp's, to improve 00801 * allocator efficiency. 00802 * 00803 * Not every pointer in the %map array will point to a node. If 00804 * the initial number of elements in the deque is small, the 00805 * /middle/ %map pointers will be valid, and the ones at the edges 00806 * will be unused. This same situation will arise as the %map 00807 * grows: available %map pointers, if any, will be on the ends. As 00808 * new nodes are created, only a subset of the %map's pointers need 00809 * to be copied @a outward. 00810 * 00811 * Class invariants: 00812 * - For any nonsingular iterator i: 00813 * - i.node points to a member of the %map array. (Yes, you read that 00814 * correctly: i.node does not actually point to a node.) The member of 00815 * the %map array is what actually points to the node. 00816 * - i.first == *(i.node) (This points to the node (first Tp element).) 00817 * - i.last == i.first + node_size 00818 * - i.cur is a pointer in the range [i.first, i.last). NOTE: 00819 * the implication of this is that i.cur is always a dereferenceable 00820 * pointer, even if i is a past-the-end iterator. 00821 * - Start and Finish are always nonsingular iterators. NOTE: this 00822 * means that an empty deque must have one node, a deque with <N 00823 * elements (where N is the node buffer size) must have one node, a 00824 * deque with N through (2N-1) elements must have two nodes, etc. 00825 * - For every node other than start.node and finish.node, every 00826 * element in the node is an initialized object. If start.node == 00827 * finish.node, then [start.cur, finish.cur) are initialized 00828 * objects, and the elements outside that range are uninitialized 00829 * storage. Otherwise, [start.cur, start.last) and [finish.first, 00830 * finish.cur) are initialized objects, and [start.first, start.cur) 00831 * and [finish.cur, finish.last) are uninitialized storage. 00832 * - [%map, %map + map_size) is a valid, non-empty range. 00833 * - [start.node, finish.node] is a valid range contained within 00834 * [%map, %map + map_size). 00835 * - A pointer in the range [%map, %map + map_size) points to an allocated 00836 * node if and only if the pointer is in the range 00837 * [start.node, finish.node]. 00838 * 00839 * Here's the magic: nothing in deque is @b aware of the discontiguous 00840 * storage! 00841 * 00842 * The memory setup and layout occurs in the parent, _Base, and the iterator 00843 * class is entirely responsible for @a leaping from one node to the next. 00844 * All the implementation routines for deque itself work only through the 00845 * start and finish iterators. This keeps the routines simple and sane, 00846 * and we can use other standard algorithms as well. 00847 */ 00848 template<typename _Tp, typename _Alloc = std::allocator<_Tp> > 00849 class deque : protected _Deque_base<_Tp, _Alloc> 00850 { 00851 #ifdef _GLIBCXX_CONCEPT_CHECKS 00852 // concept requirements 00853 typedef typename _Alloc::value_type _Alloc_value_type; 00854 # if __cplusplus < 201103L 00855 __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 00856 # endif 00857 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept) 00858 #endif 00859 00860 #if __cplusplus >= 201103L 00861 static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value, 00862 "std::deque must have a non-const, non-volatile value_type"); 00863 # ifdef __STRICT_ANSI__ 00864 static_assert(is_same<typename _Alloc::value_type, _Tp>::value, 00865 "std::deque must have the same value_type as its allocator"); 00866 # endif 00867 #endif 00868 00869 typedef _Deque_base<_Tp, _Alloc> _Base; 00870 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type; 00871 typedef typename _Base::_Alloc_traits _Alloc_traits; 00872 typedef typename _Base::_Map_pointer _Map_pointer; 00873 00874 public: 00875 typedef _Tp value_type; 00876 typedef typename _Alloc_traits::pointer pointer; 00877 typedef typename _Alloc_traits::const_pointer const_pointer; 00878 typedef typename _Alloc_traits::reference reference; 00879 typedef typename _Alloc_traits::const_reference const_reference; 00880 typedef typename _Base::iterator iterator; 00881 typedef typename _Base::const_iterator const_iterator; 00882 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00883 typedef std::reverse_iterator<iterator> reverse_iterator; 00884 typedef size_t size_type; 00885 typedef ptrdiff_t difference_type; 00886 typedef _Alloc allocator_type; 00887 00888 protected: 00889 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT 00890 { return __deque_buf_size(sizeof(_Tp)); } 00891 00892 // Functions controlling memory layout, and nothing else. 00893 using _Base::_M_initialize_map; 00894 using _Base::_M_create_nodes; 00895 using _Base::_M_destroy_nodes; 00896 using _Base::_M_allocate_node; 00897 using _Base::_M_deallocate_node; 00898 using _Base::_M_allocate_map; 00899 using _Base::_M_deallocate_map; 00900 using _Base::_M_get_Tp_allocator; 00901 00902 /** 00903 * A total of four data members accumulated down the hierarchy. 00904 * May be accessed via _M_impl.* 00905 */ 00906 using _Base::_M_impl; 00907 00908 public: 00909 // [23.2.1.1] construct/copy/destroy 00910 // (assign() and get_allocator() are also listed in this section) 00911 00912 /** 00913 * @brief Creates a %deque with no elements. 00914 */ 00915 deque() : _Base() { } 00916 00917 /** 00918 * @brief Creates a %deque with no elements. 00919 * @param __a An allocator object. 00920 */ 00921 explicit 00922 deque(const allocator_type& __a) 00923 : _Base(__a, 0) { } 00924 00925 #if __cplusplus >= 201103L 00926 /** 00927 * @brief Creates a %deque with default constructed elements. 00928 * @param __n The number of elements to initially create. 00929 * @param __a An allocator. 00930 * 00931 * This constructor fills the %deque with @a n default 00932 * constructed elements. 00933 */ 00934 explicit 00935 deque(size_type __n, const allocator_type& __a = allocator_type()) 00936 : _Base(__a, _S_check_init_len(__n, __a)) 00937 { _M_default_initialize(); } 00938 00939 /** 00940 * @brief Creates a %deque with copies of an exemplar element. 00941 * @param __n The number of elements to initially create. 00942 * @param __value An element to copy. 00943 * @param __a An allocator. 00944 * 00945 * This constructor fills the %deque with @a __n copies of @a __value. 00946 */ 00947 deque(size_type __n, const value_type& __value, 00948 const allocator_type& __a = allocator_type()) 00949 : _Base(__a, _S_check_init_len(__n, __a)) 00950 { _M_fill_initialize(__value); } 00951 #else 00952 /** 00953 * @brief Creates a %deque with copies of an exemplar element. 00954 * @param __n The number of elements to initially create. 00955 * @param __value An element to copy. 00956 * @param __a An allocator. 00957 * 00958 * This constructor fills the %deque with @a __n copies of @a __value. 00959 */ 00960 explicit 00961 deque(size_type __n, const value_type& __value = value_type(), 00962 const allocator_type& __a = allocator_type()) 00963 : _Base(__a, _S_check_init_len(__n, __a)) 00964 { _M_fill_initialize(__value); } 00965 #endif 00966 00967 /** 00968 * @brief %Deque copy constructor. 00969 * @param __x A %deque of identical element and allocator types. 00970 * 00971 * The newly-created %deque uses a copy of the allocator object used 00972 * by @a __x (unless the allocator traits dictate a different object). 00973 */ 00974 deque(const deque& __x) 00975 : _Base(_Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()), 00976 __x.size()) 00977 { std::__uninitialized_copy_a(__x.begin(), __x.end(), 00978 this->_M_impl._M_start, 00979 _M_get_Tp_allocator()); } 00980 00981 #if __cplusplus >= 201103L 00982 /** 00983 * @brief %Deque move constructor. 00984 * @param __x A %deque of identical element and allocator types. 00985 * 00986 * The newly-created %deque contains the exact contents of @a __x. 00987 * The contents of @a __x are a valid, but unspecified %deque. 00988 */ 00989 deque(deque&& __x) 00990 : _Base(std::move(__x)) { } 00991 00992 /// Copy constructor with alternative allocator 00993 deque(const deque& __x, const allocator_type& __a) 00994 : _Base(__a, __x.size()) 00995 { std::__uninitialized_copy_a(__x.begin(), __x.end(), 00996 this->_M_impl._M_start, 00997 _M_get_Tp_allocator()); } 00998 00999 /// Move constructor with alternative allocator 01000 deque(deque&& __x, const allocator_type& __a) 01001 : _Base(std::move(__x), __a, __x.size()) 01002 { 01003 if (__x.get_allocator() != __a) 01004 { 01005 std::__uninitialized_move_a(__x.begin(), __x.end(), 01006 this->_M_impl._M_start, 01007 _M_get_Tp_allocator()); 01008 __x.clear(); 01009 } 01010 } 01011 01012 /** 01013 * @brief Builds a %deque from an initializer list. 01014 * @param __l An initializer_list. 01015 * @param __a An allocator object. 01016 * 01017 * Create a %deque consisting of copies of the elements in the 01018 * initializer_list @a __l. 01019 * 01020 * This will call the element type's copy constructor N times 01021 * (where N is __l.size()) and do no memory reallocation. 01022 */ 01023 deque(initializer_list<value_type> __l, 01024 const allocator_type& __a = allocator_type()) 01025 : _Base(__a) 01026 { 01027 _M_range_initialize(__l.begin(), __l.end(), 01028 random_access_iterator_tag()); 01029 } 01030 #endif 01031 01032 /** 01033 * @brief Builds a %deque from a range. 01034 * @param __first An input iterator. 01035 * @param __last An input iterator. 01036 * @param __a An allocator object. 01037 * 01038 * Create a %deque consisting of copies of the elements from [__first, 01039 * __last). 01040 * 01041 * If the iterators are forward, bidirectional, or random-access, then 01042 * this will call the elements' copy constructor N times (where N is 01043 * distance(__first,__last)) and do no memory reallocation. But if only 01044 * input iterators are used, then this will do at most 2N calls to the 01045 * copy constructor, and logN memory reallocations. 01046 */ 01047 #if __cplusplus >= 201103L 01048 template<typename _InputIterator, 01049 typename = std::_RequireInputIter<_InputIterator>> 01050 deque(_InputIterator __first, _InputIterator __last, 01051 const allocator_type& __a = allocator_type()) 01052 : _Base(__a) 01053 { _M_initialize_dispatch(__first, __last, __false_type()); } 01054 #else 01055 template<typename _InputIterator> 01056 deque(_InputIterator __first, _InputIterator __last, 01057 const allocator_type& __a = allocator_type()) 01058 : _Base(__a) 01059 { 01060 // Check whether it's an integral type. If so, it's not an iterator. 01061 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 01062 _M_initialize_dispatch(__first, __last, _Integral()); 01063 } 01064 #endif 01065 01066 /** 01067 * The dtor only erases the elements, and note that if the elements 01068 * themselves are pointers, the pointed-to memory is not touched in any 01069 * way. Managing the pointer is the user's responsibility. 01070 */ 01071 ~deque() 01072 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); } 01073 01074 /** 01075 * @brief %Deque assignment operator. 01076 * @param __x A %deque of identical element and allocator types. 01077 * 01078 * All the elements of @a x are copied. 01079 * 01080 * The newly-created %deque uses a copy of the allocator object used 01081 * by @a __x (unless the allocator traits dictate a different object). 01082 */ 01083 deque& 01084 operator=(const deque& __x); 01085 01086 #if __cplusplus >= 201103L 01087 /** 01088 * @brief %Deque move assignment operator. 01089 * @param __x A %deque of identical element and allocator types. 01090 * 01091 * The contents of @a __x are moved into this deque (without copying, 01092 * if the allocators permit it). 01093 * @a __x is a valid, but unspecified %deque. 01094 */ 01095 deque& 01096 operator=(deque&& __x) noexcept(_Alloc_traits::_S_always_equal()) 01097 { 01098 using __always_equal = typename _Alloc_traits::is_always_equal; 01099 _M_move_assign1(std::move(__x), __always_equal{}); 01100 return *this; 01101 } 01102 01103 /** 01104 * @brief Assigns an initializer list to a %deque. 01105 * @param __l An initializer_list. 01106 * 01107 * This function fills a %deque with copies of the elements in the 01108 * initializer_list @a __l. 01109 * 01110 * Note that the assignment completely changes the %deque and that the 01111 * resulting %deque's size is the same as the number of elements 01112 * assigned. 01113 */ 01114 deque& 01115 operator=(initializer_list<value_type> __l) 01116 { 01117 _M_assign_aux(__l.begin(), __l.end(), 01118 random_access_iterator_tag()); 01119 return *this; 01120 } 01121 #endif 01122 01123 /** 01124 * @brief Assigns a given value to a %deque. 01125 * @param __n Number of elements to be assigned. 01126 * @param __val Value to be assigned. 01127 * 01128 * This function fills a %deque with @a n copies of the given 01129 * value. Note that the assignment completely changes the 01130 * %deque and that the resulting %deque's size is the same as 01131 * the number of elements assigned. 01132 */ 01133 void 01134 assign(size_type __n, const value_type& __val) 01135 { _M_fill_assign(__n, __val); } 01136 01137 /** 01138 * @brief Assigns a range to a %deque. 01139 * @param __first An input iterator. 01140 * @param __last An input iterator. 01141 * 01142 * This function fills a %deque with copies of the elements in the 01143 * range [__first,__last). 01144 * 01145 * Note that the assignment completely changes the %deque and that the 01146 * resulting %deque's size is the same as the number of elements 01147 * assigned. 01148 */ 01149 #if __cplusplus >= 201103L 01150 template<typename _InputIterator, 01151 typename = std::_RequireInputIter<_InputIterator>> 01152 void 01153 assign(_InputIterator __first, _InputIterator __last) 01154 { _M_assign_dispatch(__first, __last, __false_type()); } 01155 #else 01156 template<typename _InputIterator> 01157 void 01158 assign(_InputIterator __first, _InputIterator __last) 01159 { 01160 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 01161 _M_assign_dispatch(__first, __last, _Integral()); 01162 } 01163 #endif 01164 01165 #if __cplusplus >= 201103L 01166 /** 01167 * @brief Assigns an initializer list to a %deque. 01168 * @param __l An initializer_list. 01169 * 01170 * This function fills a %deque with copies of the elements in the 01171 * initializer_list @a __l. 01172 * 01173 * Note that the assignment completely changes the %deque and that the 01174 * resulting %deque's size is the same as the number of elements 01175 * assigned. 01176 */ 01177 void 01178 assign(initializer_list<value_type> __l) 01179 { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); } 01180 #endif 01181 01182 /// Get a copy of the memory allocation object. 01183 allocator_type 01184 get_allocator() const _GLIBCXX_NOEXCEPT 01185 { return _Base::get_allocator(); } 01186 01187 // iterators 01188 /** 01189 * Returns a read/write iterator that points to the first element in the 01190 * %deque. Iteration is done in ordinary element order. 01191 */ 01192 iterator 01193 begin() _GLIBCXX_NOEXCEPT 01194 { return this->_M_impl._M_start; } 01195 01196 /** 01197 * Returns a read-only (constant) iterator that points to the first 01198 * element in the %deque. Iteration is done in ordinary element order. 01199 */ 01200 const_iterator 01201 begin() const _GLIBCXX_NOEXCEPT 01202 { return this->_M_impl._M_start; } 01203 01204 /** 01205 * Returns a read/write iterator that points one past the last 01206 * element in the %deque. Iteration is done in ordinary 01207 * element order. 01208 */ 01209 iterator 01210 end() _GLIBCXX_NOEXCEPT 01211 { return this->_M_impl._M_finish; } 01212 01213 /** 01214 * Returns a read-only (constant) iterator that points one past 01215 * the last element in the %deque. Iteration is done in 01216 * ordinary element order. 01217 */ 01218 const_iterator 01219 end() const _GLIBCXX_NOEXCEPT 01220 { return this->_M_impl._M_finish; } 01221 01222 /** 01223 * Returns a read/write reverse iterator that points to the 01224 * last element in the %deque. Iteration is done in reverse 01225 * element order. 01226 */ 01227 reverse_iterator 01228 rbegin() _GLIBCXX_NOEXCEPT 01229 { return reverse_iterator(this->_M_impl._M_finish); } 01230 01231 /** 01232 * Returns a read-only (constant) reverse iterator that points 01233 * to the last element in the %deque. Iteration is done in 01234 * reverse element order. 01235 */ 01236 const_reverse_iterator 01237 rbegin() const _GLIBCXX_NOEXCEPT 01238 { return const_reverse_iterator(this->_M_impl._M_finish); } 01239 01240 /** 01241 * Returns a read/write reverse iterator that points to one 01242 * before the first element in the %deque. Iteration is done 01243 * in reverse element order. 01244 */ 01245 reverse_iterator 01246 rend() _GLIBCXX_NOEXCEPT 01247 { return reverse_iterator(this->_M_impl._M_start); } 01248 01249 /** 01250 * Returns a read-only (constant) reverse iterator that points 01251 * to one before the first element in the %deque. Iteration is 01252 * done in reverse element order. 01253 */ 01254 const_reverse_iterator 01255 rend() const _GLIBCXX_NOEXCEPT 01256 { return const_reverse_iterator(this->_M_impl._M_start); } 01257 01258 #if __cplusplus >= 201103L 01259 /** 01260 * Returns a read-only (constant) iterator that points to the first 01261 * element in the %deque. Iteration is done in ordinary element order. 01262 */ 01263 const_iterator 01264 cbegin() const noexcept 01265 { return this->_M_impl._M_start; } 01266 01267 /** 01268 * Returns a read-only (constant) iterator that points one past 01269 * the last element in the %deque. Iteration is done in 01270 * ordinary element order. 01271 */ 01272 const_iterator 01273 cend() const noexcept 01274 { return this->_M_impl._M_finish; } 01275 01276 /** 01277 * Returns a read-only (constant) reverse iterator that points 01278 * to the last element in the %deque. Iteration is done in 01279 * reverse element order. 01280 */ 01281 const_reverse_iterator 01282 crbegin() const noexcept 01283 { return const_reverse_iterator(this->_M_impl._M_finish); } 01284 01285 /** 01286 * Returns a read-only (constant) reverse iterator that points 01287 * to one before the first element in the %deque. Iteration is 01288 * done in reverse element order. 01289 */ 01290 const_reverse_iterator 01291 crend() const noexcept 01292 { return const_reverse_iterator(this->_M_impl._M_start); } 01293 #endif 01294 01295 // [23.2.1.2] capacity 01296 /** Returns the number of elements in the %deque. */ 01297 size_type 01298 size() const _GLIBCXX_NOEXCEPT 01299 { return this->_M_impl._M_finish - this->_M_impl._M_start; } 01300 01301 /** Returns the size() of the largest possible %deque. */ 01302 size_type 01303 max_size() const _GLIBCXX_NOEXCEPT 01304 { return _S_max_size(_M_get_Tp_allocator()); } 01305 01306 #if __cplusplus >= 201103L 01307 /** 01308 * @brief Resizes the %deque to the specified number of elements. 01309 * @param __new_size Number of elements the %deque should contain. 01310 * 01311 * This function will %resize the %deque to the specified 01312 * number of elements. If the number is smaller than the 01313 * %deque's current size the %deque is truncated, otherwise 01314 * default constructed elements are appended. 01315 */ 01316 void 01317 resize(size_type __new_size) 01318 { 01319 const size_type __len = size(); 01320 if (__new_size > __len) 01321 _M_default_append(__new_size - __len); 01322 else if (__new_size < __len) 01323 _M_erase_at_end(this->_M_impl._M_start 01324 + difference_type(__new_size)); 01325 } 01326 01327 /** 01328 * @brief Resizes the %deque to the specified number of elements. 01329 * @param __new_size Number of elements the %deque should contain. 01330 * @param __x Data with which new elements should be populated. 01331 * 01332 * This function will %resize the %deque to the specified 01333 * number of elements. If the number is smaller than the 01334 * %deque's current size the %deque is truncated, otherwise the 01335 * %deque is extended and new elements are populated with given 01336 * data. 01337 */ 01338 void 01339 resize(size_type __new_size, const value_type& __x) 01340 { 01341 const size_type __len = size(); 01342 if (__new_size > __len) 01343 _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x); 01344 else if (__new_size < __len) 01345 _M_erase_at_end(this->_M_impl._M_start 01346 + difference_type(__new_size)); 01347 } 01348 #else 01349 /** 01350 * @brief Resizes the %deque to the specified number of elements. 01351 * @param __new_size Number of elements the %deque should contain. 01352 * @param __x Data with which new elements should be populated. 01353 * 01354 * This function will %resize the %deque to the specified 01355 * number of elements. If the number is smaller than the 01356 * %deque's current size the %deque is truncated, otherwise the 01357 * %deque is extended and new elements are populated with given 01358 * data. 01359 */ 01360 void 01361 resize(size_type __new_size, value_type __x = value_type()) 01362 { 01363 const size_type __len = size(); 01364 if (__new_size > __len) 01365 _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x); 01366 else if (__new_size < __len) 01367 _M_erase_at_end(this->_M_impl._M_start 01368 + difference_type(__new_size)); 01369 } 01370 #endif 01371 01372 #if __cplusplus >= 201103L 01373 /** A non-binding request to reduce memory use. */ 01374 void 01375 shrink_to_fit() noexcept 01376 { _M_shrink_to_fit(); } 01377 #endif 01378 01379 /** 01380 * Returns true if the %deque is empty. (Thus begin() would 01381 * equal end().) 01382 */ 01383 _GLIBCXX_NODISCARD bool 01384 empty() const _GLIBCXX_NOEXCEPT 01385 { return this->_M_impl._M_finish == this->_M_impl._M_start; } 01386 01387 // element access 01388 /** 01389 * @brief Subscript access to the data contained in the %deque. 01390 * @param __n The index of the element for which data should be 01391 * accessed. 01392 * @return Read/write reference to data. 01393 * 01394 * This operator allows for easy, array-style, data access. 01395 * Note that data access with this operator is unchecked and 01396 * out_of_range lookups are not defined. (For checked lookups 01397 * see at().) 01398 */ 01399 reference 01400 operator[](size_type __n) _GLIBCXX_NOEXCEPT 01401 { 01402 __glibcxx_requires_subscript(__n); 01403 return this->_M_impl._M_start[difference_type(__n)]; 01404 } 01405 01406 /** 01407 * @brief Subscript access to the data contained in the %deque. 01408 * @param __n The index of the element for which data should be 01409 * accessed. 01410 * @return Read-only (constant) reference to data. 01411 * 01412 * This operator allows for easy, array-style, data access. 01413 * Note that data access with this operator is unchecked and 01414 * out_of_range lookups are not defined. (For checked lookups 01415 * see at().) 01416 */ 01417 const_reference 01418 operator[](size_type __n) const _GLIBCXX_NOEXCEPT 01419 { 01420 __glibcxx_requires_subscript(__n); 01421 return this->_M_impl._M_start[difference_type(__n)]; 01422 } 01423 01424 protected: 01425 /// Safety check used only from at(). 01426 void 01427 _M_range_check(size_type __n) const 01428 { 01429 if (__n >= this->size()) 01430 __throw_out_of_range_fmt(__N("deque::_M_range_check: __n " 01431 "(which is %zu)>= this->size() " 01432 "(which is %zu)"), 01433 __n, this->size()); 01434 } 01435 01436 public: 01437 /** 01438 * @brief Provides access to the data contained in the %deque. 01439 * @param __n The index of the element for which data should be 01440 * accessed. 01441 * @return Read/write reference to data. 01442 * @throw std::out_of_range If @a __n is an invalid index. 01443 * 01444 * This function provides for safer data access. The parameter 01445 * is first checked that it is in the range of the deque. The 01446 * function throws out_of_range if the check fails. 01447 */ 01448 reference 01449 at(size_type __n) 01450 { 01451 _M_range_check(__n); 01452 return (*this)[__n]; 01453 } 01454 01455 /** 01456 * @brief Provides access to the data contained in the %deque. 01457 * @param __n The index of the element for which data should be 01458 * accessed. 01459 * @return Read-only (constant) reference to data. 01460 * @throw std::out_of_range If @a __n is an invalid index. 01461 * 01462 * This function provides for safer data access. The parameter is first 01463 * checked that it is in the range of the deque. The function throws 01464 * out_of_range if the check fails. 01465 */ 01466 const_reference 01467 at(size_type __n) const 01468 { 01469 _M_range_check(__n); 01470 return (*this)[__n]; 01471 } 01472 01473 /** 01474 * Returns a read/write reference to the data at the first 01475 * element of the %deque. 01476 */ 01477 reference 01478 front() _GLIBCXX_NOEXCEPT 01479 { 01480 __glibcxx_requires_nonempty(); 01481 return *begin(); 01482 } 01483 01484 /** 01485 * Returns a read-only (constant) reference to the data at the first 01486 * element of the %deque. 01487 */ 01488 const_reference 01489 front() const _GLIBCXX_NOEXCEPT 01490 { 01491 __glibcxx_requires_nonempty(); 01492 return *begin(); 01493 } 01494 01495 /** 01496 * Returns a read/write reference to the data at the last element of the 01497 * %deque. 01498 */ 01499 reference 01500 back() _GLIBCXX_NOEXCEPT 01501 { 01502 __glibcxx_requires_nonempty(); 01503 iterator __tmp = end(); 01504 --__tmp; 01505 return *__tmp; 01506 } 01507 01508 /** 01509 * Returns a read-only (constant) reference to the data at the last 01510 * element of the %deque. 01511 */ 01512 const_reference 01513 back() const _GLIBCXX_NOEXCEPT 01514 { 01515 __glibcxx_requires_nonempty(); 01516 const_iterator __tmp = end(); 01517 --__tmp; 01518 return *__tmp; 01519 } 01520 01521 // [23.2.1.2] modifiers 01522 /** 01523 * @brief Add data to the front of the %deque. 01524 * @param __x Data to be added. 01525 * 01526 * This is a typical stack operation. The function creates an 01527 * element at the front of the %deque and assigns the given 01528 * data to it. Due to the nature of a %deque this operation 01529 * can be done in constant time. 01530 */ 01531 void 01532 push_front(const value_type& __x) 01533 { 01534 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first) 01535 { 01536 _Alloc_traits::construct(this->_M_impl, 01537 this->_M_impl._M_start._M_cur - 1, 01538 __x); 01539 --this->_M_impl._M_start._M_cur; 01540 } 01541 else 01542 _M_push_front_aux(__x); 01543 } 01544 01545 #if __cplusplus >= 201103L 01546 void 01547 push_front(value_type&& __x) 01548 { emplace_front(std::move(__x)); } 01549 01550 template<typename... _Args> 01551 #if __cplusplus > 201402L 01552 reference 01553 #else 01554 void 01555 #endif 01556 emplace_front(_Args&&... __args); 01557 #endif 01558 01559 /** 01560 * @brief Add data to the end of the %deque. 01561 * @param __x Data to be added. 01562 * 01563 * This is a typical stack operation. The function creates an 01564 * element at the end of the %deque and assigns the given data 01565 * to it. Due to the nature of a %deque this operation can be 01566 * done in constant time. 01567 */ 01568 void 01569 push_back(const value_type& __x) 01570 { 01571 if (this->_M_impl._M_finish._M_cur 01572 != this->_M_impl._M_finish._M_last - 1) 01573 { 01574 _Alloc_traits::construct(this->_M_impl, 01575 this->_M_impl._M_finish._M_cur, __x); 01576 ++this->_M_impl._M_finish._M_cur; 01577 } 01578 else 01579 _M_push_back_aux(__x); 01580 } 01581 01582 #if __cplusplus >= 201103L 01583 void 01584 push_back(value_type&& __x) 01585 { emplace_back(std::move(__x)); } 01586 01587 template<typename... _Args> 01588 #if __cplusplus > 201402L 01589 reference 01590 #else 01591 void 01592 #endif 01593 emplace_back(_Args&&... __args); 01594 #endif 01595 01596 /** 01597 * @brief Removes first element. 01598 * 01599 * This is a typical stack operation. It shrinks the %deque by one. 01600 * 01601 * Note that no data is returned, and if the first element's data is 01602 * needed, it should be retrieved before pop_front() is called. 01603 */ 01604 void 01605 pop_front() _GLIBCXX_NOEXCEPT 01606 { 01607 __glibcxx_requires_nonempty(); 01608 if (this->_M_impl._M_start._M_cur 01609 != this->_M_impl._M_start._M_last - 1) 01610 { 01611 _Alloc_traits::destroy(this->_M_impl, 01612 this->_M_impl._M_start._M_cur); 01613 ++this->_M_impl._M_start._M_cur; 01614 } 01615 else 01616 _M_pop_front_aux(); 01617 } 01618 01619 /** 01620 * @brief Removes last element. 01621 * 01622 * This is a typical stack operation. It shrinks the %deque by one. 01623 * 01624 * Note that no data is returned, and if the last element's data is 01625 * needed, it should be retrieved before pop_back() is called. 01626 */ 01627 void 01628 pop_back() _GLIBCXX_NOEXCEPT 01629 { 01630 __glibcxx_requires_nonempty(); 01631 if (this->_M_impl._M_finish._M_cur 01632 != this->_M_impl._M_finish._M_first) 01633 { 01634 --this->_M_impl._M_finish._M_cur; 01635 _Alloc_traits::destroy(this->_M_impl, 01636 this->_M_impl._M_finish._M_cur); 01637 } 01638 else 01639 _M_pop_back_aux(); 01640 } 01641 01642 #if __cplusplus >= 201103L 01643 /** 01644 * @brief Inserts an object in %deque before specified iterator. 01645 * @param __position A const_iterator into the %deque. 01646 * @param __args Arguments. 01647 * @return An iterator that points to the inserted data. 01648 * 01649 * This function will insert an object of type T constructed 01650 * with T(std::forward<Args>(args)...) before the specified location. 01651 */ 01652 template<typename... _Args> 01653 iterator 01654 emplace(const_iterator __position, _Args&&... __args); 01655 01656 /** 01657 * @brief Inserts given value into %deque before specified iterator. 01658 * @param __position A const_iterator into the %deque. 01659 * @param __x Data to be inserted. 01660 * @return An iterator that points to the inserted data. 01661 * 01662 * This function will insert a copy of the given value before the 01663 * specified location. 01664 */ 01665 iterator 01666 insert(const_iterator __position, const value_type& __x); 01667 #else 01668 /** 01669 * @brief Inserts given value into %deque before specified iterator. 01670 * @param __position An iterator into the %deque. 01671 * @param __x Data to be inserted. 01672 * @return An iterator that points to the inserted data. 01673 * 01674 * This function will insert a copy of the given value before the 01675 * specified location. 01676 */ 01677 iterator 01678 insert(iterator __position, const value_type& __x); 01679 #endif 01680 01681 #if __cplusplus >= 201103L 01682 /** 01683 * @brief Inserts given rvalue into %deque before specified iterator. 01684 * @param __position A const_iterator into the %deque. 01685 * @param __x Data to be inserted. 01686 * @return An iterator that points to the inserted data. 01687 * 01688 * This function will insert a copy of the given rvalue before the 01689 * specified location. 01690 */ 01691 iterator 01692 insert(const_iterator __position, value_type&& __x) 01693 { return emplace(__position, std::move(__x)); } 01694 01695 /** 01696 * @brief Inserts an initializer list into the %deque. 01697 * @param __p An iterator into the %deque. 01698 * @param __l An initializer_list. 01699 * 01700 * This function will insert copies of the data in the 01701 * initializer_list @a __l into the %deque before the location 01702 * specified by @a __p. This is known as <em>list insert</em>. 01703 */ 01704 iterator 01705 insert(const_iterator __p, initializer_list<value_type> __l) 01706 { 01707 auto __offset = __p - cbegin(); 01708 _M_range_insert_aux(__p._M_const_cast(), __l.begin(), __l.end(), 01709 std::random_access_iterator_tag()); 01710 return begin() + __offset; 01711 } 01712 #endif 01713 01714 #if __cplusplus >= 201103L 01715 /** 01716 * @brief Inserts a number of copies of given data into the %deque. 01717 * @param __position A const_iterator into the %deque. 01718 * @param __n Number of elements to be inserted. 01719 * @param __x Data to be inserted. 01720 * @return An iterator that points to the inserted data. 01721 * 01722 * This function will insert a specified number of copies of the given 01723 * data before the location specified by @a __position. 01724 */ 01725 iterator 01726 insert(const_iterator __position, size_type __n, const value_type& __x) 01727 { 01728 difference_type __offset = __position - cbegin(); 01729 _M_fill_insert(__position._M_const_cast(), __n, __x); 01730 return begin() + __offset; 01731 } 01732 #else 01733 /** 01734 * @brief Inserts a number of copies of given data into the %deque. 01735 * @param __position An iterator into the %deque. 01736 * @param __n Number of elements to be inserted. 01737 * @param __x Data to be inserted. 01738 * 01739 * This function will insert a specified number of copies of the given 01740 * data before the location specified by @a __position. 01741 */ 01742 void 01743 insert(iterator __position, size_type __n, const value_type& __x) 01744 { _M_fill_insert(__position, __n, __x); } 01745 #endif 01746 01747 #if __cplusplus >= 201103L 01748 /** 01749 * @brief Inserts a range into the %deque. 01750 * @param __position A const_iterator into the %deque. 01751 * @param __first An input iterator. 01752 * @param __last An input iterator. 01753 * @return An iterator that points to the inserted data. 01754 * 01755 * This function will insert copies of the data in the range 01756 * [__first,__last) into the %deque before the location specified 01757 * by @a __position. This is known as <em>range insert</em>. 01758 */ 01759 template<typename _InputIterator, 01760 typename = std::_RequireInputIter<_InputIterator>> 01761 iterator 01762 insert(const_iterator __position, _InputIterator __first, 01763 _InputIterator __last) 01764 { 01765 difference_type __offset = __position - cbegin(); 01766 _M_insert_dispatch(__position._M_const_cast(), 01767 __first, __last, __false_type()); 01768 return begin() + __offset; 01769 } 01770 #else 01771 /** 01772 * @brief Inserts a range into the %deque. 01773 * @param __position An iterator into the %deque. 01774 * @param __first An input iterator. 01775 * @param __last An input iterator. 01776 * 01777 * This function will insert copies of the data in the range 01778 * [__first,__last) into the %deque before the location specified 01779 * by @a __position. This is known as <em>range insert</em>. 01780 */ 01781 template<typename _InputIterator> 01782 void 01783 insert(iterator __position, _InputIterator __first, 01784 _InputIterator __last) 01785 { 01786 // Check whether it's an integral type. If so, it's not an iterator. 01787 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 01788 _M_insert_dispatch(__position, __first, __last, _Integral()); 01789 } 01790 #endif 01791 01792 /** 01793 * @brief Remove element at given position. 01794 * @param __position Iterator pointing to element to be erased. 01795 * @return An iterator pointing to the next element (or end()). 01796 * 01797 * This function will erase the element at the given position and thus 01798 * shorten the %deque by one. 01799 * 01800 * The user is cautioned that 01801 * this function only erases the element, and that if the element is 01802 * itself a pointer, the pointed-to memory is not touched in any way. 01803 * Managing the pointer is the user's responsibility. 01804 */ 01805 iterator 01806 #if __cplusplus >= 201103L 01807 erase(const_iterator __position) 01808 #else 01809 erase(iterator __position) 01810 #endif 01811 { return _M_erase(__position._M_const_cast()); } 01812 01813 /** 01814 * @brief Remove a range of elements. 01815 * @param __first Iterator pointing to the first element to be erased. 01816 * @param __last Iterator pointing to one past the last element to be 01817 * erased. 01818 * @return An iterator pointing to the element pointed to by @a last 01819 * prior to erasing (or end()). 01820 * 01821 * This function will erase the elements in the range 01822 * [__first,__last) and shorten the %deque accordingly. 01823 * 01824 * The user is cautioned that 01825 * this function only erases the elements, and that if the elements 01826 * themselves are pointers, the pointed-to memory is not touched in any 01827 * way. Managing the pointer is the user's responsibility. 01828 */ 01829 iterator 01830 #if __cplusplus >= 201103L 01831 erase(const_iterator __first, const_iterator __last) 01832 #else 01833 erase(iterator __first, iterator __last) 01834 #endif 01835 { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); } 01836 01837 /** 01838 * @brief Swaps data with another %deque. 01839 * @param __x A %deque of the same element and allocator types. 01840 * 01841 * This exchanges the elements between two deques in constant time. 01842 * (Four pointers, so it should be quite fast.) 01843 * Note that the global std::swap() function is specialized such that 01844 * std::swap(d1,d2) will feed to this function. 01845 * 01846 * Whether the allocators are swapped depends on the allocator traits. 01847 */ 01848 void 01849 swap(deque& __x) _GLIBCXX_NOEXCEPT 01850 { 01851 #if __cplusplus >= 201103L 01852 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value 01853 || _M_get_Tp_allocator() == __x._M_get_Tp_allocator()); 01854 #endif 01855 _M_impl._M_swap_data(__x._M_impl); 01856 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(), 01857 __x._M_get_Tp_allocator()); 01858 } 01859 01860 /** 01861 * Erases all the elements. Note that this function only erases the 01862 * elements, and that if the elements themselves are pointers, the 01863 * pointed-to memory is not touched in any way. Managing the pointer is 01864 * the user's responsibility. 01865 */ 01866 void 01867 clear() _GLIBCXX_NOEXCEPT 01868 { _M_erase_at_end(begin()); } 01869 01870 protected: 01871 // Internal constructor functions follow. 01872 01873 // called by the range constructor to implement [23.1.1]/9 01874 01875 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01876 // 438. Ambiguity in the "do the right thing" clause 01877 template<typename _Integer> 01878 void 01879 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) 01880 { 01881 _M_initialize_map(_S_check_init_len(static_cast<size_type>(__n), 01882 _M_get_Tp_allocator())); 01883 _M_fill_initialize(__x); 01884 } 01885 01886 static size_t 01887 _S_check_init_len(size_t __n, const allocator_type& __a) 01888 { 01889 if (__n > _S_max_size(__a)) 01890 __throw_length_error( 01891 __N("cannot create std::deque larger than max_size()")); 01892 return __n; 01893 } 01894 01895 static size_type 01896 _S_max_size(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT 01897 { 01898 const size_t __diffmax = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max; 01899 const size_t __allocmax = _Alloc_traits::max_size(__a); 01900 return (std::min)(__diffmax, __allocmax); 01901 } 01902 01903 // called by the range constructor to implement [23.1.1]/9 01904 template<typename _InputIterator> 01905 void 01906 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last, 01907 __false_type) 01908 { 01909 _M_range_initialize(__first, __last, 01910 std::__iterator_category(__first)); 01911 } 01912 01913 // called by the second initialize_dispatch above 01914 //@{ 01915 /** 01916 * @brief Fills the deque with whatever is in [first,last). 01917 * @param __first An input iterator. 01918 * @param __last An input iterator. 01919 * @return Nothing. 01920 * 01921 * If the iterators are actually forward iterators (or better), then the 01922 * memory layout can be done all at once. Else we move forward using 01923 * push_back on each value from the iterator. 01924 */ 01925 template<typename _InputIterator> 01926 void 01927 _M_range_initialize(_InputIterator __first, _InputIterator __last, 01928 std::input_iterator_tag); 01929 01930 // called by the second initialize_dispatch above 01931 template<typename _ForwardIterator> 01932 void 01933 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last, 01934 std::forward_iterator_tag); 01935 //@} 01936 01937 /** 01938 * @brief Fills the %deque with copies of value. 01939 * @param __value Initial value. 01940 * @return Nothing. 01941 * @pre _M_start and _M_finish have already been initialized, 01942 * but none of the %deque's elements have yet been constructed. 01943 * 01944 * This function is called only when the user provides an explicit size 01945 * (with or without an explicit exemplar value). 01946 */ 01947 void 01948 _M_fill_initialize(const value_type& __value); 01949 01950 #if __cplusplus >= 201103L 01951 // called by deque(n). 01952 void 01953 _M_default_initialize(); 01954 #endif 01955 01956 // Internal assign functions follow. The *_aux functions do the actual 01957 // assignment work for the range versions. 01958 01959 // called by the range assign to implement [23.1.1]/9 01960 01961 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01962 // 438. Ambiguity in the "do the right thing" clause 01963 template<typename _Integer> 01964 void 01965 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 01966 { _M_fill_assign(__n, __val); } 01967 01968 // called by the range assign to implement [23.1.1]/9 01969 template<typename _InputIterator> 01970 void 01971 _M_assign_dispatch(_InputIterator __first, _InputIterator __last, 01972 __false_type) 01973 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); } 01974 01975 // called by the second assign_dispatch above 01976 template<typename _InputIterator> 01977 void 01978 _M_assign_aux(_InputIterator __first, _InputIterator __last, 01979 std::input_iterator_tag); 01980 01981 // called by the second assign_dispatch above 01982 template<typename _ForwardIterator> 01983 void 01984 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, 01985 std::forward_iterator_tag) 01986 { 01987 const size_type __len = std::distance(__first, __last); 01988 if (__len > size()) 01989 { 01990 _ForwardIterator __mid = __first; 01991 std::advance(__mid, size()); 01992 std::copy(__first, __mid, begin()); 01993 _M_range_insert_aux(end(), __mid, __last, 01994 std::__iterator_category(__first)); 01995 } 01996 else 01997 _M_erase_at_end(std::copy(__first, __last, begin())); 01998 } 01999 02000 // Called by assign(n,t), and the range assign when it turns out 02001 // to be the same thing. 02002 void 02003 _M_fill_assign(size_type __n, const value_type& __val) 02004 { 02005 if (__n > size()) 02006 { 02007 std::fill(begin(), end(), __val); 02008 _M_fill_insert(end(), __n - size(), __val); 02009 } 02010 else 02011 { 02012 _M_erase_at_end(begin() + difference_type(__n)); 02013 std::fill(begin(), end(), __val); 02014 } 02015 } 02016 02017 //@{ 02018 /// Helper functions for push_* and pop_*. 02019 #if __cplusplus < 201103L 02020 void _M_push_back_aux(const value_type&); 02021 02022 void _M_push_front_aux(const value_type&); 02023 #else 02024 template<typename... _Args> 02025 void _M_push_back_aux(_Args&&... __args); 02026 02027 template<typename... _Args> 02028 void _M_push_front_aux(_Args&&... __args); 02029 #endif 02030 02031 void _M_pop_back_aux(); 02032 02033 void _M_pop_front_aux(); 02034 //@} 02035 02036 // Internal insert functions follow. The *_aux functions do the actual 02037 // insertion work when all shortcuts fail. 02038 02039 // called by the range insert to implement [23.1.1]/9 02040 02041 // _GLIBCXX_RESOLVE_LIB_DEFECTS 02042 // 438. Ambiguity in the "do the right thing" clause 02043 template<typename _Integer> 02044 void 02045 _M_insert_dispatch(iterator __pos, 02046 _Integer __n, _Integer __x, __true_type) 02047 { _M_fill_insert(__pos, __n, __x); } 02048 02049 // called by the range insert to implement [23.1.1]/9 02050 template<typename _InputIterator> 02051 void 02052 _M_insert_dispatch(iterator __pos, 02053 _InputIterator __first, _InputIterator __last, 02054 __false_type) 02055 { 02056 _M_range_insert_aux(__pos, __first, __last, 02057 std::__iterator_category(__first)); 02058 } 02059 02060 // called by the second insert_dispatch above 02061 template<typename _InputIterator> 02062 void 02063 _M_range_insert_aux(iterator __pos, _InputIterator __first, 02064 _InputIterator __last, std::input_iterator_tag); 02065 02066 // called by the second insert_dispatch above 02067 template<typename _ForwardIterator> 02068 void 02069 _M_range_insert_aux(iterator __pos, _ForwardIterator __first, 02070 _ForwardIterator __last, std::forward_iterator_tag); 02071 02072 // Called by insert(p,n,x), and the range insert when it turns out to be 02073 // the same thing. Can use fill functions in optimal situations, 02074 // otherwise passes off to insert_aux(p,n,x). 02075 void 02076 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x); 02077 02078 // called by insert(p,x) 02079 #if __cplusplus < 201103L 02080 iterator 02081 _M_insert_aux(iterator __pos, const value_type& __x); 02082 #else 02083 template<typename... _Args> 02084 iterator 02085 _M_insert_aux(iterator __pos, _Args&&... __args); 02086 #endif 02087 02088 // called by insert(p,n,x) via fill_insert 02089 void 02090 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x); 02091 02092 // called by range_insert_aux for forward iterators 02093 template<typename _ForwardIterator> 02094 void 02095 _M_insert_aux(iterator __pos, 02096 _ForwardIterator __first, _ForwardIterator __last, 02097 size_type __n); 02098 02099 02100 // Internal erase functions follow. 02101 02102 void 02103 _M_destroy_data_aux(iterator __first, iterator __last); 02104 02105 // Called by ~deque(). 02106 // NB: Doesn't deallocate the nodes. 02107 template<typename _Alloc1> 02108 void 02109 _M_destroy_data(iterator __first, iterator __last, const _Alloc1&) 02110 { _M_destroy_data_aux(__first, __last); } 02111 02112 void 02113 _M_destroy_data(iterator __first, iterator __last, 02114 const std::allocator<_Tp>&) 02115 { 02116 if (!__has_trivial_destructor(value_type)) 02117 _M_destroy_data_aux(__first, __last); 02118 } 02119 02120 // Called by erase(q1, q2). 02121 void 02122 _M_erase_at_begin(iterator __pos) 02123 { 02124 _M_destroy_data(begin(), __pos, _M_get_Tp_allocator()); 02125 _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node); 02126 this->_M_impl._M_start = __pos; 02127 } 02128 02129 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux, 02130 // _M_fill_assign, operator=. 02131 void 02132 _M_erase_at_end(iterator __pos) 02133 { 02134 _M_destroy_data(__pos, end(), _M_get_Tp_allocator()); 02135 _M_destroy_nodes(__pos._M_node + 1, 02136 this->_M_impl._M_finish._M_node + 1); 02137 this->_M_impl._M_finish = __pos; 02138 } 02139 02140 iterator 02141 _M_erase(iterator __pos); 02142 02143 iterator 02144 _M_erase(iterator __first, iterator __last); 02145 02146 #if __cplusplus >= 201103L 02147 // Called by resize(sz). 02148 void 02149 _M_default_append(size_type __n); 02150 02151 bool 02152 _M_shrink_to_fit(); 02153 #endif 02154 02155 //@{ 02156 /// Memory-handling helpers for the previous internal insert functions. 02157 iterator 02158 _M_reserve_elements_at_front(size_type __n) 02159 { 02160 const size_type __vacancies = this->_M_impl._M_start._M_cur 02161 - this->_M_impl._M_start._M_first; 02162 if (__n > __vacancies) 02163 _M_new_elements_at_front(__n - __vacancies); 02164 return this->_M_impl._M_start - difference_type(__n); 02165 } 02166 02167 iterator 02168 _M_reserve_elements_at_back(size_type __n) 02169 { 02170 const size_type __vacancies = (this->_M_impl._M_finish._M_last 02171 - this->_M_impl._M_finish._M_cur) - 1; 02172 if (__n > __vacancies) 02173 _M_new_elements_at_back(__n - __vacancies); 02174 return this->_M_impl._M_finish + difference_type(__n); 02175 } 02176 02177 void 02178 _M_new_elements_at_front(size_type __new_elements); 02179 02180 void 02181 _M_new_elements_at_back(size_type __new_elements); 02182 //@} 02183 02184 02185 //@{ 02186 /** 02187 * @brief Memory-handling helpers for the major %map. 02188 * 02189 * Makes sure the _M_map has space for new nodes. Does not 02190 * actually add the nodes. Can invalidate _M_map pointers. 02191 * (And consequently, %deque iterators.) 02192 */ 02193 void 02194 _M_reserve_map_at_back(size_type __nodes_to_add = 1) 02195 { 02196 if (__nodes_to_add + 1 > this->_M_impl._M_map_size 02197 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map)) 02198 _M_reallocate_map(__nodes_to_add, false); 02199 } 02200 02201 void 02202 _M_reserve_map_at_front(size_type __nodes_to_add = 1) 02203 { 02204 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node 02205 - this->_M_impl._M_map)) 02206 _M_reallocate_map(__nodes_to_add, true); 02207 } 02208 02209 void 02210 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front); 02211 //@} 02212 02213 #if __cplusplus >= 201103L 02214 // Constant-time, nothrow move assignment when source object's memory 02215 // can be moved because the allocators are equal. 02216 void 02217 _M_move_assign1(deque&& __x, /* always equal: */ true_type) noexcept 02218 { 02219 this->_M_impl._M_swap_data(__x._M_impl); 02220 __x.clear(); 02221 std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator()); 02222 } 02223 02224 // When the allocators are not equal the operation could throw, because 02225 // we might need to allocate a new map for __x after moving from it 02226 // or we might need to allocate new elements for *this. 02227 void 02228 _M_move_assign1(deque&& __x, /* always equal: */ false_type) 02229 { 02230 constexpr bool __move_storage = 02231 _Alloc_traits::_S_propagate_on_move_assign(); 02232 _M_move_assign2(std::move(__x), __bool_constant<__move_storage>()); 02233 } 02234 02235 // Destroy all elements and deallocate all memory, then replace 02236 // with elements created from __args. 02237 template<typename... _Args> 02238 void 02239 _M_replace_map(_Args&&... __args) 02240 { 02241 // Create new data first, so if allocation fails there are no effects. 02242 deque __newobj(std::forward<_Args>(__args)...); 02243 // Free existing storage using existing allocator. 02244 clear(); 02245 _M_deallocate_node(*begin()._M_node); // one node left after clear() 02246 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size); 02247 this->_M_impl._M_map = nullptr; 02248 this->_M_impl._M_map_size = 0; 02249 // Take ownership of replacement memory. 02250 this->_M_impl._M_swap_data(__newobj._M_impl); 02251 } 02252 02253 // Do move assignment when the allocator propagates. 02254 void 02255 _M_move_assign2(deque&& __x, /* propagate: */ true_type) 02256 { 02257 // Make a copy of the original allocator state. 02258 auto __alloc = __x._M_get_Tp_allocator(); 02259 // The allocator propagates so storage can be moved from __x, 02260 // leaving __x in a valid empty state with a moved-from allocator. 02261 _M_replace_map(std::move(__x)); 02262 // Move the corresponding allocator state too. 02263 _M_get_Tp_allocator() = std::move(__alloc); 02264 } 02265 02266 // Do move assignment when it may not be possible to move source 02267 // object's memory, resulting in a linear-time operation. 02268 void 02269 _M_move_assign2(deque&& __x, /* propagate: */ false_type) 02270 { 02271 if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator()) 02272 { 02273 // The allocators are equal so storage can be moved from __x, 02274 // leaving __x in a valid empty state with its current allocator. 02275 _M_replace_map(std::move(__x), __x.get_allocator()); 02276 } 02277 else 02278 { 02279 // The rvalue's allocator cannot be moved and is not equal, 02280 // so we need to individually move each element. 02281 _M_assign_aux(std::__make_move_if_noexcept_iterator(__x.begin()), 02282 std::__make_move_if_noexcept_iterator(__x.end()), 02283 std::random_access_iterator_tag()); 02284 __x.clear(); 02285 } 02286 } 02287 #endif 02288 }; 02289 02290 #if __cpp_deduction_guides >= 201606 02291 template<typename _InputIterator, typename _ValT 02292 = typename iterator_traits<_InputIterator>::value_type, 02293 typename _Allocator = allocator<_ValT>, 02294 typename = _RequireInputIter<_InputIterator>, 02295 typename = _RequireAllocator<_Allocator>> 02296 deque(_InputIterator, _InputIterator, _Allocator = _Allocator()) 02297 -> deque<_ValT, _Allocator>; 02298 #endif 02299 02300 /** 02301 * @brief Deque equality comparison. 02302 * @param __x A %deque. 02303 * @param __y A %deque of the same type as @a __x. 02304 * @return True iff the size and elements of the deques are equal. 02305 * 02306 * This is an equivalence relation. It is linear in the size of the 02307 * deques. Deques are considered equivalent if their sizes are equal, 02308 * and if corresponding elements compare equal. 02309 */ 02310 template<typename _Tp, typename _Alloc> 02311 inline bool 02312 operator==(const deque<_Tp, _Alloc>& __x, 02313 const deque<_Tp, _Alloc>& __y) 02314 { return __x.size() == __y.size() 02315 && std::equal(__x.begin(), __x.end(), __y.begin()); } 02316 02317 /** 02318 * @brief Deque ordering relation. 02319 * @param __x A %deque. 02320 * @param __y A %deque of the same type as @a __x. 02321 * @return True iff @a x is lexicographically less than @a __y. 02322 * 02323 * This is a total ordering relation. It is linear in the size of the 02324 * deques. The elements must be comparable with @c <. 02325 * 02326 * See std::lexicographical_compare() for how the determination is made. 02327 */ 02328 template<typename _Tp, typename _Alloc> 02329 inline bool 02330 operator<(const deque<_Tp, _Alloc>& __x, 02331 const deque<_Tp, _Alloc>& __y) 02332 { return std::lexicographical_compare(__x.begin(), __x.end(), 02333 __y.begin(), __y.end()); } 02334 02335 /// Based on operator== 02336 template<typename _Tp, typename _Alloc> 02337 inline bool 02338 operator!=(const deque<_Tp, _Alloc>& __x, 02339 const deque<_Tp, _Alloc>& __y) 02340 { return !(__x == __y); } 02341 02342 /// Based on operator< 02343 template<typename _Tp, typename _Alloc> 02344 inline bool 02345 operator>(const deque<_Tp, _Alloc>& __x, 02346 const deque<_Tp, _Alloc>& __y) 02347 { return __y < __x; } 02348 02349 /// Based on operator< 02350 template<typename _Tp, typename _Alloc> 02351 inline bool 02352 operator<=(const deque<_Tp, _Alloc>& __x, 02353 const deque<_Tp, _Alloc>& __y) 02354 { return !(__y < __x); } 02355 02356 /// Based on operator< 02357 template<typename _Tp, typename _Alloc> 02358 inline bool 02359 operator>=(const deque<_Tp, _Alloc>& __x, 02360 const deque<_Tp, _Alloc>& __y) 02361 { return !(__x < __y); } 02362 02363 /// See std::deque::swap(). 02364 template<typename _Tp, typename _Alloc> 02365 inline void 02366 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y) 02367 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y))) 02368 { __x.swap(__y); } 02369 02370 #undef _GLIBCXX_DEQUE_BUF_SIZE 02371 02372 _GLIBCXX_END_NAMESPACE_CONTAINER 02373 02374 #if __cplusplus >= 201103L 02375 // std::allocator is safe, but it is not the only allocator 02376 // for which this is valid. 02377 template<class _Tp> 02378 struct __is_bitwise_relocatable<_GLIBCXX_STD_C::deque<_Tp>> 02379 : true_type { }; 02380 #endif 02381 02382 _GLIBCXX_END_NAMESPACE_VERSION 02383 } // namespace std 02384 02385 #endif /* _STL_DEQUE_H */