OSDN Git Service

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