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