1 // Singly-linked list implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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.
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
45 * This file is a GNU extension to the Standard C++ Library (possibly
46 * containing extensions from the HP/SGI STL subset). You should only
47 * include this header if you are using GCC 3 or later.
50 #ifndef __SGI_STL_INTERNAL_SLIST_H
51 #define __SGI_STL_INTERNAL_SLIST_H
53 #include <bits/stl_algobase.h>
54 #include <bits/stl_alloc.h>
55 #include <bits/stl_construct.h>
56 #include <bits/stl_uninitialized.h>
57 #include <bits/concept_check.h>
63 using std::_Alloc_traits;
64 using std::_Construct;
68 struct _Slist_node_base
70 _Slist_node_base* _M_next;
73 inline _Slist_node_base*
74 __slist_make_link(_Slist_node_base* __prev_node,
75 _Slist_node_base* __new_node)
77 __new_node->_M_next = __prev_node->_M_next;
78 __prev_node->_M_next = __new_node;
82 inline _Slist_node_base*
83 __slist_previous(_Slist_node_base* __head,
84 const _Slist_node_base* __node)
86 while (__head && __head->_M_next != __node)
87 __head = __head->_M_next;
91 inline const _Slist_node_base*
92 __slist_previous(const _Slist_node_base* __head,
93 const _Slist_node_base* __node)
95 while (__head && __head->_M_next != __node)
96 __head = __head->_M_next;
100 inline void __slist_splice_after(_Slist_node_base* __pos,
101 _Slist_node_base* __before_first,
102 _Slist_node_base* __before_last)
104 if (__pos != __before_first && __pos != __before_last) {
105 _Slist_node_base* __first = __before_first->_M_next;
106 _Slist_node_base* __after = __pos->_M_next;
107 __before_first->_M_next = __before_last->_M_next;
108 __pos->_M_next = __first;
109 __before_last->_M_next = __after;
114 __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
116 _Slist_node_base* __before_last = __slist_previous(__head, 0);
117 if (__before_last != __head) {
118 _Slist_node_base* __after = __pos->_M_next;
119 __pos->_M_next = __head->_M_next;
121 __before_last->_M_next = __after;
125 inline _Slist_node_base* __slist_reverse(_Slist_node_base* __node)
127 _Slist_node_base* __result = __node;
128 __node = __node->_M_next;
129 __result->_M_next = 0;
131 _Slist_node_base* __next = __node->_M_next;
132 __node->_M_next = __result;
139 inline size_t __slist_size(_Slist_node_base* __node)
142 for ( ; __node != 0; __node = __node->_M_next)
148 struct _Slist_node : public _Slist_node_base
153 struct _Slist_iterator_base
155 typedef size_t size_type;
156 typedef ptrdiff_t difference_type;
157 typedef std::forward_iterator_tag iterator_category;
159 _Slist_node_base* _M_node;
161 _Slist_iterator_base(_Slist_node_base* __x) : _M_node(__x) {}
162 void _M_incr() { _M_node = _M_node->_M_next; }
164 bool operator==(const _Slist_iterator_base& __x) const {
165 return _M_node == __x._M_node;
167 bool operator!=(const _Slist_iterator_base& __x) const {
168 return _M_node != __x._M_node;
172 template <class _Tp, class _Ref, class _Ptr>
173 struct _Slist_iterator : public _Slist_iterator_base
175 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
176 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
177 typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
179 typedef _Tp value_type;
180 typedef _Ptr pointer;
181 typedef _Ref reference;
182 typedef _Slist_node<_Tp> _Node;
184 _Slist_iterator(_Node* __x) : _Slist_iterator_base(__x) {}
185 _Slist_iterator() : _Slist_iterator_base(0) {}
186 _Slist_iterator(const iterator& __x) : _Slist_iterator_base(__x._M_node) {}
188 reference operator*() const { return ((_Node*) _M_node)->_M_data; }
189 pointer operator->() const { return &(operator*()); }
196 _Self operator++(int)
205 // Base class that encapsulates details of allocators. Three cases:
206 // an ordinary standard-conforming allocator, a standard-conforming
207 // allocator with no non-static data, and an SGI-style allocator.
208 // This complexity is necessary only because we're worrying about backward
209 // compatibility and because we want to avoid wasting storage on an
210 // allocator instance if it isn't necessary.
212 // Base for general standard-conforming allocators.
213 template <class _Tp, class _Allocator, bool _IsStatic>
214 class _Slist_alloc_base {
216 typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
218 allocator_type get_allocator() const { return _M_node_allocator; }
220 _Slist_alloc_base(const allocator_type& __a) : _M_node_allocator(__a) {}
223 _Slist_node<_Tp>* _M_get_node()
224 { return _M_node_allocator.allocate(1); }
225 void _M_put_node(_Slist_node<_Tp>* __p)
226 { _M_node_allocator.deallocate(__p, 1); }
229 typename _Alloc_traits<_Slist_node<_Tp>,_Allocator>::allocator_type
231 _Slist_node_base _M_head;
234 // Specialization for instanceless allocators.
235 template <class _Tp, class _Allocator>
236 class _Slist_alloc_base<_Tp,_Allocator, true> {
238 typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
240 allocator_type get_allocator() const { return allocator_type(); }
242 _Slist_alloc_base(const allocator_type&) {}
245 typedef typename _Alloc_traits<_Slist_node<_Tp>, _Allocator>::_Alloc_type
247 _Slist_node<_Tp>* _M_get_node() { return _Alloc_type::allocate(1); }
248 void _M_put_node(_Slist_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); }
251 _Slist_node_base _M_head;
255 template <class _Tp, class _Alloc>
257 : public _Slist_alloc_base<_Tp, _Alloc,
258 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
260 typedef _Slist_alloc_base<_Tp, _Alloc,
261 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
263 typedef typename _Base::allocator_type allocator_type;
265 _Slist_base(const allocator_type& __a)
266 : _Base(__a) { this->_M_head._M_next = 0; }
267 ~_Slist_base() { _M_erase_after(&this->_M_head, 0); }
271 _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
273 _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
274 _Slist_node_base* __next_next = __next->_M_next;
275 __pos->_M_next = __next_next;
276 _Destroy(&__next->_M_data);
280 _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
283 template <class _Tp, class _Alloc>
285 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
286 _Slist_node_base* __last_node) {
287 _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
288 while (__cur != __last_node) {
289 _Slist_node<_Tp>* __tmp = __cur;
290 __cur = (_Slist_node<_Tp>*) __cur->_M_next;
291 _Destroy(&__tmp->_M_data);
294 __before_first->_M_next = __last_node;
298 template <class _Tp, class _Alloc = allocator<_Tp> >
299 class slist : private _Slist_base<_Tp,_Alloc>
301 // concept requirements
302 __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
305 typedef _Slist_base<_Tp,_Alloc> _Base;
307 typedef _Tp value_type;
308 typedef value_type* pointer;
309 typedef const value_type* const_pointer;
310 typedef value_type& reference;
311 typedef const value_type& const_reference;
312 typedef size_t size_type;
313 typedef ptrdiff_t difference_type;
315 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
316 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
318 typedef typename _Base::allocator_type allocator_type;
319 allocator_type get_allocator() const { return _Base::get_allocator(); }
322 typedef _Slist_node<_Tp> _Node;
323 typedef _Slist_node_base _Node_base;
324 typedef _Slist_iterator_base _Iterator_base;
326 _Node* _M_create_node(const value_type& __x) {
327 _Node* __node = this->_M_get_node();
329 _Construct(&__node->_M_data, __x);
334 this->_M_put_node(__node);
335 __throw_exception_again;
340 _Node* _M_create_node() {
341 _Node* __node = this->_M_get_node();
343 _Construct(&__node->_M_data);
348 this->_M_put_node(__node);
349 __throw_exception_again;
355 explicit slist(const allocator_type& __a = allocator_type()) : _Base(__a) {}
357 slist(size_type __n, const value_type& __x,
358 const allocator_type& __a = allocator_type()) : _Base(__a)
359 { _M_insert_after_fill(&this->_M_head, __n, __x); }
361 explicit slist(size_type __n) : _Base(allocator_type())
362 { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
364 // We don't need any dispatching tricks here, because _M_insert_after_range
365 // already does them.
366 template <class _InputIterator>
367 slist(_InputIterator __first, _InputIterator __last,
368 const allocator_type& __a = allocator_type()) : _Base(__a)
369 { _M_insert_after_range(&this->_M_head, __first, __last); }
371 slist(const slist& __x) : _Base(__x.get_allocator())
372 { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
374 slist& operator= (const slist& __x);
379 // assign(), a generalized assignment member function. Two
380 // versions: one that takes a count, and one that takes a range.
381 // The range version is a member template, so we dispatch on whether
382 // or not the type is an integer.
384 void assign(size_type __n, const _Tp& __val)
385 { _M_fill_assign(__n, __val); }
387 void _M_fill_assign(size_type __n, const _Tp& __val);
389 template <class _InputIterator>
390 void assign(_InputIterator __first, _InputIterator __last) {
391 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
392 _M_assign_dispatch(__first, __last, _Integral());
395 template <class _Integer>
396 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
397 { _M_fill_assign((size_type) __n, (_Tp) __val); }
399 template <class _InputIterator>
400 void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
405 iterator begin() { return iterator((_Node*)this->_M_head._M_next); }
406 const_iterator begin() const
407 { return const_iterator((_Node*)this->_M_head._M_next);}
409 iterator end() { return iterator(0); }
410 const_iterator end() const { return const_iterator(0); }
412 // Experimental new feature: before_begin() returns a
413 // non-dereferenceable iterator that, when incremented, yields
414 // begin(). This iterator may be used as the argument to
415 // insert_after, erase_after, etc. Note that even for an empty
416 // slist, before_begin() is not the same iterator as end(). It
417 // is always necessary to increment before_begin() at least once to
419 iterator before_begin() { return iterator((_Node*) &this->_M_head); }
420 const_iterator before_begin() const
421 { return const_iterator((_Node*) &this->_M_head); }
423 size_type size() const { return __slist_size(this->_M_head._M_next); }
425 size_type max_size() const { return size_type(-1); }
427 bool empty() const { return this->_M_head._M_next == 0; }
429 void swap(slist& __x)
430 { std::swap(this->_M_head._M_next, __x._M_head._M_next); }
434 reference front() { return ((_Node*) this->_M_head._M_next)->_M_data; }
435 const_reference front() const
436 { return ((_Node*) this->_M_head._M_next)->_M_data; }
437 void push_front(const value_type& __x) {
438 __slist_make_link(&this->_M_head, _M_create_node(__x));
440 void push_front() { __slist_make_link(&this->_M_head, _M_create_node()); }
442 _Node* __node = (_Node*) this->_M_head._M_next;
443 this->_M_head._M_next = __node->_M_next;
444 _Destroy(&__node->_M_data);
445 this->_M_put_node(__node);
448 iterator previous(const_iterator __pos) {
449 return iterator((_Node*) __slist_previous(&this->_M_head, __pos._M_node));
451 const_iterator previous(const_iterator __pos) const {
452 return const_iterator((_Node*) __slist_previous(&this->_M_head,
457 _Node* _M_insert_after(_Node_base* __pos, const value_type& __x) {
458 return (_Node*) (__slist_make_link(__pos, _M_create_node(__x)));
461 _Node* _M_insert_after(_Node_base* __pos) {
462 return (_Node*) (__slist_make_link(__pos, _M_create_node()));
465 void _M_insert_after_fill(_Node_base* __pos,
466 size_type __n, const value_type& __x) {
467 for (size_type __i = 0; __i < __n; ++__i)
468 __pos = __slist_make_link(__pos, _M_create_node(__x));
471 // Check whether it's an integral type. If so, it's not an iterator.
472 template <class _InIter>
473 void _M_insert_after_range(_Node_base* __pos,
474 _InIter __first, _InIter __last) {
475 typedef typename _Is_integer<_InIter>::_Integral _Integral;
476 _M_insert_after_range(__pos, __first, __last, _Integral());
479 template <class _Integer>
480 void _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
482 _M_insert_after_fill(__pos, __n, __x);
485 template <class _InIter>
486 void _M_insert_after_range(_Node_base* __pos,
487 _InIter __first, _InIter __last,
489 while (__first != __last) {
490 __pos = __slist_make_link(__pos, _M_create_node(*__first));
497 iterator insert_after(iterator __pos, const value_type& __x) {
498 return iterator(_M_insert_after(__pos._M_node, __x));
501 iterator insert_after(iterator __pos) {
502 return insert_after(__pos, value_type());
505 void insert_after(iterator __pos, size_type __n, const value_type& __x) {
506 _M_insert_after_fill(__pos._M_node, __n, __x);
509 // We don't need any dispatching tricks here, because _M_insert_after_range
510 // already does them.
511 template <class _InIter>
512 void insert_after(iterator __pos, _InIter __first, _InIter __last) {
513 _M_insert_after_range(__pos._M_node, __first, __last);
516 iterator insert(iterator __pos, const value_type& __x) {
517 return iterator(_M_insert_after(__slist_previous(&this->_M_head,
522 iterator insert(iterator __pos) {
523 return iterator(_M_insert_after(__slist_previous(&this->_M_head,
528 void insert(iterator __pos, size_type __n, const value_type& __x) {
529 _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
533 // We don't need any dispatching tricks here, because _M_insert_after_range
534 // already does them.
535 template <class _InIter>
536 void insert(iterator __pos, _InIter __first, _InIter __last) {
537 _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node),
542 iterator erase_after(iterator __pos) {
543 return iterator((_Node*) this->_M_erase_after(__pos._M_node));
545 iterator erase_after(iterator __before_first, iterator __last) {
546 return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
550 iterator erase(iterator __pos) {
551 return (_Node*) this->_M_erase_after(__slist_previous(&this->_M_head,
554 iterator erase(iterator __first, iterator __last) {
555 return (_Node*) this->_M_erase_after(
556 __slist_previous(&this->_M_head, __first._M_node), __last._M_node);
559 void resize(size_type new_size, const _Tp& __x);
560 void resize(size_type new_size) { resize(new_size, _Tp()); }
561 void clear() { this->_M_erase_after(&this->_M_head, 0); }
564 // Moves the range [__before_first + 1, __before_last + 1) to *this,
565 // inserting it immediately after __pos. This is constant time.
566 void splice_after(iterator __pos,
567 iterator __before_first, iterator __before_last)
569 if (__before_first != __before_last)
570 __slist_splice_after(__pos._M_node, __before_first._M_node,
571 __before_last._M_node);
574 // Moves the element that follows __prev to *this, inserting it immediately
575 // after __pos. This is constant time.
576 void splice_after(iterator __pos, iterator __prev)
578 __slist_splice_after(__pos._M_node,
579 __prev._M_node, __prev._M_node->_M_next);
583 // Removes all of the elements from the list __x to *this, inserting
584 // them immediately after __pos. __x must not be *this. Complexity:
585 // linear in __x.size().
586 void splice_after(iterator __pos, slist& __x)
588 __slist_splice_after(__pos._M_node, &__x._M_head);
591 // Linear in distance(begin(), __pos), and linear in __x.size().
592 void splice(iterator __pos, slist& __x) {
593 if (__x._M_head._M_next)
594 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
595 &__x._M_head, __slist_previous(&__x._M_head, 0));
598 // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
599 void splice(iterator __pos, slist& __x, iterator __i) {
600 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
601 __slist_previous(&__x._M_head, __i._M_node),
605 // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
606 // and in distance(__first, __last).
607 void splice(iterator __pos, slist& __x, iterator __first, iterator __last)
609 if (__first != __last)
610 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
611 __slist_previous(&__x._M_head, __first._M_node),
612 __slist_previous(__first._M_node, __last._M_node));
617 if (this->_M_head._M_next)
618 this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
621 void remove(const _Tp& __val);
623 void merge(slist& __x);
626 template <class _Predicate>
627 void remove_if(_Predicate __pred);
629 template <class _BinaryPredicate>
630 void unique(_BinaryPredicate __pred);
632 template <class _StrictWeakOrdering>
633 void merge(slist&, _StrictWeakOrdering);
635 template <class _StrictWeakOrdering>
636 void sort(_StrictWeakOrdering __comp);
639 template <class _Tp, class _Alloc>
640 slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x)
643 _Node_base* __p1 = &this->_M_head;
644 _Node* __n1 = (_Node*) this->_M_head._M_next;
645 const _Node* __n2 = (const _Node*) __x._M_head._M_next;
646 while (__n1 && __n2) {
647 __n1->_M_data = __n2->_M_data;
649 __n1 = (_Node*) __n1->_M_next;
650 __n2 = (const _Node*) __n2->_M_next;
653 this->_M_erase_after(__p1, 0);
655 _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
661 template <class _Tp, class _Alloc>
662 void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
663 _Node_base* __prev = &this->_M_head;
664 _Node* __node = (_Node*) this->_M_head._M_next;
665 for ( ; __node != 0 && __n > 0 ; --__n) {
666 __node->_M_data = __val;
668 __node = (_Node*) __node->_M_next;
671 _M_insert_after_fill(__prev, __n, __val);
673 this->_M_erase_after(__prev, 0);
676 template <class _Tp, class _Alloc> template <class _InputIter>
678 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIter __first, _InputIter __last,
681 _Node_base* __prev = &this->_M_head;
682 _Node* __node = (_Node*) this->_M_head._M_next;
683 while (__node != 0 && __first != __last) {
684 __node->_M_data = *__first;
686 __node = (_Node*) __node->_M_next;
689 if (__first != __last)
690 _M_insert_after_range(__prev, __first, __last);
692 this->_M_erase_after(__prev, 0);
695 template <class _Tp, class _Alloc>
697 operator==(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
699 typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
700 const_iterator __end1 = _SL1.end();
701 const_iterator __end2 = _SL2.end();
703 const_iterator __i1 = _SL1.begin();
704 const_iterator __i2 = _SL2.begin();
705 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
709 return __i1 == __end1 && __i2 == __end2;
713 template <class _Tp, class _Alloc>
715 operator<(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
717 return std::lexicographical_compare(_SL1.begin(), _SL1.end(),
718 _SL2.begin(), _SL2.end());
721 template <class _Tp, class _Alloc>
723 operator!=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
724 return !(_SL1 == _SL2);
727 template <class _Tp, class _Alloc>
729 operator>(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
733 template <class _Tp, class _Alloc>
735 operator<=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
736 return !(_SL2 < _SL1);
739 template <class _Tp, class _Alloc>
741 operator>=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
742 return !(_SL1 < _SL2);
745 template <class _Tp, class _Alloc>
746 inline void swap(slist<_Tp,_Alloc>& __x, slist<_Tp,_Alloc>& __y) {
751 template <class _Tp, class _Alloc>
752 void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x)
754 _Node_base* __cur = &this->_M_head;
755 while (__cur->_M_next != 0 && __len > 0) {
757 __cur = __cur->_M_next;
760 this->_M_erase_after(__cur, 0);
762 _M_insert_after_fill(__cur, __len, __x);
765 template <class _Tp, class _Alloc>
766 void slist<_Tp,_Alloc>::remove(const _Tp& __val)
768 _Node_base* __cur = &this->_M_head;
769 while (__cur && __cur->_M_next) {
770 if (((_Node*) __cur->_M_next)->_M_data == __val)
771 this->_M_erase_after(__cur);
773 __cur = __cur->_M_next;
777 template <class _Tp, class _Alloc>
778 void slist<_Tp,_Alloc>::unique()
780 _Node_base* __cur = this->_M_head._M_next;
782 while (__cur->_M_next) {
783 if (((_Node*)__cur)->_M_data ==
784 ((_Node*)(__cur->_M_next))->_M_data)
785 this->_M_erase_after(__cur);
787 __cur = __cur->_M_next;
792 template <class _Tp, class _Alloc>
793 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x)
795 _Node_base* __n1 = &this->_M_head;
796 while (__n1->_M_next && __x._M_head._M_next) {
797 if (((_Node*) __x._M_head._M_next)->_M_data <
798 ((_Node*) __n1->_M_next)->_M_data)
799 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
800 __n1 = __n1->_M_next;
802 if (__x._M_head._M_next) {
803 __n1->_M_next = __x._M_head._M_next;
804 __x._M_head._M_next = 0;
808 template <class _Tp, class _Alloc>
809 void slist<_Tp,_Alloc>::sort()
811 if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
816 __slist_splice_after(&__carry._M_head,
817 &this->_M_head, this->_M_head._M_next);
819 while (__i < __fill && !__counter[__i].empty()) {
820 __counter[__i].merge(__carry);
821 __carry.swap(__counter[__i]);
824 __carry.swap(__counter[__i]);
829 for (int __i = 1; __i < __fill; ++__i)
830 __counter[__i].merge(__counter[__i-1]);
831 this->swap(__counter[__fill-1]);
835 template <class _Tp, class _Alloc>
836 template <class _Predicate>
837 void slist<_Tp,_Alloc>::remove_if(_Predicate __pred)
839 _Node_base* __cur = &this->_M_head;
840 while (__cur->_M_next) {
841 if (__pred(((_Node*) __cur->_M_next)->_M_data))
842 this->_M_erase_after(__cur);
844 __cur = __cur->_M_next;
848 template <class _Tp, class _Alloc> template <class _BinaryPredicate>
849 void slist<_Tp,_Alloc>::unique(_BinaryPredicate __pred)
851 _Node* __cur = (_Node*) this->_M_head._M_next;
853 while (__cur->_M_next) {
854 if (__pred(((_Node*)__cur)->_M_data,
855 ((_Node*)(__cur->_M_next))->_M_data))
856 this->_M_erase_after(__cur);
858 __cur = (_Node*) __cur->_M_next;
863 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
864 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x,
865 _StrictWeakOrdering __comp)
867 _Node_base* __n1 = &this->_M_head;
868 while (__n1->_M_next && __x._M_head._M_next) {
869 if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
870 ((_Node*) __n1->_M_next)->_M_data))
871 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
872 __n1 = __n1->_M_next;
874 if (__x._M_head._M_next) {
875 __n1->_M_next = __x._M_head._M_next;
876 __x._M_head._M_next = 0;
880 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
881 void slist<_Tp,_Alloc>::sort(_StrictWeakOrdering __comp)
883 if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
888 __slist_splice_after(&__carry._M_head,
889 &this->_M_head, this->_M_head._M_next);
891 while (__i < __fill && !__counter[__i].empty()) {
892 __counter[__i].merge(__carry, __comp);
893 __carry.swap(__counter[__i]);
896 __carry.swap(__counter[__i]);
901 for (int __i = 1; __i < __fill; ++__i)
902 __counter[__i].merge(__counter[__i-1], __comp);
903 this->swap(__counter[__fill-1]);
907 } // namespace __gnu_cxx
911 // Specialization of insert_iterator so that insertions will be constant
912 // time rather than linear time.
914 template <class _Tp, class _Alloc>
915 class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> > {
917 typedef __gnu_cxx::slist<_Tp, _Alloc> _Container;
918 _Container* container;
919 typename _Container::iterator iter;
921 typedef _Container container_type;
922 typedef output_iterator_tag iterator_category;
923 typedef void value_type;
924 typedef void difference_type;
925 typedef void pointer;
926 typedef void reference;
928 insert_iterator(_Container& __x, typename _Container::iterator __i)
930 if (__i == __x.begin())
931 iter = __x.before_begin();
933 iter = __x.previous(__i);
936 insert_iterator<_Container>&
937 operator=(const typename _Container::value_type& __value) {
938 iter = container->insert_after(iter, __value);
941 insert_iterator<_Container>& operator*() { return *this; }
942 insert_iterator<_Container>& operator++() { return *this; }
943 insert_iterator<_Container>& operator++(int) { return *this; }
948 #endif /* __SGI_STL_INTERNAL_SLIST_H */