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

Generated on 12 Mar 2010 for libstdc++ by  doxygen 1.6.1