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