OSDN Git Service

2009-02-20 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 sequences
404    *
405    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
406    *  <a href="tables.html#67">sequence</a>, including the
407    *  <a href="tables.html#68">optional sequence requirements</a> with the
408    *  %exception of @c at and @c operator[].
409    *
410    *  This is a @e singly @e linked %list.  Traversal up the
411    *  %list requires linear time, but adding and removing elements (or
412    *  @e nodes) is done in constant time, regardless of where the
413    *  change takes place.  Unlike std::vector and std::deque,
414    *  random-access iterators are not provided, so subscripting ( @c
415    *  [] ) access is not allowed.  For algorithms which only need
416    *  sequential access, this lack makes no difference.
417    *
418    *  Also unlike the other standard containers, std::forward_list provides
419    *  specialized algorithms %unique to linked lists, such as
420    *  splicing, sorting, and in-place reversal.
421    *
422    *  A couple points on memory allocation for forward_list<Tp>:
423    *
424    *  First, we never actually allocate a Tp, we allocate
425    *  Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT.  This is to ensure
426    *  that after elements from %forward_list<X,Alloc1> are spliced into
427    *  %forward_list<X,Alloc2>, destroying the memory of the second %list is a
428    *  valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
429    */
430   template<typename _Tp, typename _Alloc = allocator<_Tp> >
431     class forward_list : private _Fwd_list_base<_Tp, _Alloc>
432     {
433     private:
434       typedef _Fwd_list_base<_Tp, _Alloc>                  _Base;
435       typedef typename _Base::_Node                        _Node;
436       typedef typename _Base::_Node_base                   _Node_base;
437       typedef typename _Base::_Tp_alloc_type               _Tp_alloc_type;
438
439     public:
440       // types:
441       typedef _Tp                                          value_type;
442       typedef typename _Tp_alloc_type::pointer             pointer;
443       typedef typename _Tp_alloc_type::const_pointer       const_pointer;
444       typedef typename _Tp_alloc_type::reference           reference;
445       typedef typename _Tp_alloc_type::const_reference     const_reference;
446  
447       typedef typename _Base::iterator                     iterator;
448       typedef typename _Base::const_iterator               const_iterator;
449       typedef std::size_t                                  size_type;
450       typedef std::ptrdiff_t                               difference_type;
451       typedef _Alloc                                       allocator_type;
452
453       // 23.2.3.1 construct/copy/destroy:
454
455       /**
456        *  @brief  Creates a %forward_list with no elements.
457        *  @param  al  An allocator object.
458        */
459       explicit
460       forward_list(const _Alloc& __al = _Alloc())
461       : _Base(__al)
462       { }
463
464       /**
465        *  @brief  Copy constructor with allocator argument.
466        *  @param  list  Input list to copy.
467        *  @param  al    An allocator object.
468        */
469       forward_list(const forward_list& __list, const _Alloc& __al)
470       : _Base(__list, __al)
471       { }
472
473       /**
474        *  @brief  Move constructor with allocator argument.
475        *  @param  list  Input list to move.
476        *  @param  al    An allocator object.
477        */
478       forward_list(forward_list&& __list, const _Alloc& __al)
479       : _Base(std::forward<_Base>(__list), __al)
480       { }
481
482       /**
483        *  @brief  Creates a %forward_list with copies of the default element
484        *          type.
485        *  @param  n  The number of elements to initially create.
486        *
487        *  This constructor fills the %forward_list with @a n copies of
488        *  the default value.
489        */
490       explicit
491       forward_list(size_type __n)
492       : _Base()
493       { _M_fill_initialize(__n, value_type()); }
494
495       /**
496        *  @brief  Creates a %forward_list with copies of an exemplar element.
497        *  @param  n      The number of elements to initially create.
498        *  @param  value  An element to copy.
499        *  @param  al     An allocator object.
500        *
501        *  This constructor fills the %forward_list with @a n copies of @a
502        *  value.
503        */
504       forward_list(size_type __n, const _Tp& __value,
505                    const _Alloc& __al = _Alloc())
506       : _Base(__al)
507       { _M_fill_initialize(__n, __value); }
508
509       /**
510        *  @brief  Builds a %forward_list from a range.
511        *  @param  first  An input iterator.
512        *  @param  last   An input iterator.
513        *  @param  al     An allocator object.
514        *
515        *  Create a %forward_list consisting of copies of the elements from
516        *  [@a first,@a last).  This is linear in N (where N is
517        *  distance(@a first,@a last)).
518        */
519       template<typename _InputIterator>
520         forward_list(_InputIterator __first, _InputIterator __last,
521                      const _Alloc& __al = _Alloc())
522         : _Base(__al)
523         {
524           // Check whether it's an integral type.  If so, it's not an iterator.
525           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
526           _M_initialize_dispatch(__first, __last, _Integral());
527         }
528
529       /**
530        *  @brief  The %forward_list copy constructor.
531        *  @param  list  A %forward_list of identical element and allocator
532        *                types.
533        *
534        *  The newly-created %forward_list uses a copy of the allocation
535        *  object used by @a list.
536        */
537       forward_list(const forward_list& __list)
538       : _Base(__list.get_allocator())
539       { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
540
541       /**
542        *  @brief  The %forward_list move constructor.
543        *  @param  list  A %forward_list of identical element and allocator
544        *                types.
545        *
546        *  The newly-created %forward_list contains the exact contents of @a
547        *  forward_list. The contents of @a list are a valid, but unspecified
548        *  %forward_list.
549        */
550       forward_list(forward_list&& __list)
551       : _Base(std::forward<_Base>(__list)) { }
552
553       /**
554        *  @brief  Builds a %forward_list from an initializer_list
555        *  @param  il  An initializer_list of value_type.
556        *  @param  al  An allocator object.
557        *
558        *  Create a %forward_list consisting of copies of the elements
559        *  in the initializer_list @a il.  This is linear in il.size().
560        */
561       forward_list(std::initializer_list<_Tp> __il,
562                    const _Alloc& __al = _Alloc())
563       : _Base(__al)
564       { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
565
566       /**
567        *  @brief  The forward_list dtor.
568        */
569       ~forward_list()
570       { _M_erase_after(&this->_M_impl._M_head, 0); }
571
572       /**
573        *  @brief  The %forward_list assignment operator.
574        *  @param  list  A %forward_list of identical element and allocator
575        *                types.
576        *
577        *  All the elements of @a list are copied, but unlike the copy
578        *  constructor, the allocator object is not copied.
579        */
580       forward_list&
581       operator=(const forward_list& __list);
582
583       /**
584        *  @brief  The %forward_list move assignment operator.
585        *  @param  list  A %forward_list of identical element and allocator
586        *                types.
587        *
588        *  The contents of @a list are moved into this %forward_list
589        *  (without copying). @a list is a valid, but unspecified
590        *  %forward_list
591        */
592       forward_list&
593       operator=(forward_list&& __list)
594       {
595         if (&__list != this)
596           {
597             this->clear();
598             this->swap(__list);
599           }
600         return *this;
601       }
602
603       /**
604        *  @brief  The %forward_list initializer list assignment operator.
605        *  @param  il  An initializer_list of value_type.
606        *
607        *  Replace the contents of the %forward_list with copies of the
608        *  elements in the initializer_list @a il.  This is linear in
609        *  il.size().
610        */
611       forward_list&
612       operator=(std::initializer_list<_Tp> __il)
613       {
614         assign(__il);
615         return *this;
616       }
617
618       /**
619        *  @brief  Assigns a range to a %forward_list.
620        *  @param  first  An input iterator.
621        *  @param  last   An input iterator.
622        *
623        *  This function fills a %forward_list with copies of the elements
624        *  in the range [@a first,@a last).
625        *
626        *  Note that the assignment completely changes the %forward_list and
627        *  that the resulting %forward_list's size is the same as the number
628        *  of elements assigned.  Old data may be lost.
629        */
630       template<typename _InputIterator>
631         void
632         assign(_InputIterator __first, _InputIterator __last)
633         {
634           clear();
635           insert_after(cbefore_begin(), __first, __last);
636         }
637
638       /**
639        *  @brief  Assigns a given value to a %forward_list.
640        *  @param  n  Number of elements to be assigned.
641        *  @param  val  Value to be assigned.
642        *
643        *  This function fills a %forward_list with @a n copies of the given
644        *  value.  Note that the assignment completely changes the
645        *  %forward_list and that the resulting %forward_list's size is the
646        *  same as the number of elements assigned.  Old data may be lost.
647        */
648       void
649       assign(size_type __n, const _Tp& __val)
650       {
651         clear();
652         insert_after(cbefore_begin(), __n, __val);
653       }
654
655       /**
656        *  @brief  Assigns an initializer_list to a %forward_list.
657        *  @param  il  An initializer_list of value_type.
658        *
659        *  Replace the contents of the %forward_list with copies of the
660        *  elements in the initializer_list @a il.  This is linear in
661        *  il.size().
662        */
663       void
664       assign(std::initializer_list<_Tp> __il)
665       {
666         clear();
667         insert_after(cbefore_begin(), __il);
668       }
669
670       /// Get a copy of the memory allocation object.
671       allocator_type
672       get_allocator() const
673       { return this->_M_get_Node_allocator(); }
674
675       // 23.2.3.2 iterators:
676
677       /**
678        *  Returns a read/write iterator that points before the first element
679        *  in the %forward_list.  Iteration is done in ordinary element order.
680        */
681       iterator
682       before_begin()
683       { return iterator(&this->_M_impl._M_head); }
684
685       /**
686        *  Returns a read-only (constant) iterator that points before the
687        *  first element in the %forward_list.  Iteration is done in ordinary
688        *  element order.
689        */
690       const_iterator
691       before_begin() const
692       { return const_iterator(&this->_M_impl._M_head); }
693
694       /**
695        *  Returns a read/write iterator that points to the first element
696        *  in the %forward_list.  Iteration is done in ordinary element order.
697        */
698       iterator
699       begin()
700       { return iterator(this->_M_impl._M_head._M_next); }
701
702       /**
703        *  Returns a read-only (constant) iterator that points to the first
704        *  element in the %forward_list.  Iteration is done in ordinary
705        *  element order.
706        */
707       const_iterator
708       begin() const
709       { return const_iterator(this->_M_impl._M_head._M_next); }
710
711       /**
712        *  Returns a read/write iterator that points one past the last
713        *  element in the %forward_list.  Iteration is done in ordinary
714        *  element order.
715        */
716       iterator
717       end()
718       { return iterator(0); }
719
720       /**
721        *  Returns a read-only iterator that points one past the last
722        *  element in the %forward_list.  Iteration is done in ordinary
723        *  element order.
724        */
725       const_iterator
726       end() const
727       { return const_iterator(0); }
728
729       /**
730        *  Returns a read-only (constant) iterator that points to the
731        *  first element in the %forward_list.  Iteration is done in ordinary
732        *  element order.
733        */
734       const_iterator
735       cbegin() const
736       { return const_iterator(this->_M_impl._M_head._M_next); }
737
738       /**
739        *  Returns a read-only (constant) iterator that points before the
740        *  first element in the %forward_list.  Iteration is done in ordinary
741        *  element order.
742        */
743       const_iterator
744       cbefore_begin() const
745       { return const_iterator(&this->_M_impl._M_head); }
746
747       /**
748        *  Returns a read-only (constant) iterator that points one past
749        *  the last element in the %forward_list.  Iteration is done in
750        *  ordinary element order.
751        */
752       const_iterator
753       cend() const
754       { return const_iterator(0); }
755
756       /**
757        *  Returns true if the %forward_list is empty.  (Thus begin() would
758        *  equal end().)
759        */
760       bool
761       empty() const
762       { return this->_M_impl._M_head._M_next == 0; }
763
764       /**
765        *  Returns the largest possible size of %forward_list.
766        */
767       size_type
768       max_size() const
769       { return this->_M_get_Node_allocator().max_size(); }
770
771       // 23.2.3.3 element access:
772
773       /**
774        *  Returns a read/write reference to the data at the first
775        *  element of the %forward_list.
776        */
777       reference
778       front()
779       {
780         _Node* __front = __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
781         return __front->_M_value;
782       }
783
784       /**
785        *  Returns a read-only (constant) reference to the data at the first
786        *  element of the %forward_list.
787        */
788       const_reference
789       front() const
790       {
791         _Node* __front =
792           __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
793         return __front->_M_value;
794       }
795
796       // 23.2.3.4 modifiers:
797
798       /**
799        *  @brief  Constructs object in %forward_list at the front of the
800        *          list.
801        *  @param  args  Arguments.
802        *
803        *  This function will insert an object of type Tp constructed
804        *  with Tp(std::forward<Args>(args)...) at the front of the list
805        *  Due to the nature of a %forward_list this operation can
806        *  be done in constant time, and does not invalidate iterators
807        *  and references.
808        */
809       template<typename... _Args>
810         void
811         emplace_front(_Args&&... __args)
812         { this->_M_insert_after(cbefore_begin(),
813                                 std::forward<_Args>(__args)...); }
814
815       /**
816        *  @brief  Add data to the front of the %forward_list.
817        *  @param  val  Data to be added.
818        *
819        *  This is a typical stack operation.  The function creates an
820        *  element at the front of the %forward_list and assigns the given
821        *  data to it.  Due to the nature of a %forward_list this operation
822        *  can be done in constant time, and does not invalidate iterators
823        *  and references.
824        */
825       void
826       push_front(const _Tp& __val)
827       { this->_M_insert_after(cbefore_begin(), __val); }
828
829       /**
830        *
831        */
832       void
833       push_front(_Tp&& __val)
834       { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
835
836       /**
837        *  @brief  Removes first element.
838        *
839        *  This is a typical stack operation.  It shrinks the %forward_list
840        *  by one.  Due to the nature of a %forward_list this operation can
841        *  be done in constant time, and only invalidates iterators/references
842        *  to the element being removed.
843        *
844        *  Note that no data is returned, and if the first element's data
845        *  is needed, it should be retrieved before pop_front() is
846        *  called.
847        */
848       void
849       pop_front()
850       { this->_M_erase_after(&this->_M_impl._M_head); }
851
852       /**
853        *  @brief  Constructs object in %forward_list after the specified
854        *          iterator.
855        *  @param  pos  A const_iterator into the %forward_list.
856        *  @param  args  Arguments.
857        *  @return  An iterator that points to the inserted data.
858        *
859        *  This function will insert an object of type T constructed
860        *  with T(std::forward<Args>(args)...) after the specified
861        *  location.  Due to the nature of a %forward_list this operation can
862        *  be done in constant time, and does not invalidate iterators
863        *  and references.
864        */
865       template<typename... _Args>
866         iterator
867         emplace_after(const_iterator __pos, _Args&&... __args)
868         { return iterator(this->_M_insert_after(__pos,
869                                           std::forward<_Args>(__args)...)); }
870
871       /**
872        *  @brief  Inserts given value into %forward_list after specified
873        *          iterator.
874        *  @param  pos  An iterator into the %forward_list.
875        *  @param  val  Data to be inserted.
876        *  @return  An iterator that points to the inserted data.
877        *
878        *  This function will insert a copy of the given value after
879        *  the specified location.  Due to the nature of a %forward_list this
880        *  operation can be done in constant time, and does not
881        *  invalidate iterators and references.
882        */
883       iterator
884       insert_after(const_iterator __pos, const _Tp& __val)
885       { return iterator(this->_M_insert_after(__pos, __val)); }
886
887       /**
888        *
889        */
890       iterator
891       insert_after(const_iterator __pos, _Tp&& __val)
892       { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
893
894       /**
895        *  @brief  Inserts a number of copies of given data into the
896        *          %forward_list.
897        *  @param  pos  An iterator into the %forward_list.
898        *  @param  n  Number of elements to be inserted.
899        *  @param  val  Data to be inserted.
900        *
901        *  This function will insert a specified number of copies of the
902        *  given data after the location specified by @a pos.
903        *
904        *  This operation is linear in the number of elements inserted and
905        *  does not invalidate iterators and references.
906        */
907       void
908       insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
909       {
910         forward_list __tmp(__n, __val, this->get_allocator());
911         this->splice_after(__pos, std::move(__tmp));
912       }
913
914       /**
915        *  @brief  Inserts a range into the %forward_list.
916        *  @param  position  An iterator into the %forward_list.
917        *  @param  first  An input iterator.
918        *  @param  last   An input iterator.
919        *
920        *  This function will insert copies of the data in the range [@a
921        *  first,@a last) into the %forward_list after the location specified
922        *  by @a pos.
923        *
924        *  This operation is linear in the number of elements inserted and
925        *  does not invalidate iterators and references.
926        */
927       template<typename _InputIterator>
928         void
929         insert_after(const_iterator __pos,
930                      _InputIterator __first, _InputIterator __last)
931         {
932           forward_list __tmp(__first, __last, this->get_allocator());
933           this->splice_after(__pos, std::move(__tmp));
934         }
935
936       /**
937        *  @brief  Inserts the contents of an initializer_list into
938        *          %forward_list after the specified iterator.
939        *  @param  pos  An iterator into the %forward_list.
940        *  @param  il  An initializer_list of value_type.
941        *
942        *  This function will insert copies of the data in the
943        *  initializer_list @a il into the %forward_list before the location
944        *  specified by @a pos.
945        *
946        *  This operation is linear in the number of elements inserted and
947        *  does not invalidate iterators and references.
948        */
949       void
950       insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
951       {
952         forward_list __tmp(__il, this->get_allocator());
953         this->splice_after(__pos, std::move(__tmp));
954       }
955
956       /**
957        *  @brief  Removes the element pointed to by the iterator following
958        *          @c pos.
959        *  @param  pos  Iterator pointing to element to be erased.
960        *  @return  An iterator pointing to the next element (or end()).
961        *
962        *  This function will erase the element at the given position and
963        *  thus shorten the %forward_list by one.
964        *
965        *  Due to the nature of a %forward_list this operation can be done
966        *  in constant time, and only invalidates iterators/references to
967        *  the element being removed.  The user is also cautioned that
968        *  this function only erases the element, and that if the element
969        *  is itself a pointer, the pointed-to memory is not touched in
970        *  any way.  Managing the pointer is the user's responsibility.
971        */
972       iterator
973       erase_after(const_iterator __pos)
974       {
975         _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node);
976         if (__tmp)
977           return iterator(this->_M_erase_after(__tmp));
978         else
979           return end();
980       }
981
982       /**
983        *  @brief  Remove a range of elements.
984        *  @param  pos  Iterator pointing before the first element to be
985        *               erased.
986        *  @param  last  Iterator pointing to one past the last element to be
987        *                erased.
988        *  @return  An iterator pointing to the element pointed to by @a last
989        *           prior to erasing (or end()).
990        *
991        *  This function will erase the elements in the range @a
992        *  (pos,last) and shorten the %forward_list accordingly.
993        *
994        *  This operation is linear time in the size of the range and only
995        *  invalidates iterators/references to the element being removed.
996        *  The user is also cautioned that this function only erases the
997        *  elements, and that if the elements themselves are pointers, the
998        *  pointed-to memory is not touched in any way.  Managing the pointer
999        *  is the user's responsibility.
1000        */
1001       iterator
1002       erase_after(const_iterator __pos, iterator __last)
1003       {
1004         _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node);
1005         return iterator(this->_M_erase_after(__tmp, &*__last._M_node));
1006       }
1007
1008       /**
1009        *  @brief  Swaps data with another %forward_list.
1010        *  @param  list  A %forward_list of the same element and allocator
1011        *                types.
1012        *
1013        *  This exchanges the elements between two lists in constant
1014        *  time.  Note that the global std::swap() function is
1015        *  specialized such that std::swap(l1,l2) will feed to this
1016        *  function.
1017        */
1018       void
1019       swap(forward_list&& __list)
1020       { _Node_base::swap(this->_M_impl._M_head, __list._M_impl._M_head); }
1021
1022       /**
1023        *  @brief Resizes the %forward_list to the specified number of
1024        *         elements.
1025        *  @param sz Number of elements the %forward_list should contain.
1026        *
1027        *  This function will %resize the %forward_list to the specified
1028        *  number of elements.  If the number is smaller than the
1029        *  %forward_list's current size the %forward_list is truncated,
1030        *  otherwise the %forward_list is extended and new elements are
1031        *  populated with given data.
1032        */
1033       void
1034       resize(size_type __sz)
1035       { resize(__sz, _Tp()); }
1036
1037       /**
1038        *  @brief Resizes the %forward_list to the specified number of
1039        *         elements.
1040        *  @param sz Number of elements the %forward_list should contain.
1041        *  @param val Data with which new elements should be populated.
1042        *
1043        *  This function will %resize the %forward_list to the specified
1044        *  number of elements.  If the number is smaller than the
1045        *  %forward_list's current size the %forward_list is truncated,
1046        *  otherwise the %forward_list is extended and new elements are
1047        *  populated with given data.
1048        */
1049       void
1050       resize(size_type __sz, value_type __val);
1051
1052       /**
1053        *  @brief  Erases all the elements.
1054        *
1055        *  Note that this function only erases
1056        *  the elements, and that if the elements themselves are
1057        *  pointers, the pointed-to memory is not touched in any way.
1058        *  Managing the pointer is the user's responsibility.
1059        */
1060       void
1061       clear()
1062       { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1063
1064       // 23.2.3.5 forward_list operations:
1065
1066       /**
1067        *  @brief  Insert contents of another %forward_list.
1068        *  @param  pos  Iterator referencing the element to insert after.
1069        *  @param  list  Source list.
1070        *
1071        *  The elements of @a list are inserted in constant time after
1072        *  the element referenced by @a pos.  @a list becomes an empty
1073        *  list.
1074        *
1075        *  Requires this != @a x.
1076        */
1077       void
1078       splice_after(const_iterator __pos, forward_list&& __list);
1079
1080       /**
1081        *  @brief  Insert element from another %forward_list.
1082        *  @param  pos  Iterator referencing the element to insert after.
1083        *  @param  list  Source list.
1084        *  @param  it  Iterator referencing the element before the element
1085        *              to move.
1086        *
1087        *  Removes the element in list @a list referenced by @a i and
1088        *  inserts it into the current list after @a pos.
1089        */
1090       void
1091       splice_after(const_iterator __pos, forward_list&& __list,
1092                    const_iterator __it)
1093       { this->splice_after(__pos, __list, __it, __it._M_next()); }
1094
1095       /**
1096        *  @brief  Insert range from another %forward_list.
1097        *  @param  pos  Iterator referencing the element to insert after.
1098        *  @param  list  Source list.
1099        *  @param  before  Iterator referencing before the start of range
1100        *                  in list.
1101        *  @param  last  Iterator referencing the end of range in list.
1102        *
1103        *  Removes elements in the range (before,last) and inserts them
1104        *  after @a pos in constant time.
1105        *
1106        *  Undefined if @a pos is in (before,last).
1107        */
1108       void
1109       splice_after(const_iterator __pos, forward_list&& __list,
1110                    const_iterator __before, const_iterator __last);
1111
1112       /**
1113        *  @brief  Remove all elements equal to value.
1114        *  @param  val  The value to remove.
1115        *
1116        *  Removes every element in the list equal to @a value.
1117        *  Remaining elements stay in list order.  Note that this
1118        *  function only erases the elements, and that if the elements
1119        *  themselves are pointers, the pointed-to memory is not
1120        *  touched in any way.  Managing the pointer is the user's
1121        *  responsibility.
1122        */
1123       void
1124       remove(const _Tp& __val);
1125
1126       /**
1127        *  @brief  Remove all elements satisfying a predicate.
1128        *  @param  pred  Unary predicate function or object.
1129        *
1130        *  Removes every element in the list for which the predicate
1131        *  returns true.  Remaining elements stay in list order.  Note
1132        *  that this function only erases the elements, and that if the
1133        *  elements themselves are pointers, the pointed-to memory is
1134        *  not touched in any way.  Managing the pointer is the user's
1135        *  responsibility.
1136        */
1137       template<typename _Pred>
1138         void
1139         remove_if(_Pred __pred);
1140
1141       /**
1142        *  @brief  Remove consecutive duplicate elements.
1143        *
1144        *  For each consecutive set of elements with the same value,
1145        *  remove all but the first one.  Remaining elements stay in
1146        *  list order.  Note that this function only erases the
1147        *  elements, and that if the elements themselves are pointers,
1148        *  the pointed-to memory is not touched in any way.  Managing
1149        *  the pointer is the user's responsibility.
1150        */
1151       void
1152       unique()
1153       { this->unique(std::equal_to<_Tp>()); }
1154
1155       /**
1156        *  @brief  Remove consecutive elements satisfying a predicate.
1157        *  @param  binary_pred  Binary predicate function or object.
1158        *
1159        *  For each consecutive set of elements [first,last) that
1160        *  satisfy predicate(first,i) where i is an iterator in
1161        *  [first,last), remove all but the first one.  Remaining
1162        *  elements stay in list order.  Note that this function only
1163        *  erases the elements, and that if the elements themselves are
1164        *  pointers, the pointed-to memory is not touched in any way.
1165        *  Managing the pointer is the user's responsibility.
1166        */
1167       template<typename _BinPred>
1168         void
1169         unique(_BinPred __binary_pred);
1170
1171       /**
1172        *  @brief  Merge sorted lists.
1173        *  @param  list  Sorted list to merge.
1174        *
1175        *  Assumes that both @a list and this list are sorted according to
1176        *  operator<().  Merges elements of @a list into this list in
1177        *  sorted order, leaving @a list empty when complete.  Elements in
1178        *  this list precede elements in @a list that are equal.
1179        */
1180       void
1181       merge(forward_list&& __list)
1182       { this->merge(__list, std::less<_Tp>()); }
1183
1184       /**
1185        *  @brief  Merge sorted lists according to comparison function.
1186        *  @param  list  Sorted list to merge.
1187        *  @param  comp Comparison function defining sort order.
1188        *
1189        *  Assumes that both @a list and this list are sorted according to
1190        *  comp.  Merges elements of @a list into this list
1191        *  in sorted order, leaving @a list empty when complete.  Elements
1192        *  in this list precede elements in @a list that are equivalent
1193        *  according to comp().
1194        */
1195       template<typename _Comp>
1196         void
1197         merge(forward_list&& __list, _Comp __comp);
1198
1199       /**
1200        *  @brief  Sort the elements of the list.
1201        *
1202        *  Sorts the elements of this list in NlogN time.  Equivalent
1203        *  elements remain in list order.
1204        */
1205       void
1206       sort()
1207       {
1208         _Node* __tmp = __static_pointer_cast<_Node*>(&this->_M_impl._M_head);
1209         __tmp->_M_sort_after(std::less<_Tp>());
1210       }
1211
1212       /**
1213        *  @brief  Sort the forward_list using a comparison function.
1214        *
1215        *  Sorts the elements of this list in NlogN time.  Equivalent
1216        *  elements remain in list order.
1217        */
1218       template<typename _Comp>
1219         void
1220         sort(_Comp __comp)
1221         {
1222           _Node* __tmp = __static_pointer_cast<_Node*>(&this->_M_impl._M_head);
1223           __tmp->_M_sort_after(__comp);
1224         }
1225
1226       /**
1227        *  @brief  Reverse the elements in list.
1228        *
1229        *  Reverse the order of elements in the list in linear time.
1230        */
1231       void
1232       reverse();
1233
1234     private:
1235       template<typename _Integer>
1236         void
1237         _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1238         { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1239
1240       // Called by the range constructor to implement [23.1.1]/9
1241       template<typename _InputIterator>
1242         void
1243         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1244                                __false_type);
1245
1246       // Called by forward_list(n,v,a), and the range constructor when it
1247       // turns out to be the same thing.
1248       void
1249       _M_fill_initialize(size_type __n, const value_type& __value);
1250     };
1251
1252   /**
1253    *  @brief  Forward list equality comparison.
1254    *  @param  lx  A %forward_list
1255    *  @param  ly  A %forward_list of the same type as @a lx.
1256    *  @return  True iff the size and elements of the forward lists are equal.
1257    *
1258    *  This is an equivalence relation.  It is linear in the size of the
1259    *  forward lists.  Deques are considered equivalent if corresponding
1260    *  elements compare equal.
1261    */
1262   template<typename _Tp, typename _Alloc>
1263     bool
1264     operator==(const forward_list<_Tp, _Alloc>& __lx,
1265                const forward_list<_Tp, _Alloc>& __ly);
1266
1267   /**
1268    *  @brief  Forward list ordering relation.
1269    *  @param  lx  A %forward_list.
1270    *  @param  ly  A %forward_list of the same type as @a lx.
1271    *  @return  True iff @a lx is lexicographically less than @a ly.
1272    *
1273    *  This is a total ordering relation.  It is linear in the size of the
1274    *  forward lists.  The elements must be comparable with @c <.
1275    *
1276    *  See std::lexicographical_compare() for how the determination is made.
1277    */
1278   template<typename _Tp, typename _Alloc>
1279     inline bool
1280     operator<(const forward_list<_Tp, _Alloc>& __lx,
1281               const forward_list<_Tp, _Alloc>& __ly)
1282     { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1283                                           __ly.cbegin(), __ly.cend()); }
1284
1285   /// Based on operator==
1286   template<typename _Tp, typename _Alloc>
1287     inline bool
1288     operator!=(const forward_list<_Tp, _Alloc>& __lx,
1289                const forward_list<_Tp, _Alloc>& __ly)
1290     { return !(__lx == __ly); }
1291
1292   /// Based on operator<
1293   template<typename _Tp, typename _Alloc>
1294     inline bool
1295     operator>(const forward_list<_Tp, _Alloc>& __lx,
1296               const forward_list<_Tp, _Alloc>& __ly)
1297     { return (__ly < __lx); }
1298
1299   /// Based on operator<
1300   template<typename _Tp, typename _Alloc>
1301     inline bool
1302     operator>=(const forward_list<_Tp, _Alloc>& __lx,
1303                const forward_list<_Tp, _Alloc>& __ly)
1304     { return !(__lx < __ly); }
1305
1306   /// Based on operator<
1307   template<typename _Tp, typename _Alloc>
1308     inline bool
1309     operator<=(const forward_list<_Tp, _Alloc>& __lx,
1310                const forward_list<_Tp, _Alloc>& __ly)
1311     { return !(__ly < __lx); }
1312
1313   /// See std::forward_list::swap().
1314   template<typename _Tp, typename _Alloc>
1315     inline void
1316     swap(forward_list<_Tp, _Alloc>& __lx,
1317          forward_list<_Tp, _Alloc>& __ly)
1318     { __lx.swap(__ly); }
1319
1320   /// See std::forward_list::swap().
1321   template<typename _Tp, typename _Alloc>
1322     inline void
1323     swap(forward_list<_Tp, _Alloc>&& __lx,
1324          forward_list<_Tp, _Alloc>& __ly)
1325     { __lx.swap(__ly); }
1326
1327   /// See std::forward_list::swap().
1328   template<typename _Tp, typename _Alloc>
1329     inline void 
1330     swap(forward_list<_Tp, _Alloc>& __lx,
1331          forward_list<_Tp, _Alloc>&& __ly)
1332     { __lx.swap(__ly); }
1333
1334 _GLIBCXX_END_NAMESPACE // namespace std
1335
1336 #endif // __GXX_EXPERIMENTAL_CXX0X__
1337
1338 #endif // _FORWARD_LIST_H