OSDN Git Service

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