OSDN Git Service

2009-02-18 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / forward_list.h
1 // <forward_list.h> -*- C++ -*-
2
3 // Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file forward_list.h
31  *  This is a Standard C++ Library header.
32  */
33
34 #ifndef _FORWARD_LIST_H
35 #define _FORWARD_LIST_H 1
36
37 #pragma GCC system_header
38
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
41 #else
42
43 #include <memory>
44 #include <initializer_list>
45 #include <ext/cast.h>
46
47 _GLIBCXX_BEGIN_NAMESPACE(std)
48
49   using __gnu_cxx::__static_pointer_cast;
50   using __gnu_cxx::__const_pointer_cast;
51
52   /**
53    *  @brief  A helper basic node class for @forward_list.
54    *          This is just a linked list with nothing inside it.
55    *          There are purely list shuffling utility methods here.
56    */
57   template<typename _Alloc>
58     struct _Fwd_list_node_base
59     {
60       // The type allocated by _Alloc cannot be this type, so we rebind
61       typedef typename _Alloc::template rebind<_Fwd_list_node_base<_Alloc> >
62         ::other::pointer        _Pointer;
63       typedef typename _Alloc::template rebind<_Fwd_list_node_base<_Alloc> >
64         ::other::const_pointer  _Const_pointer;
65   
66       _Pointer _M_next;
67   
68       _Fwd_list_node_base() : _M_next(0) { }
69   
70       static void
71       swap(_Fwd_list_node_base& __x, _Fwd_list_node_base& __y)
72       { std::swap(__x._M_next, __y._M_next); }
73   
74       void
75       _M_transfer_after(_Pointer __bbegin, _Pointer __bend)
76       { 
77         _Pointer __keep = __bbegin->_M_next;
78         if (__bend)
79           {
80             __bbegin->_M_next = __bend->_M_next;
81             __bend->_M_next = this->_M_next;
82           }
83         else
84           __bbegin->_M_next = 0;
85         this->_M_next = __keep;
86       }
87   
88       void
89       _M_transfer_after(_Pointer __bbegin)
90       {
91         _Pointer __bend = __bbegin;
92         while (__bend && __bend->_M_next)
93           __bend = __bend->_M_next;
94         _M_transfer_after(__bbegin, __bend);
95       }
96   
97       void
98       _M_reverse_after()
99       { 
100         _Pointer __tail = this->_M_next;
101         if (!__tail)
102           return;
103         while (_Pointer __temp = __tail->_M_next)
104           {
105             _Pointer __keep = this->_M_next;
106             this->_M_next = __temp;
107             __tail->_M_next = __temp->_M_next;
108             this->_M_next->_M_next = __keep;
109           }
110       }    
111     };
112
113   /**
114    *  @brief  A helper node class for @forward_list.
115    *          This is just a linked list with a data value in each node.
116    *          There is a sorting utility method.
117    */
118   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
119     struct _Fwd_list_node : public _Fwd_list_node_base<_Alloc>
120     {
121       typedef typename _Alloc::template rebind<_Fwd_list_node<_Tp, _Alloc> >
122         ::other::pointer        _Pointer;
123
124       template<typename... _Args>
125         _Fwd_list_node(_Args&&... __args)
126         : _Fwd_list_node_base<_Alloc>(), 
127           _M_value(std::forward<_Args>(__args)...) { }
128
129       template<typename _Comp>
130         void
131         _M_sort_after(_Comp __comp);
132
133       _Tp _M_value;
134     };
135
136   /**
137    *   @brief A forward_list::iterator.
138    * 
139    *   All the functions are op overloads.
140    */
141   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
142     struct _Fwd_list_iterator
143     {
144       typedef _Fwd_list_iterator<_Tp, _Alloc>   _Self;
145       typedef _Fwd_list_node<_Tp, _Alloc>       _Node;
146       typedef _Fwd_list_node_base<_Alloc>       _Node_base;
147
148       typedef _Tp                               value_type;
149       typedef typename _Alloc::pointer          pointer;
150       typedef typename _Alloc::reference        reference;
151       typedef typename _Alloc::difference_type  difference_type;
152       typedef std::forward_iterator_tag         iterator_category;
153
154       _Fwd_list_iterator() : _M_node() { }
155
156       explicit
157       _Fwd_list_iterator(typename _Node_base::_Pointer __n) 
158       : _M_node(__n) { }
159
160       reference
161       operator*() const
162       { return __static_pointer_cast<_Node*>(this->_M_node)->_M_value; }
163
164       pointer
165       operator->() const
166       { return &__static_pointer_cast<_Node*>(this->_M_node)->_M_value; }
167
168       _Self&
169       operator++()
170       {
171         this->_M_node = this->_M_node->_M_next;
172         return *this;
173       }
174
175       _Self
176       operator++(int)
177       {
178         _Self __tmp(*this);
179         this->_M_node = this->_M_node->_M_next;
180         return __tmp;
181       }
182
183       bool
184       operator==(const _Self& __x) const
185       { return this->_M_node == __x._M_node; }
186
187       bool
188       operator!=(const _Self& __x) const
189       { return this->_M_node != __x._M_node; }
190
191       _Self
192       _M_next() const
193       {
194         if (_M_node)
195           return _Fwd_list_iterator(this->_M_node->_M_next);
196         else
197           return _Fwd_list_iterator(0);
198       }
199
200       typename _Node_base::_Pointer _M_node;
201     };
202
203   /**
204    *   @brief A forward_list::const_iterator.
205    * 
206    *   All the functions are op overloads.
207    */
208   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
209     struct _Fwd_list_const_iterator
210     {
211       typedef _Fwd_list_const_iterator<_Tp, _Alloc>   _Self;
212       typedef const _Fwd_list_node<_Tp, _Alloc>       _Node;
213       typedef const _Fwd_list_node_base<_Alloc>       _Node_base;
214       typedef _Fwd_list_iterator<_Tp, _Alloc>         iterator;
215
216       typedef _Tp                                     value_type;
217       typedef typename _Alloc::const_pointer          pointer;
218       typedef typename _Alloc::const_reference        reference;
219       typedef typename _Alloc::difference_type        difference_type;
220       typedef std::forward_iterator_tag               iterator_category;
221
222       _Fwd_list_const_iterator() : _M_node() { }
223
224       explicit
225       _Fwd_list_const_iterator(typename _Node_base::_Const_pointer __n) 
226       : _M_node(__n) { }
227
228       _Fwd_list_const_iterator(const iterator& __iter)
229       : _M_node(__iter._M_node) { }
230
231       reference
232       operator*() const
233       { return __static_pointer_cast<_Node*>(this->_M_node)->_M_value; }
234
235       pointer
236       operator->() const
237       { return &__static_pointer_cast<_Node*>(this->_M_node)->_M_value; }
238
239       _Self&
240       operator++()
241       {
242         this->_M_node = this->_M_node->_M_next;
243         return *this;
244       }
245
246       _Self
247       operator++(int)
248       {
249         _Self __tmp(*this);
250         this->_M_node = this->_M_node->_M_next;
251         return __tmp;
252       }
253
254       bool
255       operator==(const _Self& __x) const
256       { return this->_M_node == __x._M_node; }
257
258       bool
259       operator!=(const _Self& __x) const
260       { return this->_M_node != __x._M_node; }
261
262       _Self
263       _M_next() const
264       {
265         if (this->_M_node)
266           return _Fwd_list_const_iterator(this->_M_node->_M_next);
267         else
268           return _Fwd_list_const_iterator(0);
269       }
270
271       typename _Node_base::_Const_pointer _M_node;
272     };
273
274   /**
275    *  @brief  Forward list iterator equality comparison.
276    */
277   template<typename _Tp,class _Alloc>
278     inline bool
279     operator==(const _Fwd_list_iterator<_Tp, _Alloc>& __x,
280                const _Fwd_list_const_iterator<_Tp, _Alloc>& __y)
281     { return __x._M_node == __y._M_node; }
282
283   /**
284    *  @brief  Forward list iterator inequality comparison.
285    */
286   template<typename _Tp,class _Alloc>
287     inline bool
288     operator!=(const _Fwd_list_iterator<_Tp, _Alloc>& __x,
289                const _Fwd_list_const_iterator<_Tp, _Alloc>& __y)
290     { return __x._M_node != __y._M_node; }
291
292   /**
293    *  @brief  Base class for @forward_list.
294    */
295   template<typename _Tp, typename _Alloc = allocator<_Tp> >
296     struct _Fwd_list_base
297     {
298     protected:
299       typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
300
301       typedef typename _Alloc::template 
302         rebind<_Fwd_list_node<_Tp, _Tp_alloc_type>>::other _Node_alloc_type;
303
304       struct _Fwd_list_impl 
305       : public _Node_alloc_type
306       {
307         _Fwd_list_node_base<_Tp_alloc_type> _M_head;
308
309         _Fwd_list_impl()
310         : _Node_alloc_type(), _M_head()
311         { }
312
313         _Fwd_list_impl(const _Node_alloc_type& __a)
314         : _Node_alloc_type(__a), _M_head()
315         { }
316       };
317
318       _Fwd_list_impl _M_impl;
319
320     public:
321       typedef _Fwd_list_iterator<_Tp, _Tp_alloc_type>        iterator;
322       typedef _Fwd_list_const_iterator<_Tp, _Tp_alloc_type>  const_iterator;
323
324       typedef _Fwd_list_node<_Tp, _Tp_alloc_type>            _Node;
325       typedef _Fwd_list_node_base<_Tp_alloc_type>            _Node_base;
326
327       _Node_alloc_type&
328       _M_get_Node_allocator()
329       { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
330
331       const _Node_alloc_type&
332       _M_get_Node_allocator() const
333       { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
334
335       _Fwd_list_base()
336       : _M_impl()
337       { this->_M_impl._M_head._M_next = 0; }
338
339       _Fwd_list_base(const _Alloc& __a)
340       : _M_impl(__a)
341       { this->_M_impl._M_head._M_next = 0; }
342
343       _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a);
344
345       _Fwd_list_base(_Fwd_list_base&& __lst, const _Alloc& __a)
346       : _M_impl(__a)
347       { _Node_base::swap(this->_M_impl._M_head, 
348                          __lst._M_impl._M_head); }
349
350       _Fwd_list_base(_Fwd_list_base&& __lst)
351       : _M_impl(__lst._M_get_Node_allocator())
352       { _Node_base::swap(this->_M_impl._M_head, 
353                          __lst._M_impl._M_head); }
354
355       ~_Fwd_list_base()
356       { _M_erase_after(&_M_impl._M_head, 0); }
357
358     protected:
359
360       typename _Node::_Pointer
361       _M_get_node()
362       { return _M_get_Node_allocator().allocate(1); }
363
364       template<typename... _Args>
365         typename _Node::_Pointer
366         _M_create_node(_Args&&... __args)
367         {
368           typename _Node::_Pointer __node = this->_M_get_node();
369           __try
370             {
371               _M_get_Node_allocator().construct(__node,
372                                               std::forward<_Args>(__args)...);
373               __node->_M_next = 0;
374             }
375           __catch(...)
376             {
377               this->_M_put_node(__node);
378               __throw_exception_again;
379             }
380           return __node;
381         }
382
383       template<typename... _Args>
384         typename _Node_base::_Pointer
385         _M_insert_after(const_iterator __pos, _Args&&... __args);
386
387       void
388       _M_put_node(typename _Node::_Pointer __p)
389       { _M_get_Node_allocator().deallocate(__p, 1); }
390
391       typename _Node_base::_Pointer
392       _M_erase_after(typename _Node_base::_Pointer __pos);
393
394       typename _Node_base::_Pointer
395       _M_erase_after(typename _Node_base::_Pointer __pos, 
396                      typename _Node_base::_Pointer __last);
397     };
398
399   /**
400    *  @brief A standard container with linear time access to elements,
401    *  and fixed time insertion/deletion at any point in the sequence.
402    *
403    *  @ingroup containers
404    *  @ingroup sequences
405    *
406    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
407    *  <a href="tables.html#67">sequence</a>, including the
408    *  <a href="tables.html#68">optional sequence requirements</a> with the
409    *  %exception of @c at and @c operator[].
410    *
411    *  This is a @e singly @e linked %list.  Traversal up the
412    *  %list requires linear time, but adding and removing elements (or
413    *  @e nodes) is done in constant time, regardless of where the
414    *  change takes place.  Unlike std::vector and std::deque,
415    *  random-access iterators are not provided, so subscripting ( @c
416    *  [] ) access is not allowed.  For algorithms which only need
417    *  sequential access, this lack makes no difference.
418    *
419    *  Also unlike the other standard containers, std::forward_list provides
420    *  specialized algorithms %unique to linked lists, such as
421    *  splicing, sorting, and in-place reversal.
422    *
423    *  A couple points on memory allocation for forward_list<Tp>:
424    *
425    *  First, we never actually allocate a Tp, we allocate
426    *  Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT.  This is to ensure
427    *  that after elements from %forward_list<X,Alloc1> are spliced into
428    *  %forward_list<X,Alloc2>, destroying the memory of the second %list is a
429    *  valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
430    */
431   template<typename _Tp, typename _Alloc = allocator<_Tp> >
432     class forward_list : private _Fwd_list_base<_Tp, _Alloc>
433     {
434     private:
435       typedef _Fwd_list_base<_Tp, _Alloc>                  _Base;
436       typedef typename _Base::_Node                        _Node;
437       typedef typename _Base::_Node_base                   _Node_base;
438       typedef typename _Base::_Tp_alloc_type               _Tp_alloc_type;
439
440     public:
441       // types:
442       typedef _Tp                                          value_type;
443       typedef typename _Tp_alloc_type::pointer             pointer;
444       typedef typename _Tp_alloc_type::const_pointer       const_pointer;
445       typedef typename _Tp_alloc_type::reference           reference;
446       typedef typename _Tp_alloc_type::const_reference     const_reference;
447  
448       typedef typename _Base::iterator                     iterator;
449       typedef typename _Base::const_iterator               const_iterator;
450       typedef std::size_t                                  size_type;
451       typedef std::ptrdiff_t                               difference_type;
452       typedef _Alloc                                       allocator_type;
453
454       // 23.2.3.1 construct/copy/destroy:
455
456       /**
457        *  @brief  Creates a %forward_list with no elements.
458        *  @param  al  An allocator object.
459        */
460       explicit
461       forward_list(const _Alloc& __al = _Alloc())
462       : _Base(__al)
463       { }
464
465       /**
466        *  @brief  Copy constructor with allocator argument.
467        *  @param  list  Input list to copy.
468        *  @param  al    An allocator object.
469        */
470       forward_list(const forward_list& __list, const _Alloc& __al)
471       : _Base(__list, __al)
472       { }
473
474       /**
475        *  @brief  Move constructor with allocator argument.
476        *  @param  list  Input list to move.
477        *  @param  al    An allocator object.
478        */
479       forward_list(forward_list&& __list, const _Alloc& __al)
480       : _Base(std::forward<_Base>(__list), __al)
481       { }
482
483       /**
484        *  @brief  Creates a %forward_list with copies of the default element
485        *          type.
486        *  @param  n  The number of elements to initially create.
487        *
488        *  This constructor fills the %forward_list with @a n copies of
489        *  the default value.
490        */
491       explicit
492       forward_list(size_type __n)
493       : _Base()
494       { _M_fill_initialize(__n, value_type()); }
495
496       /**
497        *  @brief  Creates a %forward_list with copies of an exemplar element.
498        *  @param  n      The number of elements to initially create.
499        *  @param  value  An element to copy.
500        *  @param  al     An allocator object.
501        *
502        *  This constructor fills the %forward_list with @a n copies of @a
503        *  value.
504        */
505       forward_list(size_type __n, const _Tp& __value,
506                    const _Alloc& __al = _Alloc())
507       : _Base(__al)
508       { _M_fill_initialize(__n, __value); }
509
510       /**
511        *  @brief  Builds a %forward_list from a range.
512        *  @param  first  An input iterator.
513        *  @param  last   An input iterator.
514        *  @param  al     An allocator object.
515        *
516        *  Create a %forward_list consisting of copies of the elements from
517        *  [@a first,@a last).  This is linear in N (where N is
518        *  distance(@a first,@a last)).
519        */
520       template<typename _InputIterator>
521         forward_list(_InputIterator __first, _InputIterator __last,
522                      const _Alloc& __al = _Alloc())
523         : _Base(__al)
524         {
525           // Check whether it's an integral type.  If so, it's not an iterator.
526           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
527           _M_initialize_dispatch(__first, __last, _Integral());
528         }
529
530       /**
531        *  @brief  The %forward_list copy constructor.
532        *  @param  list  A %forward_list of identical element and allocator
533        *                types.
534        *
535        *  The newly-created %forward_list uses a copy of the allocation
536        *  object used by @a list.
537        */
538       forward_list(const forward_list& __list)
539       : _Base(__list.get_allocator())
540       { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
541
542       /**
543        *  @brief  The %forward_list move constructor.
544        *  @param  list  A %forward_list of identical element and allocator
545        *                types.
546        *
547        *  The newly-created %forward_list contains the exact contents of @a
548        *  forward_list. The contents of @a list are a valid, but unspecified
549        *  %forward_list.
550        */
551       forward_list(forward_list&& __list)
552       : _Base(std::forward<_Base>(__list)) { }
553
554       /**
555        *  @brief  Builds a %forward_list from an initializer_list
556        *  @param  il  An initializer_list of value_type.
557        *  @param  al  An allocator object.
558        *
559        *  Create a %forward_list consisting of copies of the elements
560        *  in the initializer_list @a il.  This is linear in il.size().
561        */
562       forward_list(std::initializer_list<_Tp> __il,
563                    const _Alloc& __al = _Alloc())
564       : _Base(__al)
565       { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
566
567       /**
568        *  @brief  The forward_list dtor.
569        */
570       ~forward_list()
571       { _M_erase_after(&this->_M_impl._M_head, 0); }
572
573       /**
574        *  @brief  The %forward_list assignment operator.
575        *  @param  list  A %forward_list of identical element and allocator
576        *                types.
577        *
578        *  All the elements of @a list are copied, but unlike the copy
579        *  constructor, the allocator object is not copied.
580        */
581       forward_list&
582       operator=(const forward_list& __list);
583
584       /**
585        *  @brief  The %forward_list move assignment operator.
586        *  @param  list  A %forward_list of identical element and allocator
587        *                types.
588        *
589        *  The contents of @a list are moved into this %forward_list
590        *  (without copying). @a list is a valid, but unspecified
591        *  %forward_list
592        */
593       forward_list&
594       operator=(forward_list&& __list)
595       {
596         if (&__list != this)
597           {
598             this->clear();
599             this->swap(__list);
600           }
601         return *this;
602       }
603
604       /**
605        *  @brief  The %forward_list initializer list assignment operator.
606        *  @param  il  An initializer_list of value_type.
607        *
608        *  Replace the contents of the %forward_list with copies of the
609        *  elements in the initializer_list @a il.  This is linear in
610        *  il.size().
611        */
612       forward_list&
613       operator=(std::initializer_list<_Tp> __il)
614       {
615         assign(__il);
616         return *this;
617       }
618
619       /**
620        *  @brief  Assigns a range to a %forward_list.
621        *  @param  first  An input iterator.
622        *  @param  last   An input iterator.
623        *
624        *  This function fills a %forward_list with copies of the elements
625        *  in the range [@a first,@a last).
626        *
627        *  Note that the assignment completely changes the %forward_list and
628        *  that the resulting %forward_list's size is the same as the number
629        *  of elements assigned.  Old data may be lost.
630        */
631       template<typename _InputIterator>
632         void
633         assign(_InputIterator __first, _InputIterator __last)
634         {
635           clear();
636           insert_after(cbefore_begin(), __first, __last);
637         }
638
639       /**
640        *  @brief  Assigns a given value to a %forward_list.
641        *  @param  n  Number of elements to be assigned.
642        *  @param  val  Value to be assigned.
643        *
644        *  This function fills a %forward_list with @a n copies of the given
645        *  value.  Note that the assignment completely changes the
646        *  %forward_list and that the resulting %forward_list's size is the
647        *  same as the number of elements assigned.  Old data may be lost.
648        */
649       void
650       assign(size_type __n, const _Tp& __val)
651       {
652         clear();
653         insert_after(cbefore_begin(), __n, __val);
654       }
655
656       /**
657        *  @brief  Assigns an initializer_list to a %forward_list.
658        *  @param  il  An initializer_list of value_type.
659        *
660        *  Replace the contents of the %forward_list with copies of the
661        *  elements in the initializer_list @a il.  This is linear in
662        *  il.size().
663        */
664       void
665       assign(std::initializer_list<_Tp> __il)
666       {
667         clear();
668         insert_after(cbefore_begin(), __il);
669       }
670
671       /// Get a copy of the memory allocation object.
672       allocator_type
673       get_allocator() const
674       { return this->_M_get_Node_allocator(); }
675
676       // 23.2.3.2 iterators:
677
678       /**
679        *  Returns a read/write iterator that points before the first element
680        *  in the %forward_list.  Iteration is done in ordinary element order.
681        */
682       iterator
683       before_begin()
684       { return iterator(&this->_M_impl._M_head); }
685
686       /**
687        *  Returns a read-only (constant) iterator that points before the
688        *  first element in the %forward_list.  Iteration is done in ordinary
689        *  element order.
690        */
691       const_iterator
692       before_begin() const
693       { return const_iterator(&this->_M_impl._M_head); }
694
695       /**
696        *  Returns a read/write iterator that points to the first element
697        *  in the %forward_list.  Iteration is done in ordinary element order.
698        */
699       iterator
700       begin()
701       { return iterator(this->_M_impl._M_head._M_next); }
702
703       /**
704        *  Returns a read-only (constant) iterator that points to the first
705        *  element in the %forward_list.  Iteration is done in ordinary
706        *  element order.
707        */
708       const_iterator
709       begin() const
710       { return const_iterator(this->_M_impl._M_head._M_next); }
711
712       /**
713        *  Returns a read/write iterator that points one past the last
714        *  element in the %forward_list.  Iteration is done in ordinary
715        *  element order.
716        */
717       iterator
718       end()
719       { return iterator(0); }
720
721       /**
722        *  Returns a read-only iterator that points one past the last
723        *  element in the %forward_list.  Iteration is done in ordinary
724        *  element order.
725        */
726       const_iterator
727       end() const
728       { return const_iterator(0); }
729
730       /**
731        *  Returns a read-only (constant) iterator that points to the
732        *  first element in the %forward_list.  Iteration is done in ordinary
733        *  element order.
734        */
735       const_iterator
736       cbegin() const
737       { return const_iterator(this->_M_impl._M_head._M_next); }
738
739       /**
740        *  Returns a read-only (constant) iterator that points before the
741        *  first element in the %forward_list.  Iteration is done in ordinary
742        *  element order.
743        */
744       const_iterator
745       cbefore_begin() const
746       { return const_iterator(&this->_M_impl._M_head); }
747
748       /**
749        *  Returns a read-only (constant) iterator that points one past
750        *  the last element in the %forward_list.  Iteration is done in
751        *  ordinary element order.
752        */
753       const_iterator
754       cend() const
755       { return const_iterator(0); }
756
757       /**
758        *  Returns true if the %forward_list is empty.  (Thus begin() would
759        *  equal end().)
760        */
761       bool
762       empty() const
763       { return this->_M_impl._M_head._M_next == 0; }
764
765       /**
766        *  Returns the largest possible size of %forward_list.
767        */
768       size_type
769       max_size() const
770       { return this->_M_get_Node_allocator().max_size(); }
771
772       // 23.2.3.3 element access:
773
774       /**
775        *  Returns a read/write reference to the data at the first
776        *  element of the %forward_list.
777        */
778       reference
779       front()
780       {
781         _Node* __front = __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
782         return __front->_M_value;
783       }
784
785       /**
786        *  Returns a read-only (constant) reference to the data at the first
787        *  element of the %forward_list.
788        */
789       const_reference
790       front() const
791       {
792         _Node* __front =
793           __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
794         return __front->_M_value;
795       }
796
797       // 23.2.3.4 modifiers:
798
799       /**
800        *  @brief  Constructs object in %forward_list at the front of the
801        *          list.
802        *  @param  args  Arguments.
803        *
804        *  This function will insert an object of type Tp constructed
805        *  with Tp(std::forward<Args>(args)...) at the front of the list
806        *  Due to the nature of a %forward_list this operation can
807        *  be done in constant time, and does not invalidate iterators
808        *  and references.
809        */
810       template<typename... _Args>
811         void
812         emplace_front(_Args&&... __args)
813         { this->_M_insert_after(cbefore_begin(),
814                                 std::forward<_Args>(__args)...); }
815
816       /**
817        *  @brief  Add data to the front of the %forward_list.
818        *  @param  val  Data to be added.
819        *
820        *  This is a typical stack operation.  The function creates an
821        *  element at the front of the %forward_list and assigns the given
822        *  data to it.  Due to the nature of a %forward_list this operation
823        *  can be done in constant time, and does not invalidate iterators
824        *  and references.
825        */
826       void
827       push_front(const _Tp& __val)
828       { this->_M_insert_after(cbefore_begin(), __val); }
829
830       /**
831        *
832        */
833       void
834       push_front(_Tp&& __val)
835       { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
836
837       /**
838        *  @brief  Removes first element.
839        *
840        *  This is a typical stack operation.  It shrinks the %forward_list
841        *  by one.  Due to the nature of a %forward_list this operation can
842        *  be done in constant time, and only invalidates iterators/references
843        *  to the element being removed.
844        *
845        *  Note that no data is returned, and if the first element's data
846        *  is needed, it should be retrieved before pop_front() is
847        *  called.
848        */
849       void
850       pop_front()
851       { this->_M_erase_after(&this->_M_impl._M_head); }
852
853       /**
854        *  @brief  Constructs object in %forward_list after the specified
855        *          iterator.
856        *  @param  pos  A const_iterator into the %forward_list.
857        *  @param  args  Arguments.
858        *  @return  An iterator that points to the inserted data.
859        *
860        *  This function will insert an object of type T constructed
861        *  with T(std::forward<Args>(args)...) after the specified
862        *  location.  Due to the nature of a %forward_list this operation can
863        *  be done in constant time, and does not invalidate iterators
864        *  and references.
865        */
866       template<typename... _Args>
867         iterator
868         emplace_after(const_iterator __pos, _Args&&... __args)
869         { return iterator(this->_M_insert_after(__pos,
870                                           std::forward<_Args>(__args)...)); }
871
872       /**
873        *  @brief  Inserts given value into %forward_list after specified
874        *          iterator.
875        *  @param  pos  An iterator into the %forward_list.
876        *  @param  val  Data to be inserted.
877        *  @return  An iterator that points to the inserted data.
878        *
879        *  This function will insert a copy of the given value after
880        *  the specified location.  Due to the nature of a %forward_list this
881        *  operation can be done in constant time, and does not
882        *  invalidate iterators and references.
883        */
884       iterator
885       insert_after(const_iterator __pos, const _Tp& __val)
886       { return iterator(this->_M_insert_after(__pos, __val)); }
887
888       /**
889        *
890        */
891       iterator
892       insert_after(const_iterator __pos, _Tp&& __val)
893       { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
894
895       /**
896        *  @brief  Inserts a number of copies of given data into the
897        *          %forward_list.
898        *  @param  pos  An iterator into the %forward_list.
899        *  @param  n  Number of elements to be inserted.
900        *  @param  val  Data to be inserted.
901        *
902        *  This function will insert a specified number of copies of the
903        *  given data after the location specified by @a pos.
904        *
905        *  This operation is linear in the number of elements inserted and
906        *  does not invalidate iterators and references.
907        */
908       void
909       insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
910       {
911         forward_list __tmp(__n, __val, this->get_allocator());
912         this->splice_after(__pos, std::move(__tmp));
913       }
914
915       /**
916        *  @brief  Inserts a range into the %forward_list.
917        *  @param  position  An iterator into the %forward_list.
918        *  @param  first  An input iterator.
919        *  @param  last   An input iterator.
920        *
921        *  This function will insert copies of the data in the range [@a
922        *  first,@a last) into the %forward_list after the location specified
923        *  by @a pos.
924        *
925        *  This operation is linear in the number of elements inserted and
926        *  does not invalidate iterators and references.
927        */
928       template<typename _InputIterator>
929         void
930         insert_after(const_iterator __pos,
931                      _InputIterator __first, _InputIterator __last)
932         {
933           forward_list __tmp(__first, __last, this->get_allocator());
934           this->splice_after(__pos, std::move(__tmp));
935         }
936
937       /**
938        *  @brief  Inserts the contents of an initializer_list into
939        *          %forward_list after the specified iterator.
940        *  @param  pos  An iterator into the %forward_list.
941        *  @param  il  An initializer_list of value_type.
942        *
943        *  This function will insert copies of the data in the
944        *  initializer_list @a il into the %forward_list before the location
945        *  specified by @a pos.
946        *
947        *  This operation is linear in the number of elements inserted and
948        *  does not invalidate iterators and references.
949        */
950       void
951       insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
952       {
953         forward_list __tmp(__il, this->get_allocator());
954         this->splice_after(__pos, std::move(__tmp));
955       }
956
957       /**
958        *  @brief  Removes the element pointed to by the iterator following
959        *          @c pos.
960        *  @param  pos  Iterator pointing to element to be erased.
961        *  @return  An iterator pointing to the next element (or end()).
962        *
963        *  This function will erase the element at the given position and
964        *  thus shorten the %forward_list by one.
965        *
966        *  Due to the nature of a %forward_list this operation can be done
967        *  in constant time, and only invalidates iterators/references to
968        *  the element being removed.  The user is also cautioned that
969        *  this function only erases the element, and that if the element
970        *  is itself a pointer, the pointed-to memory is not touched in
971        *  any way.  Managing the pointer is the user's responsibility.
972        */
973       iterator
974       erase_after(const_iterator __pos)
975       {
976         _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node);
977         if (__tmp)
978           return iterator(this->_M_erase_after(__tmp));
979         else
980           return end();
981       }
982
983       /**
984        *  @brief  Remove a range of elements.
985        *  @param  pos  Iterator pointing before the first element to be
986        *               erased.
987        *  @param  last  Iterator pointing to one past the last element to be
988        *                erased.
989        *  @return  An iterator pointing to the element pointed to by @a last
990        *           prior to erasing (or end()).
991        *
992        *  This function will erase the elements in the range @a
993        *  (pos,last) and shorten the %forward_list accordingly.
994        *
995        *  This operation is linear time in the size of the range and only
996        *  invalidates iterators/references to the element being removed.
997        *  The user is also cautioned that this function only erases the
998        *  elements, and that if the elements themselves are pointers, the
999        *  pointed-to memory is not touched in any way.  Managing the pointer
1000        *  is the user's responsibility.
1001        */
1002       iterator
1003       erase_after(const_iterator __pos, iterator __last)
1004       {
1005         _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node);
1006         return iterator(this->_M_erase_after(__tmp, &*__last._M_node));
1007       }
1008
1009       /**
1010        *  @brief  Swaps data with another %forward_list.
1011        *  @param  list  A %forward_list of the same element and allocator
1012        *                types.
1013        *
1014        *  This exchanges the elements between two lists in constant
1015        *  time.  Note that the global std::swap() function is
1016        *  specialized such that std::swap(l1,l2) will feed to this
1017        *  function.
1018        */
1019       void
1020       swap(forward_list&& __list)
1021       { _Node_base::swap(this->_M_impl._M_head, __list._M_impl._M_head); }
1022
1023       /**
1024        *  @brief Resizes the %forward_list to the specified number of
1025        *         elements.
1026        *  @param sz Number of elements the %forward_list should contain.
1027        *
1028        *  This function will %resize the %forward_list to the specified
1029        *  number of elements.  If the number is smaller than the
1030        *  %forward_list's current size the %forward_list is truncated,
1031        *  otherwise the %forward_list is extended and new elements are
1032        *  populated with given data.
1033        */
1034       void
1035       resize(size_type __sz)
1036       { resize(__sz, _Tp()); }
1037
1038       /**
1039        *  @brief Resizes the %forward_list to the specified number of
1040        *         elements.
1041        *  @param sz Number of elements the %forward_list should contain.
1042        *  @param val Data with which new elements should be populated.
1043        *
1044        *  This function will %resize the %forward_list to the specified
1045        *  number of elements.  If the number is smaller than the
1046        *  %forward_list's current size the %forward_list is truncated,
1047        *  otherwise the %forward_list is extended and new elements are
1048        *  populated with given data.
1049        */
1050       void
1051       resize(size_type __sz, value_type __val);
1052
1053       /**
1054        *  @brief  Erases all the elements.
1055        *
1056        *  Note that this function only erases
1057        *  the elements, and that if the elements themselves are
1058        *  pointers, the pointed-to memory is not touched in any way.
1059        *  Managing the pointer is the user's responsibility.
1060        */
1061       void
1062       clear()
1063       { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1064
1065       // 23.2.3.5 forward_list operations:
1066
1067       /**
1068        *  @brief  Insert contents of another %forward_list.
1069        *  @param  pos  Iterator referencing the element to insert after.
1070        *  @param  list  Source list.
1071        *
1072        *  The elements of @a list are inserted in constant time after
1073        *  the element referenced by @a pos.  @a list becomes an empty
1074        *  list.
1075        *
1076        *  Requires this != @a x.
1077        */
1078       void
1079       splice_after(const_iterator __pos, forward_list&& __list);
1080
1081       /**
1082        *  @brief  Insert element from another %forward_list.
1083        *  @param  pos  Iterator referencing the element to insert after.
1084        *  @param  list  Source list.
1085        *  @param  it  Iterator referencing the element before the element
1086        *              to move.
1087        *
1088        *  Removes the element in list @a list referenced by @a i and
1089        *  inserts it into the current list after @a pos.
1090        */
1091       void
1092       splice_after(const_iterator __pos, forward_list&& __list,
1093                    const_iterator __it)
1094       { this->splice_after(__pos, __list, __it, __it._M_next()); }
1095
1096       /**
1097        *  @brief  Insert range from another %forward_list.
1098        *  @param  pos  Iterator referencing the element to insert after.
1099        *  @param  list  Source list.
1100        *  @param  before  Iterator referencing before the start of range
1101        *                  in list.
1102        *  @param  last  Iterator referencing the end of range in list.
1103        *
1104        *  Removes elements in the range (before,last) and inserts them
1105        *  after @a pos in constant time.
1106        *
1107        *  Undefined if @a pos is in (before,last).
1108        */
1109       void
1110       splice_after(const_iterator __pos, forward_list&& __list,
1111                    const_iterator __before, const_iterator __last);
1112
1113       /**
1114        *  @brief  Remove all elements equal to value.
1115        *  @param  val  The value to remove.
1116        *
1117        *  Removes every element in the list equal to @a value.
1118        *  Remaining elements stay in list order.  Note that this
1119        *  function only erases the elements, and that if the elements
1120        *  themselves are pointers, the pointed-to memory is not
1121        *  touched in any way.  Managing the pointer is the user's
1122        *  responsibility.
1123        */
1124       void
1125       remove(const _Tp& __val);
1126
1127       /**
1128        *  @brief  Remove all elements satisfying a predicate.
1129        *  @param  pred  Unary predicate function or object.
1130        *
1131        *  Removes every element in the list for which the predicate
1132        *  returns true.  Remaining elements stay in list order.  Note
1133        *  that this function only erases the elements, and that if the
1134        *  elements themselves are pointers, the pointed-to memory is
1135        *  not touched in any way.  Managing the pointer is the user's
1136        *  responsibility.
1137        */
1138       template<typename _Pred>
1139         void
1140         remove_if(_Pred __pred);
1141
1142       /**
1143        *  @brief  Remove consecutive duplicate elements.
1144        *
1145        *  For each consecutive set of elements with the same value,
1146        *  remove all but the first one.  Remaining elements stay in
1147        *  list order.  Note that this function only erases the
1148        *  elements, and that if the elements themselves are pointers,
1149        *  the pointed-to memory is not touched in any way.  Managing
1150        *  the pointer is the user's responsibility.
1151        */
1152       void
1153       unique()
1154       { this->unique(std::equal_to<_Tp>()); }
1155
1156       /**
1157        *  @brief  Remove consecutive elements satisfying a predicate.
1158        *  @param  binary_pred  Binary predicate function or object.
1159        *
1160        *  For each consecutive set of elements [first,last) that
1161        *  satisfy predicate(first,i) where i is an iterator in
1162        *  [first,last), remove all but the first one.  Remaining
1163        *  elements stay in list order.  Note that this function only
1164        *  erases the elements, and that if the elements themselves are
1165        *  pointers, the pointed-to memory is not touched in any way.
1166        *  Managing the pointer is the user's responsibility.
1167        */
1168       template<typename _BinPred>
1169         void
1170         unique(_BinPred __binary_pred);
1171
1172       /**
1173        *  @brief  Merge sorted lists.
1174        *  @param  list  Sorted list to merge.
1175        *
1176        *  Assumes that both @a list and this list are sorted according to
1177        *  operator<().  Merges elements of @a list into this list in
1178        *  sorted order, leaving @a list empty when complete.  Elements in
1179        *  this list precede elements in @a list that are equal.
1180        */
1181       void
1182       merge(forward_list&& __list)
1183       { this->merge(__list, std::less<_Tp>()); }
1184
1185       /**
1186        *  @brief  Merge sorted lists according to comparison function.
1187        *  @param  list  Sorted list to merge.
1188        *  @param  comp Comparison function defining sort order.
1189        *
1190        *  Assumes that both @a list and this list are sorted according to
1191        *  comp.  Merges elements of @a list into this list
1192        *  in sorted order, leaving @a list empty when complete.  Elements
1193        *  in this list precede elements in @a list that are equivalent
1194        *  according to comp().
1195        */
1196       template<typename _Comp>
1197         void
1198         merge(forward_list&& __list, _Comp __comp);
1199
1200       /**
1201        *  @brief  Sort the elements of the list.
1202        *
1203        *  Sorts the elements of this list in NlogN time.  Equivalent
1204        *  elements remain in list order.
1205        */
1206       void
1207       sort()
1208       {
1209         _Node* __tmp = __static_pointer_cast<_Node*>(&this->_M_impl._M_head);
1210         __tmp->_M_sort_after(std::less<_Tp>());
1211       }
1212
1213       /**
1214        *  @brief  Sort the forward_list using a comparison function.
1215        *
1216        *  Sorts the elements of this list in NlogN time.  Equivalent
1217        *  elements remain in list order.
1218        */
1219       template<typename _Comp>
1220         void
1221         sort(_Comp __comp)
1222         {
1223           _Node* __tmp = __static_pointer_cast<_Node*>(&this->_M_impl._M_head);
1224           __tmp->_M_sort_after(__comp);
1225         }
1226
1227       /**
1228        *  @brief  Reverse the elements in list.
1229        *
1230        *  Reverse the order of elements in the list in linear time.
1231        */
1232       void
1233       reverse();
1234
1235     private:
1236       template<typename _Integer>
1237         void
1238         _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1239         { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1240
1241       // Called by the range constructor to implement [23.1.1]/9
1242       template<typename _InputIterator>
1243         void
1244         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1245                                __false_type);
1246
1247       // Called by forward_list(n,v,a), and the range constructor when it
1248       // turns out to be the same thing.
1249       void
1250       _M_fill_initialize(size_type __n, const value_type& __value);
1251     };
1252
1253   /**
1254    *  @brief  Forward list equality comparison.
1255    *  @param  lx  A %forward_list
1256    *  @param  ly  A %forward_list of the same type as @a lx.
1257    *  @return  True iff the size and elements of the forward lists are equal.
1258    *
1259    *  This is an equivalence relation.  It is linear in the size of the
1260    *  forward lists.  Deques are considered equivalent if corresponding
1261    *  elements compare equal.
1262    */
1263   template<typename _Tp, typename _Alloc>
1264     bool
1265     operator==(const forward_list<_Tp, _Alloc>& __lx,
1266                const forward_list<_Tp, _Alloc>& __ly);
1267
1268   /**
1269    *  @brief  Forward list ordering relation.
1270    *  @param  lx  A %forward_list.
1271    *  @param  ly  A %forward_list of the same type as @a lx.
1272    *  @return  True iff @a lx is lexicographically less than @a ly.
1273    *
1274    *  This is a total ordering relation.  It is linear in the size of the
1275    *  forward lists.  The elements must be comparable with @c <.
1276    *
1277    *  See std::lexicographical_compare() for how the determination is made.
1278    */
1279   template<typename _Tp, typename _Alloc>
1280     inline bool
1281     operator<(const forward_list<_Tp, _Alloc>& __lx,
1282               const forward_list<_Tp, _Alloc>& __ly)
1283     { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1284                                           __ly.cbegin(), __ly.cend()); }
1285
1286   /// Based on operator==
1287   template<typename _Tp, typename _Alloc>
1288     inline bool
1289     operator!=(const forward_list<_Tp, _Alloc>& __lx,
1290                const forward_list<_Tp, _Alloc>& __ly)
1291     { return !(__lx == __ly); }
1292
1293   /// Based on operator<
1294   template<typename _Tp, typename _Alloc>
1295     inline bool
1296     operator>(const forward_list<_Tp, _Alloc>& __lx,
1297               const forward_list<_Tp, _Alloc>& __ly)
1298     { return (__ly < __lx); }
1299
1300   /// Based on operator<
1301   template<typename _Tp, typename _Alloc>
1302     inline bool
1303     operator>=(const forward_list<_Tp, _Alloc>& __lx,
1304                const forward_list<_Tp, _Alloc>& __ly)
1305     { return !(__lx < __ly); }
1306
1307   /// Based on operator<
1308   template<typename _Tp, typename _Alloc>
1309     inline bool
1310     operator<=(const forward_list<_Tp, _Alloc>& __lx,
1311                const forward_list<_Tp, _Alloc>& __ly)
1312     { return !(__ly < __lx); }
1313
1314   /// See std::forward_list::swap().
1315   template<typename _Tp, typename _Alloc>
1316     inline void
1317     swap(forward_list<_Tp, _Alloc>& __lx,
1318          forward_list<_Tp, _Alloc>& __ly)
1319     { __lx.swap(__ly); }
1320
1321   /// See std::forward_list::swap().
1322   template<typename _Tp, typename _Alloc>
1323     inline void
1324     swap(forward_list<_Tp, _Alloc>&& __lx,
1325          forward_list<_Tp, _Alloc>& __ly)
1326     { __lx.swap(__ly); }
1327
1328   /// See std::forward_list::swap().
1329   template<typename _Tp, typename _Alloc>
1330     inline void 
1331     swap(forward_list<_Tp, _Alloc>& __lx,
1332          forward_list<_Tp, _Alloc>&& __ly)
1333     { __lx.swap(__ly); }
1334
1335 _GLIBCXX_END_NAMESPACE // namespace std
1336
1337 #endif // __GXX_EXPERIMENTAL_CXX0X__
1338
1339 #endif // _FORWARD_LIST_H