00001 // List implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 2, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // You should have received a copy of the GNU General Public License along 00018 // with this library; see the file COPYING. If not, write to the Free 00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 00020 // USA. 00021 00022 // As a special exception, you may use this file as part of a free software 00023 // library without restriction. Specifically, if other files instantiate 00024 // templates or use macros or inline functions from this file, or you compile 00025 // this file and link it with other files to produce an executable, this 00026 // file does not by itself cause the resulting executable to be covered by 00027 // the GNU General Public License. This exception does not however 00028 // invalidate any other reasons why the executable file might be covered by 00029 // the GNU General Public License. 00030 00031 /* 00032 * 00033 * Copyright (c) 1994 00034 * Hewlett-Packard Company 00035 * 00036 * Permission to use, copy, modify, distribute and sell this software 00037 * and its documentation for any purpose is hereby granted without fee, 00038 * provided that the above copyright notice appear in all copies and 00039 * that both that copyright notice and this permission notice appear 00040 * in supporting documentation. Hewlett-Packard Company makes no 00041 * representations about the suitability of this software for any 00042 * purpose. It is provided "as is" without express or implied warranty. 00043 * 00044 * 00045 * Copyright (c) 1996,1997 00046 * Silicon Graphics Computer Systems, Inc. 00047 * 00048 * Permission to use, copy, modify, distribute and sell this software 00049 * and its documentation for any purpose is hereby granted without fee, 00050 * provided that the above copyright notice appear in all copies and 00051 * that both that copyright notice and this permission notice appear 00052 * in supporting documentation. Silicon Graphics makes no 00053 * representations about the suitability of this software for any 00054 * purpose. It is provided "as is" without express or implied warranty. 00055 */ 00056 00057 /** @file stl_list.h 00058 * This is an internal header file, included by other library headers. 00059 * You should not attempt to use it directly. 00060 */ 00061 00062 #ifndef _LIST_H 00063 #define _LIST_H 1 00064 00065 #include <bits/concept_check.h> 00066 00067 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD) 00068 00069 // Supporting structures are split into common and templated types; the 00070 // latter publicly inherits from the former in an effort to reduce code 00071 // duplication. This results in some "needless" static_cast'ing later on, 00072 // but it's all safe downcasting. 00073 00074 /// @if maint Common part of a node in the %list. @endif 00075 struct _List_node_base 00076 { 00077 _List_node_base* _M_next; ///< Self-explanatory 00078 _List_node_base* _M_prev; ///< Self-explanatory 00079 00080 static void 00081 swap(_List_node_base& __x, _List_node_base& __y); 00082 00083 void 00084 transfer(_List_node_base * const __first, 00085 _List_node_base * const __last); 00086 00087 void 00088 reverse(); 00089 00090 void 00091 hook(_List_node_base * const __position); 00092 00093 void 00094 unhook(); 00095 }; 00096 00097 /// @if maint An actual node in the %list. @endif 00098 template<typename _Tp> 00099 struct _List_node : public _List_node_base 00100 { 00101 _Tp _M_data; ///< User's data. 00102 }; 00103 00104 /** 00105 * @brief A list::iterator. 00106 * 00107 * @if maint 00108 * All the functions are op overloads. 00109 * @endif 00110 */ 00111 template<typename _Tp> 00112 struct _List_iterator 00113 { 00114 typedef _List_iterator<_Tp> _Self; 00115 typedef _List_node<_Tp> _Node; 00116 00117 typedef ptrdiff_t difference_type; 00118 typedef std::bidirectional_iterator_tag iterator_category; 00119 typedef _Tp value_type; 00120 typedef _Tp* pointer; 00121 typedef _Tp& reference; 00122 00123 _List_iterator() 00124 : _M_node() { } 00125 00126 explicit 00127 _List_iterator(_List_node_base* __x) 00128 : _M_node(__x) { } 00129 00130 // Must downcast from List_node_base to _List_node to get to _M_data. 00131 reference 00132 operator*() const 00133 { return static_cast<_Node*>(_M_node)->_M_data; } 00134 00135 pointer 00136 operator->() const 00137 { return &static_cast<_Node*>(_M_node)->_M_data; } 00138 00139 _Self& 00140 operator++() 00141 { 00142 _M_node = _M_node->_M_next; 00143 return *this; 00144 } 00145 00146 _Self 00147 operator++(int) 00148 { 00149 _Self __tmp = *this; 00150 _M_node = _M_node->_M_next; 00151 return __tmp; 00152 } 00153 00154 _Self& 00155 operator--() 00156 { 00157 _M_node = _M_node->_M_prev; 00158 return *this; 00159 } 00160 00161 _Self 00162 operator--(int) 00163 { 00164 _Self __tmp = *this; 00165 _M_node = _M_node->_M_prev; 00166 return __tmp; 00167 } 00168 00169 bool 00170 operator==(const _Self& __x) const 00171 { return _M_node == __x._M_node; } 00172 00173 bool 00174 operator!=(const _Self& __x) const 00175 { return _M_node != __x._M_node; } 00176 00177 // The only member points to the %list element. 00178 _List_node_base* _M_node; 00179 }; 00180 00181 /** 00182 * @brief A list::const_iterator. 00183 * 00184 * @if maint 00185 * All the functions are op overloads. 00186 * @endif 00187 */ 00188 template<typename _Tp> 00189 struct _List_const_iterator 00190 { 00191 typedef _List_const_iterator<_Tp> _Self; 00192 typedef const _List_node<_Tp> _Node; 00193 typedef _List_iterator<_Tp> iterator; 00194 00195 typedef ptrdiff_t difference_type; 00196 typedef std::bidirectional_iterator_tag iterator_category; 00197 typedef _Tp value_type; 00198 typedef const _Tp* pointer; 00199 typedef const _Tp& reference; 00200 00201 _List_const_iterator() 00202 : _M_node() { } 00203 00204 explicit 00205 _List_const_iterator(const _List_node_base* __x) 00206 : _M_node(__x) { } 00207 00208 _List_const_iterator(const iterator& __x) 00209 : _M_node(__x._M_node) { } 00210 00211 // Must downcast from List_node_base to _List_node to get to 00212 // _M_data. 00213 reference 00214 operator*() const 00215 { return static_cast<_Node*>(_M_node)->_M_data; } 00216 00217 pointer 00218 operator->() const 00219 { return &static_cast<_Node*>(_M_node)->_M_data; } 00220 00221 _Self& 00222 operator++() 00223 { 00224 _M_node = _M_node->_M_next; 00225 return *this; 00226 } 00227 00228 _Self 00229 operator++(int) 00230 { 00231 _Self __tmp = *this; 00232 _M_node = _M_node->_M_next; 00233 return __tmp; 00234 } 00235 00236 _Self& 00237 operator--() 00238 { 00239 _M_node = _M_node->_M_prev; 00240 return *this; 00241 } 00242 00243 _Self 00244 operator--(int) 00245 { 00246 _Self __tmp = *this; 00247 _M_node = _M_node->_M_prev; 00248 return __tmp; 00249 } 00250 00251 bool 00252 operator==(const _Self& __x) const 00253 { return _M_node == __x._M_node; } 00254 00255 bool 00256 operator!=(const _Self& __x) const 00257 { return _M_node != __x._M_node; } 00258 00259 // The only member points to the %list element. 00260 const _List_node_base* _M_node; 00261 }; 00262 00263 template<typename _Val> 00264 inline bool 00265 operator==(const _List_iterator<_Val>& __x, 00266 const _List_const_iterator<_Val>& __y) 00267 { return __x._M_node == __y._M_node; } 00268 00269 template<typename _Val> 00270 inline bool 00271 operator!=(const _List_iterator<_Val>& __x, 00272 const _List_const_iterator<_Val>& __y) 00273 { return __x._M_node != __y._M_node; } 00274 00275 00276 /** 00277 * @if maint 00278 * See bits/stl_deque.h's _Deque_base for an explanation. 00279 * @endif 00280 */ 00281 template<typename _Tp, typename _Alloc> 00282 class _List_base 00283 { 00284 protected: 00285 // NOTA BENE 00286 // The stored instance is not actually of "allocator_type"'s 00287 // type. Instead we rebind the type to 00288 // Allocator<List_node<Tp>>, which according to [20.1.5]/4 00289 // should probably be the same. List_node<Tp> is not the same 00290 // size as Tp (it's two pointers larger), and specializations on 00291 // Tp may go unused because List_node<Tp> is being bound 00292 // instead. 00293 // 00294 // We put this to the test in the constructors and in 00295 // get_allocator, where we use conversions between 00296 // allocator_type and _Node_alloc_type. The conversion is 00297 // required by table 32 in [20.1.5]. 00298 typedef typename _Alloc::template rebind<_List_node<_Tp> >::other 00299 _Node_alloc_type; 00300 00301 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type; 00302 00303 struct _List_impl 00304 : public _Node_alloc_type 00305 { 00306 _List_node_base _M_node; 00307 00308 _List_impl(const _Node_alloc_type& __a) 00309 : _Node_alloc_type(__a), _M_node() 00310 { } 00311 }; 00312 00313 _List_impl _M_impl; 00314 00315 _List_node<_Tp>* 00316 _M_get_node() 00317 { return _M_impl._Node_alloc_type::allocate(1); } 00318 00319 void 00320 _M_put_node(_List_node<_Tp>* __p) 00321 { _M_impl._Node_alloc_type::deallocate(__p, 1); } 00322 00323 public: 00324 typedef _Alloc allocator_type; 00325 00326 _Node_alloc_type& 00327 _M_get_Node_allocator() 00328 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); } 00329 00330 const _Node_alloc_type& 00331 _M_get_Node_allocator() const 00332 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); } 00333 00334 _Tp_alloc_type 00335 _M_get_Tp_allocator() const 00336 { return _Tp_alloc_type(_M_get_Node_allocator()); } 00337 00338 allocator_type 00339 get_allocator() const 00340 { return allocator_type(_M_get_Node_allocator()); } 00341 00342 _List_base(const allocator_type& __a) 00343 : _M_impl(__a) 00344 { _M_init(); } 00345 00346 // This is what actually destroys the list. 00347 ~_List_base() 00348 { _M_clear(); } 00349 00350 void 00351 _M_clear(); 00352 00353 void 00354 _M_init() 00355 { 00356 this->_M_impl._M_node._M_next = &this->_M_impl._M_node; 00357 this->_M_impl._M_node._M_prev = &this->_M_impl._M_node; 00358 } 00359 }; 00360 00361 /** 00362 * @brief A standard container with linear time access to elements, 00363 * and fixed time insertion/deletion at any point in the sequence. 00364 * 00365 * @ingroup Containers 00366 * @ingroup Sequences 00367 * 00368 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00369 * <a href="tables.html#66">reversible container</a>, and a 00370 * <a href="tables.html#67">sequence</a>, including the 00371 * <a href="tables.html#68">optional sequence requirements</a> with the 00372 * %exception of @c at and @c operator[]. 00373 * 00374 * This is a @e doubly @e linked %list. Traversal up and down the 00375 * %list requires linear time, but adding and removing elements (or 00376 * @e nodes) is done in constant time, regardless of where the 00377 * change takes place. Unlike std::vector and std::deque, 00378 * random-access iterators are not provided, so subscripting ( @c 00379 * [] ) access is not allowed. For algorithms which only need 00380 * sequential access, this lack makes no difference. 00381 * 00382 * Also unlike the other standard containers, std::list provides 00383 * specialized algorithms %unique to linked lists, such as 00384 * splicing, sorting, and in-place reversal. 00385 * 00386 * @if maint 00387 * A couple points on memory allocation for list<Tp>: 00388 * 00389 * First, we never actually allocate a Tp, we allocate 00390 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure 00391 * that after elements from %list<X,Alloc1> are spliced into 00392 * %list<X,Alloc2>, destroying the memory of the second %list is a 00393 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away. 00394 * 00395 * Second, a %list conceptually represented as 00396 * @code 00397 * A <---> B <---> C <---> D 00398 * @endcode 00399 * is actually circular; a link exists between A and D. The %list 00400 * class holds (as its only data member) a private list::iterator 00401 * pointing to @e D, not to @e A! To get to the head of the %list, 00402 * we start at the tail and move forward by one. When this member 00403 * iterator's next/previous pointers refer to itself, the %list is 00404 * %empty. @endif 00405 */ 00406 template<typename _Tp, typename _Alloc = std::allocator<_Tp> > 00407 class list : protected _List_base<_Tp, _Alloc> 00408 { 00409 // concept requirements 00410 typedef typename _Alloc::value_type _Alloc_value_type; 00411 __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 00412 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept) 00413 00414 typedef _List_base<_Tp, _Alloc> _Base; 00415 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type; 00416 00417 public: 00418 typedef _Tp value_type; 00419 typedef typename _Tp_alloc_type::pointer pointer; 00420 typedef typename _Tp_alloc_type::const_pointer const_pointer; 00421 typedef typename _Tp_alloc_type::reference reference; 00422 typedef typename _Tp_alloc_type::const_reference const_reference; 00423 typedef _List_iterator<_Tp> iterator; 00424 typedef _List_const_iterator<_Tp> const_iterator; 00425 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00426 typedef std::reverse_iterator<iterator> reverse_iterator; 00427 typedef size_t size_type; 00428 typedef ptrdiff_t difference_type; 00429 typedef _Alloc allocator_type; 00430 00431 protected: 00432 // Note that pointers-to-_Node's can be ctor-converted to 00433 // iterator types. 00434 typedef _List_node<_Tp> _Node; 00435 00436 using _Base::_M_impl; 00437 using _Base::_M_put_node; 00438 using _Base::_M_get_node; 00439 using _Base::_M_get_Tp_allocator; 00440 using _Base::_M_get_Node_allocator; 00441 00442 /** 00443 * @if maint 00444 * @param x An instance of user data. 00445 * 00446 * Allocates space for a new node and constructs a copy of @a x in it. 00447 * @endif 00448 */ 00449 _Node* 00450 _M_create_node(const value_type& __x) 00451 { 00452 _Node* __p = this->_M_get_node(); 00453 try 00454 { 00455 _M_get_Tp_allocator().construct(&__p->_M_data, __x); 00456 } 00457 catch(...) 00458 { 00459 _M_put_node(__p); 00460 __throw_exception_again; 00461 } 00462 return __p; 00463 } 00464 00465 public: 00466 // [23.2.2.1] construct/copy/destroy 00467 // (assign() and get_allocator() are also listed in this section) 00468 /** 00469 * @brief Default constructor creates no elements. 00470 */ 00471 explicit 00472 list(const allocator_type& __a = allocator_type()) 00473 : _Base(__a) { } 00474 00475 /** 00476 * @brief Create a %list with copies of an exemplar element. 00477 * @param n The number of elements to initially create. 00478 * @param value An element to copy. 00479 * 00480 * This constructor fills the %list with @a n copies of @a value. 00481 */ 00482 explicit 00483 list(size_type __n, const value_type& __value = value_type(), 00484 const allocator_type& __a = allocator_type()) 00485 : _Base(__a) 00486 { _M_fill_initialize(__n, __value); } 00487 00488 /** 00489 * @brief %List copy constructor. 00490 * @param x A %list of identical element and allocator types. 00491 * 00492 * The newly-created %list uses a copy of the allocation object used 00493 * by @a x. 00494 */ 00495 list(const list& __x) 00496 : _Base(__x._M_get_Node_allocator()) 00497 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); } 00498 00499 /** 00500 * @brief Builds a %list from a range. 00501 * @param first An input iterator. 00502 * @param last An input iterator. 00503 * 00504 * Create a %list consisting of copies of the elements from 00505 * [@a first,@a last). This is linear in N (where N is 00506 * distance(@a first,@a last)). 00507 */ 00508 template<typename _InputIterator> 00509 list(_InputIterator __first, _InputIterator __last, 00510 const allocator_type& __a = allocator_type()) 00511 : _Base(__a) 00512 { 00513 // Check whether it's an integral type. If so, it's not an iterator. 00514 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 00515 _M_initialize_dispatch(__first, __last, _Integral()); 00516 } 00517 00518 /** 00519 * No explicit dtor needed as the _Base dtor takes care of 00520 * things. The _Base dtor only erases the elements, and note 00521 * that if the elements themselves are pointers, the pointed-to 00522 * memory is not touched in any way. Managing the pointer is 00523 * the user's responsibilty. 00524 */ 00525 00526 /** 00527 * @brief %List assignment operator. 00528 * @param x A %list of identical element and allocator types. 00529 * 00530 * All the elements of @a x are copied, but unlike the copy 00531 * constructor, the allocator object is not copied. 00532 */ 00533 list& 00534 operator=(const list& __x); 00535 00536 /** 00537 * @brief Assigns a given value to a %list. 00538 * @param n Number of elements to be assigned. 00539 * @param val Value to be assigned. 00540 * 00541 * This function fills a %list with @a n copies of the given 00542 * value. Note that the assignment completely changes the %list 00543 * and that the resulting %list's size is the same as the number 00544 * of elements assigned. Old data may be lost. 00545 */ 00546 void 00547 assign(size_type __n, const value_type& __val) 00548 { _M_fill_assign(__n, __val); } 00549 00550 /** 00551 * @brief Assigns a range to a %list. 00552 * @param first An input iterator. 00553 * @param last An input iterator. 00554 * 00555 * This function fills a %list with copies of the elements in the 00556 * range [@a first,@a last). 00557 * 00558 * Note that the assignment completely changes the %list and 00559 * that the resulting %list's size is the same as the number of 00560 * elements assigned. Old data may be lost. 00561 */ 00562 template<typename _InputIterator> 00563 void 00564 assign(_InputIterator __first, _InputIterator __last) 00565 { 00566 // Check whether it's an integral type. If so, it's not an iterator. 00567 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 00568 _M_assign_dispatch(__first, __last, _Integral()); 00569 } 00570 00571 /// Get a copy of the memory allocation object. 00572 allocator_type 00573 get_allocator() const 00574 { return _Base::get_allocator(); } 00575 00576 // iterators 00577 /** 00578 * Returns a read/write iterator that points to the first element in the 00579 * %list. Iteration is done in ordinary element order. 00580 */ 00581 iterator 00582 begin() 00583 { return iterator(this->_M_impl._M_node._M_next); } 00584 00585 /** 00586 * Returns a read-only (constant) iterator that points to the 00587 * first element in the %list. Iteration is done in ordinary 00588 * element order. 00589 */ 00590 const_iterator 00591 begin() const 00592 { return const_iterator(this->_M_impl._M_node._M_next); } 00593 00594 /** 00595 * Returns a read/write iterator that points one past the last 00596 * element in the %list. Iteration is done in ordinary element 00597 * order. 00598 */ 00599 iterator 00600 end() 00601 { return iterator(&this->_M_impl._M_node); } 00602 00603 /** 00604 * Returns a read-only (constant) iterator that points one past 00605 * the last element in the %list. Iteration is done in ordinary 00606 * element order. 00607 */ 00608 const_iterator 00609 end() const 00610 { return const_iterator(&this->_M_impl._M_node); } 00611 00612 /** 00613 * Returns a read/write reverse iterator that points to the last 00614 * element in the %list. Iteration is done in reverse element 00615 * order. 00616 */ 00617 reverse_iterator 00618 rbegin() 00619 { return reverse_iterator(end()); } 00620 00621 /** 00622 * Returns a read-only (constant) reverse iterator that points to 00623 * the last element in the %list. Iteration is done in reverse 00624 * element order. 00625 */ 00626 const_reverse_iterator 00627 rbegin() const 00628 { return const_reverse_iterator(end()); } 00629 00630 /** 00631 * Returns a read/write reverse iterator that points to one 00632 * before the first element in the %list. Iteration is done in 00633 * reverse element order. 00634 */ 00635 reverse_iterator 00636 rend() 00637 { return reverse_iterator(begin()); } 00638 00639 /** 00640 * Returns a read-only (constant) reverse iterator that points to one 00641 * before the first element in the %list. Iteration is done in reverse 00642 * element order. 00643 */ 00644 const_reverse_iterator 00645 rend() const 00646 { return const_reverse_iterator(begin()); } 00647 00648 // [23.2.2.2] capacity 00649 /** 00650 * Returns true if the %list is empty. (Thus begin() would equal 00651 * end().) 00652 */ 00653 bool 00654 empty() const 00655 { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; } 00656 00657 /** Returns the number of elements in the %list. */ 00658 size_type 00659 size() const 00660 { return std::distance(begin(), end()); } 00661 00662 /** Returns the size() of the largest possible %list. */ 00663 size_type 00664 max_size() const 00665 { return _M_get_Tp_allocator().max_size(); } 00666 00667 /** 00668 * @brief Resizes the %list to the specified number of elements. 00669 * @param new_size Number of elements the %list should contain. 00670 * @param x Data with which new elements should be populated. 00671 * 00672 * This function will %resize the %list to the specified number 00673 * of elements. If the number is smaller than the %list's 00674 * current size the %list is truncated, otherwise the %list is 00675 * extended and new elements are populated with given data. 00676 */ 00677 void 00678 resize(size_type __new_size, value_type __x = value_type()); 00679 00680 // element access 00681 /** 00682 * Returns a read/write reference to the data at the first 00683 * element of the %list. 00684 */ 00685 reference 00686 front() 00687 { return *begin(); } 00688 00689 /** 00690 * Returns a read-only (constant) reference to the data at the first 00691 * element of the %list. 00692 */ 00693 const_reference 00694 front() const 00695 { return *begin(); } 00696 00697 /** 00698 * Returns a read/write reference to the data at the last element 00699 * of the %list. 00700 */ 00701 reference 00702 back() 00703 { 00704 iterator __tmp = end(); 00705 --__tmp; 00706 return *__tmp; 00707 } 00708 00709 /** 00710 * Returns a read-only (constant) reference to the data at the last 00711 * element of the %list. 00712 */ 00713 const_reference 00714 back() const 00715 { 00716 const_iterator __tmp = end(); 00717 --__tmp; 00718 return *__tmp; 00719 } 00720 00721 // [23.2.2.3] modifiers 00722 /** 00723 * @brief Add data to the front of the %list. 00724 * @param x Data to be added. 00725 * 00726 * This is a typical stack operation. The function creates an 00727 * element at the front of the %list and assigns the given data 00728 * to it. Due to the nature of a %list this operation can be 00729 * done in constant time, and does not invalidate iterators and 00730 * references. 00731 */ 00732 void 00733 push_front(const value_type& __x) 00734 { this->_M_insert(begin(), __x); } 00735 00736 /** 00737 * @brief Removes first element. 00738 * 00739 * This is a typical stack operation. It shrinks the %list by 00740 * one. Due to the nature of a %list this operation can be done 00741 * in constant time, and only invalidates iterators/references to 00742 * the element being removed. 00743 * 00744 * Note that no data is returned, and if the first element's data 00745 * is needed, it should be retrieved before pop_front() is 00746 * called. 00747 */ 00748 void 00749 pop_front() 00750 { this->_M_erase(begin()); } 00751 00752 /** 00753 * @brief Add data to the end of the %list. 00754 * @param x Data to be added. 00755 * 00756 * This is a typical stack operation. The function creates an 00757 * element at the end of the %list and assigns the given data to 00758 * it. Due to the nature of a %list this operation can be done 00759 * in constant time, and does not invalidate iterators and 00760 * references. 00761 */ 00762 void 00763 push_back(const value_type& __x) 00764 { this->_M_insert(end(), __x); } 00765 00766 /** 00767 * @brief Removes last element. 00768 * 00769 * This is a typical stack operation. It shrinks the %list by 00770 * one. Due to the nature of a %list this operation can be done 00771 * in constant time, and only invalidates iterators/references to 00772 * the element being removed. 00773 * 00774 * Note that no data is returned, and if the last element's data 00775 * is needed, it should be retrieved before pop_back() is called. 00776 */ 00777 void 00778 pop_back() 00779 { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); } 00780 00781 /** 00782 * @brief Inserts given value into %list before specified iterator. 00783 * @param position An iterator into the %list. 00784 * @param x Data to be inserted. 00785 * @return An iterator that points to the inserted data. 00786 * 00787 * This function will insert a copy of the given value before 00788 * the specified location. Due to the nature of a %list this 00789 * operation can be done in constant time, and does not 00790 * invalidate iterators and references. 00791 */ 00792 iterator 00793 insert(iterator __position, const value_type& __x); 00794 00795 /** 00796 * @brief Inserts a number of copies of given data into the %list. 00797 * @param position An iterator into the %list. 00798 * @param n Number of elements to be inserted. 00799 * @param x Data to be inserted. 00800 * 00801 * This function will insert a specified number of copies of the 00802 * given data before the location specified by @a position. 00803 * 00804 * This operation is linear in the number of elements inserted and 00805 * does not invalidate iterators and references. 00806 */ 00807 void 00808 insert(iterator __position, size_type __n, const value_type& __x) 00809 { 00810 list __tmp(__n, __x, _M_get_Node_allocator()); 00811 splice(__position, __tmp); 00812 } 00813 00814 /** 00815 * @brief Inserts a range into the %list. 00816 * @param position An iterator into the %list. 00817 * @param first An input iterator. 00818 * @param last An input iterator. 00819 * 00820 * This function will insert copies of the data in the range [@a 00821 * first,@a last) into the %list before the location specified by 00822 * @a position. 00823 * 00824 * This operation is linear in the number of elements inserted and 00825 * does not invalidate iterators and references. 00826 */ 00827 template<typename _InputIterator> 00828 void 00829 insert(iterator __position, _InputIterator __first, 00830 _InputIterator __last) 00831 { 00832 list __tmp(__first, __last, _M_get_Node_allocator()); 00833 splice(__position, __tmp); 00834 } 00835 00836 /** 00837 * @brief Remove element at given position. 00838 * @param position Iterator pointing to element to be erased. 00839 * @return An iterator pointing to the next element (or end()). 00840 * 00841 * This function will erase the element at the given position and thus 00842 * shorten the %list by one. 00843 * 00844 * Due to the nature of a %list this operation can be done in 00845 * constant time, and only invalidates iterators/references to 00846 * the element being removed. The user is also cautioned that 00847 * this function only erases the element, and that if the element 00848 * is itself a pointer, the pointed-to memory is not touched in 00849 * any way. Managing the pointer is the user's responsibilty. 00850 */ 00851 iterator 00852 erase(iterator __position); 00853 00854 /** 00855 * @brief Remove a range of elements. 00856 * @param first Iterator pointing to the first element to be erased. 00857 * @param last Iterator pointing to one past the last element to be 00858 * erased. 00859 * @return An iterator pointing to the element pointed to by @a last 00860 * prior to erasing (or end()). 00861 * 00862 * This function will erase the elements in the range @a 00863 * [first,last) and shorten the %list accordingly. 00864 * 00865 * This operation is linear time in the size of the range and only 00866 * invalidates iterators/references to the element being removed. 00867 * The user is also cautioned that this function only erases the 00868 * elements, and that if the elements themselves are pointers, the 00869 * pointed-to memory is not touched in any way. Managing the pointer 00870 * is the user's responsibilty. 00871 */ 00872 iterator 00873 erase(iterator __first, iterator __last) 00874 { 00875 while (__first != __last) 00876 __first = erase(__first); 00877 return __last; 00878 } 00879 00880 /** 00881 * @brief Swaps data with another %list. 00882 * @param x A %list of the same element and allocator types. 00883 * 00884 * This exchanges the elements between two lists in constant 00885 * time. Note that the global std::swap() function is 00886 * specialized such that std::swap(l1,l2) will feed to this 00887 * function. 00888 */ 00889 void 00890 swap(list& __x) 00891 { 00892 _List_node_base::swap(this->_M_impl._M_node, __x._M_impl._M_node); 00893 00894 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00895 // 431. Swapping containers with unequal allocators. 00896 std::__alloc_swap<typename _Base::_Node_alloc_type>:: 00897 _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator()); 00898 } 00899 00900 /** 00901 * Erases all the elements. Note that this function only erases 00902 * the elements, and that if the elements themselves are 00903 * pointers, the pointed-to memory is not touched in any way. 00904 * Managing the pointer is the user's responsibilty. 00905 */ 00906 void 00907 clear() 00908 { 00909 _Base::_M_clear(); 00910 _Base::_M_init(); 00911 } 00912 00913 // [23.2.2.4] list operations 00914 /** 00915 * @brief Insert contents of another %list. 00916 * @param position Iterator referencing the element to insert before. 00917 * @param x Source list. 00918 * 00919 * The elements of @a x are inserted in constant time in front of 00920 * the element referenced by @a position. @a x becomes an empty 00921 * list. 00922 * 00923 * Requires this != @a x. 00924 */ 00925 void 00926 splice(iterator __position, list& __x) 00927 { 00928 if (!__x.empty()) 00929 { 00930 _M_check_equal_allocators(__x); 00931 00932 this->_M_transfer(__position, __x.begin(), __x.end()); 00933 } 00934 } 00935 00936 /** 00937 * @brief Insert element from another %list. 00938 * @param position Iterator referencing the element to insert before. 00939 * @param x Source list. 00940 * @param i Iterator referencing the element to move. 00941 * 00942 * Removes the element in list @a x referenced by @a i and 00943 * inserts it into the current list before @a position. 00944 */ 00945 void 00946 splice(iterator __position, list& __x, iterator __i) 00947 { 00948 iterator __j = __i; 00949 ++__j; 00950 if (__position == __i || __position == __j) 00951 return; 00952 00953 if (this != &__x) 00954 _M_check_equal_allocators(__x); 00955 00956 this->_M_transfer(__position, __i, __j); 00957 } 00958 00959 /** 00960 * @brief Insert range from another %list. 00961 * @param position Iterator referencing the element to insert before. 00962 * @param x Source list. 00963 * @param first Iterator referencing the start of range in x. 00964 * @param last Iterator referencing the end of range in x. 00965 * 00966 * Removes elements in the range [first,last) and inserts them 00967 * before @a position in constant time. 00968 * 00969 * Undefined if @a position is in [first,last). 00970 */ 00971 void 00972 splice(iterator __position, list& __x, iterator __first, iterator __last) 00973 { 00974 if (__first != __last) 00975 { 00976 if (this != &__x) 00977 _M_check_equal_allocators(__x); 00978 00979 this->_M_transfer(__position, __first, __last); 00980 } 00981 } 00982 00983 /** 00984 * @brief Remove all elements equal to value. 00985 * @param value The value to remove. 00986 * 00987 * Removes every element in the list equal to @a value. 00988 * Remaining elements stay in list order. Note that this 00989 * function only erases the elements, and that if the elements 00990 * themselves are pointers, the pointed-to memory is not 00991 * touched in any way. Managing the pointer is the user's 00992 * responsibilty. 00993 */ 00994 void 00995 remove(const _Tp& __value); 00996 00997 /** 00998 * @brief Remove all elements satisfying a predicate. 00999 * @param Predicate Unary predicate function or object. 01000 * 01001 * Removes every element in the list for which the predicate 01002 * returns true. Remaining elements stay in list order. Note 01003 * that this function only erases the elements, and that if the 01004 * elements themselves are pointers, the pointed-to memory is 01005 * not touched in any way. Managing the pointer is the user's 01006 * responsibilty. 01007 */ 01008 template<typename _Predicate> 01009 void 01010 remove_if(_Predicate); 01011 01012 /** 01013 * @brief Remove consecutive duplicate elements. 01014 * 01015 * For each consecutive set of elements with the same value, 01016 * remove all but the first one. Remaining elements stay in 01017 * list order. Note that this function only erases the 01018 * elements, and that if the elements themselves are pointers, 01019 * the pointed-to memory is not touched in any way. Managing 01020 * the pointer is the user's responsibilty. 01021 */ 01022 void 01023 unique(); 01024 01025 /** 01026 * @brief Remove consecutive elements satisfying a predicate. 01027 * @param BinaryPredicate Binary predicate function or object. 01028 * 01029 * For each consecutive set of elements [first,last) that 01030 * satisfy predicate(first,i) where i is an iterator in 01031 * [first,last), remove all but the first one. Remaining 01032 * elements stay in list order. Note that this function only 01033 * erases the elements, and that if the elements themselves are 01034 * pointers, the pointed-to memory is not touched in any way. 01035 * Managing the pointer is the user's responsibilty. 01036 */ 01037 template<typename _BinaryPredicate> 01038 void 01039 unique(_BinaryPredicate); 01040 01041 /** 01042 * @brief Merge sorted lists. 01043 * @param x Sorted list to merge. 01044 * 01045 * Assumes that both @a x and this list are sorted according to 01046 * operator<(). Merges elements of @a x into this list in 01047 * sorted order, leaving @a x empty when complete. Elements in 01048 * this list precede elements in @a x that are equal. 01049 */ 01050 void 01051 merge(list& __x); 01052 01053 /** 01054 * @brief Merge sorted lists according to comparison function. 01055 * @param x Sorted list to merge. 01056 * @param StrictWeakOrdering Comparison function definining 01057 * sort order. 01058 * 01059 * Assumes that both @a x and this list are sorted according to 01060 * StrictWeakOrdering. Merges elements of @a x into this list 01061 * in sorted order, leaving @a x empty when complete. Elements 01062 * in this list precede elements in @a x that are equivalent 01063 * according to StrictWeakOrdering(). 01064 */ 01065 template<typename _StrictWeakOrdering> 01066 void 01067 merge(list&, _StrictWeakOrdering); 01068 01069 /** 01070 * @brief Reverse the elements in list. 01071 * 01072 * Reverse the order of elements in the list in linear time. 01073 */ 01074 void 01075 reverse() 01076 { this->_M_impl._M_node.reverse(); } 01077 01078 /** 01079 * @brief Sort the elements. 01080 * 01081 * Sorts the elements of this list in NlogN time. Equivalent 01082 * elements remain in list order. 01083 */ 01084 void 01085 sort(); 01086 01087 /** 01088 * @brief Sort the elements according to comparison function. 01089 * 01090 * Sorts the elements of this list in NlogN time. Equivalent 01091 * elements remain in list order. 01092 */ 01093 template<typename _StrictWeakOrdering> 01094 void 01095 sort(_StrictWeakOrdering); 01096 01097 protected: 01098 // Internal constructor functions follow. 01099 01100 // Called by the range constructor to implement [23.1.1]/9 01101 template<typename _Integer> 01102 void 01103 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) 01104 { 01105 _M_fill_initialize(static_cast<size_type>(__n), 01106 static_cast<value_type>(__x)); 01107 } 01108 01109 // Called by the range constructor to implement [23.1.1]/9 01110 template<typename _InputIterator> 01111 void 01112 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last, 01113 __false_type) 01114 { 01115 for (; __first != __last; ++__first) 01116 push_back(*__first); 01117 } 01118 01119 // Called by list(n,v,a), and the range constructor when it turns out 01120 // to be the same thing. 01121 void 01122 _M_fill_initialize(size_type __n, const value_type& __x) 01123 { 01124 for (; __n > 0; --__n) 01125 push_back(__x); 01126 } 01127 01128 01129 // Internal assign functions follow. 01130 01131 // Called by the range assign to implement [23.1.1]/9 01132 template<typename _Integer> 01133 void 01134 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 01135 { 01136 _M_fill_assign(static_cast<size_type>(__n), 01137 static_cast<value_type>(__val)); 01138 } 01139 01140 // Called by the range assign to implement [23.1.1]/9 01141 template<typename _InputIterator> 01142 void 01143 _M_assign_dispatch(_InputIterator __first, _InputIterator __last, 01144 __false_type); 01145 01146 // Called by assign(n,t), and the range assign when it turns out 01147 // to be the same thing. 01148 void 01149 _M_fill_assign(size_type __n, const value_type& __val); 01150 01151 01152 // Moves the elements from [first,last) before position. 01153 void 01154 _M_transfer(iterator __position, iterator __first, iterator __last) 01155 { __position._M_node->transfer(__first._M_node, __last._M_node); } 01156 01157 // Inserts new element at position given and with value given. 01158 void 01159 _M_insert(iterator __position, const value_type& __x) 01160 { 01161 _Node* __tmp = _M_create_node(__x); 01162 __tmp->hook(__position._M_node); 01163 } 01164 01165 // Erases element at position given. 01166 void 01167 _M_erase(iterator __position) 01168 { 01169 __position._M_node->unhook(); 01170 _Node* __n = static_cast<_Node*>(__position._M_node); 01171 _M_get_Tp_allocator().destroy(&__n->_M_data); 01172 _M_put_node(__n); 01173 } 01174 01175 // To implement the splice (and merge) bits of N1599. 01176 void 01177 _M_check_equal_allocators(list& __x) 01178 { 01179 if (_M_get_Node_allocator() != __x._M_get_Node_allocator()) 01180 __throw_runtime_error(__N("list::_M_check_equal_allocators")); 01181 } 01182 }; 01183 01184 /** 01185 * @brief List equality comparison. 01186 * @param x A %list. 01187 * @param y A %list of the same type as @a x. 01188 * @return True iff the size and elements of the lists are equal. 01189 * 01190 * This is an equivalence relation. It is linear in the size of 01191 * the lists. Lists are considered equivalent if their sizes are 01192 * equal, and if corresponding elements compare equal. 01193 */ 01194 template<typename _Tp, typename _Alloc> 01195 inline bool 01196 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 01197 { 01198 typedef typename list<_Tp, _Alloc>::const_iterator const_iterator; 01199 const_iterator __end1 = __x.end(); 01200 const_iterator __end2 = __y.end(); 01201 01202 const_iterator __i1 = __x.begin(); 01203 const_iterator __i2 = __y.begin(); 01204 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) 01205 { 01206 ++__i1; 01207 ++__i2; 01208 } 01209 return __i1 == __end1 && __i2 == __end2; 01210 } 01211 01212 /** 01213 * @brief List ordering relation. 01214 * @param x A %list. 01215 * @param y A %list of the same type as @a x. 01216 * @return True iff @a x is lexicographically less than @a y. 01217 * 01218 * This is a total ordering relation. It is linear in the size of the 01219 * lists. The elements must be comparable with @c <. 01220 * 01221 * See std::lexicographical_compare() for how the determination is made. 01222 */ 01223 template<typename _Tp, typename _Alloc> 01224 inline bool 01225 operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 01226 { return std::lexicographical_compare(__x.begin(), __x.end(), 01227 __y.begin(), __y.end()); } 01228 01229 /// Based on operator== 01230 template<typename _Tp, typename _Alloc> 01231 inline bool 01232 operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 01233 { return !(__x == __y); } 01234 01235 /// Based on operator< 01236 template<typename _Tp, typename _Alloc> 01237 inline bool 01238 operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 01239 { return __y < __x; } 01240 01241 /// Based on operator< 01242 template<typename _Tp, typename _Alloc> 01243 inline bool 01244 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 01245 { return !(__y < __x); } 01246 01247 /// Based on operator< 01248 template<typename _Tp, typename _Alloc> 01249 inline bool 01250 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 01251 { return !(__x < __y); } 01252 01253 /// See std::list::swap(). 01254 template<typename _Tp, typename _Alloc> 01255 inline void 01256 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y) 01257 { __x.swap(__y); } 01258 01259 _GLIBCXX_END_NESTED_NAMESPACE 01260 01261 #endif /* _LIST_H */ 01262