forward_list.h

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

Generated on 11 Jan 2010 for libstdc++ by  doxygen 1.6.1