forward_list.h

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