xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/bits/list.tcc (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // List implementation (out of line) -*- 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 list.tcc
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_TCC
63*404b540aSrobert #define _LIST_TCC 1
64*404b540aSrobert 
_GLIBCXX_BEGIN_NESTED_NAMESPACE(std,_GLIBCXX_STD)65*404b540aSrobert _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
66*404b540aSrobert 
67*404b540aSrobert   template<typename _Tp, typename _Alloc>
68*404b540aSrobert     void
69*404b540aSrobert     _List_base<_Tp, _Alloc>::
70*404b540aSrobert     _M_clear()
71*404b540aSrobert     {
72*404b540aSrobert       typedef _List_node<_Tp>  _Node;
73*404b540aSrobert       _Node* __cur = static_cast<_Node*>(this->_M_impl._M_node._M_next);
74*404b540aSrobert       while (__cur != &this->_M_impl._M_node)
75*404b540aSrobert 	{
76*404b540aSrobert 	  _Node* __tmp = __cur;
77*404b540aSrobert 	  __cur = static_cast<_Node*>(__cur->_M_next);
78*404b540aSrobert 	  _M_get_Tp_allocator().destroy(&__tmp->_M_data);
79*404b540aSrobert 	  _M_put_node(__tmp);
80*404b540aSrobert 	}
81*404b540aSrobert     }
82*404b540aSrobert 
83*404b540aSrobert   template<typename _Tp, typename _Alloc>
84*404b540aSrobert     typename list<_Tp, _Alloc>::iterator
85*404b540aSrobert     list<_Tp, _Alloc>::
insert(iterator __position,const value_type & __x)86*404b540aSrobert     insert(iterator __position, const value_type& __x)
87*404b540aSrobert     {
88*404b540aSrobert       _Node* __tmp = _M_create_node(__x);
89*404b540aSrobert       __tmp->hook(__position._M_node);
90*404b540aSrobert       return iterator(__tmp);
91*404b540aSrobert     }
92*404b540aSrobert 
93*404b540aSrobert   template<typename _Tp, typename _Alloc>
94*404b540aSrobert     typename list<_Tp, _Alloc>::iterator
95*404b540aSrobert     list<_Tp, _Alloc>::
erase(iterator __position)96*404b540aSrobert     erase(iterator __position)
97*404b540aSrobert     {
98*404b540aSrobert       iterator __ret = iterator(__position._M_node->_M_next);
99*404b540aSrobert       _M_erase(__position);
100*404b540aSrobert       return __ret;
101*404b540aSrobert     }
102*404b540aSrobert 
103*404b540aSrobert   template<typename _Tp, typename _Alloc>
104*404b540aSrobert     void
105*404b540aSrobert     list<_Tp, _Alloc>::
resize(size_type __new_size,value_type __x)106*404b540aSrobert     resize(size_type __new_size, value_type __x)
107*404b540aSrobert     {
108*404b540aSrobert       iterator __i = begin();
109*404b540aSrobert       size_type __len = 0;
110*404b540aSrobert       for (; __i != end() && __len < __new_size; ++__i, ++__len)
111*404b540aSrobert         ;
112*404b540aSrobert       if (__len == __new_size)
113*404b540aSrobert         erase(__i, end());
114*404b540aSrobert       else                          // __i == end()
115*404b540aSrobert         insert(end(), __new_size - __len, __x);
116*404b540aSrobert     }
117*404b540aSrobert 
118*404b540aSrobert   template<typename _Tp, typename _Alloc>
119*404b540aSrobert     list<_Tp, _Alloc>&
120*404b540aSrobert     list<_Tp, _Alloc>::
operator =(const list & __x)121*404b540aSrobert     operator=(const list& __x)
122*404b540aSrobert     {
123*404b540aSrobert       if (this != &__x)
124*404b540aSrobert 	{
125*404b540aSrobert 	  iterator __first1 = begin();
126*404b540aSrobert 	  iterator __last1 = end();
127*404b540aSrobert 	  const_iterator __first2 = __x.begin();
128*404b540aSrobert 	  const_iterator __last2 = __x.end();
129*404b540aSrobert 	  for (; __first1 != __last1 && __first2 != __last2;
130*404b540aSrobert 	       ++__first1, ++__first2)
131*404b540aSrobert 	    *__first1 = *__first2;
132*404b540aSrobert 	  if (__first2 == __last2)
133*404b540aSrobert 	    erase(__first1, __last1);
134*404b540aSrobert 	  else
135*404b540aSrobert 	    insert(__last1, __first2, __last2);
136*404b540aSrobert 	}
137*404b540aSrobert       return *this;
138*404b540aSrobert     }
139*404b540aSrobert 
140*404b540aSrobert   template<typename _Tp, typename _Alloc>
141*404b540aSrobert     void
142*404b540aSrobert     list<_Tp, _Alloc>::
_M_fill_assign(size_type __n,const value_type & __val)143*404b540aSrobert     _M_fill_assign(size_type __n, const value_type& __val)
144*404b540aSrobert     {
145*404b540aSrobert       iterator __i = begin();
146*404b540aSrobert       for (; __i != end() && __n > 0; ++__i, --__n)
147*404b540aSrobert         *__i = __val;
148*404b540aSrobert       if (__n > 0)
149*404b540aSrobert         insert(end(), __n, __val);
150*404b540aSrobert       else
151*404b540aSrobert         erase(__i, end());
152*404b540aSrobert     }
153*404b540aSrobert 
154*404b540aSrobert   template<typename _Tp, typename _Alloc>
155*404b540aSrobert     template <typename _InputIterator>
156*404b540aSrobert       void
157*404b540aSrobert       list<_Tp, _Alloc>::
_M_assign_dispatch(_InputIterator __first2,_InputIterator __last2,__false_type)158*404b540aSrobert       _M_assign_dispatch(_InputIterator __first2, _InputIterator __last2,
159*404b540aSrobert 			 __false_type)
160*404b540aSrobert       {
161*404b540aSrobert         iterator __first1 = begin();
162*404b540aSrobert         iterator __last1 = end();
163*404b540aSrobert         for (; __first1 != __last1 && __first2 != __last2;
164*404b540aSrobert 	     ++__first1, ++__first2)
165*404b540aSrobert           *__first1 = *__first2;
166*404b540aSrobert         if (__first2 == __last2)
167*404b540aSrobert           erase(__first1, __last1);
168*404b540aSrobert         else
169*404b540aSrobert           insert(__last1, __first2, __last2);
170*404b540aSrobert       }
171*404b540aSrobert 
172*404b540aSrobert   template<typename _Tp, typename _Alloc>
173*404b540aSrobert     void
174*404b540aSrobert     list<_Tp, _Alloc>::
remove(const value_type & __value)175*404b540aSrobert     remove(const value_type& __value)
176*404b540aSrobert     {
177*404b540aSrobert       iterator __first = begin();
178*404b540aSrobert       iterator __last = end();
179*404b540aSrobert       while (__first != __last)
180*404b540aSrobert 	{
181*404b540aSrobert 	  iterator __next = __first;
182*404b540aSrobert 	  ++__next;
183*404b540aSrobert 	  if (*__first == __value)
184*404b540aSrobert 	    _M_erase(__first);
185*404b540aSrobert 	  __first = __next;
186*404b540aSrobert 	}
187*404b540aSrobert     }
188*404b540aSrobert 
189*404b540aSrobert   template<typename _Tp, typename _Alloc>
190*404b540aSrobert     void
191*404b540aSrobert     list<_Tp, _Alloc>::
unique()192*404b540aSrobert     unique()
193*404b540aSrobert     {
194*404b540aSrobert       iterator __first = begin();
195*404b540aSrobert       iterator __last = end();
196*404b540aSrobert       if (__first == __last)
197*404b540aSrobert 	return;
198*404b540aSrobert       iterator __next = __first;
199*404b540aSrobert       while (++__next != __last)
200*404b540aSrobert 	{
201*404b540aSrobert 	  if (*__first == *__next)
202*404b540aSrobert 	    _M_erase(__next);
203*404b540aSrobert 	  else
204*404b540aSrobert 	    __first = __next;
205*404b540aSrobert 	  __next = __first;
206*404b540aSrobert 	}
207*404b540aSrobert     }
208*404b540aSrobert 
209*404b540aSrobert   template<typename _Tp, typename _Alloc>
210*404b540aSrobert     void
211*404b540aSrobert     list<_Tp, _Alloc>::
merge(list & __x)212*404b540aSrobert     merge(list& __x)
213*404b540aSrobert     {
214*404b540aSrobert       // _GLIBCXX_RESOLVE_LIB_DEFECTS
215*404b540aSrobert       // 300. list::merge() specification incomplete
216*404b540aSrobert       if (this != &__x)
217*404b540aSrobert 	{
218*404b540aSrobert 	  _M_check_equal_allocators(__x);
219*404b540aSrobert 
220*404b540aSrobert 	  iterator __first1 = begin();
221*404b540aSrobert 	  iterator __last1 = end();
222*404b540aSrobert 	  iterator __first2 = __x.begin();
223*404b540aSrobert 	  iterator __last2 = __x.end();
224*404b540aSrobert 	  while (__first1 != __last1 && __first2 != __last2)
225*404b540aSrobert 	    if (*__first2 < *__first1)
226*404b540aSrobert 	      {
227*404b540aSrobert 		iterator __next = __first2;
228*404b540aSrobert 		_M_transfer(__first1, __first2, ++__next);
229*404b540aSrobert 		__first2 = __next;
230*404b540aSrobert 	      }
231*404b540aSrobert 	    else
232*404b540aSrobert 	      ++__first1;
233*404b540aSrobert 	  if (__first2 != __last2)
234*404b540aSrobert 	    _M_transfer(__last1, __first2, __last2);
235*404b540aSrobert 	}
236*404b540aSrobert     }
237*404b540aSrobert 
238*404b540aSrobert   template<typename _Tp, typename _Alloc>
239*404b540aSrobert     template <typename _StrictWeakOrdering>
240*404b540aSrobert       void
241*404b540aSrobert       list<_Tp, _Alloc>::
merge(list & __x,_StrictWeakOrdering __comp)242*404b540aSrobert       merge(list& __x, _StrictWeakOrdering __comp)
243*404b540aSrobert       {
244*404b540aSrobert 	// _GLIBCXX_RESOLVE_LIB_DEFECTS
245*404b540aSrobert 	// 300. list::merge() specification incomplete
246*404b540aSrobert 	if (this != &__x)
247*404b540aSrobert 	  {
248*404b540aSrobert 	    _M_check_equal_allocators(__x);
249*404b540aSrobert 
250*404b540aSrobert 	    iterator __first1 = begin();
251*404b540aSrobert 	    iterator __last1 = end();
252*404b540aSrobert 	    iterator __first2 = __x.begin();
253*404b540aSrobert 	    iterator __last2 = __x.end();
254*404b540aSrobert 	    while (__first1 != __last1 && __first2 != __last2)
255*404b540aSrobert 	      if (__comp(*__first2, *__first1))
256*404b540aSrobert 		{
257*404b540aSrobert 		  iterator __next = __first2;
258*404b540aSrobert 		  _M_transfer(__first1, __first2, ++__next);
259*404b540aSrobert 		  __first2 = __next;
260*404b540aSrobert 		}
261*404b540aSrobert 	      else
262*404b540aSrobert 		++__first1;
263*404b540aSrobert 	    if (__first2 != __last2)
264*404b540aSrobert 	      _M_transfer(__last1, __first2, __last2);
265*404b540aSrobert 	  }
266*404b540aSrobert       }
267*404b540aSrobert 
268*404b540aSrobert   template<typename _Tp, typename _Alloc>
269*404b540aSrobert     void
270*404b540aSrobert     list<_Tp, _Alloc>::
sort()271*404b540aSrobert     sort()
272*404b540aSrobert     {
273*404b540aSrobert       // Do nothing if the list has length 0 or 1.
274*404b540aSrobert       if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
275*404b540aSrobert 	  && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
276*404b540aSrobert       {
277*404b540aSrobert         list __carry;
278*404b540aSrobert         list __tmp[64];
279*404b540aSrobert         list * __fill = &__tmp[0];
280*404b540aSrobert         list * __counter;
281*404b540aSrobert 
282*404b540aSrobert         do
283*404b540aSrobert 	  {
284*404b540aSrobert 	    __carry.splice(__carry.begin(), *this, begin());
285*404b540aSrobert 
286*404b540aSrobert 	    for(__counter = &__tmp[0];
287*404b540aSrobert 		__counter != __fill && !__counter->empty();
288*404b540aSrobert 		++__counter)
289*404b540aSrobert 	      {
290*404b540aSrobert 		__counter->merge(__carry);
291*404b540aSrobert 		__carry.swap(*__counter);
292*404b540aSrobert 	      }
293*404b540aSrobert 	    __carry.swap(*__counter);
294*404b540aSrobert 	    if (__counter == __fill)
295*404b540aSrobert 	      ++__fill;
296*404b540aSrobert 	  }
297*404b540aSrobert 	while ( !empty() );
298*404b540aSrobert 
299*404b540aSrobert         for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
300*404b540aSrobert           __counter->merge(*(__counter - 1));
301*404b540aSrobert         swap( *(__fill - 1) );
302*404b540aSrobert       }
303*404b540aSrobert     }
304*404b540aSrobert 
305*404b540aSrobert   template<typename _Tp, typename _Alloc>
306*404b540aSrobert     template <typename _Predicate>
307*404b540aSrobert       void
308*404b540aSrobert       list<_Tp, _Alloc>::
remove_if(_Predicate __pred)309*404b540aSrobert       remove_if(_Predicate __pred)
310*404b540aSrobert       {
311*404b540aSrobert         iterator __first = begin();
312*404b540aSrobert         iterator __last = end();
313*404b540aSrobert         while (__first != __last)
314*404b540aSrobert 	  {
315*404b540aSrobert 	    iterator __next = __first;
316*404b540aSrobert 	    ++__next;
317*404b540aSrobert 	    if (__pred(*__first))
318*404b540aSrobert 	      _M_erase(__first);
319*404b540aSrobert 	    __first = __next;
320*404b540aSrobert 	  }
321*404b540aSrobert       }
322*404b540aSrobert 
323*404b540aSrobert   template<typename _Tp, typename _Alloc>
324*404b540aSrobert     template <typename _BinaryPredicate>
325*404b540aSrobert       void
326*404b540aSrobert       list<_Tp, _Alloc>::
unique(_BinaryPredicate __binary_pred)327*404b540aSrobert       unique(_BinaryPredicate __binary_pred)
328*404b540aSrobert       {
329*404b540aSrobert         iterator __first = begin();
330*404b540aSrobert         iterator __last = end();
331*404b540aSrobert         if (__first == __last)
332*404b540aSrobert 	  return;
333*404b540aSrobert         iterator __next = __first;
334*404b540aSrobert         while (++__next != __last)
335*404b540aSrobert 	  {
336*404b540aSrobert 	    if (__binary_pred(*__first, *__next))
337*404b540aSrobert 	      _M_erase(__next);
338*404b540aSrobert 	    else
339*404b540aSrobert 	      __first = __next;
340*404b540aSrobert 	    __next = __first;
341*404b540aSrobert 	  }
342*404b540aSrobert       }
343*404b540aSrobert 
344*404b540aSrobert   template<typename _Tp, typename _Alloc>
345*404b540aSrobert     template <typename _StrictWeakOrdering>
346*404b540aSrobert       void
347*404b540aSrobert       list<_Tp, _Alloc>::
sort(_StrictWeakOrdering __comp)348*404b540aSrobert       sort(_StrictWeakOrdering __comp)
349*404b540aSrobert       {
350*404b540aSrobert 	// Do nothing if the list has length 0 or 1.
351*404b540aSrobert 	if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
352*404b540aSrobert 	    && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
353*404b540aSrobert 	  {
354*404b540aSrobert 	    list __carry;
355*404b540aSrobert 	    list __tmp[64];
356*404b540aSrobert 	    list * __fill = &__tmp[0];
357*404b540aSrobert 	    list * __counter;
358*404b540aSrobert 
359*404b540aSrobert 	    do
360*404b540aSrobert 	      {
361*404b540aSrobert 		__carry.splice(__carry.begin(), *this, begin());
362*404b540aSrobert 
363*404b540aSrobert 		for(__counter = &__tmp[0];
364*404b540aSrobert 		    __counter != __fill && !__counter->empty();
365*404b540aSrobert 		    ++__counter)
366*404b540aSrobert 		  {
367*404b540aSrobert 		    __counter->merge(__carry, __comp);
368*404b540aSrobert 		    __carry.swap(*__counter);
369*404b540aSrobert 		  }
370*404b540aSrobert 		__carry.swap(*__counter);
371*404b540aSrobert 		if (__counter == __fill)
372*404b540aSrobert 		  ++__fill;
373*404b540aSrobert 	      }
374*404b540aSrobert 	    while ( !empty() );
375*404b540aSrobert 
376*404b540aSrobert 	    for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
377*404b540aSrobert 	      __counter->merge(*(__counter - 1), __comp);
378*404b540aSrobert 	    swap(*(__fill - 1));
379*404b540aSrobert 	  }
380*404b540aSrobert       }
381*404b540aSrobert 
382*404b540aSrobert _GLIBCXX_END_NESTED_NAMESPACE
383*404b540aSrobert 
384*404b540aSrobert #endif /* _LIST_TCC */
385*404b540aSrobert 
386