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