OSDN Git Service

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