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