stl_list.h

Go to the documentation of this file.
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 _List_node_base _M_node; 00300 _List_impl (const _Node_Alloc_type& __a) 00301 : _Node_Alloc_type(__a) 00302 { } 00303 }; 00304 00305 _List_impl _M_impl; 00306 00307 _List_node<_Tp>* 00308 _M_get_node() 00309 { return _M_impl._Node_Alloc_type::allocate(1); } 00310 00311 void 00312 _M_put_node(_List_node<_Tp>* __p) 00313 { _M_impl._Node_Alloc_type::deallocate(__p, 1); } 00314 00315 public: 00316 typedef _Alloc allocator_type; 00317 00318 allocator_type 00319 get_allocator() const 00320 { return allocator_type(*static_cast<const _Node_Alloc_type*>(&this->_M_impl)); } 00321 00322 _List_base(const allocator_type& __a) 00323 : _M_impl(__a) 00324 { _M_init(); } 00325 00326 // This is what actually destroys the list. 00327 ~_List_base() 00328 { _M_clear(); } 00329 00330 void 00331 _M_clear(); 00332 00333 void 00334 _M_init() 00335 { 00336 this->_M_impl._M_node._M_next = &this->_M_impl._M_node; 00337 this->_M_impl._M_node._M_prev = &this->_M_impl._M_node; 00338 } 00339 }; 00340 00341 /** 00342 * @brief A standard container with linear time access to elements, 00343 * and fixed time insertion/deletion at any point in the sequence. 00344 * 00345 * @ingroup Containers 00346 * @ingroup Sequences 00347 * 00348 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00349 * <a href="tables.html#66">reversible container</a>, and a 00350 * <a href="tables.html#67">sequence</a>, including the 00351 * <a href="tables.html#68">optional sequence requirements</a> with the 00352 * %exception of @c at and @c operator[]. 00353 * 00354 * This is a @e doubly @e linked %list. Traversal up and down the 00355 * %list requires linear time, but adding and removing elements (or 00356 * @e nodes) is done in constant time, regardless of where the 00357 * change takes place. Unlike std::vector and std::deque, 00358 * random-access iterators are not provided, so subscripting ( @c 00359 * [] ) access is not allowed. For algorithms which only need 00360 * sequential access, this lack makes no difference. 00361 * 00362 * Also unlike the other standard containers, std::list provides 00363 * specialized algorithms %unique to linked lists, such as 00364 * splicing, sorting, and in-place reversal. 00365 * 00366 * @if maint 00367 * A couple points on memory allocation for list<Tp>: 00368 * 00369 * First, we never actually allocate a Tp, we allocate 00370 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure 00371 * that after elements from %list<X,Alloc1> are spliced into 00372 * %list<X,Alloc2>, destroying the memory of the second %list is a 00373 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away. 00374 * 00375 * Second, a %list conceptually represented as 00376 * @code 00377 * A <---> B <---> C <---> D 00378 * @endcode 00379 * is actually circular; a link exists between A and D. The %list 00380 * class holds (as its only data member) a private list::iterator 00381 * pointing to @e D, not to @e A! To get to the head of the %list, 00382 * we start at the tail and move forward by one. When this member 00383 * iterator's next/previous pointers refer to itself, the %list is 00384 * %empty. @endif 00385 */ 00386 template<typename _Tp, typename _Alloc = allocator<_Tp> > 00387 class list : protected _List_base<_Tp, _Alloc> 00388 { 00389 // concept requirements 00390 __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 00391 00392 typedef _List_base<_Tp, _Alloc> _Base; 00393 00394 public: 00395 typedef _Tp value_type; 00396 typedef typename _Alloc::pointer pointer; 00397 typedef typename _Alloc::const_pointer const_pointer; 00398 typedef typename _Alloc::reference reference; 00399 typedef typename _Alloc::const_reference const_reference; 00400 typedef _List_iterator<_Tp> iterator; 00401 typedef _List_const_iterator<_Tp> const_iterator; 00402 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00403 typedef std::reverse_iterator<iterator> reverse_iterator; 00404 typedef size_t size_type; 00405 typedef ptrdiff_t difference_type; 00406 typedef typename _Base::allocator_type allocator_type; 00407 00408 protected: 00409 // Note that pointers-to-_Node's can be ctor-converted to 00410 // iterator types. 00411 typedef _List_node<_Tp> _Node; 00412 00413 /** @if maint 00414 * One data member plus two memory-handling functions. If the 00415 * _Alloc type requires separate instances, then one of those 00416 * will also be included, accumulated from the topmost parent. 00417 * @endif 00418 */ 00419 using _Base::_M_impl; 00420 using _Base::_M_put_node; 00421 using _Base::_M_get_node; 00422 00423 /** 00424 * @if maint 00425 * @param x An instance of user data. 00426 * 00427 * Allocates space for a new node and constructs a copy of @a x in it. 00428 * @endif 00429 */ 00430 _Node* 00431 _M_create_node(const value_type& __x) 00432 { 00433 _Node* __p = this->_M_get_node(); 00434 try 00435 { 00436 std::_Construct(&__p->_M_data, __x); 00437 } 00438 catch(...) 00439 { 00440 _M_put_node(__p); 00441 __throw_exception_again; 00442 } 00443 return __p; 00444 } 00445 00446 /** 00447 * @if maint 00448 * Allocates space for a new node and default-constructs a new 00449 * instance of @c value_type in it. 00450 * @endif 00451 */ 00452 _Node* 00453 _M_create_node() 00454 { 00455 _Node* __p = this->_M_get_node(); 00456 try 00457 { 00458 std::_Construct(&__p->_M_data); 00459 } 00460 catch(...) 00461 { 00462 _M_put_node(__p); 00463 __throw_exception_again; 00464 } 00465 return __p; 00466 } 00467 00468 public: 00469 // [23.2.2.1] construct/copy/destroy 00470 // (assign() and get_allocator() are also listed in this section) 00471 /** 00472 * @brief Default constructor creates no elements. 00473 */ 00474 explicit 00475 list(const allocator_type& __a = allocator_type()) 00476 : _Base(__a) { } 00477 00478 /** 00479 * @brief Create a %list with copies of an exemplar element. 00480 * @param n The number of elements to initially create. 00481 * @param value An element to copy. 00482 * 00483 * This constructor fills the %list with @a n copies of @a value. 00484 */ 00485 list(size_type __n, const value_type& __value, 00486 const allocator_type& __a = allocator_type()) 00487 : _Base(__a) 00488 { this->insert(begin(), __n, __value); } 00489 00490 /** 00491 * @brief Create a %list with default elements. 00492 * @param n The number of elements to initially create. 00493 * 00494 * This constructor fills the %list with @a n copies of a 00495 * default-constructed element. 00496 */ 00497 explicit 00498 list(size_type __n) 00499 : _Base(allocator_type()) 00500 { this->insert(begin(), __n, value_type()); } 00501 00502 /** 00503 * @brief %List copy constructor. 00504 * @param x A %list of identical element and allocator types. 00505 * 00506 * The newly-created %list uses a copy of the allocation object used 00507 * by @a x. 00508 */ 00509 list(const list& __x) 00510 : _Base(__x.get_allocator()) 00511 { this->insert(begin(), __x.begin(), __x.end()); } 00512 00513 /** 00514 * @brief Builds a %list from a range. 00515 * @param first An input iterator. 00516 * @param last An input iterator. 00517 * 00518 * Create a %list consisting of copies of the elements from 00519 * [@a first,@a last). This is linear in N (where N is 00520 * distance(@a first,@a last)). 00521 * 00522 * @if maint 00523 * We don't need any dispatching tricks here, because insert does all of 00524 * that anyway. 00525 * @endif 00526 */ 00527 template<typename _InputIterator> 00528 list(_InputIterator __first, _InputIterator __last, 00529 const allocator_type& __a = allocator_type()) 00530 : _Base(__a) 00531 { this->insert(begin(), __first, __last); } 00532 00533 /** 00534 * No explicit dtor needed as the _Base dtor takes care of 00535 * things. The _Base dtor only erases the elements, and note 00536 * that if the elements themselves are pointers, the pointed-to 00537 * memory is not touched in any way. Managing the pointer is 00538 * the user's responsibilty. 00539 */ 00540 00541 /** 00542 * @brief %List assignment operator. 00543 * @param x A %list of identical element and allocator types. 00544 * 00545 * All the elements of @a x are copied, but unlike the copy 00546 * constructor, the allocator object is not copied. 00547 */ 00548 list& 00549 operator=(const list& __x); 00550 00551 /** 00552 * @brief Assigns a given value to a %list. 00553 * @param n Number of elements to be assigned. 00554 * @param val Value to be assigned. 00555 * 00556 * This function fills a %list with @a n copies of the given 00557 * value. Note that the assignment completely changes the %list 00558 * and that the resulting %list's size is the same as the number 00559 * of elements assigned. Old data may be lost. 00560 */ 00561 void 00562 assign(size_type __n, const value_type& __val) 00563 { _M_fill_assign(__n, __val); } 00564 00565 /** 00566 * @brief Assigns a range to a %list. 00567 * @param first An input iterator. 00568 * @param last An input iterator. 00569 * 00570 * This function fills a %list with copies of the elements in the 00571 * range [@a first,@a last). 00572 * 00573 * Note that the assignment completely changes the %list and 00574 * that the resulting %list's size is the same as the number of 00575 * elements assigned. Old data may be lost. 00576 */ 00577 template<typename _InputIterator> 00578 void 00579 assign(_InputIterator __first, _InputIterator __last) 00580 { 00581 // Check whether it's an integral type. If so, it's not an iterator. 00582 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00583 _M_assign_dispatch(__first, __last, _Integral()); 00584 } 00585 00586 /// Get a copy of the memory allocation object. 00587 allocator_type 00588 get_allocator() const 00589 { return _Base::get_allocator(); } 00590 00591 // iterators 00592 /** 00593 * Returns a read/write iterator that points to the first element in the 00594 * %list. Iteration is done in ordinary element order. 00595 */ 00596 iterator 00597 begin() 00598 { return this->_M_impl._M_node._M_next; } 00599 00600 /** 00601 * Returns a read-only (constant) iterator that points to the 00602 * first element in the %list. Iteration is done in ordinary 00603 * element order. 00604 */ 00605 const_iterator 00606 begin() const 00607 { return this->_M_impl._M_node._M_next; } 00608 00609 /** 00610 * Returns a read/write iterator that points one past the last 00611 * element in the %list. Iteration is done in ordinary element 00612 * order. 00613 */ 00614 iterator 00615 end() { return &this->_M_impl._M_node; } 00616 00617 /** 00618 * Returns a read-only (constant) iterator that points one past 00619 * the last element in the %list. Iteration is done in ordinary 00620 * element order. 00621 */ 00622 const_iterator 00623 end() const 00624 { return &this->_M_impl._M_node; } 00625 00626 /** 00627 * Returns a read/write reverse iterator that points to the last 00628 * element in the %list. Iteration is done in reverse element 00629 * order. 00630 */ 00631 reverse_iterator 00632 rbegin() 00633 { return reverse_iterator(end()); } 00634 00635 /** 00636 * Returns a read-only (constant) reverse iterator that points to 00637 * the last element in the %list. Iteration is done in reverse 00638 * element order. 00639 */ 00640 const_reverse_iterator 00641 rbegin() const 00642 { return const_reverse_iterator(end()); } 00643 00644 /** 00645 * Returns a read/write reverse iterator that points to one 00646 * before the first element in the %list. Iteration is done in 00647 * reverse element order. 00648 */ 00649 reverse_iterator 00650 rend() 00651 { return reverse_iterator(begin()); } 00652 00653 /** 00654 * Returns a read-only (constant) reverse iterator that points to one 00655 * before the first element in the %list. Iteration is done in reverse 00656 * element order. 00657 */ 00658 const_reverse_iterator 00659 rend() const 00660 { return const_reverse_iterator(begin()); } 00661 00662 // [23.2.2.2] capacity 00663 /** 00664 * Returns true if the %list is empty. (Thus begin() would equal 00665 * end().) 00666 */ 00667 bool 00668 empty() const 00669 { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; } 00670 00671 /** Returns the number of elements in the %list. */ 00672 size_type 00673 size() const 00674 { return std::distance(begin(), end()); } 00675 00676 /** Returns the size() of the largest possible %list. */ 00677 size_type 00678 max_size() const 00679 { return size_type(-1); } 00680 00681 /** 00682 * @brief Resizes the %list to the specified number of elements. 00683 * @param new_size Number of elements the %list should contain. 00684 * @param x Data with which new elements should be populated. 00685 * 00686 * This function will %resize the %list to the specified number 00687 * of elements. If the number is smaller than the %list's 00688 * current size the %list is truncated, otherwise the %list is 00689 * extended and new elements are populated with given data. 00690 */ 00691 void 00692 resize(size_type __new_size, const value_type& __x); 00693 00694 /** 00695 * @brief Resizes the %list to the specified number of elements. 00696 * @param new_size Number of elements the %list should contain. 00697 * 00698 * This function will resize the %list to the specified number of 00699 * elements. If the number is smaller than the %list's current 00700 * size the %list is truncated, otherwise the %list is extended 00701 * and new elements are default-constructed. 00702 */ 00703 void 00704 resize(size_type __new_size) 00705 { this->resize(__new_size, value_type()); } 00706 00707 // element access 00708 /** 00709 * Returns a read/write reference to the data at the first 00710 * element of the %list. 00711 */ 00712 reference 00713 front() 00714 { return *begin(); } 00715 00716 /** 00717 * Returns a read-only (constant) reference to the data at the first 00718 * element of the %list. 00719 */ 00720 const_reference 00721 front() const 00722 { return *begin(); } 00723 00724 /** 00725 * Returns a read/write reference to the data at the last element 00726 * of the %list. 00727 */ 00728 reference 00729 back() 00730 { return *(--end()); } 00731 00732 /** 00733 * Returns a read-only (constant) reference to the data at the last 00734 * element of the %list. 00735 */ 00736 const_reference 00737 back() const 00738 { return *(--end()); } 00739 00740 // [23.2.2.3] modifiers 00741 /** 00742 * @brief Add data to the front of the %list. 00743 * @param x Data to be added. 00744 * 00745 * This is a typical stack operation. The function creates an 00746 * element at the front of the %list and assigns the given data 00747 * to it. Due to the nature of a %list this operation can be 00748 * done in constant time, and does not invalidate iterators and 00749 * references. 00750 */ 00751 void 00752 push_front(const value_type& __x) 00753 { this->_M_insert(begin(), __x); } 00754 00755 /** 00756 * @brief Removes first element. 00757 * 00758 * This is a typical stack operation. It shrinks the %list by 00759 * one. Due to the nature of a %list this operation can be done 00760 * in constant time, and only invalidates iterators/references to 00761 * the element being removed. 00762 * 00763 * Note that no data is returned, and if the first element's data 00764 * is needed, it should be retrieved before pop_front() is 00765 * called. 00766 */ 00767 void 00768 pop_front() 00769 { this->_M_erase(begin()); } 00770 00771 /** 00772 * @brief Add data to the end of the %list. 00773 * @param x Data to be added. 00774 * 00775 * This is a typical stack operation. The function creates an 00776 * element at the end of the %list and assigns the given data to 00777 * it. Due to the nature of a %list this operation can be done 00778 * in constant time, and does not invalidate iterators and 00779 * references. 00780 */ 00781 void 00782 push_back(const value_type& __x) 00783 { this->_M_insert(end(), __x); } 00784 00785 /** 00786 * @brief Removes last element. 00787 * 00788 * This is a typical stack operation. It shrinks the %list by 00789 * one. Due to the nature of a %list this operation can be done 00790 * in constant time, and only invalidates iterators/references to 00791 * the element being removed. 00792 * 00793 * Note that no data is returned, and if the last element's data 00794 * is needed, it should be retrieved before pop_back() is called. 00795 */ 00796 void 00797 pop_back() 00798 { this->_M_erase(this->_M_impl._M_node._M_prev); } 00799 00800 /** 00801 * @brief Inserts given value into %list before specified iterator. 00802 * @param position An iterator into the %list. 00803 * @param x Data to be inserted. 00804 * @return An iterator that points to the inserted data. 00805 * 00806 * This function will insert a copy of the given value before 00807 * the specified location. Due to the nature of a %list this 00808 * operation can be done in constant time, and does not 00809 * invalidate iterators and references. 00810 */ 00811 iterator 00812 insert(iterator __position, const value_type& __x); 00813 00814 /** 00815 * @brief Inserts a number of copies of given data into the %list. 00816 * @param position An iterator into the %list. 00817 * @param n Number of elements to be inserted. 00818 * @param x Data to be inserted. 00819 * 00820 * This function will insert a specified number of copies of the 00821 * given data before the location specified by @a position. 00822 * 00823 * Due to the nature of a %list this operation can be done in 00824 * constant time, and does not invalidate iterators and 00825 * references. 00826 */ 00827 void 00828 insert(iterator __position, size_type __n, const value_type& __x) 00829 { _M_fill_insert(__position, __n, __x); } 00830 00831 /** 00832 * @brief Inserts a range into the %list. 00833 * @param position An iterator into the %list. 00834 * @param first An input iterator. 00835 * @param last An input iterator. 00836 * 00837 * This function will insert copies of the data in the range [@a 00838 * first,@a last) into the %list before the location specified by 00839 * @a position. 00840 * 00841 * Due to the nature of a %list this operation can be done in 00842 * constant time, and does not invalidate iterators and 00843 * references. 00844 */ 00845 template<typename _InputIterator> 00846 void 00847 insert(iterator __position, _InputIterator __first, 00848 _InputIterator __last) 00849 { 00850 // Check whether it's an integral type. If so, it's not an iterator. 00851 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00852 _M_insert_dispatch(__position, __first, __last, _Integral()); 00853 } 00854 00855 /** 00856 * @brief Remove element at given position. 00857 * @param position Iterator pointing to element to be erased. 00858 * @return An iterator pointing to the next element (or end()). 00859 * 00860 * This function will erase the element at the given position and thus 00861 * shorten the %list by one. 00862 * 00863 * Due to the nature of a %list this operation can be done in 00864 * constant time, and only invalidates iterators/references to 00865 * the element being removed. The user is also cautioned that 00866 * this function only erases the element, and that if the element 00867 * is itself a pointer, the pointed-to memory is not touched in 00868 * any way. Managing the pointer is the user's responsibilty. 00869 */ 00870 iterator 00871 erase(iterator __position); 00872 00873 /** 00874 * @brief Remove a range of elements. 00875 * @param first Iterator pointing to the first element to be erased. 00876 * @param last Iterator pointing to one past the last element to be 00877 * erased. 00878 * @return An iterator pointing to the element pointed to by @a last 00879 * prior to erasing (or end()). 00880 * 00881 * This function will erase the elements in the range @a 00882 * [first,last) and shorten the %list accordingly. 00883 * 00884 * Due to the nature of a %list this operation can be done in 00885 * constant time, and only invalidates iterators/references to 00886 * the element being removed. The user is also cautioned that 00887 * this function only erases the elements, and that if the 00888 * elements themselves are pointers, the pointed-to memory is not 00889 * touched in any way. Managing the pointer is the user's 00890 * responsibilty. 00891 */ 00892 iterator 00893 erase(iterator __first, iterator __last) 00894 { 00895 while (__first != __last) 00896 __first = erase(__first); 00897 return __last; 00898 } 00899 00900 /** 00901 * @brief Swaps data with another %list. 00902 * @param x A %list of the same element and allocator types. 00903 * 00904 * This exchanges the elements between two lists in constant 00905 * time. Note that the global std::swap() function is 00906 * specialized such that std::swap(l1,l2) will feed to this 00907 * function. 00908 */ 00909 void 00910 swap(list& __x) 00911 { _List_node_base::swap(this->_M_impl._M_node,__x._M_impl._M_node); } 00912 00913 /** 00914 * Erases all the elements. Note that this function only erases 00915 * the elements, and that if the elements themselves are 00916 * pointers, the pointed-to memory is not touched in any way. 00917 * Managing the pointer is the user's responsibilty. 00918 */ 00919 void 00920 clear() 00921 { 00922 _Base::_M_clear(); 00923 _Base::_M_init(); 00924 } 00925 00926 // [23.2.2.4] list operations 00927 /** 00928 * @brief Insert contents of another %list. 00929 * @param position Iterator referencing the element to insert before. 00930 * @param x Source list. 00931 * 00932 * The elements of @a x are inserted in constant time in front of 00933 * the element referenced by @a position. @a x becomes an empty 00934 * list. 00935 */ 00936 void 00937 splice(iterator __position, list& __x) 00938 { 00939 if (!__x.empty()) 00940 this->_M_transfer(__position, __x.begin(), __x.end()); 00941 } 00942 00943 /** 00944 * @brief Insert element from another %list. 00945 * @param position Iterator referencing the element to insert before. 00946 * @param x Source list. 00947 * @param i Iterator referencing the element to move. 00948 * 00949 * Removes the element in list @a x referenced by @a i and 00950 * inserts it into the current list before @a position. 00951 */ 00952 void 00953 splice(iterator __position, list&, iterator __i) 00954 { 00955 iterator __j = __i; 00956 ++__j; 00957 if (__position == __i || __position == __j) 00958 return; 00959 this->_M_transfer(__position, __i, __j); 00960 } 00961 00962 /** 00963 * @brief Insert range from another %list. 00964 * @param position Iterator referencing the element to insert before. 00965 * @param x Source list. 00966 * @param first Iterator referencing the start of range in x. 00967 * @param last Iterator referencing the end of range in x. 00968 * 00969 * Removes elements in the range [first,last) and inserts them 00970 * before @a position in constant time. 00971 * 00972 * Undefined if @a position is in [first,last). 00973 */ 00974 void 00975 splice(iterator __position, list&, iterator __first, iterator __last) 00976 { 00977 if (__first != __last) 00978 this->_M_transfer(__position, __first, __last); 00979 } 00980 00981 /** 00982 * @brief Remove all elements equal to value. 00983 * @param value The value to remove. 00984 * 00985 * Removes every element in the list equal to @a value. 00986 * Remaining elements stay in list order. Note that this 00987 * function only erases the elements, and that if the elements 00988 * themselves are pointers, the pointed-to memory is not 00989 * touched in any way. Managing the pointer is the user's 00990 * responsibilty. 00991 */ 00992 void 00993 remove(const _Tp& __value); 00994 00995 /** 00996 * @brief Remove all elements satisfying a predicate. 00997 * @param Predicate Unary predicate function or object. 00998 * 00999 * Removes every element in the list for which the predicate 01000 * returns true. Remaining elements stay in list order. Note 01001 * that this function only erases the elements, and that if the 01002 * elements themselves are pointers, the pointed-to memory is 01003 * not touched in any way. Managing the pointer is the user's 01004 * responsibilty. 01005 */ 01006 template<typename _Predicate> 01007 void 01008 remove_if(_Predicate); 01009 01010 /** 01011 * @brief Remove consecutive duplicate elements. 01012 * 01013 * For each consecutive set of elements with the same value, 01014 * remove all but the first one. Remaining elements stay in 01015 * list order. Note that this function only erases the 01016 * elements, and that if the elements themselves are pointers, 01017 * the pointed-to memory is not touched in any way. Managing 01018 * the pointer is the user's responsibilty. 01019 */ 01020 void 01021 unique(); 01022 01023 /** 01024 * @brief Remove consecutive elements satisfying a predicate. 01025 * @param BinaryPredicate Binary predicate function or object. 01026 * 01027 * For each consecutive set of elements [first,last) that 01028 * satisfy predicate(first,i) where i is an iterator in 01029 * [first,last), remove all but the first one. Remaining 01030 * elements stay in list order. Note that this function only 01031 * erases the elements, and that if the elements themselves are 01032 * pointers, the pointed-to memory is not touched in any way. 01033 * Managing the pointer is the user's responsibilty. 01034 */ 01035 template<typename _BinaryPredicate> 01036 void 01037 unique(_BinaryPredicate); 01038 01039 /** 01040 * @brief Merge sorted lists. 01041 * @param x Sorted list to merge. 01042 * 01043 * Assumes that both @a x and this list are sorted according to 01044 * operator<(). Merges elements of @a x into this list in 01045 * sorted order, leaving @a x empty when complete. Elements in 01046 * this list precede elements in @a x that are equal. 01047 */ 01048 void 01049 merge(list& __x); 01050 01051 /** 01052 * @brief Merge sorted lists according to comparison function. 01053 * @param x Sorted list to merge. 01054 * @param StrictWeakOrdering Comparison function definining 01055 * sort order. 01056 * 01057 * Assumes that both @a x and this list are sorted according to 01058 * StrictWeakOrdering. Merges elements of @a x into this list 01059 * in sorted order, leaving @a x empty when complete. Elements 01060 * in this list precede elements in @a x that are equivalent 01061 * according to StrictWeakOrdering(). 01062 */ 01063 template<typename _StrictWeakOrdering> 01064 void 01065 merge(list&, _StrictWeakOrdering); 01066 01067 /** 01068 * @brief Reverse the elements in list. 01069 * 01070 * Reverse the order of elements in the list in linear time. 01071 */ 01072 void 01073 reverse() 01074 { this->_M_impl._M_node.reverse(); } 01075 01076 /** 01077 * @brief Sort the elements. 01078 * 01079 * Sorts the elements of this list in NlogN time. Equivalent 01080 * elements remain in list order. 01081 */ 01082 void 01083 sort(); 01084 01085 /** 01086 * @brief Sort the elements according to comparison function. 01087 * 01088 * Sorts the elements of this list in NlogN time. Equivalent 01089 * elements remain in list order. 01090 */ 01091 template<typename _StrictWeakOrdering> 01092 void 01093 sort(_StrictWeakOrdering); 01094 01095 protected: 01096 // Internal assign functions follow. 01097 01098 // Called by the range assign to implement [23.1.1]/9 01099 template<typename _Integer> 01100 void 01101 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 01102 { 01103 _M_fill_assign(static_cast<size_type>(__n), 01104 static_cast<value_type>(__val)); 01105 } 01106 01107 // Called by the range assign to implement [23.1.1]/9 01108 template<typename _InputIterator> 01109 void 01110 _M_assign_dispatch(_InputIterator __first, _InputIterator __last, 01111 __false_type); 01112 01113 // Called by assign(n,t), and the range assign when it turns out 01114 // to be the same thing. 01115 void 01116 _M_fill_assign(size_type __n, const value_type& __val); 01117 01118 01119 // Internal insert functions follow. 01120 01121 // Called by the range insert to implement [23.1.1]/9 01122 template<typename _Integer> 01123 void 01124 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, 01125 __true_type) 01126 { 01127 _M_fill_insert(__pos, static_cast<size_type>(__n), 01128 static_cast<value_type>(__x)); 01129 } 01130 01131 // Called by the range insert to implement [23.1.1]/9 01132 template<typename _InputIterator> 01133 void 01134 _M_insert_dispatch(iterator __pos, 01135 _InputIterator __first, _InputIterator __last, 01136 __false_type) 01137 { 01138 for ( ; __first != __last; ++__first) 01139 _M_insert(__pos, *__first); 01140 } 01141 01142 // Called by insert(p,n,x), and the range insert when it turns out 01143 // to be the same thing. 01144 void 01145 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x) 01146 { 01147 for ( ; __n > 0; --__n) 01148 _M_insert(__pos, __x); 01149 } 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 std::_Destroy(&__n->_M_data); 01172 _M_put_node(__n); 01173 } 01174 }; 01175 01176 /** 01177 * @brief List equality comparison. 01178 * @param x A %list. 01179 * @param y A %list of the same type as @a x. 01180 * @return True iff the size and elements of the lists are equal. 01181 * 01182 * This is an equivalence relation. It is linear in the size of 01183 * the lists. Lists are considered equivalent if their sizes are 01184 * equal, and if corresponding elements compare equal. 01185 */ 01186 template<typename _Tp, typename _Alloc> 01187 inline bool 01188 operator==(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y) 01189 { 01190 typedef typename list<_Tp,_Alloc>::const_iterator const_iterator; 01191 const_iterator __end1 = __x.end(); 01192 const_iterator __end2 = __y.end(); 01193 01194 const_iterator __i1 = __x.begin(); 01195 const_iterator __i2 = __y.begin(); 01196 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) 01197 { 01198 ++__i1; 01199 ++__i2; 01200 } 01201 return __i1 == __end1 && __i2 == __end2; 01202 } 01203 01204 /** 01205 * @brief List ordering relation. 01206 * @param x A %list. 01207 * @param y A %list of the same type as @a x. 01208 * @return True iff @a x is lexicographically less than @a y. 01209 * 01210 * This is a total ordering relation. It is linear in the size of the 01211 * lists. The elements must be comparable with @c <. 01212 * 01213 * See std::lexicographical_compare() for how the determination is made. 01214 */ 01215 template<typename _Tp, typename _Alloc> 01216 inline bool 01217 operator<(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y) 01218 { return std::lexicographical_compare(__x.begin(), __x.end(), 01219 __y.begin(), __y.end()); } 01220 01221 /// Based on operator== 01222 template<typename _Tp, typename _Alloc> 01223 inline bool 01224 operator!=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y) 01225 { return !(__x == __y); } 01226 01227 /// Based on operator< 01228 template<typename _Tp, typename _Alloc> 01229 inline bool 01230 operator>(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y) 01231 { return __y < __x; } 01232 01233 /// Based on operator< 01234 template<typename _Tp, typename _Alloc> 01235 inline bool 01236 operator<=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y) 01237 { return !(__y < __x); } 01238 01239 /// Based on operator< 01240 template<typename _Tp, typename _Alloc> 01241 inline bool 01242 operator>=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y) 01243 { return !(__x < __y); } 01244 01245 /// See std::list::swap(). 01246 template<typename _Tp, typename _Alloc> 01247 inline void 01248 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y) 01249 { __x.swap(__y); } 01250 } // namespace std 01251 01252 #endif /* _LIST_H */ 01253

Generated on Wed Sep 8 10:19:45 2004 for libstdc++-v3 Source by doxygen 1.3.8