stl_deque.h

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

Generated on Sun Sep 12 15:49:59 2004 for libstdc++ source by doxygen 1.3.8