OSDN Git Service

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