1 // List implementation -*- C++ -*-
2 
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20 
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29 
30 /*
31  *
32  * Copyright (c) 1994
33  * Hewlett-Packard Company
34  *
35  * Permission to use, copy, modify, distribute and sell this software
36  * and its documentation for any purpose is hereby granted without fee,
37  * provided that the above copyright notice appear in all copies and
38  * that both that copyright notice and this permission notice appear
39  * in supporting documentation.  Hewlett-Packard Company makes no
40  * representations about the suitability of this software for any
41  * purpose.  It is provided "as is" without express or implied warranty.
42  *
43  *
44  * Copyright (c) 1996,1997
45  * Silicon Graphics Computer Systems, Inc.
46  *
47  * Permission to use, copy, modify, distribute and sell this software
48  * and its documentation for any purpose is hereby granted without fee,
49  * provided that the above copyright notice appear in all copies and
50  * that both that copyright notice and this permission notice appear
51  * in supporting documentation.  Silicon Graphics makes no
52  * representations about the suitability of this software for any
53  * purpose.  It is provided "as is" without express or implied warranty.
54  */
55 
56 /** @file stl_list.h
57  *  This is an internal header file, included by other library headers.
58  *  You should not attempt to use it directly.
59  */
60 
61 #ifndef __GLIBCPP_INTERNAL_LIST_H
62 #define __GLIBCPP_INTERNAL_LIST_H
63 
64 #include <bits/concept_check.h>
65 
66 namespace std
67 {
68 
69   struct _List_node_base
70   {
71     _List_node_base* _M_next;
72     _List_node_base* _M_prev;
73   };
74 
75   template<typename _Tp>
76     struct _List_node : public _List_node_base
77     {
78       _Tp _M_data;
79     };
80 
81   struct _List_iterator_base
82   {
83     typedef size_t                     size_type;
84     typedef ptrdiff_t                  difference_type;
85     typedef bidirectional_iterator_tag iterator_category;
86 
87     _List_node_base* _M_node;
88 
89     _List_iterator_base(_List_node_base* __x)
90     : _M_node(__x)
91     { }
92 
93     _List_iterator_base()
94     { }
95 
96     void
97     _M_incr()
98     { _M_node = _M_node->_M_next; }
99 
100     void
101     _M_decr()
102     { _M_node = _M_node->_M_prev; }
103 
104     bool
105     operator==(const _List_iterator_base& __x) const
106     { return _M_node == __x._M_node; }
107 
108     bool
109     operator!=(const _List_iterator_base& __x) const
110     { return _M_node != __x._M_node; }
111   };
112 
113   template<typename _Tp, typename _Ref, typename _Ptr>
114     struct _List_iterator : public _List_iterator_base
115     {
116       typedef _List_iterator<_Tp,_Tp&,_Tp*>             iterator;
117       typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
118       typedef _List_iterator<_Tp,_Ref,_Ptr>             _Self;
119 
120       typedef _Tp value_type;
121       typedef _Ptr pointer;
122       typedef _Ref reference;
123       typedef _List_node<_Tp> _Node;
124 
125       _List_iterator(_Node* __x)
126       : _List_iterator_base(__x)
127       { }
128 
129       _List_iterator()
130       { }
131 
132       _List_iterator(const iterator& __x)
133       : _List_iterator_base(__x._M_node)
134       { }
135 
136       reference
137       operator*() const
138       { return ((_Node*) _M_node)->_M_data; }
139 
140       pointer
141       operator->() const
142       { return &(operator*()); }
143 
144       _Self&
145       operator++()
146       {
147         this->_M_incr();
148         return *this;
149       }
150 
151       _Self
152       operator++(int)
153       {
154         _Self __tmp = *this;
155         this->_M_incr();
156         return __tmp;
157       }
158 
159       _Self&
160       operator--()
161       {
162         this->_M_decr();
163         return *this;
164       }
165 
166       _Self
167       operator--(int)
168       {
169         _Self __tmp = *this;
170         this->_M_decr();
171         return __tmp;
172       }
173     };
174 
175 
176   // Base class that encapsulates details of allocators.  Three cases:
177   // an ordinary standard-conforming allocator, a standard-conforming
178   // allocator with no non-static data, and an SGI-style allocator.
179   // This complexity is necessary only because we're worrying about backward
180   // compatibility and because we want to avoid wasting storage on an
181   // allocator instance if it isn't necessary.
182 
183 
184   // Base for general standard-conforming allocators.
185   template<typename _Tp, typename _Allocator, bool _IsStatic>
186     class _List_alloc_base
187     {
188     public:
189       typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
190               allocator_type;
191 
192       allocator_type
193       get_allocator() const
194       { return _Node_allocator; }
195 
196       _List_alloc_base(const allocator_type& __a)
197       : _Node_allocator(__a)
198       { }
199 
200     protected:
201       _List_node<_Tp>*
202       _M_get_node()
203       { return _Node_allocator.allocate(1); }
204 
205       void
206       _M_put_node(_List_node<_Tp>* __p)
207       { _Node_allocator.deallocate(__p, 1); }
208 
209     protected:
210       typename _Alloc_traits<_List_node<_Tp>, _Allocator>::allocator_type
211                _Node_allocator;
212 
213       _List_node<_Tp>* _M_node;
214     };
215 
216   // Specialization for instanceless allocators.
217 
218   template<typename _Tp, typename _Allocator>
219     class _List_alloc_base<_Tp, _Allocator, true>
220     {
221     public:
222       typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
223               allocator_type;
224 
225       allocator_type
226       get_allocator() const
227       { return allocator_type(); }
228 
229       _List_alloc_base(const allocator_type&)
230       { }
231 
232     protected:
233       typedef typename _Alloc_traits<_List_node<_Tp>, _Allocator>::_Alloc_type
234               _Alloc_type;
235 
236       _List_node<_Tp>*
237       _M_get_node()
238       { return _Alloc_type::allocate(1); }
239 
240       void
241       _M_put_node(_List_node<_Tp>* __p)
242       { _Alloc_type::deallocate(__p, 1); }
243 
244     protected:
245       _List_node<_Tp>* _M_node;
246     };
247 
248   template<typename _Tp, typename _Alloc>
249     class _List_base
250       : public _List_alloc_base<_Tp, _Alloc,
251                                 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
252     {
253     public:
254       typedef _List_alloc_base<_Tp, _Alloc,
255                                _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
256               _Base;
257       typedef typename _Base::allocator_type allocator_type;
258 
259       _List_base(const allocator_type& __a)
260       : _Base(__a)
261       {
262         _M_node = _M_get_node();
263         _M_node->_M_next = _M_node;
264         _M_node->_M_prev = _M_node;
265       }
266 
267       ~_List_base()
268       {
269         clear();
270         _M_put_node(_M_node);
271       }
272 
273       void clear();
274     };
275 
276   /**
277    *  @ingroup Containers
278    *  @ingroup Sequences
279    *
280    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
281    *  <a href="tables.html#66">reversible container</a>, and a
282    *  <a href="tables.html#67">sequence</a>, including the
283    *  <a href="tables.html#68">optional sequence requirements</a> with the
284    *  %exception of @c at and @c operator[].
285    *
286    *  @doctodo
287    *
288   */
289   template<typename _Tp, typename _Alloc = allocator<_Tp> >
290     class list : protected _List_base<_Tp, _Alloc>
291     {
292       // concept requirements
293       __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
294 
295       typedef _List_base<_Tp, _Alloc> _Base;
296     protected:
297       typedef void* _Void_pointer;
298 
299     public:
300       typedef _Tp value_type;
301       typedef value_type* pointer;
302       typedef const value_type* const_pointer;
303       typedef value_type& reference;
304       typedef const value_type& const_reference;
305       typedef _List_node<_Tp> _Node;
306       typedef size_t size_type;
307       typedef ptrdiff_t difference_type;
308 
309       typedef typename _Base::allocator_type allocator_type;
310 
311       typedef _List_iterator<_Tp,_Tp&,_Tp*>             iterator;
312       typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
313 
314       typedef reverse_iterator<const_iterator> const_reverse_iterator;
315       typedef reverse_iterator<iterator>       reverse_iterator;
316 
317     protected:
318       using _Base::_M_node;
319       using _Base::_M_put_node;
320       using _Base::_M_get_node;
321 
322     protected:
323       _Node*
324       _M_create_node(const _Tp& __x)
325       {
326         _Node* __p = _M_get_node();
327         try {
328           _Construct(&__p->_M_data, __x);
329         }
330         catch(...)
331         {
332           _M_put_node(__p);
333           __throw_exception_again;
334         }
335         return __p;
336       }
337 
338       _Node*
339       _M_create_node()
340       {
341         _Node* __p = _M_get_node();
342         try {
343           _Construct(&__p->_M_data);
344         }
345         catch(...)
346         {
347           _M_put_node(__p);
348           __throw_exception_again;
349         }
350         return __p;
351       }
352 
353     public:
354       allocator_type
355       get_allocator() const
356       { return _Base::get_allocator(); }
357 
358       explicit
359       list(const allocator_type& __a = allocator_type())
360       : _Base(__a)
361       { }
362 
363       iterator
364       begin()
365       { return static_cast<_Node*>(_M_node->_M_next); }
366 
367       const_iterator
368       begin() const
369       { return static_cast<_Node*>(_M_node->_M_next); }
370 
371       iterator
372       end()
373       { return _M_node; }
374 
375       const_iterator
376       end() const
377       { return _M_node; }
378 
379       reverse_iterator
380       rbegin()
381       { return reverse_iterator(end()); }
382 
383       const_reverse_iterator
384       rbegin() const
385       { return const_reverse_iterator(end()); }
386 
387       reverse_iterator
388       rend()
389       { return reverse_iterator(begin()); }
390 
391       const_reverse_iterator
392       rend() const
393       { return const_reverse_iterator(begin()); }
394 
395       bool
396       empty() const
397       { return _M_node->_M_next == _M_node; }
398 
399       size_type
400       size() const
401       { return distance(begin(), end()); }
402 
403       size_type
404       max_size() const
405       { return size_type(-1); }
406 
407       reference
408       front()
409       { return *begin(); }
410 
411       const_reference
412       front() const
413       { return *begin(); }
414 
415       reference
416       back()
417       { return *(--end()); }
418 
419       const_reference
420       back() const
421       { return *(--end()); }
422 
423       void
424       swap(list<_Tp, _Alloc>& __x)
425       { std::swap(_M_node, __x._M_node); }
426 
427       iterator
428       insert(iterator __position, const _Tp& __x)
429       {
430         _Node* __tmp = _M_create_node(__x);
431         __tmp->_M_next = __position._M_node;
432         __tmp->_M_prev = __position._M_node->_M_prev;
433         __position._M_node->_M_prev->_M_next = __tmp;
434         __position._M_node->_M_prev = __tmp;
435         return __tmp;
436       }
437 
438       iterator
439       insert(iterator __position)
440       { return insert(__position, _Tp()); }
441 
442       // Check whether it's an integral type.  If so, it's not an iterator.
443       template<typename _Integer>
444         void
445         _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, __true_type)
446         { _M_fill_insert(__pos, (size_type) __n, (_Tp) __x); }
447 
448       template<typename _InputIterator>
449         void
450         _M_insert_dispatch(iterator __pos,
451                            _InputIterator __first, _InputIterator __last,
452                            __false_type);
453 
454       template<typename _InputIterator>
455         void
456         insert(iterator __pos, _InputIterator __first, _InputIterator __last)
457         {
458           typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
459           _M_insert_dispatch(__pos, __first, __last, _Integral());
460         }
461 
462       void
463       insert(iterator __pos, size_type __n, const _Tp& __x)
464       { _M_fill_insert(__pos, __n, __x); }
465 
466       void
467       _M_fill_insert(iterator __pos, size_type __n, const _Tp& __x);
468 
469       void
470       push_front(const _Tp& __x)
471       { insert(begin(), __x); }
472 
473       void
474       push_front()
475       { insert(begin()); }
476 
477       void
478       push_back(const _Tp& __x)
479       { insert(end(), __x); }
480 
481       void
482       push_back()
483       { insert(end()); }
484 
485       iterator
486       erase(iterator __position)
487       {
488         _List_node_base* __next_node = __position._M_node->_M_next;
489         _List_node_base* __prev_node = __position._M_node->_M_prev;
490         _Node* __n = static_cast<_Node*>(__position._M_node);
491         __prev_node->_M_next = __next_node;
492         __next_node->_M_prev = __prev_node;
493         _Destroy(&__n->_M_data);
494         _M_put_node(__n);
495         return iterator(static_cast<_Node*>(__next_node));
496       }
497 
498       iterator
499       erase(iterator __first, iterator __last);
500 
501       void
502       clear()
503       { _Base::clear(); }
504 
505       void
506       resize(size_type __new_size, const _Tp& __x);
507 
508       void
509       resize(size_type __new_size)
510       { this->resize(__new_size, _Tp()); }
511 
512       void
513       pop_front()
514       { erase(begin()); }
515 
516       void
517       pop_back()
518       {
519         iterator __tmp = end();
520         erase(--__tmp);
521       }
522 
523       list(size_type __n, const _Tp& __value,
524            const allocator_type& __a = allocator_type())
525       : _Base(__a)
526       { insert(begin(), __n, __value); }
527 
528       explicit
529       list(size_type __n)
530       : _Base(allocator_type())
531       { insert(begin(), __n, _Tp()); }
532 
533       // We don't need any dispatching tricks here, because insert does all of
534       // that anyway.
535       template<typename _InputIterator>
536       list(_InputIterator __first, _InputIterator __last,
537            const allocator_type& __a = allocator_type())
538       : _Base(__a)
539       { insert(begin(), __first, __last); }
540 
541       list(const list<_Tp, _Alloc>& __x)
542       : _Base(__x.get_allocator())
543       { insert(begin(), __x.begin(), __x.end()); }
544 
545       ~list()
546       { }
547 
548       list<_Tp, _Alloc>&
549       operator=(const list<_Tp, _Alloc>& __x);
550 
551     public:
552       // assign(), a generalized assignment member function.  Two
553       // versions: one that takes a count, and one that takes a range.
554       // The range version is a member template, so we dispatch on whether
555       // or not the type is an integer.
556 
557       void
558       assign(size_type __n, const _Tp& __val)
559       { _M_fill_assign(__n, __val); }
560 
561       void
562       _M_fill_assign(size_type __n, const _Tp& __val);
563 
564       template<typename _InputIterator>
565         void
566         assign(_InputIterator __first, _InputIterator __last)
567         {
568           typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
569           _M_assign_dispatch(__first, __last, _Integral());
570         }
571 
572       template<typename _Integer>
573         void
574         _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
575         { _M_fill_assign((size_type) __n, (_Tp) __val); }
576 
577       template<typename _InputIterator>
578         void
579         _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
580                            __false_type);
581 
582     protected:
583       void
584       _M_transfer(iterator __position, iterator __first, iterator __last)
585       {
586         if (__position != __last) {
587           // Remove [first, last) from its old position.
588           __last._M_node->_M_prev->_M_next     = __position._M_node;
589           __first._M_node->_M_prev->_M_next    = __last._M_node;
590           __position._M_node->_M_prev->_M_next = __first._M_node;
591 
592           // Splice [first, last) into its new position.
593           _List_node_base* __tmp      = __position._M_node->_M_prev;
594           __position._M_node->_M_prev = __last._M_node->_M_prev;
595           __last._M_node->_M_prev     = __first._M_node->_M_prev;
596           __first._M_node->_M_prev    = __tmp;
597         }
598       }
599 
600     public:
601       void
602       splice(iterator __position, list& __x)
603       {
604         if (!__x.empty())
605           this->_M_transfer(__position, __x.begin(), __x.end());
606       }
607 
608       void
609       splice(iterator __position, list&, iterator __i)
610       {
611         iterator __j = __i;
612         ++__j;
613         if (__position == __i || __position == __j) return;
614         this->_M_transfer(__position, __i, __j);
615       }
616 
617       void
618       splice(iterator __position, list&, iterator __first, iterator __last)
619       {
620         if (__first != __last)
621           this->_M_transfer(__position, __first, __last);
622       }
623 
624       void
625       remove(const _Tp& __value);
626 
627       void
628       unique();
629 
630       void
631       merge(list& __x);
632 
633       void
634       reverse();
635 
636       void
637       sort();
638 
639       template<typename _Predicate>
640         void
641         remove_if(_Predicate);
642 
643       template<typename _BinaryPredicate>
644         void
645         unique(_BinaryPredicate);
646 
647       template<typename _StrictWeakOrdering>
648         void
649         merge(list&, _StrictWeakOrdering);
650 
651       template<typename _StrictWeakOrdering>
652         void
653         sort(_StrictWeakOrdering);
654     };
655 
656   template<typename _Tp, typename _Alloc>
657     inline bool
658     operator==(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
659     {
660       typedef typename list<_Tp,_Alloc>::const_iterator const_iterator;
661       const_iterator __end1 = __x.end();
662       const_iterator __end2 = __y.end();
663 
664       const_iterator __i1 = __x.begin();
665       const_iterator __i2 = __y.begin();
666       while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
667         ++__i1;
668         ++__i2;
669       }
670       return __i1 == __end1 && __i2 == __end2;
671     }
672 
673   template<typename _Tp, typename _Alloc>
674     inline bool
675     operator<(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
676     {
677       return lexicographical_compare(__x.begin(), __x.end(),
678                                      __y.begin(), __y.end());
679     }
680 
681   template<typename _Tp, typename _Alloc>
682     inline bool
683     operator!=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
684     { return !(__x == __y); }
685 
686   template<typename _Tp, typename _Alloc>
687     inline bool
688     operator>(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
689     { return __y < __x; }
690 
691   template<typename _Tp, typename _Alloc>
692     inline bool
693     operator<=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
694     { return !(__y < __x); }
695 
696   template<typename _Tp, typename _Alloc>
697     inline bool
698     operator>=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
699     { return !(__x < __y); }
700 
701   template<typename _Tp, typename _Alloc>
702     inline void
703     swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
704     { __x.swap(__y); }
705 
706   // move these to stl_list.tcc
707 
708   template<typename _Tp, typename _Alloc>
709     void _List_base<_Tp,_Alloc>::
710     clear()
711     {
712       _List_node<_Tp>* __cur = static_cast<_List_node<_Tp>*>(_M_node->_M_next);
713       while (__cur != _M_node) {
714         _List_node<_Tp>* __tmp = __cur;
715         __cur = static_cast<_List_node<_Tp>*>(__cur->_M_next);
716         _Destroy(&__tmp->_M_data);
717         _M_put_node(__tmp);
718       }
719       _M_node->_M_next = _M_node;
720       _M_node->_M_prev = _M_node;
721     }
722 
723   template<typename _Tp, typename _Alloc>
724     template <typename _InputIter>
725       void list<_Tp, _Alloc>::
726       _M_insert_dispatch(iterator __position, _InputIter __first, _InputIter __last,
727                                             __false_type)
728       {
729         for ( ; __first != __last; ++__first)
730           insert(__position, *__first);
731 
732       }
733 
734   template<typename _Tp, typename _Alloc>
735     void list<_Tp, _Alloc>::
736     _M_fill_insert(iterator __position, size_type __n, const _Tp& __x)
737     {
738       for ( ; __n > 0; --__n)
739         insert(__position, __x);
740     }
741 
742   template<typename _Tp, typename _Alloc>
743     typename list<_Tp,_Alloc>::iterator list<_Tp, _Alloc>::
744     erase(iterator __first, iterator __last)
745     {
746       while (__first != __last)
747         erase(__first++);
748       return __last;
749     }
750 
751   template<typename _Tp, typename _Alloc>
752     void list<_Tp, _Alloc>::
753     resize(size_type __new_size, const _Tp& __x)
754     {
755       iterator __i = begin();
756       size_type __len = 0;
757       for ( ; __i != end() && __len < __new_size; ++__i, ++__len)
758         ;
759       if (__len == __new_size)
760         erase(__i, end());
761       else                          // __i == end()
762         insert(end(), __new_size - __len, __x);
763     }
764 
765   template<typename _Tp, typename _Alloc>
766     list<_Tp, _Alloc>& list<_Tp, _Alloc>::
767     operator=(const list<_Tp, _Alloc>& __x)
768     {
769       if (this != &__x) {
770         iterator __first1 = begin();
771         iterator __last1 = end();
772         const_iterator __first2 = __x.begin();
773         const_iterator __last2 = __x.end();
774         while (__first1 != __last1 && __first2 != __last2)
775           *__first1++ = *__first2++;
776         if (__first2 == __last2)
777           erase(__first1, __last1);
778         else
779           insert(__last1, __first2, __last2);
780       }
781       return *this;
782     }
783 
784   template<typename _Tp, typename _Alloc>
785     void list<_Tp, _Alloc>::
786     _M_fill_assign(size_type __n, const _Tp& __val) {
787       iterator __i = begin();
788       for ( ; __i != end() && __n > 0; ++__i, --__n)
789         *__i = __val;
790       if (__n > 0)
791         insert(end(), __n, __val);
792       else
793         erase(__i, end());
794     }
795 
796   template<typename _Tp, typename _Alloc>
797     template <typename _InputIter>
798       void list<_Tp, _Alloc>::
799       _M_assign_dispatch(_InputIter __first2, _InputIter __last2, __false_type)
800       {
801         iterator __first1 = begin();
802         iterator __last1 = end();
803         for ( ; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
804           *__first1 = *__first2;
805         if (__first2 == __last2)
806           erase(__first1, __last1);
807         else
808           insert(__last1, __first2, __last2);
809       }
810 
811   template<typename _Tp, typename _Alloc>
812     void list<_Tp, _Alloc>::
813     remove(const _Tp& __value)
814     {
815       iterator __first = begin();
816       iterator __last = end();
817       while (__first != __last) {
818         iterator __next = __first;
819         ++__next;
820         if (*__first == __value) erase(__first);
821         __first = __next;
822       }
823     }
824 
825   template<typename _Tp, typename _Alloc>
826     void list<_Tp, _Alloc>::
827     unique()
828     {
829       iterator __first = begin();
830       iterator __last = end();
831       if (__first == __last) return;
832       iterator __next = __first;
833       while (++__next != __last) {
834         if (*__first == *__next)
835           erase(__next);
836         else
837           __first = __next;
838         __next = __first;
839       }
840     }
841 
842   template<typename _Tp, typename _Alloc>
843     void list<_Tp, _Alloc>::
844     merge(list<_Tp, _Alloc>& __x)
845     {
846       iterator __first1 = begin();
847       iterator __last1 = end();
848       iterator __first2 = __x.begin();
849       iterator __last2 = __x.end();
850       while (__first1 != __last1 && __first2 != __last2)
851         if (*__first2 < *__first1) {
852           iterator __next = __first2;
853           _M_transfer(__first1, __first2, ++__next);
854           __first2 = __next;
855         }
856         else
857           ++__first1;
858       if (__first2 != __last2) _M_transfer(__last1, __first2, __last2);
859     }
860 
861   inline void
862   __List_base_reverse(_List_node_base* __p)
863   {
864     _List_node_base* __tmp = __p;
865     do {
866       std::swap(__tmp->_M_next, __tmp->_M_prev);
867       __tmp = __tmp->_M_prev;     // Old next node is now prev.
868     } while (__tmp != __p);
869   }
870 
871   template<typename _Tp, typename _Alloc>
872   inline void list<_Tp, _Alloc>::
873   reverse()
874   { __List_base_reverse(this->_M_node); }
875 
876   template<typename _Tp, typename _Alloc>
877     void list<_Tp, _Alloc>::
878     sort()
879     {
880       // Do nothing if the list has length 0 or 1.
881       if (_M_node->_M_next != _M_node && _M_node->_M_next->_M_next != _M_node) {
882         list<_Tp, _Alloc> __carry;
883         list<_Tp, _Alloc> __counter[64];
884         int __fill = 0;
885         while (!empty()) {
886           __carry.splice(__carry.begin(), *this, begin());
887           int __i = 0;
888           while(__i < __fill && !__counter[__i].empty()) {
889             __counter[__i].merge(__carry);
890             __carry.swap(__counter[__i++]);
891           }
892           __carry.swap(__counter[__i]);
893           if (__i == __fill) ++__fill;
894         }
895 
896         for (int __i = 1; __i < __fill; ++__i)
897           __counter[__i].merge(__counter[__i-1]);
898         swap(__counter[__fill-1]);
899       }
900     }
901 
902   template<typename _Tp, typename _Alloc>
903     template <typename _Predicate>
904       void list<_Tp, _Alloc>::
905       remove_if(_Predicate __pred)
906       {
907         iterator __first = begin();
908         iterator __last = end();
909         while (__first != __last) {
910           iterator __next = __first;
911           ++__next;
912           if (__pred(*__first)) erase(__first);
913           __first = __next;
914         }
915       }
916 
917   template<typename _Tp, typename _Alloc>
918     template <typename _BinaryPredicate>
919       void list<_Tp, _Alloc>::
920       unique(_BinaryPredicate __binary_pred)
921       {
922         iterator __first = begin();
923         iterator __last = end();
924         if (__first == __last) return;
925         iterator __next = __first;
926         while (++__next != __last) {
927           if (__binary_pred(*__first, *__next))
928             erase(__next);
929           else
930             __first = __next;
931           __next = __first;
932         }
933       }
934 
935   template<typename _Tp, typename _Alloc>
936     template <typename _StrictWeakOrdering>
937       void list<_Tp, _Alloc>::
938       merge(list<_Tp, _Alloc>& __x, _StrictWeakOrdering __comp)
939       {
940         iterator __first1 = begin();
941         iterator __last1 = end();
942         iterator __first2 = __x.begin();
943         iterator __last2 = __x.end();
944         while (__first1 != __last1 && __first2 != __last2)
945           if (__comp(*__first2, *__first1)) {
946             iterator __next = __first2;
947             _M_transfer(__first1, __first2, ++__next);
948             __first2 = __next;
949           }
950           else
951             ++__first1;
952         if (__first2 != __last2) _M_transfer(__last1, __first2, __last2);
953       }
954 
955   template<typename _Tp, typename _Alloc>
956     template <typename _StrictWeakOrdering>
957     void list<_Tp, _Alloc>::
958     sort(_StrictWeakOrdering __comp)
959     {
960       // Do nothing if the list has length 0 or 1.
961       if (_M_node->_M_next != _M_node && _M_node->_M_next->_M_next != _M_node) {
962         list<_Tp, _Alloc> __carry;
963         list<_Tp, _Alloc> __counter[64];
964         int __fill = 0;
965         while (!empty()) {
966           __carry.splice(__carry.begin(), *this, begin());
967           int __i = 0;
968           while(__i < __fill && !__counter[__i].empty()) {
969             __counter[__i].merge(__carry, __comp);
970             __carry.swap(__counter[__i++]);
971           }
972           __carry.swap(__counter[__i]);
973           if (__i == __fill) ++__fill;
974         }
975 
976         for (int __i = 1; __i < __fill; ++__i)
977           __counter[__i].merge(__counter[__i-1], __comp);
978         swap(__counter[__fill-1]);
979       }
980     }
981 
982 } // namespace std
983 
984 #endif /* __GLIBCPP_INTERNAL_LIST_H */
985 
986 // vi:set ts=2 sw=2:
987 // Local Variables:
988 // mode:C++
989 // End:
990