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 #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 default constructed elements.
00446        *  @param  n  The number of elements to initially create.
00447        *
00448        *  This constructor creates the %forward_list with @a n default
00449        *  constructed elements.
00450        */
00451       explicit
00452       forward_list(size_type __n);
00453 
00454       /**
00455        *  @brief  Creates a %forward_list with copies of an exemplar element.
00456        *  @param  n      The number of elements to initially create.
00457        *  @param  value  An element to copy.
00458        *  @param  al     An allocator object.
00459        *
00460        *  This constructor fills the %forward_list with @a n copies of @a
00461        *  value.
00462        */
00463       forward_list(size_type __n, const _Tp& __value,
00464                    const _Alloc& __al = _Alloc())
00465       : _Base(__al)
00466       { _M_fill_initialize(__n, __value); }
00467 
00468       /**
00469        *  @brief  Builds a %forward_list from a range.
00470        *  @param  first  An input iterator.
00471        *  @param  last   An input iterator.
00472        *  @param  al     An allocator object.
00473        *
00474        *  Create a %forward_list consisting of copies of the elements from
00475        *  [@a first,@a last).  This is linear in N (where N is
00476        *  distance(@a first,@a last)).
00477        */
00478       template<typename _InputIterator>
00479         forward_list(_InputIterator __first, _InputIterator __last,
00480                      const _Alloc& __al = _Alloc())
00481         : _Base(__al)
00482         {
00483           // Check whether it's an integral type.  If so, it's not an iterator.
00484           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00485           _M_initialize_dispatch(__first, __last, _Integral());
00486         }
00487 
00488       /**
00489        *  @brief  The %forward_list copy constructor.
00490        *  @param  list  A %forward_list of identical element and allocator
00491        *                types.
00492        *
00493        *  The newly-created %forward_list uses a copy of the allocation
00494        *  object used by @a list.
00495        */
00496       forward_list(const forward_list& __list)
00497       : _Base(__list._M_get_Node_allocator())
00498       { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
00499 
00500       /**
00501        *  @brief  The %forward_list move constructor.
00502        *  @param  list  A %forward_list of identical element and allocator
00503        *                types.
00504        *
00505        *  The newly-created %forward_list contains the exact contents of @a
00506        *  forward_list. The contents of @a list are a valid, but unspecified
00507        *  %forward_list.
00508        */
00509       forward_list(forward_list&& __list)
00510       : _Base(std::forward<_Base>(__list)) { }
00511 
00512       /**
00513        *  @brief  Builds a %forward_list from an initializer_list
00514        *  @param  il  An initializer_list of value_type.
00515        *  @param  al  An allocator object.
00516        *
00517        *  Create a %forward_list consisting of copies of the elements
00518        *  in the initializer_list @a il.  This is linear in il.size().
00519        */
00520       forward_list(std::initializer_list<_Tp> __il,
00521                    const _Alloc& __al = _Alloc())
00522       : _Base(__al)
00523       { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
00524 
00525       /**
00526        *  @brief  The forward_list dtor.
00527        */
00528       ~forward_list()
00529       { }
00530 
00531       /**
00532        *  @brief  The %forward_list assignment operator.
00533        *  @param  list  A %forward_list of identical element and allocator
00534        *                types.
00535        *
00536        *  All the elements of @a list are copied, but unlike the copy
00537        *  constructor, the allocator object is not copied.
00538        */
00539       forward_list&
00540       operator=(const forward_list& __list);
00541 
00542       /**
00543        *  @brief  The %forward_list move assignment operator.
00544        *  @param  list  A %forward_list of identical element and allocator
00545        *                types.
00546        *
00547        *  The contents of @a list are moved into this %forward_list
00548        *  (without copying). @a list is a valid, but unspecified
00549        *  %forward_list
00550        */
00551       forward_list&
00552       operator=(forward_list&& __list)
00553       {
00554     // NB: DR 1204.
00555     // NB: DR 675.
00556     this->clear();
00557     this->swap(__list);
00558     return *this;
00559       }
00560 
00561       /**
00562        *  @brief  The %forward_list initializer list assignment operator.
00563        *  @param  il  An initializer_list of value_type.
00564        *
00565        *  Replace the contents of the %forward_list with copies of the
00566        *  elements in the initializer_list @a il.  This is linear in
00567        *  il.size().
00568        */
00569       forward_list&
00570       operator=(std::initializer_list<_Tp> __il)
00571       {
00572         assign(__il);
00573         return *this;
00574       }
00575 
00576       /**
00577        *  @brief  Assigns a range to a %forward_list.
00578        *  @param  first  An input iterator.
00579        *  @param  last   An input iterator.
00580        *
00581        *  This function fills a %forward_list with copies of the elements
00582        *  in the range [@a first,@a last).
00583        *
00584        *  Note that the assignment completely changes the %forward_list and
00585        *  that the resulting %forward_list's size is the same as the number
00586        *  of elements assigned.  Old data may be lost.
00587        */
00588       template<typename _InputIterator>
00589         void
00590         assign(_InputIterator __first, _InputIterator __last)
00591         {
00592           clear();
00593           insert_after(cbefore_begin(), __first, __last);
00594         }
00595 
00596       /**
00597        *  @brief  Assigns a given value to a %forward_list.
00598        *  @param  n  Number of elements to be assigned.
00599        *  @param  val  Value to be assigned.
00600        *
00601        *  This function fills a %forward_list with @a n copies of the given
00602        *  value.  Note that the assignment completely changes the
00603        *  %forward_list and that the resulting %forward_list's size is the
00604        *  same as the number of elements assigned.  Old data may be lost.
00605        */
00606       void
00607       assign(size_type __n, const _Tp& __val)
00608       {
00609         clear();
00610         insert_after(cbefore_begin(), __n, __val);
00611       }
00612 
00613       /**
00614        *  @brief  Assigns an initializer_list to a %forward_list.
00615        *  @param  il  An initializer_list of value_type.
00616        *
00617        *  Replace the contents of the %forward_list with copies of the
00618        *  elements in the initializer_list @a il.  This is linear in
00619        *  il.size().
00620        */
00621       void
00622       assign(std::initializer_list<_Tp> __il)
00623       {
00624         clear();
00625         insert_after(cbefore_begin(), __il);
00626       }
00627 
00628       /// Get a copy of the memory allocation object.
00629       allocator_type
00630       get_allocator() const
00631       { return this->_M_get_Node_allocator(); }
00632 
00633       // 23.2.3.2 iterators:
00634 
00635       /**
00636        *  Returns a read/write iterator that points before the first element
00637        *  in the %forward_list.  Iteration is done in ordinary element order.
00638        */
00639       iterator
00640       before_begin()
00641       { return iterator(&this->_M_impl._M_head); }
00642 
00643       /**
00644        *  Returns a read-only (constant) iterator that points before the
00645        *  first element in the %forward_list.  Iteration is done in ordinary
00646        *  element order.
00647        */
00648       const_iterator
00649       before_begin() const
00650       { return const_iterator(&this->_M_impl._M_head); }
00651 
00652       /**
00653        *  Returns a read/write iterator that points to the first element
00654        *  in the %forward_list.  Iteration is done in ordinary element order.
00655        */
00656       iterator
00657       begin()
00658       { return iterator(this->_M_impl._M_head._M_next); }
00659 
00660       /**
00661        *  Returns a read-only (constant) iterator that points to the first
00662        *  element in the %forward_list.  Iteration is done in ordinary
00663        *  element order.
00664        */
00665       const_iterator
00666       begin() const
00667       { return const_iterator(this->_M_impl._M_head._M_next); }
00668 
00669       /**
00670        *  Returns a read/write iterator that points one past the last
00671        *  element in the %forward_list.  Iteration is done in ordinary
00672        *  element order.
00673        */
00674       iterator
00675       end()
00676       { return iterator(0); }
00677 
00678       /**
00679        *  Returns a read-only iterator that points one past the last
00680        *  element in the %forward_list.  Iteration is done in ordinary
00681        *  element order.
00682        */
00683       const_iterator
00684       end() const
00685       { return const_iterator(0); }
00686 
00687       /**
00688        *  Returns a read-only (constant) iterator that points to the
00689        *  first element in the %forward_list.  Iteration is done in ordinary
00690        *  element order.
00691        */
00692       const_iterator
00693       cbegin() const
00694       { return const_iterator(this->_M_impl._M_head._M_next); }
00695 
00696       /**
00697        *  Returns a read-only (constant) iterator that points before the
00698        *  first element in the %forward_list.  Iteration is done in ordinary
00699        *  element order.
00700        */
00701       const_iterator
00702       cbefore_begin() const
00703       { return const_iterator(&this->_M_impl._M_head); }
00704 
00705       /**
00706        *  Returns a read-only (constant) iterator that points one past
00707        *  the last element in the %forward_list.  Iteration is done in
00708        *  ordinary element order.
00709        */
00710       const_iterator
00711       cend() const
00712       { return const_iterator(0); }
00713 
00714       /**
00715        *  Returns true if the %forward_list is empty.  (Thus begin() would
00716        *  equal end().)
00717        */
00718       bool
00719       empty() const
00720       { return this->_M_impl._M_head._M_next == 0; }
00721 
00722       /**
00723        *  Returns the largest possible size of %forward_list.
00724        */
00725       size_type
00726       max_size() const
00727       { return this->_M_get_Node_allocator().max_size(); }
00728 
00729       // 23.2.3.3 element access:
00730 
00731       /**
00732        *  Returns a read/write reference to the data at the first
00733        *  element of the %forward_list.
00734        */
00735       reference
00736       front()
00737       {
00738         _Node* __front =
00739       __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
00740         return __front->_M_value;
00741       }
00742 
00743       /**
00744        *  Returns a read-only (constant) reference to the data at the first
00745        *  element of the %forward_list.
00746        */
00747       const_reference
00748       front() const
00749       {
00750         _Node* __front =
00751       __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
00752         return __front->_M_value;
00753       }
00754 
00755       // 23.2.3.4 modifiers:
00756 
00757       /**
00758        *  @brief  Constructs object in %forward_list at the front of the
00759        *          list.
00760        *  @param  args  Arguments.
00761        *
00762        *  This function will insert an object of type Tp constructed
00763        *  with Tp(std::forward<Args>(args)...) at the front of the list
00764        *  Due to the nature of a %forward_list this operation can
00765        *  be done in constant time, and does not invalidate iterators
00766        *  and references.
00767        */
00768       template<typename... _Args>
00769         void
00770         emplace_front(_Args&&... __args)
00771         { this->_M_insert_after(cbefore_begin(),
00772                                 std::forward<_Args>(__args)...); }
00773 
00774       /**
00775        *  @brief  Add data to the front of the %forward_list.
00776        *  @param  val  Data to be added.
00777        *
00778        *  This is a typical stack operation.  The function creates an
00779        *  element at the front of the %forward_list and assigns the given
00780        *  data to it.  Due to the nature of a %forward_list this operation
00781        *  can be done in constant time, and does not invalidate iterators
00782        *  and references.
00783        */
00784       void
00785       push_front(const _Tp& __val)
00786       { this->_M_insert_after(cbefore_begin(), __val); }
00787 
00788       /**
00789        *
00790        */
00791       void
00792       push_front(_Tp&& __val)
00793       { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
00794 
00795       /**
00796        *  @brief  Removes first element.
00797        *
00798        *  This is a typical stack operation.  It shrinks the %forward_list
00799        *  by one.  Due to the nature of a %forward_list this operation can
00800        *  be done in constant time, and only invalidates iterators/references
00801        *  to the element being removed.
00802        *
00803        *  Note that no data is returned, and if the first element's data
00804        *  is needed, it should be retrieved before pop_front() is
00805        *  called.
00806        */
00807       void
00808       pop_front()
00809       { this->_M_erase_after(&this->_M_impl._M_head); }
00810 
00811       /**
00812        *  @brief  Constructs object in %forward_list after the specified
00813        *          iterator.
00814        *  @param  pos  A const_iterator into the %forward_list.
00815        *  @param  args  Arguments.
00816        *  @return  An iterator that points to the inserted data.
00817        *
00818        *  This function will insert an object of type T constructed
00819        *  with T(std::forward<Args>(args)...) after the specified
00820        *  location.  Due to the nature of a %forward_list this operation can
00821        *  be done in constant time, and does not invalidate iterators
00822        *  and references.
00823        */
00824       template<typename... _Args>
00825         iterator
00826         emplace_after(const_iterator __pos, _Args&&... __args)
00827         { return iterator(this->_M_insert_after(__pos,
00828                                           std::forward<_Args>(__args)...)); }
00829 
00830       /**
00831        *  @brief  Inserts given value into %forward_list after specified
00832        *          iterator.
00833        *  @param  pos  An iterator into the %forward_list.
00834        *  @param  val  Data to be inserted.
00835        *  @return  An iterator that points to the inserted data.
00836        *
00837        *  This function will insert a copy of the given value after
00838        *  the specified location.  Due to the nature of a %forward_list this
00839        *  operation can be done in constant time, and does not
00840        *  invalidate iterators and references.
00841        */
00842       iterator
00843       insert_after(const_iterator __pos, const _Tp& __val)
00844       { return iterator(this->_M_insert_after(__pos, __val)); }
00845 
00846       /**
00847        *
00848        */
00849       iterator
00850       insert_after(const_iterator __pos, _Tp&& __val)
00851       { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
00852 
00853       /**
00854        *  @brief  Inserts a number of copies of given data into the
00855        *          %forward_list.
00856        *  @param  pos  An iterator into the %forward_list.
00857        *  @param  n  Number of elements to be inserted.
00858        *  @param  val  Data to be inserted.
00859        *  @return  pos.
00860        *
00861        *  This function will insert a specified number of copies of the
00862        *  given data after the location specified by @a pos.
00863        *
00864        *  This operation is linear in the number of elements inserted and
00865        *  does not invalidate iterators and references.
00866        */
00867       iterator
00868       insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
00869       {
00870         forward_list __tmp(__n, __val, this->_M_get_Node_allocator());
00871         splice_after(__pos, std::move(__tmp));
00872     return iterator(__const_pointer_cast<typename _Node_base::_Pointer>
00873             (__pos._M_node));
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        *  @return  pos.
00882        *
00883        *  This function will insert copies of the data in the range [@a
00884        *  first,@a last) into the %forward_list after the location specified
00885        *  by @a pos.
00886        *
00887        *  This operation is linear in the number of elements inserted and
00888        *  does not invalidate iterators and references.
00889        */
00890       template<typename _InputIterator>
00891         iterator
00892         insert_after(const_iterator __pos,
00893                      _InputIterator __first, _InputIterator __last)
00894         {
00895           forward_list __tmp(__first, __last, this->_M_get_Node_allocator());
00896           splice_after(__pos, std::move(__tmp));
00897       return iterator(__const_pointer_cast<typename _Node_base::_Pointer>
00898               (__pos._M_node));
00899         }
00900 
00901       /**
00902        *  @brief  Inserts the contents of an initializer_list into
00903        *          %forward_list after the specified iterator.
00904        *  @param  pos  An iterator into the %forward_list.
00905        *  @param  il  An initializer_list of value_type.
00906        *  @return  pos.
00907        *
00908        *  This function will insert copies of the data in the
00909        *  initializer_list @a il into the %forward_list before the location
00910        *  specified by @a pos.
00911        *
00912        *  This operation is linear in the number of elements inserted and
00913        *  does not invalidate iterators and references.
00914        */
00915       iterator
00916       insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
00917       {
00918         forward_list __tmp(__il, this->_M_get_Node_allocator());
00919         splice_after(__pos, std::move(__tmp));
00920     return iterator(__const_pointer_cast<typename _Node_base::_Pointer>
00921             (__pos._M_node));
00922       }
00923 
00924       /**
00925        *  @brief  Removes the element pointed to by the iterator following
00926        *          @c pos.
00927        *  @param  pos  Iterator pointing before element to be erased.
00928        *
00929        *  This function will erase the element at the given position and
00930        *  thus shorten the %forward_list by one.
00931        *
00932        *  Due to the nature of a %forward_list this operation can be done
00933        *  in constant time, and only invalidates iterators/references to
00934        *  the element being removed.  The user is also cautioned that
00935        *  this function only erases the element, and that if the element
00936        *  is itself a pointer, the pointed-to memory is not touched in
00937        *  any way.  Managing the pointer is the user's responsibility.
00938        */
00939       void
00940       erase_after(const_iterator __pos)
00941       {
00942         _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node);
00943     this->_M_erase_after(__tmp);
00944       }
00945 
00946       /**
00947        *  @brief  Remove a range of elements.
00948        *  @param  pos  Iterator pointing before the first element to be
00949        *               erased.
00950        *  @param  last  Iterator pointing to one past the last element to be
00951        *                erased.
00952        *
00953        *  This function will erase the elements in the range @a
00954        *  (pos,last) and shorten the %forward_list accordingly.
00955        *
00956        *  This operation is linear time in the size of the range and only
00957        *  invalidates iterators/references to the element being removed.
00958        *  The user is also cautioned that this function only erases the
00959        *  elements, and that if the elements themselves are pointers, the
00960        *  pointed-to memory is not touched in any way.  Managing the pointer
00961        *  is the user's responsibility.
00962        */
00963       void
00964       erase_after(const_iterator __pos, const_iterator __last)
00965       {
00966         _Node_base* __tmpp = __const_pointer_cast<_Node_base*>(__pos._M_node);
00967     _Node_base* __tmpl = __const_pointer_cast<_Node_base*>(__last._M_node);
00968         this->_M_erase_after(__tmpp, __tmpl);
00969       }
00970 
00971       /**
00972        *  @brief  Swaps data with another %forward_list.
00973        *  @param  list  A %forward_list of the same element and allocator
00974        *                types.
00975        *
00976        *  This exchanges the elements between two lists in constant
00977        *  time.  Note that the global std::swap() function is
00978        *  specialized such that std::swap(l1,l2) will feed to this
00979        *  function.
00980        */
00981       void
00982       swap(forward_list& __list)
00983       { _Node_base::swap(this->_M_impl._M_head, __list._M_impl._M_head); }
00984 
00985       /**
00986        *  @brief Resizes the %forward_list to the specified number of
00987        *         elements.
00988        *  @param sz Number of elements the %forward_list should contain.
00989        *
00990        *  This function will %resize the %forward_list to the specified
00991        *  number of elements.  If the number is smaller than the
00992        *  %forward_list's current size the %forward_list is truncated,
00993        *  otherwise the %forward_list is extended and the new elements
00994        *  are default constructed.
00995        */
00996       void
00997       resize(size_type __sz);
00998 
00999       /**
01000        *  @brief Resizes the %forward_list to the specified number of
01001        *         elements.
01002        *  @param sz Number of elements the %forward_list should contain.
01003        *  @param val Data with which new elements should be populated.
01004        *
01005        *  This function will %resize the %forward_list to the specified
01006        *  number of elements.  If the number is smaller than the
01007        *  %forward_list's current size the %forward_list is truncated,
01008        *  otherwise the %forward_list is extended and new elements are
01009        *  populated with given data.
01010        */
01011       void
01012       resize(size_type __sz, value_type __val);
01013 
01014       /**
01015        *  @brief  Erases all the elements.
01016        *
01017        *  Note that this function only erases
01018        *  the elements, and that if the elements themselves are
01019        *  pointers, the pointed-to memory is not touched in any way.
01020        *  Managing the pointer is the user's responsibility.
01021        */
01022       void
01023       clear()
01024       { this->_M_erase_after(&this->_M_impl._M_head, 0); }
01025 
01026       // 23.2.3.5 forward_list operations:
01027 
01028       /**
01029        *  @brief  Insert contents of another %forward_list.
01030        *  @param  pos  Iterator referencing the element to insert after.
01031        *  @param  list  Source list.
01032        *
01033        *  The elements of @a list are inserted in constant time after
01034        *  the element referenced by @a pos.  @a list becomes an empty
01035        *  list.
01036        *
01037        *  Requires this != @a x.
01038        */
01039       void
01040       splice_after(const_iterator __pos, forward_list&& __list);
01041 
01042       /**
01043        *  @brief  Insert element from another %forward_list.
01044        *  @param  pos  Iterator referencing the element to insert after.
01045        *  @param  list  Source list.
01046        *  @param  i   Iterator referencing the element before the element
01047        *              to move.
01048        *
01049        *  Removes the element in list @a list referenced by @a i and
01050        *  inserts it into the current list after @a pos.
01051        */
01052       void
01053       splice_after(const_iterator __pos, forward_list&& __list,
01054                    const_iterator __i)
01055       {
01056     const_iterator __j = __i;
01057     ++__j;
01058     if (__pos == __i || __pos == __j)
01059       return;
01060 
01061     splice_after(__pos, std::move(__list), __i, __j);
01062       }
01063 
01064       /**
01065        *  @brief  Insert range from another %forward_list.
01066        *  @param  pos  Iterator referencing the element to insert after.
01067        *  @param  list  Source list.
01068        *  @param  before  Iterator referencing before the start of range
01069        *                  in list.
01070        *  @param  last  Iterator referencing the end of range in list.
01071        *
01072        *  Removes elements in the range (before,last) and inserts them
01073        *  after @a pos in constant time.
01074        *
01075        *  Undefined if @a pos is in (before,last).
01076        */
01077       void
01078       splice_after(const_iterator __pos, forward_list&& __list,
01079                    const_iterator __before, const_iterator __last);
01080 
01081       /**
01082        *  @brief  Remove all elements equal to value.
01083        *  @param  val  The value to remove.
01084        *
01085        *  Removes every element in the list equal to @a value.
01086        *  Remaining elements stay in list order.  Note that this
01087        *  function only erases the elements, and that if the elements
01088        *  themselves are pointers, the pointed-to memory is not
01089        *  touched in any way.  Managing the pointer is the user's
01090        *  responsibility.
01091        */
01092       void
01093       remove(const _Tp& __val);
01094 
01095       /**
01096        *  @brief  Remove all elements satisfying a predicate.
01097        *  @param  pred  Unary predicate function or object.
01098        *
01099        *  Removes every element in the list for which the predicate
01100        *  returns true.  Remaining elements stay in list order.  Note
01101        *  that this function only erases the elements, and that if the
01102        *  elements themselves are pointers, the pointed-to memory is
01103        *  not touched in any way.  Managing the pointer is the user's
01104        *  responsibility.
01105        */
01106       template<typename _Pred>
01107         void
01108         remove_if(_Pred __pred);
01109 
01110       /**
01111        *  @brief  Remove consecutive duplicate elements.
01112        *
01113        *  For each consecutive set of elements with the same value,
01114        *  remove all but the first one.  Remaining elements stay in
01115        *  list order.  Note that this function only erases the
01116        *  elements, and that if the elements themselves are pointers,
01117        *  the pointed-to memory is not touched in any way.  Managing
01118        *  the pointer is the user's responsibility.
01119        */
01120       void
01121       unique()
01122       { this->unique(std::equal_to<_Tp>()); }
01123 
01124       /**
01125        *  @brief  Remove consecutive elements satisfying a predicate.
01126        *  @param  binary_pred  Binary predicate function or object.
01127        *
01128        *  For each consecutive set of elements [first,last) that
01129        *  satisfy predicate(first,i) where i is an iterator in
01130        *  [first,last), remove all but the first one.  Remaining
01131        *  elements stay in list order.  Note that this function only
01132        *  erases the elements, and that if the elements themselves are
01133        *  pointers, the pointed-to memory is not touched in any way.
01134        *  Managing the pointer is the user's responsibility.
01135        */
01136       template<typename _BinPred>
01137         void
01138         unique(_BinPred __binary_pred);
01139 
01140       /**
01141        *  @brief  Merge sorted lists.
01142        *  @param  list  Sorted list to merge.
01143        *
01144        *  Assumes that both @a list and this list are sorted according to
01145        *  operator<().  Merges elements of @a list into this list in
01146        *  sorted order, leaving @a list empty when complete.  Elements in
01147        *  this list precede elements in @a list that are equal.
01148        */
01149       void
01150       merge(forward_list&& __list)
01151       { this->merge(std::move(__list), std::less<_Tp>()); }
01152 
01153       /**
01154        *  @brief  Merge sorted lists according to comparison function.
01155        *  @param  list  Sorted list to merge.
01156        *  @param  comp Comparison function defining sort order.
01157        *
01158        *  Assumes that both @a list and this list are sorted according to
01159        *  comp.  Merges elements of @a list into this list
01160        *  in sorted order, leaving @a list empty when complete.  Elements
01161        *  in this list precede elements in @a list that are equivalent
01162        *  according to comp().
01163        */
01164       template<typename _Comp>
01165         void
01166         merge(forward_list&& __list, _Comp __comp);
01167 
01168       /**
01169        *  @brief  Sort the elements of the list.
01170        *
01171        *  Sorts the elements of this list in NlogN time.  Equivalent
01172        *  elements remain in list order.
01173        */
01174       void
01175       sort()
01176       { this->sort(std::less<_Tp>()); }
01177 
01178       /**
01179        *  @brief  Sort the forward_list using a comparison function.
01180        *
01181        *  Sorts the elements of this list in NlogN time.  Equivalent
01182        *  elements remain in list order.
01183        */
01184       template<typename _Comp>
01185         void
01186         sort(_Comp __comp);
01187 
01188       /**
01189        *  @brief  Reverse the elements in list.
01190        *
01191        *  Reverse the order of elements in the list in linear time.
01192        */
01193       void
01194       reverse()
01195       { this->_M_impl._M_head._M_reverse_after(); }
01196 
01197     private:
01198       template<typename _Integer>
01199         void
01200         _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
01201         { _M_fill_initialize(static_cast<size_type>(__n), __x); }
01202 
01203       // Called by the range constructor to implement [23.1.1]/9
01204       template<typename _InputIterator>
01205         void
01206         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
01207                                __false_type);
01208 
01209       // Called by forward_list(n,v,a), and the range constructor when it
01210       // turns out to be the same thing.
01211       void
01212       _M_fill_initialize(size_type __n, const value_type& __value);
01213     };
01214 
01215   /**
01216    *  @brief  Forward list equality comparison.
01217    *  @param  lx  A %forward_list
01218    *  @param  ly  A %forward_list of the same type as @a lx.
01219    *  @return  True iff the size and elements of the forward lists are equal.
01220    *
01221    *  This is an equivalence relation.  It is linear in the size of the
01222    *  forward lists.  Deques are considered equivalent if corresponding
01223    *  elements compare equal.
01224    */
01225   template<typename _Tp, typename _Alloc>
01226     bool
01227     operator==(const forward_list<_Tp, _Alloc>& __lx,
01228                const forward_list<_Tp, _Alloc>& __ly);
01229 
01230   /**
01231    *  @brief  Forward list ordering relation.
01232    *  @param  lx  A %forward_list.
01233    *  @param  ly  A %forward_list of the same type as @a lx.
01234    *  @return  True iff @a lx is lexicographically less than @a ly.
01235    *
01236    *  This is a total ordering relation.  It is linear in the size of the
01237    *  forward lists.  The elements must be comparable with @c <.
01238    *
01239    *  See std::lexicographical_compare() for how the determination is made.
01240    */
01241   template<typename _Tp, typename _Alloc>
01242     inline bool
01243     operator<(const forward_list<_Tp, _Alloc>& __lx,
01244               const forward_list<_Tp, _Alloc>& __ly)
01245     { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
01246                       __ly.cbegin(), __ly.cend()); }
01247 
01248   /// Based on operator==
01249   template<typename _Tp, typename _Alloc>
01250     inline bool
01251     operator!=(const forward_list<_Tp, _Alloc>& __lx,
01252                const forward_list<_Tp, _Alloc>& __ly)
01253     { return !(__lx == __ly); }
01254 
01255   /// Based on operator<
01256   template<typename _Tp, typename _Alloc>
01257     inline bool
01258     operator>(const forward_list<_Tp, _Alloc>& __lx,
01259               const forward_list<_Tp, _Alloc>& __ly)
01260     { return (__ly < __lx); }
01261 
01262   /// Based on operator<
01263   template<typename _Tp, typename _Alloc>
01264     inline bool
01265     operator>=(const forward_list<_Tp, _Alloc>& __lx,
01266                const forward_list<_Tp, _Alloc>& __ly)
01267     { return !(__lx < __ly); }
01268 
01269   /// Based on operator<
01270   template<typename _Tp, typename _Alloc>
01271     inline bool
01272     operator<=(const forward_list<_Tp, _Alloc>& __lx,
01273                const forward_list<_Tp, _Alloc>& __ly)
01274     { return !(__ly < __lx); }
01275 
01276   /// See std::forward_list::swap().
01277   template<typename _Tp, typename _Alloc>
01278     inline void
01279     swap(forward_list<_Tp, _Alloc>& __lx,
01280      forward_list<_Tp, _Alloc>& __ly)
01281     { __lx.swap(__ly); }
01282 
01283 _GLIBCXX_END_NAMESPACE // namespace std
01284 
01285 #endif // __GXX_EXPERIMENTAL_CXX0X__
01286 
01287 #endif // _FORWARD_LIST_H

Generated on 9 Feb 2010 for libstdc++ by  doxygen 1.6.1