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