OSDN Git Service

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