stl_deque.h

Go to the documentation of this file.
00001 // Deque implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002 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 2, 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 // You should have received a copy of the GNU General Public License along 00017 // with this library; see the file COPYING. If not, write to the Free 00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00019 // USA. 00020 00021 // As a special exception, you may use this file as part of a free software 00022 // library without restriction. Specifically, if other files instantiate 00023 // templates or use macros or inline functions from this file, or you compile 00024 // this file and link it with other files to produce an executable, this 00025 // file does not by itself cause the resulting executable to be covered by 00026 // the GNU General Public License. This exception does not however 00027 // invalidate any other reasons why the executable file might be covered by 00028 // the GNU General Public License. 00029 00030 /* 00031 * 00032 * Copyright (c) 1994 00033 * Hewlett-Packard Company 00034 * 00035 * Permission to use, copy, modify, distribute and sell this software 00036 * and its documentation for any purpose is hereby granted without fee, 00037 * provided that the above copyright notice appear in all copies and 00038 * that both that copyright notice and this permission notice appear 00039 * in supporting documentation. Hewlett-Packard Company makes no 00040 * representations about the suitability of this software for any 00041 * purpose. It is provided "as is" without express or implied warranty. 00042 * 00043 * 00044 * Copyright (c) 1997 00045 * Silicon Graphics Computer Systems, Inc. 00046 * 00047 * Permission to use, copy, modify, distribute and sell this software 00048 * and its documentation for any purpose is hereby granted without fee, 00049 * provided that the above copyright notice appear in all copies and 00050 * that both that copyright notice and this permission notice appear 00051 * in supporting documentation. Silicon Graphics makes no 00052 * representations about the suitability of this software for any 00053 * purpose. It is provided "as is" without express or implied warranty. 00054 */ 00055 00056 /** @file stl_deque.h 00057 * This is an internal header file, included by other library headers. 00058 * You should not attempt to use it directly. 00059 */ 00060 00061 #ifndef __GLIBCPP_INTERNAL_DEQUE_H 00062 #define __GLIBCPP_INTERNAL_DEQUE_H 00063 00064 #include <bits/concept_check.h> 00065 #include <bits/stl_iterator_base_types.h> 00066 #include <bits/stl_iterator_base_funcs.h> 00067 00068 namespace std 00069 { 00070 /** 00071 * @if maint 00072 * @brief This function controls the size of memory nodes. 00073 * @param size The size of an element. 00074 * @return The number (not byte size) of elements per node. 00075 * 00076 * This function started off as a compiler kludge from SGI, but seems to 00077 * be a useful wrapper around a repeated constant expression. The '512' is 00078 * tuneable (and no other code needs to change), but no investigation has 00079 * been done since inheriting the SGI code. 00080 * @endif 00081 */ 00082 inline size_t 00083 __deque_buf_size(size_t __size) 00084 { return __size < 512 ? size_t(512 / __size) : size_t(1); } 00085 00086 00087 /** 00088 * @brief A deque::iterator. 00089 * 00090 * Quite a bit of intelligence here. Much of the functionality of deque is 00091 * actually passed off to this class. A deque holds two of these internally, 00092 * marking its valid range. Access to elements is done as offsets of either 00093 * of those two, relying on operator overloading in this class. 00094 * 00095 * @if maint 00096 * All the functions are op overloads except for _M_set_node. 00097 * @endif 00098 */ 00099 template <typename _Tp, typename _Ref, typename _Ptr> 00100 struct _Deque_iterator 00101 { 00102 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator; 00103 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator; 00104 static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); } 00105 00106 typedef random_access_iterator_tag iterator_category; 00107 typedef _Tp value_type; 00108 typedef _Ptr pointer; 00109 typedef _Ref reference; 00110 typedef size_t size_type; 00111 typedef ptrdiff_t difference_type; 00112 typedef _Tp** _Map_pointer; 00113 typedef _Deque_iterator _Self; 00114 00115 _Tp* _M_cur; 00116 _Tp* _M_first; 00117 _Tp* _M_last; 00118 _Map_pointer _M_node; 00119 00120 _Deque_iterator(_Tp* __x, _Map_pointer __y) 00121 : _M_cur(__x), _M_first(*__y), 00122 _M_last(*__y + _S_buffer_size()), _M_node(__y) {} 00123 _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {} 00124 _Deque_iterator(const iterator& __x) 00125 : _M_cur(__x._M_cur), _M_first(__x._M_first), 00126 _M_last(__x._M_last), _M_node(__x._M_node) {} 00127 00128 reference operator*() const { return *_M_cur; } 00129 pointer operator->() const { return _M_cur; } 00130 00131 _Self& operator++() { 00132 ++_M_cur; 00133 if (_M_cur == _M_last) { 00134 _M_set_node(_M_node + 1); 00135 _M_cur = _M_first; 00136 } 00137 return *this; 00138 } 00139 _Self operator++(int) { 00140 _Self __tmp = *this; 00141 ++*this; 00142 return __tmp; 00143 } 00144 00145 _Self& operator--() { 00146 if (_M_cur == _M_first) { 00147 _M_set_node(_M_node - 1); 00148 _M_cur = _M_last; 00149 } 00150 --_M_cur; 00151 return *this; 00152 } 00153 _Self operator--(int) { 00154 _Self __tmp = *this; 00155 --*this; 00156 return __tmp; 00157 } 00158 00159 _Self& operator+=(difference_type __n) 00160 { 00161 difference_type __offset = __n + (_M_cur - _M_first); 00162 if (__offset >= 0 && __offset < difference_type(_S_buffer_size())) 00163 _M_cur += __n; 00164 else { 00165 difference_type __node_offset = 00166 __offset > 0 ? __offset / difference_type(_S_buffer_size()) 00167 : -difference_type((-__offset - 1) / _S_buffer_size()) - 1; 00168 _M_set_node(_M_node + __node_offset); 00169 _M_cur = _M_first + 00170 (__offset - __node_offset * difference_type(_S_buffer_size())); 00171 } 00172 return *this; 00173 } 00174 00175 _Self operator+(difference_type __n) const 00176 { 00177 _Self __tmp = *this; 00178 return __tmp += __n; 00179 } 00180 00181 _Self& operator-=(difference_type __n) { return *this += -__n; } 00182 00183 _Self operator-(difference_type __n) const { 00184 _Self __tmp = *this; 00185 return __tmp -= __n; 00186 } 00187 00188 reference operator[](difference_type __n) const { return *(*this + __n); } 00189 00190 /** @if maint 00191 * Prepares to traverse new_node. Sets everything except _M_cur, which 00192 * should therefore be set by the caller immediately afterwards, based on 00193 * _M_first and _M_last. 00194 * @endif 00195 */ 00196 void 00197 _M_set_node(_Map_pointer __new_node) 00198 { 00199 _M_node = __new_node; 00200 _M_first = *__new_node; 00201 _M_last = _M_first + difference_type(_S_buffer_size()); 00202 } 00203 }; 00204 00205 // Note: we also provide overloads whose operands are of the same type in 00206 // order to avoid ambiguous overload resolution when std::rel_ops operators 00207 // are in scope (for additional details, see libstdc++/3628) 00208 template <typename _Tp, typename _Ref, typename _Ptr> 00209 inline bool 00210 operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00211 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) 00212 { 00213 return __x._M_cur == __y._M_cur; 00214 } 00215 00216 template <typename _Tp, typename _RefL, typename _PtrL, 00217 typename _RefR, typename _PtrR> 00218 inline bool 00219 operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00220 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) 00221 { 00222 return __x._M_cur == __y._M_cur; 00223 } 00224 00225 template <typename _Tp, typename _Ref, typename _Ptr> 00226 inline bool 00227 operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00228 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) 00229 { 00230 return !(__x == __y); 00231 } 00232 00233 template <typename _Tp, typename _RefL, typename _PtrL, 00234 typename _RefR, typename _PtrR> 00235 inline bool 00236 operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00237 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) 00238 { 00239 return !(__x == __y); 00240 } 00241 00242 template <typename _Tp, typename _Ref, typename _Ptr> 00243 inline bool 00244 operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00245 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) 00246 { 00247 return (__x._M_node == __y._M_node) ? 00248 (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node); 00249 } 00250 00251 template <typename _Tp, typename _RefL, typename _PtrL, 00252 typename _RefR, typename _PtrR> 00253 inline bool 00254 operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00255 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) 00256 { 00257 return (__x._M_node == __y._M_node) ? 00258 (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node); 00259 } 00260 00261 template <typename _Tp, typename _Ref, typename _Ptr> 00262 inline bool 00263 operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00264 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) 00265 { 00266 return __y < __x; 00267 } 00268 00269 template <typename _Tp, typename _RefL, typename _PtrL, 00270 typename _RefR, typename _PtrR> 00271 inline bool 00272 operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00273 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) 00274 { 00275 return __y < __x; 00276 } 00277 00278 template <typename _Tp, typename _Ref, typename _Ptr> 00279 inline bool 00280 operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00281 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) 00282 { 00283 return !(__y < __x); 00284 } 00285 00286 template <typename _Tp, typename _RefL, typename _PtrL, 00287 typename _RefR, typename _PtrR> 00288 inline bool 00289 operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00290 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) 00291 { 00292 return !(__y < __x); 00293 } 00294 00295 template <typename _Tp, typename _Ref, typename _Ptr> 00296 inline bool 00297 operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x, 00298 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) 00299 { 00300 return !(__x < __y); 00301 } 00302 00303 template <typename _Tp, typename _RefL, typename _PtrL, 00304 typename _RefR, typename _PtrR> 00305 inline bool 00306 operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00307 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) 00308 { 00309 return !(__x < __y); 00310 } 00311 00312 // _GLIBCPP_RESOLVE_LIB_DEFECTS 00313 // According to the resolution of DR179 not only the various comparison 00314 // operators but also operator- must accept mixed iterator/const_iterator 00315 // parameters. 00316 template <typename _Tp, typename _RefL, typename _PtrL, 00317 typename _RefR, typename _PtrR> 00318 inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type 00319 operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x, 00320 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) 00321 { 00322 return _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type 00323 (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size()) * 00324 (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first) + 00325 (__y._M_last - __y._M_cur); 00326 } 00327 00328 template <typename _Tp, typename _Ref, typename _Ptr> 00329 inline _Deque_iterator<_Tp, _Ref, _Ptr> 00330 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x) 00331 { 00332 return __x + __n; 00333 } 00334 00335 00336 /// @if maint Primary default version. @endif 00337 /** 00338 * @if maint 00339 * Deque base class. It has two purposes. First, its constructor 00340 * and destructor allocate (but don't initialize) storage. This makes 00341 * %exception safety easier. Second, the base class encapsulates all of 00342 * the differences between SGI-style allocators and standard-conforming 00343 * allocators. (See stl_alloc.h for more on this topic.) There are two 00344 * versions: this ordinary one, and the space-saving specialization for 00345 * instanceless allocators. 00346 * @endif 00347 */ 00348 template <typename _Tp, typename _Alloc, bool __is_static> 00349 class _Deque_alloc_base 00350 { 00351 public: 00352 typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type; 00353 allocator_type get_allocator() const { return _M_node_allocator; } 00354 00355 _Deque_alloc_base(const allocator_type& __a) 00356 : _M_node_allocator(__a), _M_map_allocator(__a), 00357 _M_map(0), _M_map_size(0) 00358 {} 00359 00360 protected: 00361 typedef typename _Alloc_traits<_Tp*, _Alloc>::allocator_type 00362 _Map_allocator_type; 00363 00364 _Tp* 00365 _M_allocate_node() 00366 { 00367 return _M_node_allocator.allocate(__deque_buf_size(sizeof(_Tp))); 00368 } 00369 00370 void 00371 _M_deallocate_node(_Tp* __p) 00372 { 00373 _M_node_allocator.deallocate(__p, __deque_buf_size(sizeof(_Tp))); 00374 } 00375 00376 _Tp** 00377 _M_allocate_map(size_t __n) 00378 { return _M_map_allocator.allocate(__n); } 00379 00380 void 00381 _M_deallocate_map(_Tp** __p, size_t __n) 00382 { _M_map_allocator.deallocate(__p, __n); } 00383 00384 allocator_type _M_node_allocator; 00385 _Map_allocator_type _M_map_allocator; 00386 _Tp** _M_map; 00387 size_t _M_map_size; 00388 }; 00389 00390 /// @if maint Specialization for instanceless allocators. @endif 00391 template <typename _Tp, typename _Alloc> 00392 class _Deque_alloc_base<_Tp, _Alloc, true> 00393 { 00394 public: 00395 typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type; 00396 allocator_type get_allocator() const { return allocator_type(); } 00397 00398 _Deque_alloc_base(const allocator_type&) 00399 : _M_map(0), _M_map_size(0) 00400 {} 00401 00402 protected: 00403 typedef typename _Alloc_traits<_Tp,_Alloc>::_Alloc_type _Node_alloc_type; 00404 typedef typename _Alloc_traits<_Tp*,_Alloc>::_Alloc_type _Map_alloc_type; 00405 00406 _Tp* 00407 _M_allocate_node() 00408 { 00409 return _Node_alloc_type::allocate(__deque_buf_size(sizeof(_Tp))); 00410 } 00411 00412 void 00413 _M_deallocate_node(_Tp* __p) 00414 { 00415 _Node_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp))); 00416 } 00417 00418 _Tp** 00419 _M_allocate_map(size_t __n) 00420 { return _Map_alloc_type::allocate(__n); } 00421 00422 void 00423 _M_deallocate_map(_Tp** __p, size_t __n) 00424 { _Map_alloc_type::deallocate(__p, __n); } 00425 00426 _Tp** _M_map; 00427 size_t _M_map_size; 00428 }; 00429 00430 00431 /** 00432 * @if maint 00433 * Deque base class. Using _Alloc_traits in the instantiation of the parent 00434 * class provides the compile-time dispatching mentioned in the parent's 00435 * docs. This class provides the unified face for %deque's allocation. 00436 * 00437 * Nothing in this class ever constructs or destroys an actual Tp element. 00438 * (Deque handles that itself.) Only/All memory management is performed 00439 * here. 00440 * @endif 00441 */ 00442 template <typename _Tp, typename _Alloc> 00443 class _Deque_base 00444 : public _Deque_alloc_base<_Tp,_Alloc, 00445 _Alloc_traits<_Tp, _Alloc>::_S_instanceless> 00446 { 00447 public: 00448 typedef _Deque_alloc_base<_Tp,_Alloc, 00449 _Alloc_traits<_Tp, _Alloc>::_S_instanceless> 00450 _Base; 00451 typedef typename _Base::allocator_type allocator_type; 00452 typedef _Deque_iterator<_Tp,_Tp&,_Tp*> iterator; 00453 typedef _Deque_iterator<_Tp,const _Tp&,const _Tp*> const_iterator; 00454 00455 _Deque_base(const allocator_type& __a, size_t __num_elements) 00456 : _Base(__a), _M_start(), _M_finish() 00457 { _M_initialize_map(__num_elements); } 00458 _Deque_base(const allocator_type& __a) 00459 : _Base(__a), _M_start(), _M_finish() {} 00460 ~_Deque_base(); 00461 00462 protected: 00463 void _M_initialize_map(size_t); 00464 void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish); 00465 void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish); 00466 enum { _S_initial_map_size = 8 }; 00467 00468 iterator _M_start; 00469 iterator _M_finish; 00470 }; 00471 00472 00473 template <typename _Tp, typename _Alloc> 00474 _Deque_base<_Tp,_Alloc>::~_Deque_base() 00475 { 00476 if (_M_map) 00477 { 00478 _M_destroy_nodes(_M_start._M_node, _M_finish._M_node + 1); 00479 _M_deallocate_map(_M_map, _M_map_size); 00480 } 00481 } 00482 00483 /** 00484 * @if maint 00485 * @brief Layout storage. 00486 * @param num_elements The count of T's for which to allocate space 00487 * at first. 00488 * @return Nothing. 00489 * 00490 * The initial underlying memory layout is a bit complicated... 00491 * @endif 00492 */ 00493 template <typename _Tp, typename _Alloc> 00494 void 00495 _Deque_base<_Tp,_Alloc>::_M_initialize_map(size_t __num_elements) 00496 { 00497 size_t __num_nodes = 00498 __num_elements / __deque_buf_size(sizeof(_Tp)) + 1; 00499 00500 _M_map_size = max((size_t) _S_initial_map_size, __num_nodes + 2); 00501 _M_map = _M_allocate_map(_M_map_size); 00502 00503 // For "small" maps (needing less than _M_map_size nodes), allocation 00504 // starts in the middle elements and grows outwards. So nstart may be the 00505 // beginning of _M_map, but for small maps it may be as far in as _M_map+3. 00506 00507 _Tp** __nstart = _M_map + (_M_map_size - __num_nodes) / 2; 00508 _Tp** __nfinish = __nstart + __num_nodes; 00509 00510 try 00511 { _M_create_nodes(__nstart, __nfinish); } 00512 catch(...) 00513 { 00514 _M_deallocate_map(_M_map, _M_map_size); 00515 _M_map = 0; 00516 _M_map_size = 0; 00517 __throw_exception_again; 00518 } 00519 00520 _M_start._M_set_node(__nstart); 00521 _M_finish._M_set_node(__nfinish - 1); 00522 _M_start._M_cur = _M_start._M_first; 00523 _M_finish._M_cur = _M_finish._M_first + 00524 __num_elements % __deque_buf_size(sizeof(_Tp)); 00525 } 00526 00527 template <typename _Tp, typename _Alloc> 00528 void _Deque_base<_Tp,_Alloc>::_M_create_nodes(_Tp** __nstart, _Tp** __nfinish) 00529 { 00530 _Tp** __cur; 00531 try 00532 { 00533 for (__cur = __nstart; __cur < __nfinish; ++__cur) 00534 *__cur = _M_allocate_node(); 00535 } 00536 catch(...) 00537 { 00538 _M_destroy_nodes(__nstart, __cur); 00539 __throw_exception_again; 00540 } 00541 } 00542 00543 template <typename _Tp, typename _Alloc> 00544 void 00545 _Deque_base<_Tp,_Alloc>::_M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish) 00546 { 00547 for (_Tp** __n = __nstart; __n < __nfinish; ++__n) 00548 _M_deallocate_node(*__n); 00549 } 00550 00551 00552 /** 00553 * @brief A standard container using fixed-size memory allocation and 00554 * constant-time manipulation of elements at either end. 00555 * 00556 * @ingroup Containers 00557 * @ingroup Sequences 00558 * 00559 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00560 * <a href="tables.html#66">reversible container</a>, and a 00561 * <a href="tables.html#67">sequence</a>, including the 00562 * <a href="tables.html#68">optional sequence requirements</a>. 00563 * 00564 * In previous HP/SGI versions of deque, there was an extra template 00565 * parameter so users could control the node size. This extension turned 00566 * out to violate the C++ standard (it can be detected using template 00567 * template parameters), and it was removed. 00568 * 00569 * @if maint 00570 * Here's how a deque<Tp> manages memory. Each deque has 4 members: 00571 * 00572 * - Tp** _M_map 00573 * - size_t _M_map_size 00574 * - iterator _M_start, _M_finish 00575 * 00576 * map_size is at least 8. %map is an array of map_size pointers-to-"nodes". 00577 * (The name %map has nothing to do with the std::map class, and "nodes" 00578 * should not be confused with std::list's usage of "node".) 00579 * 00580 * A "node" has no specific type name as such, but it is referred to as 00581 * "node" in this file. It is a simple array-of-Tp. If Tp is very large, 00582 * there will be one Tp element per node (i.e., an "array" of one). 00583 * For non-huge Tp's, node size is inversely related to Tp size: the 00584 * larger the Tp, the fewer Tp's will fit in a node. The goal here is to 00585 * keep the total size of a node relatively small and constant over different 00586 * Tp's, to improve allocator efficiency. 00587 * 00588 * **** As I write this, the nodes are /not/ allocated using the high-speed 00589 * memory pool. There are 20 hours left in the year; perhaps I can fix 00590 * this before 2002. 00591 * 00592 * Not every pointer in the %map array will point to a node. If the initial 00593 * number of elements in the deque is small, the /middle/ %map pointers will 00594 * be valid, and the ones at the edges will be unused. This same situation 00595 * will arise as the %map grows: available %map pointers, if any, will be on 00596 * the ends. As new nodes are created, only a subset of the %map's pointers 00597 * need to be copied "outward". 00598 * 00599 * Class invariants: 00600 * - For any nonsingular iterator i: 00601 * - i.node points to a member of the %map array. (Yes, you read that 00602 * correctly: i.node does not actually point to a node.) The member of 00603 * the %map array is what actually points to the node. 00604 * - i.first == *(i.node) (This points to the node (first Tp element).) 00605 * - i.last == i.first + node_size 00606 * - i.cur is a pointer in the range [i.first, i.last). NOTE: 00607 * the implication of this is that i.cur is always a dereferenceable 00608 * pointer, even if i is a past-the-end iterator. 00609 * - Start and Finish are always nonsingular iterators. NOTE: this means that 00610 * an empty deque must have one node, a deque with <N elements (where N is 00611 * the node buffer size) must have one node, a deque with N through (2N-1) 00612 * elements must have two nodes, etc. 00613 * - For every node other than start.node and finish.node, every element in 00614 * the node is an initialized object. If start.node == finish.node, then 00615 * [start.cur, finish.cur) are initialized objects, and the elements outside 00616 * that range are uninitialized storage. Otherwise, [start.cur, start.last) 00617 * and [finish.first, finish.cur) are initialized objects, and [start.first, 00618 * start.cur) and [finish.cur, finish.last) are uninitialized storage. 00619 * - [%map, %map + map_size) is a valid, non-empty range. 00620 * - [start.node, finish.node] is a valid range contained within 00621 * [%map, %map + map_size). 00622 * - A pointer in the range [%map, %map + map_size) points to an allocated 00623 * node if and only if the pointer is in the range 00624 * [start.node, finish.node]. 00625 * 00626 * Here's the magic: nothing in deque is "aware" of the discontiguous 00627 * storage! 00628 * 00629 * The memory setup and layout occurs in the parent, _Base, and the iterator 00630 * class is entirely responsible for "leaping" from one node to the next. 00631 * All the implementation routines for deque itself work only through the 00632 * start and finish iterators. This keeps the routines simple and sane, 00633 * and we can use other standard algorithms as well. 00634 * @endif 00635 */ 00636 template <typename _Tp, typename _Alloc = allocator<_Tp> > 00637 class deque : protected _Deque_base<_Tp, _Alloc> 00638 { 00639 // concept requirements 00640 __glibcpp_class_requires(_Tp, _SGIAssignableConcept) 00641 00642 typedef _Deque_base<_Tp, _Alloc> _Base; 00643 00644 public: 00645 typedef _Tp value_type; 00646 typedef value_type* pointer; 00647 typedef const value_type* const_pointer; 00648 typedef typename _Base::iterator iterator; 00649 typedef typename _Base::const_iterator const_iterator; 00650 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00651 typedef std::reverse_iterator<iterator> reverse_iterator; 00652 typedef value_type& reference; 00653 typedef const value_type& const_reference; 00654 typedef size_t size_type; 00655 typedef ptrdiff_t difference_type; 00656 typedef typename _Base::allocator_type allocator_type; 00657 00658 protected: 00659 typedef pointer* _Map_pointer; 00660 static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); } 00661 00662 // Functions controlling memory layout, and nothing else. 00663 using _Base::_M_initialize_map; 00664 using _Base::_M_create_nodes; 00665 using _Base::_M_destroy_nodes; 00666 using _Base::_M_allocate_node; 00667 using _Base::_M_deallocate_node; 00668 using _Base::_M_allocate_map; 00669 using _Base::_M_deallocate_map; 00670 00671 /** @if maint 00672 * A total of four data members accumulated down the heirarchy. If the 00673 * _Alloc type requires separate instances, then two of them will also be 00674 * included in each deque. 00675 * @endif 00676 */ 00677 using _Base::_M_map; 00678 using _Base::_M_map_size; 00679 using _Base::_M_start; 00680 using _Base::_M_finish; 00681 00682 public: 00683 // [23.2.1.1] construct/copy/destroy 00684 // (assign() and get_allocator() are also listed in this section) 00685 /** 00686 * @brief Default constructor creates no elements. 00687 */ 00688 explicit 00689 deque(const allocator_type& __a = allocator_type()) 00690 : _Base(__a, 0) {} 00691 00692 /** 00693 * @brief Create a %deque with copies of an exemplar element. 00694 * @param n The number of elements to initially create. 00695 * @param value An element to copy. 00696 * 00697 * This constructor fills the %deque with @a n copies of @a value. 00698 */ 00699 deque(size_type __n, const value_type& __value, 00700 const allocator_type& __a = allocator_type()) 00701 : _Base(__a, __n) 00702 { _M_fill_initialize(__value); } 00703 00704 /** 00705 * @brief Create a %deque with default elements. 00706 * @param n The number of elements to initially create. 00707 * 00708 * This constructor fills the %deque with @a n copies of a 00709 * default-constructed element. 00710 */ 00711 explicit 00712 deque(size_type __n) 00713 : _Base(allocator_type(), __n) 00714 { _M_fill_initialize(value_type()); } 00715 00716 /** 00717 * @brief %Deque copy constructor. 00718 * @param x A %deque of identical element and allocator types. 00719 * 00720 * The newly-created %deque uses a copy of the allocation object used 00721 * by @a x. 00722 */ 00723 deque(const deque& __x) 00724 : _Base(__x.get_allocator(), __x.size()) 00725 { uninitialized_copy(__x.begin(), __x.end(), _M_start); } 00726 00727 /** 00728 * @brief Builds a %deque from a range. 00729 * @param first An input iterator. 00730 * @param last An input iterator. 00731 * 00732 * Create a %deque consisting of copies of the elements from [first,last). 00733 * 00734 * If the iterators are forward, bidirectional, or random-access, then 00735 * this will call the elements' copy constructor N times (where N is 00736 * distance(first,last)) and do no memory reallocation. But if only 00737 * input iterators are used, then this will do at most 2N calls to the 00738 * copy constructor, and logN memory reallocations. 00739 */ 00740 template<typename _InputIterator> 00741 deque(_InputIterator __first, _InputIterator __last, 00742 const allocator_type& __a = allocator_type()) 00743 : _Base(__a) 00744 { 00745 // Check whether it's an integral type. If so, it's not an iterator. 00746 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00747 _M_initialize_dispatch(__first, __last, _Integral()); 00748 } 00749 00750 /** 00751 * The dtor only erases the elements, and note that if the elements 00752 * themselves are pointers, the pointed-to memory is not touched in any 00753 * way. Managing the pointer is the user's responsibilty. 00754 */ 00755 ~deque() { _Destroy(_M_start, _M_finish); } 00756 00757 /** 00758 * @brief %Deque assignment operator. 00759 * @param x A %deque of identical element and allocator types. 00760 * 00761 * All the elements of @a x are copied, but unlike the copy constructor, 00762 * the allocator object is not copied. 00763 */ 00764 deque& 00765 operator=(const deque& __x); 00766 00767 /** 00768 * @brief Assigns a given value to a %deque. 00769 * @param n Number of elements to be assigned. 00770 * @param val Value to be assigned. 00771 * 00772 * This function fills a %deque with @a n copies of the given value. 00773 * Note that the assignment completely changes the %deque and that the 00774 * resulting %deque's size is the same as the number of elements assigned. 00775 * Old data may be lost. 00776 */ 00777 void 00778 assign(size_type __n, const value_type& __val) { _M_fill_assign(__n, __val); } 00779 00780 /** 00781 * @brief Assigns a range to a %deque. 00782 * @param first An input iterator. 00783 * @param last An input iterator. 00784 * 00785 * This function fills a %deque with copies of the elements in the 00786 * range [first,last). 00787 * 00788 * Note that the assignment completely changes the %deque and that the 00789 * resulting %deque's size is the same as the number of elements assigned. 00790 * Old data may be lost. 00791 */ 00792 template<typename _InputIterator> 00793 void 00794 assign(_InputIterator __first, _InputIterator __last) 00795 { 00796 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00797 _M_assign_dispatch(__first, __last, _Integral()); 00798 } 00799 00800 /// Get a copy of the memory allocation object. 00801 allocator_type 00802 get_allocator() const { return _Base::get_allocator(); } 00803 00804 // iterators 00805 /** 00806 * Returns a read/write iterator that points to the first element in the 00807 * %deque. Iteration is done in ordinary element order. 00808 */ 00809 iterator 00810 begin() { return _M_start; } 00811 00812 /** 00813 * Returns a read-only (constant) iterator that points to the first element 00814 * in the %deque. Iteration is done in ordinary element order. 00815 */ 00816 const_iterator 00817 begin() const { return _M_start; } 00818 00819 /** 00820 * Returns a read/write iterator that points one past the last element in 00821 * the %deque. Iteration is done in ordinary element order. 00822 */ 00823 iterator 00824 end() { return _M_finish; } 00825 00826 /** 00827 * Returns a read-only (constant) iterator that points one past the last 00828 * element in the %deque. Iteration is done in ordinary element order. 00829 */ 00830 const_iterator 00831 end() const { return _M_finish; } 00832 00833 /** 00834 * Returns a read/write reverse iterator that points to the last element in 00835 * the %deque. Iteration is done in reverse element order. 00836 */ 00837 reverse_iterator 00838 rbegin() { return reverse_iterator(_M_finish); } 00839 00840 /** 00841 * Returns a read-only (constant) reverse iterator that points to the last 00842 * element in the %deque. Iteration is done in reverse element order. 00843 */ 00844 const_reverse_iterator 00845 rbegin() const { return const_reverse_iterator(_M_finish); } 00846 00847 /** 00848 * Returns a read/write reverse iterator that points to one before the 00849 * first element in the %deque. Iteration is done in reverse element 00850 * order. 00851 */ 00852 reverse_iterator 00853 rend() { return reverse_iterator(_M_start); } 00854 00855 /** 00856 * Returns a read-only (constant) reverse iterator that points to one 00857 * before the first element in the %deque. Iteration is done in reverse 00858 * element order. 00859 */ 00860 const_reverse_iterator 00861 rend() const { return const_reverse_iterator(_M_start); } 00862 00863 // [23.2.1.2] capacity 00864 /** Returns the number of elements in the %deque. */ 00865 size_type 00866 size() const { return _M_finish - _M_start; } 00867 00868 /** Returns the size() of the largest possible %deque. */ 00869 size_type 00870 max_size() const { return size_type(-1); } 00871 00872 /** 00873 * @brief Resizes the %deque to the specified number of elements. 00874 * @param new_size Number of elements the %deque should contain. 00875 * @param x Data with which new elements should be populated. 00876 * 00877 * This function will %resize the %deque to the specified number of 00878 * elements. If the number is smaller than the %deque's current size the 00879 * %deque is truncated, otherwise the %deque is extended and new elements 00880 * are populated with given data. 00881 */ 00882 void 00883 resize(size_type __new_size, const value_type& __x) 00884 { 00885 const size_type __len = size(); 00886 if (__new_size < __len) 00887 erase(_M_start + __new_size, _M_finish); 00888 else 00889 insert(_M_finish, __new_size - __len, __x); 00890 } 00891 00892 /** 00893 * @brief Resizes the %deque to the specified number of elements. 00894 * @param new_size Number of elements the %deque should contain. 00895 * 00896 * This function will resize the %deque to the specified number of 00897 * elements. If the number is smaller than the %deque's current size the 00898 * %deque is truncated, otherwise the %deque is extended and new elements 00899 * are default-constructed. 00900 */ 00901 void 00902 resize(size_type new_size) { resize(new_size, value_type()); } 00903 00904 /** 00905 * Returns true if the %deque is empty. (Thus begin() would equal end().) 00906 */ 00907 bool empty() const { return _M_finish == _M_start; } 00908 00909 // element access 00910 /** 00911 * @brief Subscript access to the data contained in the %deque. 00912 * @param n The index of the element for which data should be accessed. 00913 * @return Read/write reference to data. 00914 * 00915 * This operator allows for easy, array-style, data access. 00916 * Note that data access with this operator is unchecked and out_of_range 00917 * lookups are not defined. (For checked lookups see at().) 00918 */ 00919 reference 00920 operator[](size_type __n) { return _M_start[difference_type(__n)]; } 00921 00922 /** 00923 * @brief Subscript access to the data contained in the %deque. 00924 * @param n The index of the element for which data should be accessed. 00925 * @return Read-only (constant) reference to data. 00926 * 00927 * This operator allows for easy, array-style, data access. 00928 * Note that data access with this operator is unchecked and out_of_range 00929 * lookups are not defined. (For checked lookups see at().) 00930 */ 00931 const_reference 00932 operator[](size_type __n) const { return _M_start[difference_type(__n)]; } 00933 00934 protected: 00935 /// @if maint Safety check used only from at(). @endif 00936 void 00937 _M_range_check(size_type __n) const 00938 { 00939 if (__n >= this->size()) 00940 __throw_out_of_range("deque [] access out of range"); 00941 } 00942 00943 public: 00944 /** 00945 * @brief Provides access to the data contained in the %deque. 00946 * @param n The index of the element for which data should be accessed. 00947 * @return Read/write reference to data. 00948 * @throw std::out_of_range If @a n is an invalid index. 00949 * 00950 * This function provides for safer data access. The parameter is first 00951 * checked that it is in the range of the deque. The function throws 00952 * out_of_range if the check fails. 00953 */ 00954 reference 00955 at(size_type __n) { _M_range_check(__n); return (*this)[__n]; } 00956 00957 /** 00958 * @brief Provides access to the data contained in the %deque. 00959 * @param n The index of the element for which data should be accessed. 00960 * @return Read-only (constant) reference to data. 00961 * @throw std::out_of_range If @a n is an invalid index. 00962 * 00963 * This function provides for safer data access. The parameter is first 00964 * checked that it is in the range of the deque. The function throws 00965 * out_of_range if the check fails. 00966 */ 00967 const_reference 00968 at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; } 00969 00970 /** 00971 * Returns a read/write reference to the data at the first element of the 00972 * %deque. 00973 */ 00974 reference 00975 front() { return *_M_start; } 00976 00977 /** 00978 * Returns a read-only (constant) reference to the data at the first 00979 * element of the %deque. 00980 */ 00981 const_reference 00982 front() const { return *_M_start; } 00983 00984 /** 00985 * Returns a read/write reference to the data at the last element of the 00986 * %deque. 00987 */ 00988 reference 00989 back() 00990 { 00991 iterator __tmp = _M_finish; 00992 --__tmp; 00993 return *__tmp; 00994 } 00995 00996 /** 00997 * Returns a read-only (constant) reference to the data at the last 00998 * element of the %deque. 00999 */ 01000 const_reference 01001 back() const 01002 { 01003 const_iterator __tmp = _M_finish; 01004 --__tmp; 01005 return *__tmp; 01006 } 01007 01008 // [23.2.1.2] modifiers 01009 /** 01010 * @brief Add data to the front of the %deque. 01011 * @param x Data to be added. 01012 * 01013 * This is a typical stack operation. The function creates an element at 01014 * the front of the %deque and assigns the given data to it. Due to the 01015 * nature of a %deque this operation can be done in constant time. 01016 */ 01017 void 01018 push_front(const value_type& __x) 01019 { 01020 if (_M_start._M_cur != _M_start._M_first) { 01021 _Construct(_M_start._M_cur - 1, __x); 01022 --_M_start._M_cur; 01023 } 01024 else 01025 _M_push_front_aux(__x); 01026 } 01027 01028 #ifdef _GLIBCPP_DEPRECATED 01029 /** 01030 * @brief Add data to the front of the %deque. 01031 * 01032 * This is a typical stack operation. The function creates a 01033 * default-constructed element at the front of the %deque. Due to the 01034 * nature of a %deque this operation can be done in constant time. You 01035 * should consider using push_front(value_type()) instead. 01036 * 01037 * @note This was deprecated in 3.2 and will be removed in 3.4. You must 01038 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see 01039 * c++config.h. 01040 */ 01041 void 01042 push_front() 01043 { 01044 if (_M_start._M_cur != _M_start._M_first) { 01045 _Construct(_M_start._M_cur - 1); 01046 --_M_start._M_cur; 01047 } 01048 else 01049 _M_push_front_aux(); 01050 } 01051 #endif 01052 01053 /** 01054 * @brief Add data to the end of the %deque. 01055 * @param x Data to be added. 01056 * 01057 * This is a typical stack operation. The function creates an element at 01058 * the end of the %deque and assigns the given data to it. Due to the 01059 * nature of a %deque this operation can be done in constant time. 01060 */ 01061 void 01062 push_back(const value_type& __x) 01063 { 01064 if (_M_finish._M_cur != _M_finish._M_last - 1) { 01065 _Construct(_M_finish._M_cur, __x); 01066 ++_M_finish._M_cur; 01067 } 01068 else 01069 _M_push_back_aux(__x); 01070 } 01071 01072 #ifdef _GLIBCPP_DEPRECATED 01073 /** 01074 * @brief Add data to the end of the %deque. 01075 * 01076 * This is a typical stack operation. The function creates a 01077 * default-constructed element at the end of the %deque. Due to the nature 01078 * of a %deque this operation can be done in constant time. You should 01079 * consider using push_back(value_type()) instead. 01080 * 01081 * @note This was deprecated in 3.2 and will be removed in 3.4. You must 01082 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see 01083 * c++config.h. 01084 */ 01085 void 01086 push_back() 01087 { 01088 if (_M_finish._M_cur != _M_finish._M_last - 1) { 01089 _Construct(_M_finish._M_cur); 01090 ++_M_finish._M_cur; 01091 } 01092 else 01093 _M_push_back_aux(); 01094 } 01095 #endif 01096 01097 /** 01098 * @brief Removes first element. 01099 * 01100 * This is a typical stack operation. It shrinks the %deque by one. 01101 * 01102 * Note that no data is returned, and if the first element's data is 01103 * needed, it should be retrieved before pop_front() is called. 01104 */ 01105 void 01106 pop_front() 01107 { 01108 if (_M_start._M_cur != _M_start._M_last - 1) { 01109 _Destroy(_M_start._M_cur); 01110 ++_M_start._M_cur; 01111 } 01112 else 01113 _M_pop_front_aux(); 01114 } 01115 01116 /** 01117 * @brief Removes last element. 01118 * 01119 * This is a typical stack operation. It shrinks the %deque by one. 01120 * 01121 * Note that no data is returned, and if the last element's data is 01122 * needed, it should be retrieved before pop_back() is called. 01123 */ 01124 void 01125 pop_back() 01126 { 01127 if (_M_finish._M_cur != _M_finish._M_first) { 01128 --_M_finish._M_cur; 01129 _Destroy(_M_finish._M_cur); 01130 } 01131 else 01132 _M_pop_back_aux(); 01133 } 01134 01135 /** 01136 * @brief Inserts given value into %deque before specified iterator. 01137 * @param position An iterator into the %deque. 01138 * @param x Data to be inserted. 01139 * @return An iterator that points to the inserted data. 01140 * 01141 * This function will insert a copy of the given value before the specified 01142 * location. 01143 */ 01144 iterator 01145 insert(iterator position, const value_type& __x); 01146 01147 #ifdef _GLIBCPP_DEPRECATED 01148 /** 01149 * @brief Inserts an element into the %deque. 01150 * @param position An iterator into the %deque. 01151 * @return An iterator that points to the inserted element. 01152 * 01153 * This function will insert a default-constructed element before the 01154 * specified location. You should consider using 01155 * insert(position,value_type()) instead. 01156 * 01157 * @note This was deprecated in 3.2 and will be removed in 3.4. You must 01158 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see 01159 * c++config.h. 01160 */ 01161 iterator 01162 insert(iterator __position) 01163 { return insert(__position, value_type()); } 01164 #endif 01165 01166 /** 01167 * @brief Inserts a number of copies of given data into the %deque. 01168 * @param position An iterator into the %deque. 01169 * @param n Number of elements to be inserted. 01170 * @param x Data to be inserted. 01171 * 01172 * This function will insert a specified number of copies of the given data 01173 * before the location specified by @a position. 01174 */ 01175 void 01176 insert(iterator __position, size_type __n, const value_type& __x) 01177 { _M_fill_insert(__position, __n, __x); } 01178 01179 /** 01180 * @brief Inserts a range into the %deque. 01181 * @param pos An iterator into the %deque. 01182 * @param first An input iterator. 01183 * @param last An input iterator. 01184 * 01185 * This function will insert copies of the data in the range [first,last) 01186 * into the %deque before the location specified by @a pos. This is 01187 * known as "range insert." 01188 */ 01189 template<typename _InputIterator> 01190 void 01191 insert(iterator __pos, _InputIterator __first, _InputIterator __last) 01192 { 01193 // Check whether it's an integral type. If so, it's not an iterator. 01194 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 01195 _M_insert_dispatch(__pos, __first, __last, _Integral()); 01196 } 01197 01198 /** 01199 * @brief Remove element at given position. 01200 * @param position Iterator pointing to element to be erased. 01201 * @return An iterator pointing to the next element (or end()). 01202 * 01203 * This function will erase the element at the given position and thus 01204 * shorten the %deque by one. 01205 * 01206 * The user is cautioned that 01207 * this function only erases the element, and that if the element is itself 01208 * a pointer, the pointed-to memory is not touched in any way. Managing 01209 * the pointer is the user's responsibilty. 01210 */ 01211 iterator 01212 erase(iterator __position); 01213 01214 /** 01215 * @brief Remove a range of elements. 01216 * @param first Iterator pointing to the first element to be erased. 01217 * @param last Iterator pointing to one past the last element to be 01218 * erased. 01219 * @return An iterator pointing to the element pointed to by @a last 01220 * prior to erasing (or end()). 01221 * 01222 * This function will erase the elements in the range [first,last) and 01223 * shorten the %deque accordingly. 01224 * 01225 * The user is cautioned that 01226 * this function only erases the elements, and that if the elements 01227 * themselves are pointers, the pointed-to memory is not touched in any 01228 * way. Managing the pointer is the user's responsibilty. 01229 */ 01230 iterator 01231 erase(iterator __first, iterator __last); 01232 01233 /** 01234 * @brief Swaps data with another %deque. 01235 * @param x A %deque of the same element and allocator types. 01236 * 01237 * This exchanges the elements between two deques in constant time. 01238 * (Four pointers, so it should be quite fast.) 01239 * Note that the global std::swap() function is specialized such that 01240 * std::swap(d1,d2) will feed to this function. 01241 */ 01242 void 01243 swap(deque& __x) 01244 { 01245 std::swap(_M_start, __x._M_start); 01246 std::swap(_M_finish, __x._M_finish); 01247 std::swap(_M_map, __x._M_map); 01248 std::swap(_M_map_size, __x._M_map_size); 01249 } 01250 01251 /** 01252 * Erases all the elements. Note that this function only erases the 01253 * elements, and that if the elements themselves are pointers, the 01254 * pointed-to memory is not touched in any way. Managing the pointer is 01255 * the user's responsibilty. 01256 */ 01257 void clear(); 01258 01259 protected: 01260 // Internal constructor functions follow. 01261 01262 // called by the range constructor to implement [23.1.1]/9 01263 template<typename _Integer> 01264 void 01265 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) 01266 { 01267 _M_initialize_map(__n); 01268 _M_fill_initialize(__x); 01269 } 01270 01271 // called by the range constructor to implement [23.1.1]/9 01272 template<typename _InputIter> 01273 void 01274 _M_initialize_dispatch(_InputIter __first, _InputIter __last, 01275 __false_type) 01276 { 01277 typedef typename iterator_traits<_InputIter>::iterator_category 01278 _IterCategory; 01279 _M_range_initialize(__first, __last, _IterCategory()); 01280 } 01281 01282 // called by the second initialize_dispatch above 01283 //@{ 01284 /** 01285 * @if maint 01286 * @brief Fills the deque with whatever is in [first,last). 01287 * @param first An input iterator. 01288 * @param last An input iterator. 01289 * @return Nothing. 01290 * 01291 * If the iterators are actually forward iterators (or better), then the 01292 * memory layout can be done all at once. Else we move forward using 01293 * push_back on each value from the iterator. 01294 * @endif 01295 */ 01296 template <typename _InputIterator> 01297 void 01298 _M_range_initialize(_InputIterator __first, _InputIterator __last, 01299 input_iterator_tag); 01300 01301 // called by the second initialize_dispatch above 01302 template <typename _ForwardIterator> 01303 void 01304 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last, 01305 forward_iterator_tag); 01306 //@} 01307 01308 /** 01309 * @if maint 01310 * @brief Fills the %deque with copies of value. 01311 * @param value Initial value. 01312 * @return Nothing. 01313 * @pre _M_start and _M_finish have already been initialized, but none of 01314 * the %deque's elements have yet been constructed. 01315 * 01316 * This function is called only when the user provides an explicit size 01317 * (with or without an explicit exemplar value). 01318 * @endif 01319 */ 01320 void 01321 _M_fill_initialize(const value_type& __value); 01322 01323 01324 // Internal assign functions follow. The *_aux functions do the actual 01325 // assignment work for the range versions. 01326 01327 // called by the range assign to implement [23.1.1]/9 01328 template<typename _Integer> 01329 void 01330 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 01331 { 01332 _M_fill_assign(static_cast<size_type>(__n), 01333 static_cast<value_type>(__val)); 01334 } 01335 01336 // called by the range assign to implement [23.1.1]/9 01337 template<typename _InputIter> 01338 void 01339 _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type) 01340 { 01341 typedef typename iterator_traits<_InputIter>::iterator_category 01342 _IterCategory; 01343 _M_assign_aux(__first, __last, _IterCategory()); 01344 } 01345 01346 // called by the second assign_dispatch above 01347 template <typename _InputIterator> 01348 void 01349 _M_assign_aux(_InputIterator __first, _InputIterator __last, 01350 input_iterator_tag); 01351 01352 // called by the second assign_dispatch above 01353 template <typename _ForwardIterator> 01354 void 01355 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, 01356 forward_iterator_tag) 01357 { 01358 size_type __len = distance(__first, __last); 01359 if (__len > size()) { 01360 _ForwardIterator __mid = __first; 01361 advance(__mid, size()); 01362 copy(__first, __mid, begin()); 01363 insert(end(), __mid, __last); 01364 } 01365 else 01366 erase(copy(__first, __last, begin()), end()); 01367 } 01368 01369 // Called by assign(n,t), and the range assign when it turns out to be the 01370 // same thing. 01371 void 01372 _M_fill_assign(size_type __n, const value_type& __val) 01373 { 01374 if (__n > size()) 01375 { 01376 fill(begin(), end(), __val); 01377 insert(end(), __n - size(), __val); 01378 } 01379 else 01380 { 01381 erase(begin() + __n, end()); 01382 fill(begin(), end(), __val); 01383 } 01384 } 01385 01386 01387 //@{ 01388 /** 01389 * @if maint 01390 * @brief Helper functions for push_* and pop_*. 01391 * @endif 01392 */ 01393 void _M_push_back_aux(const value_type&); 01394 void _M_push_front_aux(const value_type&); 01395 #ifdef _GLIBCPP_DEPRECATED 01396 void _M_push_back_aux(); 01397 void _M_push_front_aux(); 01398 #endif 01399 void _M_pop_back_aux(); 01400 void _M_pop_front_aux(); 01401 //@} 01402 01403 01404 // Internal insert functions follow. The *_aux functions do the actual 01405 // insertion work when all shortcuts fail. 01406 01407 // called by the range insert to implement [23.1.1]/9 01408 template<typename _Integer> 01409 void 01410 _M_insert_dispatch(iterator __pos, 01411 _Integer __n, _Integer __x, __true_type) 01412 { 01413 _M_fill_insert(__pos, static_cast<size_type>(__n), 01414 static_cast<value_type>(__x)); 01415 } 01416 01417 // called by the range insert to implement [23.1.1]/9 01418 template<typename _InputIterator> 01419 void 01420 _M_insert_dispatch(iterator __pos, 01421 _InputIterator __first, _InputIterator __last, 01422 __false_type) 01423 { 01424 typedef typename iterator_traits<_InputIterator>::iterator_category 01425 _IterCategory; 01426 _M_range_insert_aux(__pos, __first, __last, _IterCategory()); 01427 } 01428 01429 // called by the second insert_dispatch above 01430 template <typename _InputIterator> 01431 void 01432 _M_range_insert_aux(iterator __pos, _InputIterator __first, 01433 _InputIterator __last, input_iterator_tag); 01434 01435 // called by the second insert_dispatch above 01436 template <typename _ForwardIterator> 01437 void 01438 _M_range_insert_aux(iterator __pos, _ForwardIterator __first, 01439 _ForwardIterator __last, forward_iterator_tag); 01440 01441 // Called by insert(p,n,x), and the range insert when it turns out to be 01442 // the same thing. Can use fill functions in optimal situations, otherwise 01443 // passes off to insert_aux(p,n,x). 01444 void 01445 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x); 01446 01447 // called by insert(p,x) 01448 iterator 01449 _M_insert_aux(iterator __pos, const value_type& __x); 01450 01451 // called by insert(p,n,x) via fill_insert 01452 void 01453 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x); 01454 01455 // called by range_insert_aux for forward iterators 01456 template <typename _ForwardIterator> 01457 void 01458 _M_insert_aux(iterator __pos, 01459 _ForwardIterator __first, _ForwardIterator __last, 01460 size_type __n); 01461 01462 #ifdef _GLIBCPP_DEPRECATED 01463 // unused, see comment in implementation 01464 iterator _M_insert_aux(iterator __pos); 01465 #endif 01466 01467 //@{ 01468 /** 01469 * @if maint 01470 * @brief Memory-handling helpers for the previous internal insert 01471 * functions. 01472 * @endif 01473 */ 01474 iterator 01475 _M_reserve_elements_at_front(size_type __n) 01476 { 01477 size_type __vacancies = _M_start._M_cur - _M_start._M_first; 01478 if (__n > __vacancies) 01479 _M_new_elements_at_front(__n - __vacancies); 01480 return _M_start - difference_type(__n); 01481 } 01482 01483 iterator 01484 _M_reserve_elements_at_back(size_type __n) 01485 { 01486 size_type __vacancies = (_M_finish._M_last - _M_finish._M_cur) - 1; 01487 if (__n > __vacancies) 01488 _M_new_elements_at_back(__n - __vacancies); 01489 return _M_finish + difference_type(__n); 01490 } 01491 01492 void 01493 _M_new_elements_at_front(size_type __new_elements); 01494 01495 void 01496 _M_new_elements_at_back(size_type __new_elements); 01497 //@} 01498 01499 01500 //@{ 01501 /** 01502 * @if maint 01503 * @brief Memory-handling helpers for the major %map. 01504 * 01505 * Makes sure the _M_map has space for new nodes. Does not actually add 01506 * the nodes. Can invalidate _M_map pointers. (And consequently, %deque 01507 * iterators.) 01508 * @endif 01509 */ 01510 void 01511 _M_reserve_map_at_back (size_type __nodes_to_add = 1) 01512 { 01513 if (__nodes_to_add + 1 > _M_map_size - (_M_finish._M_node - _M_map)) 01514 _M_reallocate_map(__nodes_to_add, false); 01515 } 01516 01517 void 01518 _M_reserve_map_at_front (size_type __nodes_to_add = 1) 01519 { 01520 if (__nodes_to_add > size_type(_M_start._M_node - _M_map)) 01521 _M_reallocate_map(__nodes_to_add, true); 01522 } 01523 01524 void 01525 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front); 01526 //@} 01527 }; 01528 01529 01530 /** 01531 * @brief Deque equality comparison. 01532 * @param x A %deque. 01533 * @param y A %deque of the same type as @a x. 01534 * @return True iff the size and elements of the deques are equal. 01535 * 01536 * This is an equivalence relation. It is linear in the size of the 01537 * deques. Deques are considered equivalent if their sizes are equal, 01538 * and if corresponding elements compare equal. 01539 */ 01540 template <typename _Tp, typename _Alloc> 01541 inline bool operator==(const deque<_Tp, _Alloc>& __x, 01542 const deque<_Tp, _Alloc>& __y) 01543 { 01544 return __x.size() == __y.size() && 01545 equal(__x.begin(), __x.end(), __y.begin()); 01546 } 01547 01548 /** 01549 * @brief Deque ordering relation. 01550 * @param x A %deque. 01551 * @param y A %deque of the same type as @a x. 01552 * @return True iff @a x is lexographically less than @a y. 01553 * 01554 * This is a total ordering relation. It is linear in the size of the 01555 * deques. The elements must be comparable with @c <. 01556 * 01557 * See std::lexographical_compare() for how the determination is made. 01558 */ 01559 template <typename _Tp, typename _Alloc> 01560 inline bool operator<(const deque<_Tp, _Alloc>& __x, 01561 const deque<_Tp, _Alloc>& __y) 01562 { 01563 return lexicographical_compare(__x.begin(), __x.end(), 01564 __y.begin(), __y.end()); 01565 } 01566 01567 /// Based on operator== 01568 template <typename _Tp, typename _Alloc> 01569 inline bool operator!=(const deque<_Tp, _Alloc>& __x, 01570 const deque<_Tp, _Alloc>& __y) { 01571 return !(__x == __y); 01572 } 01573 01574 /// Based on operator< 01575 template <typename _Tp, typename _Alloc> 01576 inline bool operator>(const deque<_Tp, _Alloc>& __x, 01577 const deque<_Tp, _Alloc>& __y) { 01578 return __y < __x; 01579 } 01580 01581 /// Based on operator< 01582 template <typename _Tp, typename _Alloc> 01583 inline bool operator<=(const deque<_Tp, _Alloc>& __x, 01584 const deque<_Tp, _Alloc>& __y) { 01585 return !(__y < __x); 01586 } 01587 01588 /// Based on operator< 01589 template <typename _Tp, typename _Alloc> 01590 inline bool operator>=(const deque<_Tp, _Alloc>& __x, 01591 const deque<_Tp, _Alloc>& __y) { 01592 return !(__x < __y); 01593 } 01594 01595 /// See std::deque::swap(). 01596 template <typename _Tp, typename _Alloc> 01597 inline void swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y) 01598 { 01599 __x.swap(__y); 01600 } 01601 } // namespace std 01602 01603 #endif /* __GLIBCPP_INTERNAL_DEQUE_H */

Generated on Wed Aug 4 21:43:18 2004 for libstdc++-v3 Source by doxygen 1.3.8