OSDN Git Service

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