slist

Go to the documentation of this file.
00001 // Singly-linked list implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /*
00031  * Copyright (c) 1997
00032  * Silicon Graphics Computer Systems, Inc.
00033  *
00034  * Permission to use, copy, modify, distribute and sell this software
00035  * and its documentation for any purpose is hereby granted without fee,
00036  * provided that the above copyright notice appear in all copies and
00037  * that both that copyright notice and this permission notice appear
00038  * in supporting documentation.  Silicon Graphics makes no
00039  * representations about the suitability of this software for any
00040  * purpose.  It is provided "as is" without express or implied warranty.
00041  *
00042  */
00043 
00050 #ifndef __SGI_STL_INTERNAL_SLIST_H
00051 #define __SGI_STL_INTERNAL_SLIST_H
00052 
00053 #include <bits/stl_algobase.h>
00054 #include <bits/stl_alloc.h>
00055 #include <bits/stl_construct.h>
00056 #include <bits/stl_uninitialized.h>
00057 #include <bits/concept_check.h>
00058 
00059 namespace __gnu_cxx
00060 { 
00061 using std::size_t;
00062 using std::ptrdiff_t;
00063 using std::_Alloc_traits;
00064 using std::_Construct;
00065 using std::_Destroy;
00066 using std::allocator;
00067 
00068 struct _Slist_node_base
00069 {
00070   _Slist_node_base* _M_next;
00071 };
00072 
00073 inline _Slist_node_base*
00074 __slist_make_link(_Slist_node_base* __prev_node,
00075                   _Slist_node_base* __new_node)
00076 {
00077   __new_node->_M_next = __prev_node->_M_next;
00078   __prev_node->_M_next = __new_node;
00079   return __new_node;
00080 }
00081 
00082 inline _Slist_node_base* 
00083 __slist_previous(_Slist_node_base* __head,
00084                  const _Slist_node_base* __node)
00085 {
00086   while (__head && __head->_M_next != __node)
00087     __head = __head->_M_next;
00088   return __head;
00089 }
00090 
00091 inline const _Slist_node_base* 
00092 __slist_previous(const _Slist_node_base* __head,
00093                  const _Slist_node_base* __node)
00094 {
00095   while (__head && __head->_M_next != __node)
00096     __head = __head->_M_next;
00097   return __head;
00098 }
00099 
00100 inline void __slist_splice_after(_Slist_node_base* __pos,
00101                                  _Slist_node_base* __before_first,
00102                                  _Slist_node_base* __before_last)
00103 {
00104   if (__pos != __before_first && __pos != __before_last) {
00105     _Slist_node_base* __first = __before_first->_M_next;
00106     _Slist_node_base* __after = __pos->_M_next;
00107     __before_first->_M_next = __before_last->_M_next;
00108     __pos->_M_next = __first;
00109     __before_last->_M_next = __after;
00110   }
00111 }
00112 
00113 inline void
00114 __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
00115 {
00116   _Slist_node_base* __before_last = __slist_previous(__head, 0);
00117   if (__before_last != __head) {
00118     _Slist_node_base* __after = __pos->_M_next;
00119     __pos->_M_next = __head->_M_next;
00120     __head->_M_next = 0;
00121     __before_last->_M_next = __after;
00122   }
00123 }
00124 
00125 inline _Slist_node_base* __slist_reverse(_Slist_node_base* __node)
00126 {
00127   _Slist_node_base* __result = __node;
00128   __node = __node->_M_next;
00129   __result->_M_next = 0;
00130   while(__node) {
00131     _Slist_node_base* __next = __node->_M_next;
00132     __node->_M_next = __result;
00133     __result = __node;
00134     __node = __next;
00135   }
00136   return __result;
00137 }
00138 
00139 inline size_t __slist_size(_Slist_node_base* __node)
00140 {
00141   size_t __result = 0;
00142   for ( ; __node != 0; __node = __node->_M_next)
00143     ++__result;
00144   return __result;
00145 }
00146 
00147 template <class _Tp>
00148 struct _Slist_node : public _Slist_node_base
00149 {
00150   _Tp _M_data;
00151 };
00152 
00153 struct _Slist_iterator_base
00154 {
00155   typedef size_t                    size_type;
00156   typedef ptrdiff_t                 difference_type;
00157   typedef std::forward_iterator_tag iterator_category;
00158 
00159   _Slist_node_base* _M_node;
00160 
00161   _Slist_iterator_base(_Slist_node_base* __x) : _M_node(__x) {}
00162   void _M_incr() { _M_node = _M_node->_M_next; }
00163 
00164   bool operator==(const _Slist_iterator_base& __x) const {
00165     return _M_node == __x._M_node;
00166   }
00167   bool operator!=(const _Slist_iterator_base& __x) const {
00168     return _M_node != __x._M_node;
00169   }
00170 };
00171 
00172 template <class _Tp, class _Ref, class _Ptr>
00173 struct _Slist_iterator : public _Slist_iterator_base
00174 {
00175   typedef _Slist_iterator<_Tp, _Tp&, _Tp*>             iterator;
00176   typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
00177   typedef _Slist_iterator<_Tp, _Ref, _Ptr>             _Self;
00178 
00179   typedef _Tp              value_type;
00180   typedef _Ptr             pointer;
00181   typedef _Ref             reference;
00182   typedef _Slist_node<_Tp> _Node;
00183 
00184   _Slist_iterator(_Node* __x) : _Slist_iterator_base(__x) {}
00185   _Slist_iterator() : _Slist_iterator_base(0) {}
00186   _Slist_iterator(const iterator& __x) : _Slist_iterator_base(__x._M_node) {}
00187 
00188   reference operator*() const { return ((_Node*) _M_node)->_M_data; }
00189   pointer operator->() const { return &(operator*()); }
00190 
00191   _Self& operator++()
00192   {
00193     _M_incr();
00194     return *this;
00195   }
00196   _Self operator++(int)
00197   {
00198     _Self __tmp = *this;
00199     _M_incr();
00200     return __tmp;
00201   }
00202 };
00203 
00204 
00205 // Base class that encapsulates details of allocators.  Three cases:
00206 // an ordinary standard-conforming allocator, a standard-conforming
00207 // allocator with no non-static data, and an SGI-style allocator.
00208 // This complexity is necessary only because we're worrying about backward
00209 // compatibility and because we want to avoid wasting storage on an 
00210 // allocator instance if it isn't necessary.
00211 
00212 // Base for general standard-conforming allocators.
00213 template <class _Tp, class _Allocator, bool _IsStatic>
00214 class _Slist_alloc_base {
00215 public:
00216   typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
00217           allocator_type;
00218   allocator_type get_allocator() const { return _M_node_allocator; }
00219 
00220   _Slist_alloc_base(const allocator_type& __a) : _M_node_allocator(__a) {}
00221 
00222 protected:
00223   _Slist_node<_Tp>* _M_get_node() 
00224     { return _M_node_allocator.allocate(1); }
00225   void _M_put_node(_Slist_node<_Tp>* __p) 
00226     { _M_node_allocator.deallocate(__p, 1); }
00227 
00228 protected:
00229   typename _Alloc_traits<_Slist_node<_Tp>,_Allocator>::allocator_type
00230            _M_node_allocator;
00231   _Slist_node_base _M_head;
00232 };
00233 
00234 // Specialization for instanceless allocators.
00235 template <class _Tp, class _Allocator>
00236 class _Slist_alloc_base<_Tp,_Allocator, true> {
00237 public:
00238   typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
00239           allocator_type;
00240   allocator_type get_allocator() const { return allocator_type(); }
00241 
00242   _Slist_alloc_base(const allocator_type&) {}
00243 
00244 protected:
00245   typedef typename _Alloc_traits<_Slist_node<_Tp>, _Allocator>::_Alloc_type
00246           _Alloc_type;
00247   _Slist_node<_Tp>* _M_get_node() { return _Alloc_type::allocate(1); }
00248   void _M_put_node(_Slist_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); }
00249 
00250 protected:
00251   _Slist_node_base _M_head;
00252 };
00253 
00254 
00255 template <class _Tp, class _Alloc>
00256 struct _Slist_base
00257   : public _Slist_alloc_base<_Tp, _Alloc,
00258                              _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
00259 {
00260   typedef _Slist_alloc_base<_Tp, _Alloc,
00261                             _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
00262           _Base;
00263   typedef typename _Base::allocator_type allocator_type;
00264 
00265   _Slist_base(const allocator_type& __a)
00266     : _Base(__a) { this->_M_head._M_next = 0; }
00267   ~_Slist_base() { _M_erase_after(&this->_M_head, 0); }
00268 
00269 protected:
00270 
00271   _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
00272   {
00273     _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
00274     _Slist_node_base* __next_next = __next->_M_next;
00275     __pos->_M_next = __next_next;
00276     _Destroy(&__next->_M_data);
00277     _M_put_node(__next);
00278     return __next_next;
00279   }
00280   _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
00281 };
00282 
00283 template <class _Tp, class _Alloc> 
00284 _Slist_node_base*
00285 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
00286                                         _Slist_node_base* __last_node) {
00287   _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
00288   while (__cur != __last_node) {
00289     _Slist_node<_Tp>* __tmp = __cur;
00290     __cur = (_Slist_node<_Tp>*) __cur->_M_next;
00291     _Destroy(&__tmp->_M_data);
00292     _M_put_node(__tmp);
00293   }
00294   __before_first->_M_next = __last_node;
00295   return __last_node;
00296 }
00297 
00298 template <class _Tp, class _Alloc = allocator<_Tp> >
00299 class slist : private _Slist_base<_Tp,_Alloc>
00300 {
00301   // concept requirements
00302   __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
00303 
00304 private:
00305   typedef _Slist_base<_Tp,_Alloc> _Base;
00306 public:
00307   typedef _Tp               value_type;
00308   typedef value_type*       pointer;
00309   typedef const value_type* const_pointer;
00310   typedef value_type&       reference;
00311   typedef const value_type& const_reference;
00312   typedef size_t            size_type;
00313   typedef ptrdiff_t         difference_type;
00314 
00315   typedef _Slist_iterator<_Tp, _Tp&, _Tp*>             iterator;
00316   typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
00317 
00318   typedef typename _Base::allocator_type allocator_type;
00319   allocator_type get_allocator() const { return _Base::get_allocator(); }
00320 
00321 private:
00322   typedef _Slist_node<_Tp>      _Node;
00323   typedef _Slist_node_base      _Node_base;
00324   typedef _Slist_iterator_base  _Iterator_base;
00325 
00326   _Node* _M_create_node(const value_type& __x) {
00327     _Node* __node = this->_M_get_node();
00328     try {
00329       _Construct(&__node->_M_data, __x);
00330       __node->_M_next = 0;
00331     }
00332     catch(...)
00333       {
00334     this->_M_put_node(__node);
00335     __throw_exception_again;
00336       }
00337     return __node;
00338   }
00339   
00340   _Node* _M_create_node() {
00341     _Node* __node = this->_M_get_node();
00342     try {
00343       _Construct(&__node->_M_data);
00344       __node->_M_next = 0;
00345     }
00346     catch(...)
00347       {
00348     this->_M_put_node(__node);
00349     __throw_exception_again;
00350       }
00351     return __node;
00352   }
00353 
00354 public:
00355   explicit slist(const allocator_type& __a = allocator_type()) : _Base(__a) {}
00356 
00357   slist(size_type __n, const value_type& __x,
00358         const allocator_type& __a =  allocator_type()) : _Base(__a)
00359     { _M_insert_after_fill(&this->_M_head, __n, __x); }
00360 
00361   explicit slist(size_type __n) : _Base(allocator_type())
00362     { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
00363 
00364   // We don't need any dispatching tricks here, because _M_insert_after_range
00365   // already does them.
00366   template <class _InputIterator>
00367   slist(_InputIterator __first, _InputIterator __last,
00368         const allocator_type& __a =  allocator_type()) : _Base(__a)
00369     { _M_insert_after_range(&this->_M_head, __first, __last); }
00370 
00371   slist(const slist& __x) : _Base(__x.get_allocator())
00372     { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
00373 
00374   slist& operator= (const slist& __x);
00375 
00376   ~slist() {}
00377 
00378 public:
00379   // assign(), a generalized assignment member function.  Two
00380   // versions: one that takes a count, and one that takes a range.
00381   // The range version is a member template, so we dispatch on whether
00382   // or not the type is an integer.
00383 
00384   void assign(size_type __n, const _Tp& __val)
00385     { _M_fill_assign(__n, __val); }
00386 
00387   void _M_fill_assign(size_type __n, const _Tp& __val);
00388 
00389   template <class _InputIterator>
00390   void assign(_InputIterator __first, _InputIterator __last) {
00391     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
00392     _M_assign_dispatch(__first, __last, _Integral());
00393   }
00394 
00395   template <class _Integer>
00396   void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
00397     { _M_fill_assign((size_type) __n, (_Tp) __val); }
00398 
00399   template <class _InputIterator>
00400   void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
00401                           __false_type);
00402 
00403 public:
00404 
00405   iterator begin() { return iterator((_Node*)this->_M_head._M_next); }
00406   const_iterator begin() const 
00407     { return const_iterator((_Node*)this->_M_head._M_next);}
00408 
00409   iterator end() { return iterator(0); }
00410   const_iterator end() const { return const_iterator(0); }
00411 
00412   // Experimental new feature: before_begin() returns a
00413   // non-dereferenceable iterator that, when incremented, yields
00414   // begin().  This iterator may be used as the argument to
00415   // insert_after, erase_after, etc.  Note that even for an empty 
00416   // slist, before_begin() is not the same iterator as end().  It 
00417   // is always necessary to increment before_begin() at least once to
00418   // obtain end().
00419   iterator before_begin() { return iterator((_Node*) &this->_M_head); }
00420   const_iterator before_begin() const
00421     { return const_iterator((_Node*) &this->_M_head); }
00422 
00423   size_type size() const { return __slist_size(this->_M_head._M_next); }
00424   
00425   size_type max_size() const { return size_type(-1); }
00426 
00427   bool empty() const { return this->_M_head._M_next == 0; }
00428 
00429   void swap(slist& __x)
00430     { std::swap(this->_M_head._M_next, __x._M_head._M_next); }
00431 
00432 public:
00433 
00434   reference front() { return ((_Node*) this->_M_head._M_next)->_M_data; }
00435   const_reference front() const 
00436     { return ((_Node*) this->_M_head._M_next)->_M_data; }
00437   void push_front(const value_type& __x)   {
00438     __slist_make_link(&this->_M_head, _M_create_node(__x));
00439   }
00440   void push_front() { __slist_make_link(&this->_M_head, _M_create_node()); }
00441   void pop_front() {
00442     _Node* __node = (_Node*) this->_M_head._M_next;
00443     this->_M_head._M_next = __node->_M_next;
00444     _Destroy(&__node->_M_data);
00445     this->_M_put_node(__node);
00446   }
00447 
00448   iterator previous(const_iterator __pos) {
00449     return iterator((_Node*) __slist_previous(&this->_M_head, __pos._M_node));
00450   }
00451   const_iterator previous(const_iterator __pos) const {
00452     return const_iterator((_Node*) __slist_previous(&this->_M_head,
00453                                                     __pos._M_node));
00454   }
00455 
00456 private:
00457   _Node* _M_insert_after(_Node_base* __pos, const value_type& __x) {
00458     return (_Node*) (__slist_make_link(__pos, _M_create_node(__x)));
00459   }
00460 
00461   _Node* _M_insert_after(_Node_base* __pos) {
00462     return (_Node*) (__slist_make_link(__pos, _M_create_node()));
00463   }
00464 
00465   void _M_insert_after_fill(_Node_base* __pos,
00466                             size_type __n, const value_type& __x) {
00467     for (size_type __i = 0; __i < __n; ++__i)
00468       __pos = __slist_make_link(__pos, _M_create_node(__x));
00469   }
00470 
00471   // Check whether it's an integral type.  If so, it's not an iterator.
00472   template <class _InIter>
00473   void _M_insert_after_range(_Node_base* __pos, 
00474                              _InIter __first, _InIter __last) {
00475     typedef typename _Is_integer<_InIter>::_Integral _Integral;
00476     _M_insert_after_range(__pos, __first, __last, _Integral());
00477   }
00478 
00479   template <class _Integer>
00480   void _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
00481                              __true_type) {
00482     _M_insert_after_fill(__pos, __n, __x);
00483   }
00484 
00485   template <class _InIter>
00486   void _M_insert_after_range(_Node_base* __pos,
00487                              _InIter __first, _InIter __last,
00488                              __false_type) {
00489     while (__first != __last) {
00490       __pos = __slist_make_link(__pos, _M_create_node(*__first));
00491       ++__first;
00492     }
00493   }
00494 
00495 public:
00496 
00497   iterator insert_after(iterator __pos, const value_type& __x) {
00498     return iterator(_M_insert_after(__pos._M_node, __x));
00499   }
00500 
00501   iterator insert_after(iterator __pos) {
00502     return insert_after(__pos, value_type());
00503   }
00504 
00505   void insert_after(iterator __pos, size_type __n, const value_type& __x) {
00506     _M_insert_after_fill(__pos._M_node, __n, __x);
00507   }
00508 
00509   // We don't need any dispatching tricks here, because _M_insert_after_range
00510   // already does them.
00511   template <class _InIter>
00512   void insert_after(iterator __pos, _InIter __first, _InIter __last) {
00513     _M_insert_after_range(__pos._M_node, __first, __last);
00514   }
00515 
00516   iterator insert(iterator __pos, const value_type& __x) {
00517     return iterator(_M_insert_after(__slist_previous(&this->_M_head,
00518                                                      __pos._M_node),
00519                     __x));
00520   }
00521 
00522   iterator insert(iterator __pos) {
00523     return iterator(_M_insert_after(__slist_previous(&this->_M_head,
00524                                                      __pos._M_node),
00525                                     value_type()));
00526   }
00527 
00528   void insert(iterator __pos, size_type __n, const value_type& __x) {
00529     _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
00530                          __n, __x);
00531   } 
00532     
00533   // We don't need any dispatching tricks here, because _M_insert_after_range
00534   // already does them.
00535   template <class _InIter>
00536   void insert(iterator __pos, _InIter __first, _InIter __last) {
00537     _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node), 
00538                           __first, __last);
00539   }
00540 
00541 public:
00542   iterator erase_after(iterator __pos) {
00543     return iterator((_Node*) this->_M_erase_after(__pos._M_node));
00544   }
00545   iterator erase_after(iterator __before_first, iterator __last) {
00546     return iterator((_Node*) this->_M_erase_after(__before_first._M_node, 
00547                                                   __last._M_node));
00548   } 
00549 
00550   iterator erase(iterator __pos) {
00551     return (_Node*) this->_M_erase_after(__slist_previous(&this->_M_head, 
00552                                                           __pos._M_node));
00553   }
00554   iterator erase(iterator __first, iterator __last) {
00555     return (_Node*) this->_M_erase_after(
00556       __slist_previous(&this->_M_head, __first._M_node), __last._M_node);
00557   }
00558 
00559   void resize(size_type new_size, const _Tp& __x);
00560   void resize(size_type new_size) { resize(new_size, _Tp()); }
00561   void clear() { this->_M_erase_after(&this->_M_head, 0); }
00562 
00563 public:
00564   // Moves the range [__before_first + 1, __before_last + 1) to *this,
00565   //  inserting it immediately after __pos.  This is constant time.
00566   void splice_after(iterator __pos, 
00567                     iterator __before_first, iterator __before_last)
00568   {
00569     if (__before_first != __before_last) 
00570       __slist_splice_after(__pos._M_node, __before_first._M_node, 
00571                            __before_last._M_node);
00572   }
00573 
00574   // Moves the element that follows __prev to *this, inserting it immediately
00575   //  after __pos.  This is constant time.
00576   void splice_after(iterator __pos, iterator __prev)
00577   {
00578     __slist_splice_after(__pos._M_node,
00579                          __prev._M_node, __prev._M_node->_M_next);
00580   }
00581 
00582 
00583   // Removes all of the elements from the list __x to *this, inserting
00584   // them immediately after __pos.  __x must not be *this.  Complexity:
00585   // linear in __x.size().
00586   void splice_after(iterator __pos, slist& __x)
00587   {
00588     __slist_splice_after(__pos._M_node, &__x._M_head);
00589   }
00590 
00591   // Linear in distance(begin(), __pos), and linear in __x.size().
00592   void splice(iterator __pos, slist& __x) {
00593     if (__x._M_head._M_next)
00594       __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
00595                            &__x._M_head, __slist_previous(&__x._M_head, 0));
00596   }
00597 
00598   // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
00599   void splice(iterator __pos, slist& __x, iterator __i) {
00600     __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
00601                          __slist_previous(&__x._M_head, __i._M_node),
00602                          __i._M_node);
00603   }
00604 
00605   // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
00606   // and in distance(__first, __last).
00607   void splice(iterator __pos, slist& __x, iterator __first, iterator __last)
00608   {
00609     if (__first != __last)
00610       __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
00611                            __slist_previous(&__x._M_head, __first._M_node),
00612                            __slist_previous(__first._M_node, __last._M_node));
00613   }
00614 
00615 public:
00616   void reverse() { 
00617     if (this->_M_head._M_next)
00618       this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
00619   }
00620 
00621   void remove(const _Tp& __val); 
00622   void unique(); 
00623   void merge(slist& __x);
00624   void sort();     
00625 
00626   template <class _Predicate> 
00627   void remove_if(_Predicate __pred);
00628 
00629   template <class _BinaryPredicate> 
00630   void unique(_BinaryPredicate __pred); 
00631 
00632   template <class _StrictWeakOrdering> 
00633   void merge(slist&, _StrictWeakOrdering);
00634 
00635   template <class _StrictWeakOrdering> 
00636   void sort(_StrictWeakOrdering __comp); 
00637 };
00638 
00639 template <class _Tp, class _Alloc>
00640 slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x)
00641 {
00642   if (&__x != this) {
00643     _Node_base* __p1 = &this->_M_head;
00644     _Node* __n1 = (_Node*) this->_M_head._M_next;
00645     const _Node* __n2 = (const _Node*) __x._M_head._M_next;
00646     while (__n1 && __n2) {
00647       __n1->_M_data = __n2->_M_data;
00648       __p1 = __n1;
00649       __n1 = (_Node*) __n1->_M_next;
00650       __n2 = (const _Node*) __n2->_M_next;
00651     }
00652     if (__n2 == 0)
00653       this->_M_erase_after(__p1, 0);
00654     else
00655       _M_insert_after_range(__p1, const_iterator((_Node*)__n2), 
00656                                   const_iterator(0));
00657   }
00658   return *this;
00659 }
00660 
00661 template <class _Tp, class _Alloc>
00662 void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
00663   _Node_base* __prev = &this->_M_head;
00664   _Node* __node = (_Node*) this->_M_head._M_next;
00665   for ( ; __node != 0 && __n > 0 ; --__n) {
00666     __node->_M_data = __val;
00667     __prev = __node;
00668     __node = (_Node*) __node->_M_next;
00669   }
00670   if (__n > 0)
00671     _M_insert_after_fill(__prev, __n, __val);
00672   else
00673     this->_M_erase_after(__prev, 0);
00674 }
00675 
00676 template <class _Tp, class _Alloc> template <class _InputIter>
00677 void
00678 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIter __first, _InputIter __last,
00679                                        __false_type)
00680 {
00681   _Node_base* __prev = &this->_M_head;
00682   _Node* __node = (_Node*) this->_M_head._M_next;
00683   while (__node != 0 && __first != __last) {
00684     __node->_M_data = *__first;
00685     __prev = __node;
00686     __node = (_Node*) __node->_M_next;
00687     ++__first;
00688   }
00689   if (__first != __last)
00690     _M_insert_after_range(__prev, __first, __last);
00691   else
00692     this->_M_erase_after(__prev, 0);
00693 }
00694 
00695 template <class _Tp, class _Alloc>
00696 inline bool 
00697 operator==(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
00698 {
00699   typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
00700   const_iterator __end1 = _SL1.end();
00701   const_iterator __end2 = _SL2.end();
00702 
00703   const_iterator __i1 = _SL1.begin();
00704   const_iterator __i2 = _SL2.begin();
00705   while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
00706     ++__i1;
00707     ++__i2;
00708   }
00709   return __i1 == __end1 && __i2 == __end2;
00710 }
00711 
00712 
00713 template <class _Tp, class _Alloc>
00714 inline bool
00715 operator<(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
00716 {
00717   return std::lexicographical_compare(_SL1.begin(), _SL1.end(), 
00718                       _SL2.begin(), _SL2.end());
00719 }
00720 
00721 template <class _Tp, class _Alloc>
00722 inline bool 
00723 operator!=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
00724   return !(_SL1 == _SL2);
00725 }
00726 
00727 template <class _Tp, class _Alloc>
00728 inline bool 
00729 operator>(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
00730   return _SL2 < _SL1;
00731 }
00732 
00733 template <class _Tp, class _Alloc>
00734 inline bool 
00735 operator<=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
00736   return !(_SL2 < _SL1);
00737 }
00738 
00739 template <class _Tp, class _Alloc>
00740 inline bool 
00741 operator>=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
00742   return !(_SL1 < _SL2);
00743 }
00744 
00745 template <class _Tp, class _Alloc>
00746 inline void swap(slist<_Tp,_Alloc>& __x, slist<_Tp,_Alloc>& __y) {
00747   __x.swap(__y);
00748 }
00749 
00750 
00751 template <class _Tp, class _Alloc>
00752 void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x)
00753 {
00754   _Node_base* __cur = &this->_M_head;
00755   while (__cur->_M_next != 0 && __len > 0) {
00756     --__len;
00757     __cur = __cur->_M_next;
00758   }
00759   if (__cur->_M_next) 
00760     this->_M_erase_after(__cur, 0);
00761   else
00762     _M_insert_after_fill(__cur, __len, __x);
00763 }
00764 
00765 template <class _Tp, class _Alloc>
00766 void slist<_Tp,_Alloc>::remove(const _Tp& __val)
00767 {
00768   _Node_base* __cur = &this->_M_head;
00769   while (__cur && __cur->_M_next) {
00770     if (((_Node*) __cur->_M_next)->_M_data == __val)
00771       this->_M_erase_after(__cur);
00772     else
00773       __cur = __cur->_M_next;
00774   }
00775 }
00776 
00777 template <class _Tp, class _Alloc> 
00778 void slist<_Tp,_Alloc>::unique()
00779 {
00780   _Node_base* __cur = this->_M_head._M_next;
00781   if (__cur) {
00782     while (__cur->_M_next) {
00783       if (((_Node*)__cur)->_M_data == 
00784           ((_Node*)(__cur->_M_next))->_M_data)
00785         this->_M_erase_after(__cur);
00786       else
00787         __cur = __cur->_M_next;
00788     }
00789   }
00790 }
00791 
00792 template <class _Tp, class _Alloc>
00793 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x)
00794 {
00795   _Node_base* __n1 = &this->_M_head;
00796   while (__n1->_M_next && __x._M_head._M_next) {
00797     if (((_Node*) __x._M_head._M_next)->_M_data < 
00798         ((_Node*)       __n1->_M_next)->_M_data) 
00799       __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
00800     __n1 = __n1->_M_next;
00801   }
00802   if (__x._M_head._M_next) {
00803     __n1->_M_next = __x._M_head._M_next;
00804     __x._M_head._M_next = 0;
00805   }
00806 }
00807 
00808 template <class _Tp, class _Alloc>
00809 void slist<_Tp,_Alloc>::sort()
00810 {
00811   if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
00812     slist __carry;
00813     slist __counter[64];
00814     int __fill = 0;
00815     while (!empty()) {
00816       __slist_splice_after(&__carry._M_head,
00817                            &this->_M_head, this->_M_head._M_next);
00818       int __i = 0;
00819       while (__i < __fill && !__counter[__i].empty()) {
00820         __counter[__i].merge(__carry);
00821         __carry.swap(__counter[__i]);
00822         ++__i;
00823       }
00824       __carry.swap(__counter[__i]);
00825       if (__i == __fill)
00826         ++__fill;
00827     }
00828 
00829     for (int __i = 1; __i < __fill; ++__i)
00830       __counter[__i].merge(__counter[__i-1]);
00831     this->swap(__counter[__fill-1]);
00832   }
00833 }
00834 
00835 template <class _Tp, class _Alloc> 
00836 template <class _Predicate>
00837 void slist<_Tp,_Alloc>::remove_if(_Predicate __pred)
00838 {
00839   _Node_base* __cur = &this->_M_head;
00840   while (__cur->_M_next) {
00841     if (__pred(((_Node*) __cur->_M_next)->_M_data))
00842       this->_M_erase_after(__cur);
00843     else
00844       __cur = __cur->_M_next;
00845   }
00846 }
00847 
00848 template <class _Tp, class _Alloc> template <class _BinaryPredicate> 
00849 void slist<_Tp,_Alloc>::unique(_BinaryPredicate __pred)
00850 {
00851   _Node* __cur = (_Node*) this->_M_head._M_next;
00852   if (__cur) {
00853     while (__cur->_M_next) {
00854       if (__pred(((_Node*)__cur)->_M_data, 
00855                  ((_Node*)(__cur->_M_next))->_M_data))
00856         this->_M_erase_after(__cur);
00857       else
00858         __cur = (_Node*) __cur->_M_next;
00859     }
00860   }
00861 }
00862 
00863 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
00864 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x,
00865                               _StrictWeakOrdering __comp)
00866 {
00867   _Node_base* __n1 = &this->_M_head;
00868   while (__n1->_M_next && __x._M_head._M_next) {
00869     if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
00870                ((_Node*)       __n1->_M_next)->_M_data))
00871       __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
00872     __n1 = __n1->_M_next;
00873   }
00874   if (__x._M_head._M_next) {
00875     __n1->_M_next = __x._M_head._M_next;
00876     __x._M_head._M_next = 0;
00877   }
00878 }
00879 
00880 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering> 
00881 void slist<_Tp,_Alloc>::sort(_StrictWeakOrdering __comp)
00882 {
00883   if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
00884     slist __carry;
00885     slist __counter[64];
00886     int __fill = 0;
00887     while (!empty()) {
00888       __slist_splice_after(&__carry._M_head,
00889                            &this->_M_head, this->_M_head._M_next);
00890       int __i = 0;
00891       while (__i < __fill && !__counter[__i].empty()) {
00892         __counter[__i].merge(__carry, __comp);
00893         __carry.swap(__counter[__i]);
00894         ++__i;
00895       }
00896       __carry.swap(__counter[__i]);
00897       if (__i == __fill)
00898         ++__fill;
00899     }
00900 
00901     for (int __i = 1; __i < __fill; ++__i)
00902       __counter[__i].merge(__counter[__i-1], __comp);
00903     this->swap(__counter[__fill-1]);
00904   }
00905 }
00906 
00907 } // namespace __gnu_cxx
00908 
00909 namespace std
00910 {
00911 // Specialization of insert_iterator so that insertions will be constant
00912 // time rather than linear time.
00913 
00914 template <class _Tp, class _Alloc>
00915 class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> > {
00916 protected:
00917   typedef __gnu_cxx::slist<_Tp, _Alloc> _Container;
00918   _Container* container;
00919   typename _Container::iterator iter;
00920 public:
00921   typedef _Container          container_type;
00922   typedef output_iterator_tag iterator_category;
00923   typedef void                value_type;
00924   typedef void                difference_type;
00925   typedef void                pointer;
00926   typedef void                reference;
00927 
00928   insert_iterator(_Container& __x, typename _Container::iterator __i) 
00929     : container(&__x) {
00930     if (__i == __x.begin())
00931       iter = __x.before_begin();
00932     else
00933       iter = __x.previous(__i);
00934   }
00935 
00936   insert_iterator<_Container>&
00937   operator=(const typename _Container::value_type& __value) { 
00938     iter = container->insert_after(iter, __value);
00939     return *this;
00940   }
00941   insert_iterator<_Container>& operator*() { return *this; }
00942   insert_iterator<_Container>& operator++() { return *this; }
00943   insert_iterator<_Container>& operator++(int) { return *this; }
00944 };
00945 
00946 } // namespace std
00947 
00948 #endif /* __SGI_STL_INTERNAL_SLIST_H */
00949 
00950 // Local Variables:
00951 // mode:C++
00952 // End:

Generated on Wed May 1 19:19:34 2002 for libstdc++-v3 Source by doxygen1.2.15