]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/bits/stl_list.h
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / bits / stl_list.h
1 // List implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /*
32  *
33  * Copyright (c) 1994
34  * Hewlett-Packard Company
35  *
36  * Permission to use, copy, modify, distribute and sell this software
37  * and its documentation for any purpose is hereby granted without fee,
38  * provided that the above copyright notice appear in all copies and
39  * that both that copyright notice and this permission notice appear
40  * in supporting documentation.  Hewlett-Packard Company makes no
41  * representations about the suitability of this software for any
42  * purpose.  It is provided "as is" without express or implied warranty.
43  *
44  *
45  * Copyright (c) 1996,1997
46  * Silicon Graphics Computer Systems, Inc.
47  *
48  * Permission to use, copy, modify, distribute and sell this software
49  * and its documentation for any purpose is hereby granted without fee,
50  * provided that the above copyright notice appear in all copies and
51  * that both that copyright notice and this permission notice appear
52  * in supporting documentation.  Silicon Graphics makes no
53  * representations about the suitability of this software for any
54  * purpose.  It is provided "as is" without express or implied warranty.
55  */
56
57 /** @file stl_list.h
58  *  This is an internal header file, included by other library headers.
59  *  You should not attempt to use it directly.
60  */
61
62 #ifndef _STL_LIST_H
63 #define _STL_LIST_H 1
64
65 #include <bits/concept_check.h>
66
67 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
68
69   // Supporting structures are split into common and templated types; the
70   // latter publicly inherits from the former in an effort to reduce code
71   // duplication.  This results in some "needless" static_cast'ing later on,
72   // but it's all safe downcasting.
73
74   /// Common part of a node in the %list. 
75   struct _List_node_base
76   {
77     _List_node_base* _M_next;
78     _List_node_base* _M_prev;
79
80     static void
81     swap(_List_node_base& __x, _List_node_base& __y);
82
83     void
84     transfer(_List_node_base * const __first,
85              _List_node_base * const __last);
86
87     void
88     reverse();
89
90     void
91     hook(_List_node_base * const __position);
92
93     void
94     unhook();
95   };
96
97   /// An actual node in the %list.
98   template<typename _Tp>
99     struct _List_node : public _List_node_base
100     {
101       ///< User's data.
102       _Tp _M_data;
103     };
104
105   /**
106    *  @brief A list::iterator.
107    *
108    *  All the functions are op overloads.
109   */
110   template<typename _Tp>
111     struct _List_iterator
112     {
113       typedef _List_iterator<_Tp>                _Self;
114       typedef _List_node<_Tp>                    _Node;
115
116       typedef ptrdiff_t                          difference_type;
117       typedef std::bidirectional_iterator_tag    iterator_category;
118       typedef _Tp                                value_type;
119       typedef _Tp*                               pointer;
120       typedef _Tp&                               reference;
121
122       _List_iterator()
123       : _M_node() { }
124
125       explicit
126       _List_iterator(_List_node_base* __x)
127       : _M_node(__x) { }
128
129       // Must downcast from List_node_base to _List_node to get to _M_data.
130       reference
131       operator*() const
132       { return static_cast<_Node*>(_M_node)->_M_data; }
133
134       pointer
135       operator->() const
136       { return &static_cast<_Node*>(_M_node)->_M_data; }
137
138       _Self&
139       operator++()
140       {
141         _M_node = _M_node->_M_next;
142         return *this;
143       }
144
145       _Self
146       operator++(int)
147       {
148         _Self __tmp = *this;
149         _M_node = _M_node->_M_next;
150         return __tmp;
151       }
152
153       _Self&
154       operator--()
155       {
156         _M_node = _M_node->_M_prev;
157         return *this;
158       }
159
160       _Self
161       operator--(int)
162       {
163         _Self __tmp = *this;
164         _M_node = _M_node->_M_prev;
165         return __tmp;
166       }
167
168       bool
169       operator==(const _Self& __x) const
170       { return _M_node == __x._M_node; }
171
172       bool
173       operator!=(const _Self& __x) const
174       { return _M_node != __x._M_node; }
175
176       // The only member points to the %list element.
177       _List_node_base* _M_node;
178     };
179
180   /**
181    *  @brief A list::const_iterator.
182    *
183    *  All the functions are op overloads.
184   */
185   template<typename _Tp>
186     struct _List_const_iterator
187     {
188       typedef _List_const_iterator<_Tp>          _Self;
189       typedef const _List_node<_Tp>              _Node;
190       typedef _List_iterator<_Tp>                iterator;
191
192       typedef ptrdiff_t                          difference_type;
193       typedef std::bidirectional_iterator_tag    iterator_category;
194       typedef _Tp                                value_type;
195       typedef const _Tp*                         pointer;
196       typedef const _Tp&                         reference;
197
198       _List_const_iterator()
199       : _M_node() { }
200
201       explicit
202       _List_const_iterator(const _List_node_base* __x)
203       : _M_node(__x) { }
204
205       _List_const_iterator(const iterator& __x)
206       : _M_node(__x._M_node) { }
207
208       // Must downcast from List_node_base to _List_node to get to
209       // _M_data.
210       reference
211       operator*() const
212       { return static_cast<_Node*>(_M_node)->_M_data; }
213
214       pointer
215       operator->() const
216       { return &static_cast<_Node*>(_M_node)->_M_data; }
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       _Self&
234       operator--()
235       {
236         _M_node = _M_node->_M_prev;
237         return *this;
238       }
239
240       _Self
241       operator--(int)
242       {
243         _Self __tmp = *this;
244         _M_node = _M_node->_M_prev;
245         return __tmp;
246       }
247
248       bool
249       operator==(const _Self& __x) const
250       { return _M_node == __x._M_node; }
251
252       bool
253       operator!=(const _Self& __x) const
254       { return _M_node != __x._M_node; }
255
256       // The only member points to the %list element.
257       const _List_node_base* _M_node;
258     };
259
260   template<typename _Val>
261     inline bool
262     operator==(const _List_iterator<_Val>& __x,
263                const _List_const_iterator<_Val>& __y)
264     { return __x._M_node == __y._M_node; }
265
266   template<typename _Val>
267     inline bool
268     operator!=(const _List_iterator<_Val>& __x,
269                const _List_const_iterator<_Val>& __y)
270     { return __x._M_node != __y._M_node; }
271
272
273   /// See bits/stl_deque.h's _Deque_base for an explanation.
274   template<typename _Tp, typename _Alloc>
275     class _List_base
276     {
277     protected:
278       // NOTA BENE
279       // The stored instance is not actually of "allocator_type"'s
280       // type.  Instead we rebind the type to
281       // Allocator<List_node<Tp>>, which according to [20.1.5]/4
282       // should probably be the same.  List_node<Tp> is not the same
283       // size as Tp (it's two pointers larger), and specializations on
284       // Tp may go unused because List_node<Tp> is being bound
285       // instead.
286       //
287       // We put this to the test in the constructors and in
288       // get_allocator, where we use conversions between
289       // allocator_type and _Node_alloc_type. The conversion is
290       // required by table 32 in [20.1.5].
291       typedef typename _Alloc::template rebind<_List_node<_Tp> >::other
292         _Node_alloc_type;
293
294       typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
295
296       struct _List_impl 
297       : public _Node_alloc_type
298       {
299         _List_node_base _M_node;
300
301         _List_impl()
302         : _Node_alloc_type(), _M_node()
303         { }
304
305         _List_impl(const _Node_alloc_type& __a)
306         : _Node_alloc_type(__a), _M_node()
307         { }
308       };
309
310       _List_impl _M_impl;
311
312       _List_node<_Tp>*
313       _M_get_node()
314       { return _M_impl._Node_alloc_type::allocate(1); }
315       
316       void
317       _M_put_node(_List_node<_Tp>* __p)
318       { _M_impl._Node_alloc_type::deallocate(__p, 1); }
319       
320   public:
321       typedef _Alloc allocator_type;
322
323       _Node_alloc_type&
324       _M_get_Node_allocator()
325       { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
326
327       const _Node_alloc_type&
328       _M_get_Node_allocator() const
329       { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
330
331       _Tp_alloc_type
332       _M_get_Tp_allocator() const
333       { return _Tp_alloc_type(_M_get_Node_allocator()); }
334
335       allocator_type
336       get_allocator() const
337       { return allocator_type(_M_get_Node_allocator()); }
338
339       _List_base()
340       : _M_impl()
341       { _M_init(); }
342
343       _List_base(const allocator_type& __a)
344       : _M_impl(__a)
345       { _M_init(); }
346
347 #ifdef __GXX_EXPERIMENTAL_CXX0X__
348       _List_base(_List_base&& __x)
349       : _M_impl(__x._M_get_Node_allocator())
350       {
351         _M_init();
352         _List_node_base::swap(this->_M_impl._M_node, __x._M_impl._M_node);      
353       }
354 #endif
355
356       // This is what actually destroys the list.
357       ~_List_base()
358       { _M_clear(); }
359
360       void
361       _M_clear();
362
363       void
364       _M_init()
365       {
366         this->_M_impl._M_node._M_next = &this->_M_impl._M_node;
367         this->_M_impl._M_node._M_prev = &this->_M_impl._M_node;
368       }
369     };
370
371   /**
372    *  @brief A standard container with linear time access to elements,
373    *  and fixed time insertion/deletion at any point in the sequence.
374    *
375    *  @ingroup Containers
376    *  @ingroup Sequences
377    *
378    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
379    *  <a href="tables.html#66">reversible container</a>, and a
380    *  <a href="tables.html#67">sequence</a>, including the
381    *  <a href="tables.html#68">optional sequence requirements</a> with the
382    *  %exception of @c at and @c operator[].
383    *
384    *  This is a @e doubly @e linked %list.  Traversal up and down the
385    *  %list requires linear time, but adding and removing elements (or
386    *  @e nodes) is done in constant time, regardless of where the
387    *  change takes place.  Unlike std::vector and std::deque,
388    *  random-access iterators are not provided, so subscripting ( @c
389    *  [] ) access is not allowed.  For algorithms which only need
390    *  sequential access, this lack makes no difference.
391    *
392    *  Also unlike the other standard containers, std::list provides
393    *  specialized algorithms %unique to linked lists, such as
394    *  splicing, sorting, and in-place reversal.
395    *
396    *  A couple points on memory allocation for list<Tp>:
397    *
398    *  First, we never actually allocate a Tp, we allocate
399    *  List_node<Tp>'s and trust [20.1.5]/4 to DTRT.  This is to ensure
400    *  that after elements from %list<X,Alloc1> are spliced into
401    *  %list<X,Alloc2>, destroying the memory of the second %list is a
402    *  valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
403    *
404    *  Second, a %list conceptually represented as
405    *  @code
406    *    A <---> B <---> C <---> D
407    *  @endcode
408    *  is actually circular; a link exists between A and D.  The %list
409    *  class holds (as its only data member) a private list::iterator
410    *  pointing to @e D, not to @e A!  To get to the head of the %list,
411    *  we start at the tail and move forward by one.  When this member
412    *  iterator's next/previous pointers refer to itself, the %list is
413    *  %empty. 
414   */
415   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
416     class list : protected _List_base<_Tp, _Alloc>
417     {
418       // concept requirements
419       typedef typename _Alloc::value_type                _Alloc_value_type;
420       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
421       __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
422
423       typedef _List_base<_Tp, _Alloc>                    _Base;
424       typedef typename _Base::_Tp_alloc_type             _Tp_alloc_type;
425
426     public:
427       typedef _Tp                                        value_type;
428       typedef typename _Tp_alloc_type::pointer           pointer;
429       typedef typename _Tp_alloc_type::const_pointer     const_pointer;
430       typedef typename _Tp_alloc_type::reference         reference;
431       typedef typename _Tp_alloc_type::const_reference   const_reference;
432       typedef _List_iterator<_Tp>                        iterator;
433       typedef _List_const_iterator<_Tp>                  const_iterator;
434       typedef std::reverse_iterator<const_iterator>      const_reverse_iterator;
435       typedef std::reverse_iterator<iterator>            reverse_iterator;
436       typedef size_t                                     size_type;
437       typedef ptrdiff_t                                  difference_type;
438       typedef _Alloc                                     allocator_type;
439
440     protected:
441       // Note that pointers-to-_Node's can be ctor-converted to
442       // iterator types.
443       typedef _List_node<_Tp>                            _Node;
444
445       using _Base::_M_impl;
446       using _Base::_M_put_node;
447       using _Base::_M_get_node;
448       using _Base::_M_get_Tp_allocator;
449       using _Base::_M_get_Node_allocator;
450
451       /**
452        *  @param  x  An instance of user data.
453        *
454        *  Allocates space for a new node and constructs a copy of @a x in it.
455        */
456 #ifndef __GXX_EXPERIMENTAL_CXX0X__
457       _Node*
458       _M_create_node(const value_type& __x)
459       {
460         _Node* __p = this->_M_get_node();
461         try
462           {
463             _M_get_Tp_allocator().construct(&__p->_M_data, __x);
464           }
465         catch(...)
466           {
467             _M_put_node(__p);
468             __throw_exception_again;
469           }
470         return __p;
471       }
472 #else
473       template<typename... _Args>
474         _Node*
475         _M_create_node(_Args&&... __args)
476         {
477           _Node* __p = this->_M_get_node();
478           try
479             {
480               _M_get_Tp_allocator().construct(&__p->_M_data,
481                                               std::forward<_Args>(__args)...);
482             }
483           catch(...)
484             {
485               _M_put_node(__p);
486               __throw_exception_again;
487             }
488           return __p;
489         }
490 #endif
491
492     public:
493       // [23.2.2.1] construct/copy/destroy
494       // (assign() and get_allocator() are also listed in this section)
495       /**
496        *  @brief  Default constructor creates no elements.
497        */
498       list()
499       : _Base() { }
500
501       /**
502        *  @brief  Creates a %list with no elements.
503        *  @param  a  An allocator object.
504        */
505       explicit
506       list(const allocator_type& __a)
507       : _Base(__a) { }
508
509       /**
510        *  @brief  Creates a %list with copies of an exemplar element.
511        *  @param  n  The number of elements to initially create.
512        *  @param  value  An element to copy.
513        *  @param  a  An allocator object.
514        *
515        *  This constructor fills the %list with @a n copies of @a value.
516        */
517       explicit
518       list(size_type __n, const value_type& __value = value_type(),
519            const allocator_type& __a = allocator_type())
520       : _Base(__a)
521       { _M_fill_initialize(__n, __value); }
522
523       /**
524        *  @brief  %List copy constructor.
525        *  @param  x  A %list of identical element and allocator types.
526        *
527        *  The newly-created %list uses a copy of the allocation object used
528        *  by @a x.
529        */
530       list(const list& __x)
531       : _Base(__x._M_get_Node_allocator())
532       { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
533
534 #ifdef __GXX_EXPERIMENTAL_CXX0X__
535       /**
536        *  @brief  %List move constructor.
537        *  @param  x  A %list of identical element and allocator types.
538        *
539        *  The newly-created %list contains the exact contents of @a x.
540        *  The contents of @a x are a valid, but unspecified %list.
541        */
542       list(list&& __x)
543       : _Base(std::forward<_Base>(__x)) { }
544 #endif
545
546       /**
547        *  @brief  Builds a %list from a range.
548        *  @param  first  An input iterator.
549        *  @param  last  An input iterator.
550        *  @param  a  An allocator object.
551        *
552        *  Create a %list consisting of copies of the elements from
553        *  [@a first,@a last).  This is linear in N (where N is
554        *  distance(@a first,@a last)).
555        */
556       template<typename _InputIterator>
557         list(_InputIterator __first, _InputIterator __last,
558              const allocator_type& __a = allocator_type())
559         : _Base(__a)
560         { 
561           // Check whether it's an integral type.  If so, it's not an iterator.
562           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
563           _M_initialize_dispatch(__first, __last, _Integral());
564         }
565
566       /**
567        *  No explicit dtor needed as the _Base dtor takes care of
568        *  things.  The _Base dtor only erases the elements, and note
569        *  that if the elements themselves are pointers, the pointed-to
570        *  memory is not touched in any way.  Managing the pointer is
571        *  the user's responsibility.
572        */
573
574       /**
575        *  @brief  %List assignment operator.
576        *  @param  x  A %list of identical element and allocator types.
577        *
578        *  All the elements of @a x are copied, but unlike the copy
579        *  constructor, the allocator object is not copied.
580        */
581       list&
582       operator=(const list& __x);
583
584 #ifdef __GXX_EXPERIMENTAL_CXX0X__
585       /**
586        *  @brief  %List move assignment operator.
587        *  @param  x  A %list of identical element and allocator types.
588        *
589        *  The contents of @a x are moved into this %list (without copying).
590        *  @a x is a valid, but unspecified %list
591        */
592       list&
593       operator=(list&& __x)
594       {
595         // NB: DR 675.
596         this->clear();
597         this->swap(__x); 
598         return *this;
599       }
600 #endif
601
602       /**
603        *  @brief  Assigns a given value to a %list.
604        *  @param  n  Number of elements to be assigned.
605        *  @param  val  Value to be assigned.
606        *
607        *  This function fills a %list with @a n copies of the given
608        *  value.  Note that the assignment completely changes the %list
609        *  and that the resulting %list's size is the same as the number
610        *  of elements assigned.  Old data may be lost.
611        */
612       void
613       assign(size_type __n, const value_type& __val)
614       { _M_fill_assign(__n, __val); }
615
616       /**
617        *  @brief  Assigns a range to a %list.
618        *  @param  first  An input iterator.
619        *  @param  last   An input iterator.
620        *
621        *  This function fills a %list with copies of the elements in the
622        *  range [@a first,@a last).
623        *
624        *  Note that the assignment completely changes the %list and
625        *  that the resulting %list's size is the same as the number of
626        *  elements assigned.  Old data may be lost.
627        */
628       template<typename _InputIterator>
629         void
630         assign(_InputIterator __first, _InputIterator __last)
631         {
632           // Check whether it's an integral type.  If so, it's not an iterator.
633           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
634           _M_assign_dispatch(__first, __last, _Integral());
635         }
636
637       /// Get a copy of the memory allocation object.
638       allocator_type
639       get_allocator() const
640       { return _Base::get_allocator(); }
641
642       // iterators
643       /**
644        *  Returns a read/write iterator that points to the first element in the
645        *  %list.  Iteration is done in ordinary element order.
646        */
647       iterator
648       begin()
649       { return iterator(this->_M_impl._M_node._M_next); }
650
651       /**
652        *  Returns a read-only (constant) iterator that points to the
653        *  first element in the %list.  Iteration is done in ordinary
654        *  element order.
655        */
656       const_iterator
657       begin() const
658       { return const_iterator(this->_M_impl._M_node._M_next); }
659
660       /**
661        *  Returns a read/write iterator that points one past the last
662        *  element in the %list.  Iteration is done in ordinary element
663        *  order.
664        */
665       iterator
666       end()
667       { return iterator(&this->_M_impl._M_node); }
668
669       /**
670        *  Returns a read-only (constant) iterator that points one past
671        *  the last element in the %list.  Iteration is done in ordinary
672        *  element order.
673        */
674       const_iterator
675       end() const
676       { return const_iterator(&this->_M_impl._M_node); }
677
678       /**
679        *  Returns a read/write reverse iterator that points to the last
680        *  element in the %list.  Iteration is done in reverse element
681        *  order.
682        */
683       reverse_iterator
684       rbegin()
685       { return reverse_iterator(end()); }
686
687       /**
688        *  Returns a read-only (constant) reverse iterator that points to
689        *  the last element in the %list.  Iteration is done in reverse
690        *  element order.
691        */
692       const_reverse_iterator
693       rbegin() const
694       { return const_reverse_iterator(end()); }
695
696       /**
697        *  Returns a read/write reverse iterator that points to one
698        *  before the first element in the %list.  Iteration is done in
699        *  reverse element order.
700        */
701       reverse_iterator
702       rend()
703       { return reverse_iterator(begin()); }
704
705       /**
706        *  Returns a read-only (constant) reverse iterator that points to one
707        *  before the first element in the %list.  Iteration is done in reverse
708        *  element order.
709        */
710       const_reverse_iterator
711       rend() const
712       { return const_reverse_iterator(begin()); }
713
714 #ifdef __GXX_EXPERIMENTAL_CXX0X__
715       /**
716        *  Returns a read-only (constant) iterator that points to the
717        *  first element in the %list.  Iteration is done in ordinary
718        *  element order.
719        */
720       const_iterator
721       cbegin() const
722       { return const_iterator(this->_M_impl._M_node._M_next); }
723
724       /**
725        *  Returns a read-only (constant) iterator that points one past
726        *  the last element in the %list.  Iteration is done in ordinary
727        *  element order.
728        */
729       const_iterator
730       cend() const
731       { return const_iterator(&this->_M_impl._M_node); }
732
733       /**
734        *  Returns a read-only (constant) reverse iterator that points to
735        *  the last element in the %list.  Iteration is done in reverse
736        *  element order.
737        */
738       const_reverse_iterator
739       crbegin() const
740       { return const_reverse_iterator(end()); }
741
742       /**
743        *  Returns a read-only (constant) reverse iterator that points to one
744        *  before the first element in the %list.  Iteration is done in reverse
745        *  element order.
746        */
747       const_reverse_iterator
748       crend() const
749       { return const_reverse_iterator(begin()); }
750 #endif
751
752       // [23.2.2.2] capacity
753       /**
754        *  Returns true if the %list is empty.  (Thus begin() would equal
755        *  end().)
756        */
757       bool
758       empty() const
759       { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
760
761       /**  Returns the number of elements in the %list.  */
762       size_type
763       size() const
764       { return std::distance(begin(), end()); }
765
766       /**  Returns the size() of the largest possible %list.  */
767       size_type
768       max_size() const
769       { return _M_get_Tp_allocator().max_size(); }
770
771       /**
772        *  @brief Resizes the %list to the specified number of elements.
773        *  @param new_size Number of elements the %list should contain.
774        *  @param x Data with which new elements should be populated.
775        *
776        *  This function will %resize the %list to the specified number
777        *  of elements.  If the number is smaller than the %list's
778        *  current size the %list is truncated, otherwise the %list is
779        *  extended and new elements are populated with given data.
780        */
781       void
782       resize(size_type __new_size, value_type __x = value_type());
783
784       // element access
785       /**
786        *  Returns a read/write reference to the data at the first
787        *  element of the %list.
788        */
789       reference
790       front()
791       { return *begin(); }
792
793       /**
794        *  Returns a read-only (constant) reference to the data at the first
795        *  element of the %list.
796        */
797       const_reference
798       front() const
799       { return *begin(); }
800
801       /**
802        *  Returns a read/write reference to the data at the last element
803        *  of the %list.
804        */
805       reference
806       back()
807       { 
808         iterator __tmp = end();
809         --__tmp;
810         return *__tmp;
811       }
812
813       /**
814        *  Returns a read-only (constant) reference to the data at the last
815        *  element of the %list.
816        */
817       const_reference
818       back() const
819       { 
820         const_iterator __tmp = end();
821         --__tmp;
822         return *__tmp;
823       }
824
825       // [23.2.2.3] modifiers
826       /**
827        *  @brief  Add data to the front of the %list.
828        *  @param  x  Data to be added.
829        *
830        *  This is a typical stack operation.  The function creates an
831        *  element at the front of the %list and assigns the given data
832        *  to it.  Due to the nature of a %list this operation can be
833        *  done in constant time, and does not invalidate iterators and
834        *  references.
835        */
836 #ifndef __GXX_EXPERIMENTAL_CXX0X__
837       void
838       push_front(const value_type& __x)
839       { this->_M_insert(begin(), __x); }
840 #else
841       template<typename... _Args>
842         void
843         push_front(_Args&&... __args)
844         { this->_M_insert(begin(), std::forward<_Args>(__args)...); }
845 #endif
846
847       /**
848        *  @brief  Removes first element.
849        *
850        *  This is a typical stack operation.  It shrinks the %list by
851        *  one.  Due to the nature of a %list this operation can be done
852        *  in constant time, and only invalidates iterators/references to
853        *  the element being removed.
854        *
855        *  Note that no data is returned, and if the first element's data
856        *  is needed, it should be retrieved before pop_front() is
857        *  called.
858        */
859       void
860       pop_front()
861       { this->_M_erase(begin()); }
862
863       /**
864        *  @brief  Add data to the end of the %list.
865        *  @param  x  Data to be added.
866        *
867        *  This is a typical stack operation.  The function creates an
868        *  element at the end of the %list and assigns the given data to
869        *  it.  Due to the nature of a %list this operation can be done
870        *  in constant time, and does not invalidate iterators and
871        *  references.
872        */
873 #ifndef __GXX_EXPERIMENTAL_CXX0X__
874       void
875       push_back(const value_type& __x)
876       { this->_M_insert(end(), __x); }
877 #else
878       template<typename... _Args>
879         void
880         push_back(_Args&&... __args)
881         { this->_M_insert(end(), std::forward<_Args>(__args)...); }
882 #endif
883
884       /**
885        *  @brief  Removes last element.
886        *
887        *  This is a typical stack operation.  It shrinks the %list by
888        *  one.  Due to the nature of a %list this operation can be done
889        *  in constant time, and only invalidates iterators/references to
890        *  the element being removed.
891        *
892        *  Note that no data is returned, and if the last element's data
893        *  is needed, it should be retrieved before pop_back() is called.
894        */
895       void
896       pop_back()
897       { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
898
899 #ifdef __GXX_EXPERIMENTAL_CXX0X__
900       /**
901        *  @brief  Constructs object in %list before specified iterator.
902        *  @param  position  A const_iterator into the %list.
903        *  @param  args  Arguments.
904        *  @return  An iterator that points to the inserted data.
905        *
906        *  This function will insert an object of type T constructed
907        *  with T(std::forward<Args>(args)...) before the specified
908        *  location.  Due to the nature of a %list this operation can
909        *  be done in constant time, and does not invalidate iterators
910        *  and references.
911        */
912       template<typename... _Args>
913         iterator
914         emplace(iterator __position, _Args&&... __args);
915 #endif
916
917       /**
918        *  @brief  Inserts given value into %list before specified iterator.
919        *  @param  position  An iterator into the %list.
920        *  @param  x  Data to be inserted.
921        *  @return  An iterator that points to the inserted data.
922        *
923        *  This function will insert a copy of the given value before
924        *  the specified location.  Due to the nature of a %list this
925        *  operation can be done in constant time, and does not
926        *  invalidate iterators and references.
927        */
928       iterator
929       insert(iterator __position, const value_type& __x);
930
931 #ifdef __GXX_EXPERIMENTAL_CXX0X__
932       /**
933        *  @brief  Inserts given rvalue into %list before specified iterator.
934        *  @param  position  An iterator into the %list.
935        *  @param  x  Data to be inserted.
936        *  @return  An iterator that points to the inserted data.
937        *
938        *  This function will insert a copy of the given rvalue before
939        *  the specified location.  Due to the nature of a %list this
940        *  operation can be done in constant time, and does not
941        *  invalidate iterators and references.
942         */
943       iterator
944       insert(iterator __position, value_type&& __x)
945       { return emplace(__position, std::move(__x)); }
946 #endif
947
948       /**
949        *  @brief  Inserts a number of copies of given data into the %list.
950        *  @param  position  An iterator into the %list.
951        *  @param  n  Number of elements to be inserted.
952        *  @param  x  Data to be inserted.
953        *
954        *  This function will insert a specified number of copies of the
955        *  given data before the location specified by @a position.
956        *
957        *  This operation is linear in the number of elements inserted and
958        *  does not invalidate iterators and references.
959        */
960       void
961       insert(iterator __position, size_type __n, const value_type& __x)
962       {  
963         list __tmp(__n, __x, _M_get_Node_allocator());
964         splice(__position, __tmp);
965       }
966
967       /**
968        *  @brief  Inserts a range into the %list.
969        *  @param  position  An iterator into the %list.
970        *  @param  first  An input iterator.
971        *  @param  last   An input iterator.
972        *
973        *  This function will insert copies of the data in the range [@a
974        *  first,@a last) into the %list before the location specified by
975        *  @a position.
976        *
977        *  This operation is linear in the number of elements inserted and
978        *  does not invalidate iterators and references.
979        */
980       template<typename _InputIterator>
981         void
982         insert(iterator __position, _InputIterator __first,
983                _InputIterator __last)
984         {
985           list __tmp(__first, __last, _M_get_Node_allocator());
986           splice(__position, __tmp);
987         }
988
989       /**
990        *  @brief  Remove element at given position.
991        *  @param  position  Iterator pointing to element to be erased.
992        *  @return  An iterator pointing to the next element (or end()).
993        *
994        *  This function will erase the element at the given position and thus
995        *  shorten the %list by one.
996        *
997        *  Due to the nature of a %list this operation can be done in
998        *  constant time, and only invalidates iterators/references to
999        *  the element being removed.  The user is also cautioned that
1000        *  this function only erases the element, and that if the element
1001        *  is itself a pointer, the pointed-to memory is not touched in
1002        *  any way.  Managing the pointer is the user's responsibility.
1003        */
1004       iterator
1005       erase(iterator __position);
1006
1007       /**
1008        *  @brief  Remove a range of elements.
1009        *  @param  first  Iterator pointing to the first element to be erased.
1010        *  @param  last  Iterator pointing to one past the last element to be
1011        *                erased.
1012        *  @return  An iterator pointing to the element pointed to by @a last
1013        *           prior to erasing (or end()).
1014        *
1015        *  This function will erase the elements in the range @a
1016        *  [first,last) and shorten the %list accordingly.
1017        *
1018        *  This operation is linear time in the size of the range and only
1019        *  invalidates iterators/references to the element being removed.
1020        *  The user is also cautioned that this function only erases the
1021        *  elements, and that if the elements themselves are pointers, the
1022        *  pointed-to memory is not touched in any way.  Managing the pointer
1023        *  is the user's responsibility.
1024        */
1025       iterator
1026       erase(iterator __first, iterator __last)
1027       {
1028         while (__first != __last)
1029           __first = erase(__first);
1030         return __last;
1031       }
1032
1033       /**
1034        *  @brief  Swaps data with another %list.
1035        *  @param  x  A %list of the same element and allocator types.
1036        *
1037        *  This exchanges the elements between two lists in constant
1038        *  time.  Note that the global std::swap() function is
1039        *  specialized such that std::swap(l1,l2) will feed to this
1040        *  function.
1041        */
1042       void
1043 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1044       swap(list&& __x)
1045 #else
1046       swap(list& __x)
1047 #endif
1048       {
1049         _List_node_base::swap(this->_M_impl._M_node, __x._M_impl._M_node);
1050
1051         // _GLIBCXX_RESOLVE_LIB_DEFECTS
1052         // 431. Swapping containers with unequal allocators.
1053         std::__alloc_swap<typename _Base::_Node_alloc_type>::
1054           _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator());
1055       }
1056
1057       /**
1058        *  Erases all the elements.  Note that this function only erases
1059        *  the elements, and that if the elements themselves are
1060        *  pointers, the pointed-to memory is not touched in any way.
1061        *  Managing the pointer is the user's responsibility.
1062        */
1063       void
1064       clear()
1065       {
1066         _Base::_M_clear();
1067         _Base::_M_init();
1068       }
1069
1070       // [23.2.2.4] list operations
1071       /**
1072        *  @brief  Insert contents of another %list.
1073        *  @param  position  Iterator referencing the element to insert before.
1074        *  @param  x  Source list.
1075        *
1076        *  The elements of @a x are inserted in constant time in front of
1077        *  the element referenced by @a position.  @a x becomes an empty
1078        *  list.
1079        *
1080        *  Requires this != @a x.
1081        */
1082       void
1083 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1084       splice(iterator __position, list&& __x)
1085 #else
1086       splice(iterator __position, list& __x)
1087 #endif
1088       {
1089         if (!__x.empty())
1090           {
1091             _M_check_equal_allocators(__x);
1092
1093             this->_M_transfer(__position, __x.begin(), __x.end());
1094           }
1095       }
1096
1097       /**
1098        *  @brief  Insert element from another %list.
1099        *  @param  position  Iterator referencing the element to insert before.
1100        *  @param  x  Source list.
1101        *  @param  i  Iterator referencing the element to move.
1102        *
1103        *  Removes the element in list @a x referenced by @a i and
1104        *  inserts it into the current list before @a position.
1105        */
1106       void
1107 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1108       splice(iterator __position, list&& __x, iterator __i)
1109 #else
1110       splice(iterator __position, list& __x, iterator __i)
1111 #endif
1112       {
1113         iterator __j = __i;
1114         ++__j;
1115         if (__position == __i || __position == __j)
1116           return;
1117
1118         if (this != &__x)
1119           _M_check_equal_allocators(__x);
1120
1121         this->_M_transfer(__position, __i, __j);
1122       }
1123
1124       /**
1125        *  @brief  Insert range from another %list.
1126        *  @param  position  Iterator referencing the element to insert before.
1127        *  @param  x  Source list.
1128        *  @param  first  Iterator referencing the start of range in x.
1129        *  @param  last  Iterator referencing the end of range in x.
1130        *
1131        *  Removes elements in the range [first,last) and inserts them
1132        *  before @a position in constant time.
1133        *
1134        *  Undefined if @a position is in [first,last).
1135        */
1136       void
1137 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1138       splice(iterator __position, list&& __x, iterator __first,
1139              iterator __last)
1140 #else
1141       splice(iterator __position, list& __x, iterator __first,
1142              iterator __last)
1143 #endif
1144       {
1145         if (__first != __last)
1146           {
1147             if (this != &__x)
1148               _M_check_equal_allocators(__x);
1149
1150             this->_M_transfer(__position, __first, __last);
1151           }
1152       }
1153
1154       /**
1155        *  @brief  Remove all elements equal to value.
1156        *  @param  value  The value to remove.
1157        *
1158        *  Removes every element in the list equal to @a value.
1159        *  Remaining elements stay in list order.  Note that this
1160        *  function only erases the elements, and that if the elements
1161        *  themselves are pointers, the pointed-to memory is not
1162        *  touched in any way.  Managing the pointer is the user's
1163        *  responsibility.
1164        */
1165       void
1166       remove(const _Tp& __value);
1167
1168       /**
1169        *  @brief  Remove all elements satisfying a predicate.
1170        *  @param  Predicate  Unary predicate function or object.
1171        *
1172        *  Removes every element in the list for which the predicate
1173        *  returns true.  Remaining elements stay in list order.  Note
1174        *  that this function only erases the elements, and that if the
1175        *  elements themselves are pointers, the pointed-to memory is
1176        *  not touched in any way.  Managing the pointer is the user's
1177        *  responsibility.
1178        */
1179       template<typename _Predicate>
1180         void
1181         remove_if(_Predicate);
1182
1183       /**
1184        *  @brief  Remove consecutive duplicate elements.
1185        *
1186        *  For each consecutive set of elements with the same value,
1187        *  remove all but the first one.  Remaining elements stay in
1188        *  list order.  Note that this function only erases the
1189        *  elements, and that if the elements themselves are pointers,
1190        *  the pointed-to memory is not touched in any way.  Managing
1191        *  the pointer is the user's responsibility.
1192        */
1193       void
1194       unique();
1195
1196       /**
1197        *  @brief  Remove consecutive elements satisfying a predicate.
1198        *  @param  BinaryPredicate  Binary predicate function or object.
1199        *
1200        *  For each consecutive set of elements [first,last) that
1201        *  satisfy predicate(first,i) where i is an iterator in
1202        *  [first,last), remove all but the first one.  Remaining
1203        *  elements stay in list order.  Note that this function only
1204        *  erases the elements, and that if the elements themselves are
1205        *  pointers, the pointed-to memory is not touched in any way.
1206        *  Managing the pointer is the user's responsibility.
1207        */
1208       template<typename _BinaryPredicate>
1209         void
1210         unique(_BinaryPredicate);
1211
1212       /**
1213        *  @brief  Merge sorted lists.
1214        *  @param  x  Sorted list to merge.
1215        *
1216        *  Assumes that both @a x and this list are sorted according to
1217        *  operator<().  Merges elements of @a x into this list in
1218        *  sorted order, leaving @a x empty when complete.  Elements in
1219        *  this list precede elements in @a x that are equal.
1220        */
1221       void
1222 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1223       merge(list&& __x);
1224 #else
1225       merge(list& __x);
1226 #endif
1227
1228       /**
1229        *  @brief  Merge sorted lists according to comparison function.
1230        *  @param  x  Sorted list to merge.
1231        *  @param StrictWeakOrdering Comparison function defining
1232        *  sort order.
1233        *
1234        *  Assumes that both @a x and this list are sorted according to
1235        *  StrictWeakOrdering.  Merges elements of @a x into this list
1236        *  in sorted order, leaving @a x empty when complete.  Elements
1237        *  in this list precede elements in @a x that are equivalent
1238        *  according to StrictWeakOrdering().
1239        */
1240       template<typename _StrictWeakOrdering>
1241         void
1242 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1243         merge(list&&, _StrictWeakOrdering);
1244 #else
1245         merge(list&, _StrictWeakOrdering);
1246 #endif
1247
1248       /**
1249        *  @brief  Reverse the elements in list.
1250        *
1251        *  Reverse the order of elements in the list in linear time.
1252        */
1253       void
1254       reverse()
1255       { this->_M_impl._M_node.reverse(); }
1256
1257       /**
1258        *  @brief  Sort the elements.
1259        *
1260        *  Sorts the elements of this list in NlogN time.  Equivalent
1261        *  elements remain in list order.
1262        */
1263       void
1264       sort();
1265
1266       /**
1267        *  @brief  Sort the elements according to comparison function.
1268        *
1269        *  Sorts the elements of this list in NlogN time.  Equivalent
1270        *  elements remain in list order.
1271        */
1272       template<typename _StrictWeakOrdering>
1273         void
1274         sort(_StrictWeakOrdering);
1275
1276     protected:
1277       // Internal constructor functions follow.
1278
1279       // Called by the range constructor to implement [23.1.1]/9
1280
1281       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1282       // 438. Ambiguity in the "do the right thing" clause
1283       template<typename _Integer>
1284         void
1285         _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1286         { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1287
1288       // Called by the range constructor to implement [23.1.1]/9
1289       template<typename _InputIterator>
1290         void
1291         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1292                                __false_type)
1293         {
1294           for (; __first != __last; ++__first)
1295             push_back(*__first);
1296         }
1297
1298       // Called by list(n,v,a), and the range constructor when it turns out
1299       // to be the same thing.
1300       void
1301       _M_fill_initialize(size_type __n, const value_type& __x)
1302       {
1303         for (; __n > 0; --__n)
1304           push_back(__x);
1305       }
1306
1307
1308       // Internal assign functions follow.
1309
1310       // Called by the range assign to implement [23.1.1]/9
1311
1312       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1313       // 438. Ambiguity in the "do the right thing" clause
1314       template<typename _Integer>
1315         void
1316         _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1317         { _M_fill_assign(__n, __val); }
1318
1319       // Called by the range assign to implement [23.1.1]/9
1320       template<typename _InputIterator>
1321         void
1322         _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1323                            __false_type);
1324
1325       // Called by assign(n,t), and the range assign when it turns out
1326       // to be the same thing.
1327       void
1328       _M_fill_assign(size_type __n, const value_type& __val);
1329
1330
1331       // Moves the elements from [first,last) before position.
1332       void
1333       _M_transfer(iterator __position, iterator __first, iterator __last)
1334       { __position._M_node->transfer(__first._M_node, __last._M_node); }
1335
1336       // Inserts new element at position given and with value given.
1337 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1338       void
1339       _M_insert(iterator __position, const value_type& __x)
1340       {
1341         _Node* __tmp = _M_create_node(__x);
1342         __tmp->hook(__position._M_node);
1343       }
1344 #else
1345      template<typename... _Args>
1346        void
1347        _M_insert(iterator __position, _Args&&... __args)
1348        {
1349          _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
1350          __tmp->hook(__position._M_node);
1351        }
1352 #endif
1353
1354       // Erases element at position given.
1355       void
1356       _M_erase(iterator __position)
1357       {
1358         __position._M_node->unhook();
1359         _Node* __n = static_cast<_Node*>(__position._M_node);
1360         _M_get_Tp_allocator().destroy(&__n->_M_data);
1361         _M_put_node(__n);
1362       }
1363
1364       // To implement the splice (and merge) bits of N1599.
1365       void
1366       _M_check_equal_allocators(list& __x)
1367       {
1368         if (std::__alloc_neq<typename _Base::_Node_alloc_type>::
1369             _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator()))
1370           __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1371       }
1372     };
1373
1374   /**
1375    *  @brief  List equality comparison.
1376    *  @param  x  A %list.
1377    *  @param  y  A %list of the same type as @a x.
1378    *  @return  True iff the size and elements of the lists are equal.
1379    *
1380    *  This is an equivalence relation.  It is linear in the size of
1381    *  the lists.  Lists are considered equivalent if their sizes are
1382    *  equal, and if corresponding elements compare equal.
1383   */
1384   template<typename _Tp, typename _Alloc>
1385     inline bool
1386     operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1387     {
1388       typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
1389       const_iterator __end1 = __x.end();
1390       const_iterator __end2 = __y.end();
1391
1392       const_iterator __i1 = __x.begin();
1393       const_iterator __i2 = __y.begin();
1394       while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
1395         {
1396           ++__i1;
1397           ++__i2;
1398         }
1399       return __i1 == __end1 && __i2 == __end2;
1400     }
1401
1402   /**
1403    *  @brief  List ordering relation.
1404    *  @param  x  A %list.
1405    *  @param  y  A %list of the same type as @a x.
1406    *  @return  True iff @a x is lexicographically less than @a y.
1407    *
1408    *  This is a total ordering relation.  It is linear in the size of the
1409    *  lists.  The elements must be comparable with @c <.
1410    *
1411    *  See std::lexicographical_compare() for how the determination is made.
1412   */
1413   template<typename _Tp, typename _Alloc>
1414     inline bool
1415     operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1416     { return std::lexicographical_compare(__x.begin(), __x.end(),
1417                                           __y.begin(), __y.end()); }
1418
1419   /// Based on operator==
1420   template<typename _Tp, typename _Alloc>
1421     inline bool
1422     operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1423     { return !(__x == __y); }
1424
1425   /// Based on operator<
1426   template<typename _Tp, typename _Alloc>
1427     inline bool
1428     operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1429     { return __y < __x; }
1430
1431   /// Based on operator<
1432   template<typename _Tp, typename _Alloc>
1433     inline bool
1434     operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1435     { return !(__y < __x); }
1436
1437   /// Based on operator<
1438   template<typename _Tp, typename _Alloc>
1439     inline bool
1440     operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1441     { return !(__x < __y); }
1442
1443   /// See std::list::swap().
1444   template<typename _Tp, typename _Alloc>
1445     inline void
1446     swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1447     { __x.swap(__y); }
1448
1449 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1450   template<typename _Tp, typename _Alloc>
1451     inline void
1452     swap(list<_Tp, _Alloc>&& __x, list<_Tp, _Alloc>& __y)
1453     { __x.swap(__y); }
1454
1455   template<typename _Tp, typename _Alloc>
1456     inline void
1457     swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>&& __y)
1458     { __x.swap(__y); }
1459 #endif
1460
1461 _GLIBCXX_END_NESTED_NAMESPACE
1462
1463 #endif /* _STL_LIST_H */