libstdc++
|
00001 // <forward_list.h> -*- C++ -*- 00002 00003 // Copyright (C) 2008, 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file forward_list.h 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 #ifndef _FORWARD_LIST_H 00030 #define _FORWARD_LIST_H 1 00031 00032 #pragma GCC system_header 00033 00034 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 00035 # include <c++0x_warning.h> 00036 #else 00037 00038 #include <memory> 00039 #include <initializer_list> 00040 #include <ext/cast.h> 00041 00042 _GLIBCXX_BEGIN_NAMESPACE(std) 00043 00044 using __gnu_cxx::__static_pointer_cast; 00045 using __gnu_cxx::__const_pointer_cast; 00046 00047 /** 00048 * @brief A helper basic node class for %forward_list. 00049 * This is just a linked list with nothing inside it. 00050 * There are purely list shuffling utility methods here. 00051 */ 00052 template<typename _Alloc> 00053 struct _Fwd_list_node_base 00054 { 00055 // The type allocated by _Alloc cannot be this type, so we rebind 00056 typedef typename _Alloc::template rebind<_Fwd_list_node_base<_Alloc> > 00057 ::other::pointer _Pointer; 00058 typedef typename _Alloc::template rebind<_Fwd_list_node_base<_Alloc> > 00059 ::other::const_pointer _Const_pointer; 00060 00061 _Pointer _M_next; 00062 00063 _Fwd_list_node_base() : _M_next(0) { } 00064 00065 static void 00066 swap(_Fwd_list_node_base& __x, _Fwd_list_node_base& __y) 00067 { std::swap(__x._M_next, __y._M_next); } 00068 00069 void 00070 _M_transfer_after(_Pointer __bbegin); 00071 00072 void 00073 _M_transfer_after(_Pointer __bbegin, _Pointer __bend); 00074 00075 void 00076 _M_reverse_after(); 00077 }; 00078 00079 /** 00080 * @brief A helper node class for %forward_list. 00081 * This is just a linked list with a data value in each node. 00082 * There is a sorting utility method. 00083 */ 00084 template<typename _Tp, typename _Alloc> 00085 struct _Fwd_list_node : public _Fwd_list_node_base<_Alloc> 00086 { 00087 typedef typename _Alloc::template rebind<_Fwd_list_node<_Tp, _Alloc> > 00088 ::other::pointer _Pointer; 00089 00090 template<typename... _Args> 00091 _Fwd_list_node(_Args&&... __args) 00092 : _Fwd_list_node_base<_Alloc>(), 00093 _M_value(std::forward<_Args>(__args)...) { } 00094 00095 template<typename _Comp> 00096 void 00097 _M_sort_after(_Comp __comp); 00098 00099 _Tp _M_value; 00100 }; 00101 00102 /** 00103 * @brief A forward_list::iterator. 00104 * 00105 * All the functions are op overloads. 00106 */ 00107 template<typename _Tp, typename _Alloc> 00108 struct _Fwd_list_iterator 00109 { 00110 typedef _Fwd_list_iterator<_Tp, _Alloc> _Self; 00111 typedef _Fwd_list_node<_Tp, _Alloc> _Node; 00112 typedef _Fwd_list_node_base<_Alloc> _Node_base; 00113 00114 typedef _Tp value_type; 00115 typedef typename _Alloc::pointer pointer; 00116 typedef typename _Alloc::reference reference; 00117 typedef typename _Alloc::difference_type difference_type; 00118 typedef std::forward_iterator_tag iterator_category; 00119 00120 _Fwd_list_iterator() : _M_node() { } 00121 00122 explicit 00123 _Fwd_list_iterator(typename _Node_base::_Pointer __n) 00124 : _M_node(__n) { } 00125 00126 reference 00127 operator*() const 00128 { return __static_pointer_cast<_Node*>(_M_node)->_M_value; } 00129 00130 pointer 00131 operator->() const 00132 { return &__static_pointer_cast<_Node*>(_M_node)->_M_value; } 00133 00134 _Self& 00135 operator++() 00136 { 00137 _M_node = _M_node->_M_next; 00138 return *this; 00139 } 00140 00141 _Self 00142 operator++(int) 00143 { 00144 _Self __tmp(*this); 00145 _M_node = _M_node->_M_next; 00146 return __tmp; 00147 } 00148 00149 bool 00150 operator==(const _Self& __x) const 00151 { return _M_node == __x._M_node; } 00152 00153 bool 00154 operator!=(const _Self& __x) const 00155 { return _M_node != __x._M_node; } 00156 00157 _Self 00158 _M_next() const 00159 { 00160 if (_M_node) 00161 return _Fwd_list_iterator(_M_node->_M_next); 00162 else 00163 return _Fwd_list_iterator(0); 00164 } 00165 00166 typename _Node_base::_Pointer _M_node; 00167 }; 00168 00169 /** 00170 * @brief A forward_list::const_iterator. 00171 * 00172 * All the functions are op overloads. 00173 */ 00174 template<typename _Tp, typename _Alloc> 00175 struct _Fwd_list_const_iterator 00176 { 00177 typedef _Fwd_list_const_iterator<_Tp, _Alloc> _Self; 00178 typedef const _Fwd_list_node<_Tp, _Alloc> _Node; 00179 typedef const _Fwd_list_node_base<_Alloc> _Node_base; 00180 typedef _Fwd_list_iterator<_Tp, _Alloc> iterator; 00181 00182 typedef _Tp value_type; 00183 typedef typename _Alloc::const_pointer pointer; 00184 typedef typename _Alloc::const_reference reference; 00185 typedef typename _Alloc::difference_type difference_type; 00186 typedef std::forward_iterator_tag iterator_category; 00187 00188 _Fwd_list_const_iterator() : _M_node() { } 00189 00190 explicit 00191 _Fwd_list_const_iterator(typename _Node_base::_Const_pointer __n) 00192 : _M_node(__n) { } 00193 00194 _Fwd_list_const_iterator(const iterator& __iter) 00195 : _M_node(__iter._M_node) { } 00196 00197 reference 00198 operator*() const 00199 { return __static_pointer_cast<_Node*>(_M_node)->_M_value; } 00200 00201 pointer 00202 operator->() const 00203 { return &__static_pointer_cast<_Node*>(_M_node)->_M_value; } 00204 00205 _Self& 00206 operator++() 00207 { 00208 _M_node = _M_node->_M_next; 00209 return *this; 00210 } 00211 00212 _Self 00213 operator++(int) 00214 { 00215 _Self __tmp(*this); 00216 _M_node = _M_node->_M_next; 00217 return __tmp; 00218 } 00219 00220 bool 00221 operator==(const _Self& __x) const 00222 { return _M_node == __x._M_node; } 00223 00224 bool 00225 operator!=(const _Self& __x) const 00226 { return _M_node != __x._M_node; } 00227 00228 _Self 00229 _M_next() const 00230 { 00231 if (this->_M_node) 00232 return _Fwd_list_const_iterator(_M_node->_M_next); 00233 else 00234 return _Fwd_list_const_iterator(0); 00235 } 00236 00237 typename _Node_base::_Const_pointer _M_node; 00238 }; 00239 00240 /** 00241 * @brief Forward list iterator equality comparison. 00242 */ 00243 template<typename _Tp, typename _Alloc> 00244 inline bool 00245 operator==(const _Fwd_list_iterator<_Tp, _Alloc>& __x, 00246 const _Fwd_list_const_iterator<_Tp, _Alloc>& __y) 00247 { return __x._M_node == __y._M_node; } 00248 00249 /** 00250 * @brief Forward list iterator inequality comparison. 00251 */ 00252 template<typename _Tp, typename _Alloc> 00253 inline bool 00254 operator!=(const _Fwd_list_iterator<_Tp, _Alloc>& __x, 00255 const _Fwd_list_const_iterator<_Tp, _Alloc>& __y) 00256 { return __x._M_node != __y._M_node; } 00257 00258 /** 00259 * @brief Base class for %forward_list. 00260 */ 00261 template<typename _Tp, typename _Alloc> 00262 struct _Fwd_list_base 00263 { 00264 protected: 00265 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type; 00266 00267 typedef typename _Alloc::template 00268 rebind<_Fwd_list_node<_Tp, _Tp_alloc_type>>::other _Node_alloc_type; 00269 00270 struct _Fwd_list_impl 00271 : public _Node_alloc_type 00272 { 00273 _Fwd_list_node_base<_Tp_alloc_type> _M_head; 00274 00275 _Fwd_list_impl() 00276 : _Node_alloc_type(), _M_head() 00277 { } 00278 00279 _Fwd_list_impl(const _Node_alloc_type& __a) 00280 : _Node_alloc_type(__a), _M_head() 00281 { } 00282 }; 00283 00284 _Fwd_list_impl _M_impl; 00285 00286 public: 00287 typedef _Fwd_list_iterator<_Tp, _Tp_alloc_type> iterator; 00288 typedef _Fwd_list_const_iterator<_Tp, _Tp_alloc_type> const_iterator; 00289 00290 typedef _Fwd_list_node<_Tp, _Tp_alloc_type> _Node; 00291 typedef _Fwd_list_node_base<_Tp_alloc_type> _Node_base; 00292 00293 _Node_alloc_type& 00294 _M_get_Node_allocator() 00295 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); } 00296 00297 const _Node_alloc_type& 00298 _M_get_Node_allocator() const 00299 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); } 00300 00301 _Fwd_list_base() 00302 : _M_impl() 00303 { this->_M_impl._M_head._M_next = 0; } 00304 00305 _Fwd_list_base(const _Alloc& __a) 00306 : _M_impl(__a) 00307 { this->_M_impl._M_head._M_next = 0; } 00308 00309 _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a); 00310 00311 _Fwd_list_base(_Fwd_list_base&& __lst, const _Alloc& __a) 00312 : _M_impl(__a) 00313 { _Node_base::swap(this->_M_impl._M_head, 00314 __lst._M_impl._M_head); } 00315 00316 _Fwd_list_base(_Fwd_list_base&& __lst) 00317 : _M_impl(__lst._M_get_Node_allocator()) 00318 { _Node_base::swap(this->_M_impl._M_head, 00319 __lst._M_impl._M_head); } 00320 00321 ~_Fwd_list_base() 00322 { _M_erase_after(&_M_impl._M_head, 0); } 00323 00324 protected: 00325 00326 typename _Node::_Pointer 00327 _M_get_node() 00328 { return _M_get_Node_allocator().allocate(1); } 00329 00330 template<typename... _Args> 00331 typename _Node::_Pointer 00332 _M_create_node(_Args&&... __args) 00333 { 00334 typename _Node::_Pointer __node = this->_M_get_node(); 00335 __try 00336 { 00337 _M_get_Node_allocator().construct(__node, 00338 std::forward<_Args>(__args)...); 00339 __node->_M_next = 0; 00340 } 00341 __catch(...) 00342 { 00343 this->_M_put_node(__node); 00344 __throw_exception_again; 00345 } 00346 return __node; 00347 } 00348 00349 template<typename... _Args> 00350 typename _Node_base::_Pointer 00351 _M_insert_after(const_iterator __pos, _Args&&... __args); 00352 00353 void 00354 _M_put_node(typename _Node::_Pointer __p) 00355 { _M_get_Node_allocator().deallocate(__p, 1); } 00356 00357 typename _Node_base::_Pointer 00358 _M_erase_after(typename _Node_base::_Pointer __pos); 00359 00360 typename _Node_base::_Pointer 00361 _M_erase_after(typename _Node_base::_Pointer __pos, 00362 typename _Node_base::_Pointer __last); 00363 }; 00364 00365 /** 00366 * @brief A standard container with linear time access to elements, 00367 * and fixed time insertion/deletion at any point in the sequence. 00368 * 00369 * @ingroup sequences 00370 * 00371 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00372 * <a href="tables.html#67">sequence</a>, including the 00373 * <a href="tables.html#68">optional sequence requirements</a> with the 00374 * %exception of @c at and @c operator[]. 00375 * 00376 * This is a @e singly @e linked %list. Traversal up the 00377 * %list requires linear time, but adding and removing elements (or 00378 * @e nodes) is done in constant time, regardless of where the 00379 * change takes place. Unlike std::vector and std::deque, 00380 * random-access iterators are not provided, so subscripting ( @c 00381 * [] ) access is not allowed. For algorithms which only need 00382 * sequential access, this lack makes no difference. 00383 * 00384 * Also unlike the other standard containers, std::forward_list provides 00385 * specialized algorithms %unique to linked lists, such as 00386 * splicing, sorting, and in-place reversal. 00387 * 00388 * A couple points on memory allocation for forward_list<Tp>: 00389 * 00390 * First, we never actually allocate a Tp, we allocate 00391 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure 00392 * that after elements from %forward_list<X,Alloc1> are spliced into 00393 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a 00394 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away. 00395 */ 00396 template<typename _Tp, typename _Alloc = allocator<_Tp> > 00397 class forward_list : private _Fwd_list_base<_Tp, _Alloc> 00398 { 00399 private: 00400 typedef _Fwd_list_base<_Tp, _Alloc> _Base; 00401 typedef typename _Base::_Node _Node; 00402 typedef typename _Base::_Node_base _Node_base; 00403 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type; 00404 00405 public: 00406 // types: 00407 typedef _Tp value_type; 00408 typedef typename _Tp_alloc_type::pointer pointer; 00409 typedef typename _Tp_alloc_type::const_pointer const_pointer; 00410 typedef typename _Tp_alloc_type::reference reference; 00411 typedef typename _Tp_alloc_type::const_reference const_reference; 00412 00413 typedef typename _Base::iterator iterator; 00414 typedef typename _Base::const_iterator const_iterator; 00415 typedef std::size_t size_type; 00416 typedef std::ptrdiff_t difference_type; 00417 typedef _Alloc allocator_type; 00418 00419 // 23.2.3.1 construct/copy/destroy: 00420 00421 /** 00422 * @brief Creates a %forward_list with no elements. 00423 * @param al An allocator object. 00424 */ 00425 explicit 00426 forward_list(const _Alloc& __al = _Alloc()) 00427 : _Base(__al) 00428 { } 00429 00430 /** 00431 * @brief Copy constructor with allocator argument. 00432 * @param list Input list to copy. 00433 * @param al An allocator object. 00434 */ 00435 forward_list(const forward_list& __list, const _Alloc& __al) 00436 : _Base(__list, __al) 00437 { } 00438 00439 /** 00440 * @brief Move constructor with allocator argument. 00441 * @param list Input list to move. 00442 * @param al An allocator object. 00443 */ 00444 forward_list(forward_list&& __list, const _Alloc& __al) 00445 : _Base(std::forward<_Base>(__list), __al) 00446 { } 00447 00448 /** 00449 * @brief Creates a %forward_list with copies of the default element 00450 * type. 00451 * @param n The number of elements to initially create. 00452 * 00453 * This constructor fills the %forward_list with @a n copies of 00454 * the default value. 00455 */ 00456 explicit 00457 forward_list(size_type __n) 00458 : _Base() 00459 { _M_fill_initialize(__n, value_type()); } 00460 00461 /** 00462 * @brief Creates a %forward_list with copies of an exemplar element. 00463 * @param n The number of elements to initially create. 00464 * @param value An element to copy. 00465 * @param al An allocator object. 00466 * 00467 * This constructor fills the %forward_list with @a n copies of @a 00468 * value. 00469 */ 00470 forward_list(size_type __n, const _Tp& __value, 00471 const _Alloc& __al = _Alloc()) 00472 : _Base(__al) 00473 { _M_fill_initialize(__n, __value); } 00474 00475 /** 00476 * @brief Builds a %forward_list from a range. 00477 * @param first An input iterator. 00478 * @param last An input iterator. 00479 * @param al An allocator object. 00480 * 00481 * Create a %forward_list consisting of copies of the elements from 00482 * [@a first,@a last). This is linear in N (where N is 00483 * distance(@a first,@a last)). 00484 */ 00485 template<typename _InputIterator> 00486 forward_list(_InputIterator __first, _InputIterator __last, 00487 const _Alloc& __al = _Alloc()) 00488 : _Base(__al) 00489 { 00490 // Check whether it's an integral type. If so, it's not an iterator. 00491 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 00492 _M_initialize_dispatch(__first, __last, _Integral()); 00493 } 00494 00495 /** 00496 * @brief The %forward_list copy constructor. 00497 * @param list A %forward_list of identical element and allocator 00498 * types. 00499 * 00500 * The newly-created %forward_list uses a copy of the allocation 00501 * object used by @a list. 00502 */ 00503 forward_list(const forward_list& __list) 00504 : _Base(__list.get_allocator()) 00505 { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); } 00506 00507 /** 00508 * @brief The %forward_list move constructor. 00509 * @param list A %forward_list of identical element and allocator 00510 * types. 00511 * 00512 * The newly-created %forward_list contains the exact contents of @a 00513 * forward_list. The contents of @a list are a valid, but unspecified 00514 * %forward_list. 00515 */ 00516 forward_list(forward_list&& __list) 00517 : _Base(std::forward<_Base>(__list)) { } 00518 00519 /** 00520 * @brief Builds a %forward_list from an initializer_list 00521 * @param il An initializer_list of value_type. 00522 * @param al An allocator object. 00523 * 00524 * Create a %forward_list consisting of copies of the elements 00525 * in the initializer_list @a il. This is linear in il.size(). 00526 */ 00527 forward_list(std::initializer_list<_Tp> __il, 00528 const _Alloc& __al = _Alloc()) 00529 : _Base(__al) 00530 { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); } 00531 00532 /** 00533 * @brief The forward_list dtor. 00534 */ 00535 ~forward_list() 00536 { _M_erase_after(&this->_M_impl._M_head, 0); } 00537 00538 /** 00539 * @brief The %forward_list assignment operator. 00540 * @param list A %forward_list of identical element and allocator 00541 * types. 00542 * 00543 * All the elements of @a list are copied, but unlike the copy 00544 * constructor, the allocator object is not copied. 00545 */ 00546 forward_list& 00547 operator=(const forward_list& __list); 00548 00549 /** 00550 * @brief The %forward_list move assignment operator. 00551 * @param list A %forward_list of identical element and allocator 00552 * types. 00553 * 00554 * The contents of @a list are moved into this %forward_list 00555 * (without copying). @a list is a valid, but unspecified 00556 * %forward_list 00557 */ 00558 forward_list& 00559 operator=(forward_list&& __list) 00560 { 00561 if (&__list != this) 00562 { 00563 this->clear(); 00564 this->swap(__list); 00565 } 00566 return *this; 00567 } 00568 00569 /** 00570 * @brief The %forward_list initializer list assignment operator. 00571 * @param il An initializer_list of value_type. 00572 * 00573 * Replace the contents of the %forward_list with copies of the 00574 * elements in the initializer_list @a il. This is linear in 00575 * il.size(). 00576 */ 00577 forward_list& 00578 operator=(std::initializer_list<_Tp> __il) 00579 { 00580 assign(__il); 00581 return *this; 00582 } 00583 00584 /** 00585 * @brief Assigns a range to a %forward_list. 00586 * @param first An input iterator. 00587 * @param last An input iterator. 00588 * 00589 * This function fills a %forward_list with copies of the elements 00590 * in the range [@a first,@a last). 00591 * 00592 * Note that the assignment completely changes the %forward_list and 00593 * that the resulting %forward_list's size is the same as the number 00594 * of elements assigned. Old data may be lost. 00595 */ 00596 template<typename _InputIterator> 00597 void 00598 assign(_InputIterator __first, _InputIterator __last) 00599 { 00600 clear(); 00601 insert_after(cbefore_begin(), __first, __last); 00602 } 00603 00604 /** 00605 * @brief Assigns a given value to a %forward_list. 00606 * @param n Number of elements to be assigned. 00607 * @param val Value to be assigned. 00608 * 00609 * This function fills a %forward_list with @a n copies of the given 00610 * value. Note that the assignment completely changes the 00611 * %forward_list and that the resulting %forward_list's size is the 00612 * same as the number of elements assigned. Old data may be lost. 00613 */ 00614 void 00615 assign(size_type __n, const _Tp& __val) 00616 { 00617 clear(); 00618 insert_after(cbefore_begin(), __n, __val); 00619 } 00620 00621 /** 00622 * @brief Assigns an initializer_list to a %forward_list. 00623 * @param il An initializer_list of value_type. 00624 * 00625 * Replace the contents of the %forward_list with copies of the 00626 * elements in the initializer_list @a il. This is linear in 00627 * il.size(). 00628 */ 00629 void 00630 assign(std::initializer_list<_Tp> __il) 00631 { 00632 clear(); 00633 insert_after(cbefore_begin(), __il); 00634 } 00635 00636 /// Get a copy of the memory allocation object. 00637 allocator_type 00638 get_allocator() const 00639 { return this->_M_get_Node_allocator(); } 00640 00641 // 23.2.3.2 iterators: 00642 00643 /** 00644 * Returns a read/write iterator that points before the first element 00645 * in the %forward_list. Iteration is done in ordinary element order. 00646 */ 00647 iterator 00648 before_begin() 00649 { return iterator(&this->_M_impl._M_head); } 00650 00651 /** 00652 * Returns a read-only (constant) iterator that points before the 00653 * first element in the %forward_list. Iteration is done in ordinary 00654 * element order. 00655 */ 00656 const_iterator 00657 before_begin() const 00658 { return const_iterator(&this->_M_impl._M_head); } 00659 00660 /** 00661 * Returns a read/write iterator that points to the first element 00662 * in the %forward_list. Iteration is done in ordinary element order. 00663 */ 00664 iterator 00665 begin() 00666 { return iterator(this->_M_impl._M_head._M_next); } 00667 00668 /** 00669 * Returns a read-only (constant) iterator that points to the first 00670 * element in the %forward_list. Iteration is done in ordinary 00671 * element order. 00672 */ 00673 const_iterator 00674 begin() const 00675 { return const_iterator(this->_M_impl._M_head._M_next); } 00676 00677 /** 00678 * Returns a read/write iterator that points one past the last 00679 * element in the %forward_list. Iteration is done in ordinary 00680 * element order. 00681 */ 00682 iterator 00683 end() 00684 { return iterator(0); } 00685 00686 /** 00687 * Returns a read-only iterator that points one past the last 00688 * element in the %forward_list. Iteration is done in ordinary 00689 * element order. 00690 */ 00691 const_iterator 00692 end() const 00693 { return const_iterator(0); } 00694 00695 /** 00696 * Returns a read-only (constant) iterator that points to the 00697 * first element in the %forward_list. Iteration is done in ordinary 00698 * element order. 00699 */ 00700 const_iterator 00701 cbegin() const 00702 { return const_iterator(this->_M_impl._M_head._M_next); } 00703 00704 /** 00705 * Returns a read-only (constant) iterator that points before the 00706 * first element in the %forward_list. Iteration is done in ordinary 00707 * element order. 00708 */ 00709 const_iterator 00710 cbefore_begin() const 00711 { return const_iterator(&this->_M_impl._M_head); } 00712 00713 /** 00714 * Returns a read-only (constant) iterator that points one past 00715 * the last element in the %forward_list. Iteration is done in 00716 * ordinary element order. 00717 */ 00718 const_iterator 00719 cend() const 00720 { return const_iterator(0); } 00721 00722 /** 00723 * Returns true if the %forward_list is empty. (Thus begin() would 00724 * equal end().) 00725 */ 00726 bool 00727 empty() const 00728 { return this->_M_impl._M_head._M_next == 0; } 00729 00730 /** 00731 * Returns the largest possible size of %forward_list. 00732 */ 00733 size_type 00734 max_size() const 00735 { return this->_M_get_Node_allocator().max_size(); } 00736 00737 // 23.2.3.3 element access: 00738 00739 /** 00740 * Returns a read/write reference to the data at the first 00741 * element of the %forward_list. 00742 */ 00743 reference 00744 front() 00745 { 00746 _Node* __front = 00747 __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next); 00748 return __front->_M_value; 00749 } 00750 00751 /** 00752 * Returns a read-only (constant) reference to the data at the first 00753 * element of the %forward_list. 00754 */ 00755 const_reference 00756 front() const 00757 { 00758 _Node* __front = 00759 __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next); 00760 return __front->_M_value; 00761 } 00762 00763 // 23.2.3.4 modiļ¬ers: 00764 00765 /** 00766 * @brief Constructs object in %forward_list at the front of the 00767 * list. 00768 * @param args Arguments. 00769 * 00770 * This function will insert an object of type Tp constructed 00771 * with Tp(std::forward<Args>(args)...) at the front of the list 00772 * Due to the nature of a %forward_list this operation can 00773 * be done in constant time, and does not invalidate iterators 00774 * and references. 00775 */ 00776 template<typename... _Args> 00777 void 00778 emplace_front(_Args&&... __args) 00779 { this->_M_insert_after(cbefore_begin(), 00780 std::forward<_Args>(__args)...); } 00781 00782 /** 00783 * @brief Add data to the front of the %forward_list. 00784 * @param val Data to be added. 00785 * 00786 * This is a typical stack operation. The function creates an 00787 * element at the front of the %forward_list and assigns the given 00788 * data to it. Due to the nature of a %forward_list this operation 00789 * can be done in constant time, and does not invalidate iterators 00790 * and references. 00791 */ 00792 void 00793 push_front(const _Tp& __val) 00794 { this->_M_insert_after(cbefore_begin(), __val); } 00795 00796 /** 00797 * 00798 */ 00799 void 00800 push_front(_Tp&& __val) 00801 { this->_M_insert_after(cbefore_begin(), std::move(__val)); } 00802 00803 /** 00804 * @brief Removes first element. 00805 * 00806 * This is a typical stack operation. It shrinks the %forward_list 00807 * by one. Due to the nature of a %forward_list this operation can 00808 * be done in constant time, and only invalidates iterators/references 00809 * to the element being removed. 00810 * 00811 * Note that no data is returned, and if the first element's data 00812 * is needed, it should be retrieved before pop_front() is 00813 * called. 00814 */ 00815 void 00816 pop_front() 00817 { this->_M_erase_after(&this->_M_impl._M_head); } 00818 00819 /** 00820 * @brief Constructs object in %forward_list after the specified 00821 * iterator. 00822 * @param pos A const_iterator into the %forward_list. 00823 * @param args Arguments. 00824 * @return An iterator that points to the inserted data. 00825 * 00826 * This function will insert an object of type T constructed 00827 * with T(std::forward<Args>(args)...) after the specified 00828 * location. Due to the nature of a %forward_list this operation can 00829 * be done in constant time, and does not invalidate iterators 00830 * and references. 00831 */ 00832 template<typename... _Args> 00833 iterator 00834 emplace_after(const_iterator __pos, _Args&&... __args) 00835 { return iterator(this->_M_insert_after(__pos, 00836 std::forward<_Args>(__args)...)); } 00837 00838 /** 00839 * @brief Inserts given value into %forward_list after specified 00840 * iterator. 00841 * @param pos An iterator into the %forward_list. 00842 * @param val Data to be inserted. 00843 * @return An iterator that points to the inserted data. 00844 * 00845 * This function will insert a copy of the given value after 00846 * the specified location. Due to the nature of a %forward_list this 00847 * operation can be done in constant time, and does not 00848 * invalidate iterators and references. 00849 */ 00850 iterator 00851 insert_after(const_iterator __pos, const _Tp& __val) 00852 { return iterator(this->_M_insert_after(__pos, __val)); } 00853 00854 /** 00855 * 00856 */ 00857 iterator 00858 insert_after(const_iterator __pos, _Tp&& __val) 00859 { return iterator(this->_M_insert_after(__pos, std::move(__val))); } 00860 00861 /** 00862 * @brief Inserts a number of copies of given data into the 00863 * %forward_list. 00864 * @param pos An iterator into the %forward_list. 00865 * @param n Number of elements to be inserted. 00866 * @param val Data to be inserted. 00867 * 00868 * This function will insert a specified number of copies of the 00869 * given data after the location specified by @a pos. 00870 * 00871 * This operation is linear in the number of elements inserted and 00872 * does not invalidate iterators and references. 00873 */ 00874 void 00875 insert_after(const_iterator __pos, size_type __n, const _Tp& __val) 00876 { 00877 forward_list __tmp(__n, __val, this->get_allocator()); 00878 this->splice_after(__pos, std::move(__tmp)); 00879 } 00880 00881 /** 00882 * @brief Inserts a range into the %forward_list. 00883 * @param position An iterator into the %forward_list. 00884 * @param first An input iterator. 00885 * @param last An input iterator. 00886 * 00887 * This function will insert copies of the data in the range [@a 00888 * first,@a last) into the %forward_list after the location specified 00889 * by @a pos. 00890 * 00891 * This operation is linear in the number of elements inserted and 00892 * does not invalidate iterators and references. 00893 */ 00894 template<typename _InputIterator> 00895 void 00896 insert_after(const_iterator __pos, 00897 _InputIterator __first, _InputIterator __last) 00898 { 00899 forward_list __tmp(__first, __last, this->get_allocator()); 00900 this->splice_after(__pos, std::move(__tmp)); 00901 } 00902 00903 /** 00904 * @brief Inserts the contents of an initializer_list into 00905 * %forward_list after the specified iterator. 00906 * @param pos An iterator into the %forward_list. 00907 * @param il An initializer_list of value_type. 00908 * 00909 * This function will insert copies of the data in the 00910 * initializer_list @a il into the %forward_list before the location 00911 * specified by @a pos. 00912 * 00913 * This operation is linear in the number of elements inserted and 00914 * does not invalidate iterators and references. 00915 */ 00916 void 00917 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il) 00918 { 00919 forward_list __tmp(__il, this->get_allocator()); 00920 this->splice_after(__pos, std::move(__tmp)); 00921 } 00922 00923 /** 00924 * @brief Removes the element pointed to by the iterator following 00925 * @c pos. 00926 * @param pos Iterator pointing to element to be erased. 00927 * @return An iterator pointing to the next element (or end()). 00928 * 00929 * This function will erase the element at the given position and 00930 * thus shorten the %forward_list by one. 00931 * 00932 * Due to the nature of a %forward_list this operation can be done 00933 * in constant time, and only invalidates iterators/references to 00934 * the element being removed. The user is also cautioned that 00935 * this function only erases the element, and that if the element 00936 * is itself a pointer, the pointed-to memory is not touched in 00937 * any way. Managing the pointer is the user's responsibility. 00938 */ 00939 iterator 00940 erase_after(const_iterator __pos) 00941 { 00942 _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node); 00943 if (__tmp) 00944 return iterator(this->_M_erase_after(__tmp)); 00945 else 00946 return end(); 00947 } 00948 00949 /** 00950 * @brief Remove a range of elements. 00951 * @param pos Iterator pointing before the first element to be 00952 * erased. 00953 * @param last Iterator pointing to one past the last element to be 00954 * erased. 00955 * @return An iterator pointing to the element pointed to by @a last 00956 * prior to erasing (or end()). 00957 * 00958 * This function will erase the elements in the range @a 00959 * (pos,last) and shorten the %forward_list accordingly. 00960 * 00961 * This operation is linear time in the size of the range and only 00962 * invalidates iterators/references to the element being removed. 00963 * The user is also cautioned that this function only erases the 00964 * elements, and that if the elements themselves are pointers, the 00965 * pointed-to memory is not touched in any way. Managing the pointer 00966 * is the user's responsibility. 00967 */ 00968 iterator 00969 erase_after(const_iterator __pos, iterator __last) 00970 { 00971 _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node); 00972 return iterator(this->_M_erase_after(__tmp, &*__last._M_node)); 00973 } 00974 00975 /** 00976 * @brief Swaps data with another %forward_list. 00977 * @param list A %forward_list of the same element and allocator 00978 * types. 00979 * 00980 * This exchanges the elements between two lists in constant 00981 * time. Note that the global std::swap() function is 00982 * specialized such that std::swap(l1,l2) will feed to this 00983 * function. 00984 */ 00985 void 00986 swap(forward_list&& __list) 00987 { _Node_base::swap(this->_M_impl._M_head, __list._M_impl._M_head); } 00988 00989 /** 00990 * @brief Resizes the %forward_list to the specified number of 00991 * elements. 00992 * @param sz Number of elements the %forward_list should contain. 00993 * 00994 * This function will %resize the %forward_list to the specified 00995 * number of elements. If the number is smaller than the 00996 * %forward_list's current size the %forward_list is truncated, 00997 * otherwise the %forward_list is extended and new elements are 00998 * populated with given data. 00999 */ 01000 void 01001 resize(size_type __sz) 01002 { resize(__sz, _Tp()); } 01003 01004 /** 01005 * @brief Resizes the %forward_list to the specified number of 01006 * elements. 01007 * @param sz Number of elements the %forward_list should contain. 01008 * @param val Data with which new elements should be populated. 01009 * 01010 * This function will %resize the %forward_list to the specified 01011 * number of elements. If the number is smaller than the 01012 * %forward_list's current size the %forward_list is truncated, 01013 * otherwise the %forward_list is extended and new elements are 01014 * populated with given data. 01015 */ 01016 void 01017 resize(size_type __sz, value_type __val); 01018 01019 /** 01020 * @brief Erases all the elements. 01021 * 01022 * Note that this function only erases 01023 * the elements, and that if the elements themselves are 01024 * pointers, the pointed-to memory is not touched in any way. 01025 * Managing the pointer is the user's responsibility. 01026 */ 01027 void 01028 clear() 01029 { this->_M_erase_after(&this->_M_impl._M_head, 0); } 01030 01031 // 23.2.3.5 forward_list operations: 01032 01033 /** 01034 * @brief Insert contents of another %forward_list. 01035 * @param pos Iterator referencing the element to insert after. 01036 * @param list Source list. 01037 * 01038 * The elements of @a list are inserted in constant time after 01039 * the element referenced by @a pos. @a list becomes an empty 01040 * list. 01041 * 01042 * Requires this != @a x. 01043 */ 01044 void 01045 splice_after(const_iterator __pos, forward_list&& __list); 01046 01047 /** 01048 * @brief Insert element from another %forward_list. 01049 * @param pos Iterator referencing the element to insert after. 01050 * @param list Source list. 01051 * @param it Iterator referencing the element before the element 01052 * to move. 01053 * 01054 * Removes the element in list @a list referenced by @a i and 01055 * inserts it into the current list after @a pos. 01056 */ 01057 void 01058 splice_after(const_iterator __pos, forward_list&& __list, 01059 const_iterator __it) 01060 { this->splice_after(__pos, __list, __it, __it._M_next()); } 01061 01062 /** 01063 * @brief Insert range from another %forward_list. 01064 * @param pos Iterator referencing the element to insert after. 01065 * @param list Source list. 01066 * @param before Iterator referencing before the start of range 01067 * in list. 01068 * @param last Iterator referencing the end of range in list. 01069 * 01070 * Removes elements in the range (before,last) and inserts them 01071 * after @a pos in constant time. 01072 * 01073 * Undefined if @a pos is in (before,last). 01074 */ 01075 void 01076 splice_after(const_iterator __pos, forward_list&& __list, 01077 const_iterator __before, const_iterator __last); 01078 01079 /** 01080 * @brief Remove all elements equal to value. 01081 * @param val The value to remove. 01082 * 01083 * Removes every element in the list equal to @a value. 01084 * Remaining elements stay in list order. Note that this 01085 * function only erases the elements, and that if the elements 01086 * themselves are pointers, the pointed-to memory is not 01087 * touched in any way. Managing the pointer is the user's 01088 * responsibility. 01089 */ 01090 void 01091 remove(const _Tp& __val); 01092 01093 /** 01094 * @brief Remove all elements satisfying a predicate. 01095 * @param pred Unary predicate function or object. 01096 * 01097 * Removes every element in the list for which the predicate 01098 * returns true. Remaining elements stay in list order. Note 01099 * that this function only erases the elements, and that if the 01100 * elements themselves are pointers, the pointed-to memory is 01101 * not touched in any way. Managing the pointer is the user's 01102 * responsibility. 01103 */ 01104 template<typename _Pred> 01105 void 01106 remove_if(_Pred __pred); 01107 01108 /** 01109 * @brief Remove consecutive duplicate elements. 01110 * 01111 * For each consecutive set of elements with the same value, 01112 * remove all but the first one. Remaining elements stay in 01113 * list order. Note that this function only erases the 01114 * elements, and that if the elements themselves are pointers, 01115 * the pointed-to memory is not touched in any way. Managing 01116 * the pointer is the user's responsibility. 01117 */ 01118 void 01119 unique() 01120 { this->unique(std::equal_to<_Tp>()); } 01121 01122 /** 01123 * @brief Remove consecutive elements satisfying a predicate. 01124 * @param binary_pred Binary predicate function or object. 01125 * 01126 * For each consecutive set of elements [first,last) that 01127 * satisfy predicate(first,i) where i is an iterator in 01128 * [first,last), remove all but the first one. Remaining 01129 * elements stay in list order. Note that this function only 01130 * erases the elements, and that if the elements themselves are 01131 * pointers, the pointed-to memory is not touched in any way. 01132 * Managing the pointer is the user's responsibility. 01133 */ 01134 template<typename _BinPred> 01135 void 01136 unique(_BinPred __binary_pred); 01137 01138 /** 01139 * @brief Merge sorted lists. 01140 * @param list Sorted list to merge. 01141 * 01142 * Assumes that both @a list and this list are sorted according to 01143 * operator<(). Merges elements of @a list into this list in 01144 * sorted order, leaving @a list empty when complete. Elements in 01145 * this list precede elements in @a list that are equal. 01146 */ 01147 void 01148 merge(forward_list&& __list) 01149 { this->merge(__list, std::less<_Tp>()); } 01150 01151 /** 01152 * @brief Merge sorted lists according to comparison function. 01153 * @param list Sorted list to merge. 01154 * @param comp Comparison function defining sort order. 01155 * 01156 * Assumes that both @a list and this list are sorted according to 01157 * comp. Merges elements of @a list into this list 01158 * in sorted order, leaving @a list empty when complete. Elements 01159 * in this list precede elements in @a list that are equivalent 01160 * according to comp(). 01161 */ 01162 template<typename _Comp> 01163 void 01164 merge(forward_list&& __list, _Comp __comp); 01165 01166 /** 01167 * @brief Sort the elements of the list. 01168 * 01169 * Sorts the elements of this list in NlogN time. Equivalent 01170 * elements remain in list order. 01171 */ 01172 void 01173 sort() 01174 { 01175 _Node* __tmp = __static_pointer_cast<_Node*>(&this->_M_impl._M_head); 01176 __tmp->_M_sort_after(std::less<_Tp>()); 01177 } 01178 01179 /** 01180 * @brief Sort the forward_list using a comparison function. 01181 * 01182 * Sorts the elements of this list in NlogN time. Equivalent 01183 * elements remain in list order. 01184 */ 01185 template<typename _Comp> 01186 void 01187 sort(_Comp __comp) 01188 { 01189 _Node* __tmp = __static_pointer_cast<_Node*>(&this->_M_impl._M_head); 01190 __tmp->_M_sort_after(__comp); 01191 } 01192 01193 /** 01194 * @brief Reverse the elements in list. 01195 * 01196 * Reverse the order of elements in the list in linear time. 01197 */ 01198 void 01199 reverse() 01200 { this->_M_impl._M_head._M_reverse_after(); } 01201 01202 private: 01203 template<typename _Integer> 01204 void 01205 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) 01206 { _M_fill_initialize(static_cast<size_type>(__n), __x); } 01207 01208 // Called by the range constructor to implement [23.1.1]/9 01209 template<typename _InputIterator> 01210 void 01211 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last, 01212 __false_type); 01213 01214 // Called by forward_list(n,v,a), and the range constructor when it 01215 // turns out to be the same thing. 01216 void 01217 _M_fill_initialize(size_type __n, const value_type& __value); 01218 }; 01219 01220 /** 01221 * @brief Forward list equality comparison. 01222 * @param lx A %forward_list 01223 * @param ly A %forward_list of the same type as @a lx. 01224 * @return True iff the size and elements of the forward lists are equal. 01225 * 01226 * This is an equivalence relation. It is linear in the size of the 01227 * forward lists. Deques are considered equivalent if corresponding 01228 * elements compare equal. 01229 */ 01230 template<typename _Tp, typename _Alloc> 01231 bool 01232 operator==(const forward_list<_Tp, _Alloc>& __lx, 01233 const forward_list<_Tp, _Alloc>& __ly); 01234 01235 /** 01236 * @brief Forward list ordering relation. 01237 * @param lx A %forward_list. 01238 * @param ly A %forward_list of the same type as @a lx. 01239 * @return True iff @a lx is lexicographically less than @a ly. 01240 * 01241 * This is a total ordering relation. It is linear in the size of the 01242 * forward lists. The elements must be comparable with @c <. 01243 * 01244 * See std::lexicographical_compare() for how the determination is made. 01245 */ 01246 template<typename _Tp, typename _Alloc> 01247 inline bool 01248 operator<(const forward_list<_Tp, _Alloc>& __lx, 01249 const forward_list<_Tp, _Alloc>& __ly) 01250 { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(), 01251 __ly.cbegin(), __ly.cend()); } 01252 01253 /// Based on operator== 01254 template<typename _Tp, typename _Alloc> 01255 inline bool 01256 operator!=(const forward_list<_Tp, _Alloc>& __lx, 01257 const forward_list<_Tp, _Alloc>& __ly) 01258 { return !(__lx == __ly); } 01259 01260 /// Based on operator< 01261 template<typename _Tp, typename _Alloc> 01262 inline bool 01263 operator>(const forward_list<_Tp, _Alloc>& __lx, 01264 const forward_list<_Tp, _Alloc>& __ly) 01265 { return (__ly < __lx); } 01266 01267 /// Based on operator< 01268 template<typename _Tp, typename _Alloc> 01269 inline bool 01270 operator>=(const forward_list<_Tp, _Alloc>& __lx, 01271 const forward_list<_Tp, _Alloc>& __ly) 01272 { return !(__lx < __ly); } 01273 01274 /// Based on operator< 01275 template<typename _Tp, typename _Alloc> 01276 inline bool 01277 operator<=(const forward_list<_Tp, _Alloc>& __lx, 01278 const forward_list<_Tp, _Alloc>& __ly) 01279 { return !(__ly < __lx); } 01280 01281 /// See std::forward_list::swap(). 01282 template<typename _Tp, typename _Alloc> 01283 inline void 01284 swap(forward_list<_Tp, _Alloc>& __lx, 01285 forward_list<_Tp, _Alloc>& __ly) 01286 { __lx.swap(__ly); } 01287 01288 /// See std::forward_list::swap(). 01289 template<typename _Tp, typename _Alloc> 01290 inline void 01291 swap(forward_list<_Tp, _Alloc>&& __lx, 01292 forward_list<_Tp, _Alloc>& __ly) 01293 { __lx.swap(__ly); } 01294 01295 /// See std::forward_list::swap(). 01296 template<typename _Tp, typename _Alloc> 01297 inline void 01298 swap(forward_list<_Tp, _Alloc>& __lx, 01299 forward_list<_Tp, _Alloc>&& __ly) 01300 { __lx.swap(__ly); } 01301 01302 _GLIBCXX_END_NAMESPACE // namespace std 01303 01304 #endif // __GXX_EXPERIMENTAL_CXX0X__ 01305 01306 #endif // _FORWARD_LIST_H