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