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