1 // List implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
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)
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.
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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.
33 * Hewlett-Packard Company
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.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
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.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 namespace _GLIBCXX_STD
66 template<typename _Tp, typename _Alloc>
68 _List_base<_Tp, _Alloc>::
71 typedef _List_node<_Tp> _Node;
72 _Node* __cur = static_cast<_Node*>(this->_M_impl._M_node._M_next);
73 while (__cur != &this->_M_impl._M_node)
76 __cur = static_cast<_Node*>(__cur->_M_next);
77 _M_get_Tp_allocator().destroy(&__tmp->_M_data);
82 template<typename _Tp, typename _Alloc>
83 typename list<_Tp, _Alloc>::iterator
85 insert(iterator __position, const value_type& __x)
87 _Node* __tmp = _M_create_node(__x);
88 __tmp->hook(__position._M_node);
89 return iterator(__tmp);
92 template<typename _Tp, typename _Alloc>
93 typename list<_Tp, _Alloc>::iterator
95 erase(iterator __position)
97 iterator __ret = iterator(__position._M_node->_M_next);
102 template<typename _Tp, typename _Alloc>
105 resize(size_type __new_size, value_type __x)
107 iterator __i = begin();
109 for (; __i != end() && __len < __new_size; ++__i, ++__len)
111 if (__len == __new_size)
114 insert(end(), __new_size - __len, __x);
117 template<typename _Tp, typename _Alloc>
120 operator=(const list& __x)
124 iterator __first1 = begin();
125 iterator __last1 = end();
126 const_iterator __first2 = __x.begin();
127 const_iterator __last2 = __x.end();
128 for (; __first1 != __last1 && __first2 != __last2;
129 ++__first1, ++__first2)
130 *__first1 = *__first2;
131 if (__first2 == __last2)
132 erase(__first1, __last1);
134 insert(__last1, __first2, __last2);
139 template<typename _Tp, typename _Alloc>
142 _M_fill_assign(size_type __n, const value_type& __val)
144 iterator __i = begin();
145 for (; __i != end() && __n > 0; ++__i, --__n)
148 insert(end(), __n, __val);
153 template<typename _Tp, typename _Alloc>
154 template <typename _InputIterator>
157 _M_assign_dispatch(_InputIterator __first2, _InputIterator __last2,
160 iterator __first1 = begin();
161 iterator __last1 = end();
162 for (; __first1 != __last1 && __first2 != __last2;
163 ++__first1, ++__first2)
164 *__first1 = *__first2;
165 if (__first2 == __last2)
166 erase(__first1, __last1);
168 insert(__last1, __first2, __last2);
171 template<typename _Tp, typename _Alloc>
174 remove(const value_type& __value)
176 iterator __first = begin();
177 iterator __last = end();
178 while (__first != __last)
180 iterator __next = __first;
182 if (*__first == __value)
188 template<typename _Tp, typename _Alloc>
193 iterator __first = begin();
194 iterator __last = end();
195 if (__first == __last)
197 iterator __next = __first;
198 while (++__next != __last)
200 if (*__first == *__next)
208 template<typename _Tp, typename _Alloc>
213 // _GLIBCXX_RESOLVE_LIB_DEFECTS
214 // 300. list::merge() specification incomplete
217 iterator __first1 = begin();
218 iterator __last1 = end();
219 iterator __first2 = __x.begin();
220 iterator __last2 = __x.end();
221 while (__first1 != __last1 && __first2 != __last2)
222 if (*__first2 < *__first1)
224 iterator __next = __first2;
225 _M_transfer(__first1, __first2, ++__next);
230 if (__first2 != __last2)
231 _M_transfer(__last1, __first2, __last2);
235 template<typename _Tp, typename _Alloc>
240 // Do nothing if the list has length 0 or 1.
241 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
242 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
246 list * __fill = &__tmp[0];
251 __carry.splice(__carry.begin(), *this, begin());
253 for(__counter = &__tmp[0];
254 __counter != __fill && !__counter->empty();
257 __counter->merge(__carry);
258 __carry.swap(*__counter);
260 __carry.swap(*__counter);
261 if (__counter == __fill)
266 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
267 __counter->merge(*(__counter - 1));
268 swap( *(__fill - 1) );
272 template<typename _Tp, typename _Alloc>
273 template <typename _Predicate>
276 remove_if(_Predicate __pred)
278 iterator __first = begin();
279 iterator __last = end();
280 while (__first != __last)
282 iterator __next = __first;
284 if (__pred(*__first))
290 template<typename _Tp, typename _Alloc>
291 template <typename _BinaryPredicate>
294 unique(_BinaryPredicate __binary_pred)
296 iterator __first = begin();
297 iterator __last = end();
298 if (__first == __last)
300 iterator __next = __first;
301 while (++__next != __last)
303 if (__binary_pred(*__first, *__next))
311 template<typename _Tp, typename _Alloc>
312 template <typename _StrictWeakOrdering>
315 merge(list& __x, _StrictWeakOrdering __comp)
317 // _GLIBCXX_RESOLVE_LIB_DEFECTS
318 // 300. list::merge() specification incomplete
321 iterator __first1 = begin();
322 iterator __last1 = end();
323 iterator __first2 = __x.begin();
324 iterator __last2 = __x.end();
325 while (__first1 != __last1 && __first2 != __last2)
326 if (__comp(*__first2, *__first1))
328 iterator __next = __first2;
329 _M_transfer(__first1, __first2, ++__next);
334 if (__first2 != __last2)
335 _M_transfer(__last1, __first2, __last2);
339 template<typename _Tp, typename _Alloc>
340 template <typename _StrictWeakOrdering>
343 sort(_StrictWeakOrdering __comp)
345 // Do nothing if the list has length 0 or 1.
346 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
347 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
351 list * __fill = &__tmp[0];
356 __carry.splice(__carry.begin(), *this, begin());
358 for(__counter = &__tmp[0];
359 __counter != __fill && !__counter->empty();
362 __counter->merge(__carry, __comp);
363 __carry.swap(*__counter);
365 __carry.swap(*__counter);
366 if (__counter == __fill)
371 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
372 __counter->merge(*(__counter - 1), __comp);
378 #endif /* _LIST_TCC */