home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _slist.c < prev    next >
C/C++ Source or Header  |  2002-02-02  |  5KB  |  180 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1996,1997
  4.  * Silicon Graphics Computer Systems, Inc.
  5.  *
  6.  * Copyright (c) 1999 
  7.  * Boris Fomitchev
  8.  *
  9.  * This material is provided "as is", with absolutely no warranty expressed
  10.  * or implied. Any use is at your own risk.
  11.  *
  12.  * Permission to use or copy this software for any purpose is hereby granted 
  13.  * without fee, provided the above notices are retained on all copies.
  14.  * Permission to modify the code and to distribute modified code is granted,
  15.  * provided the above notices are retained, and a notice that the code was
  16.  * modified is included with the above copyright notice.
  17.  *
  18.  */
  19. #ifndef _STLP_SLIST_C
  20. #define _STLP_SLIST_C
  21.  
  22. #ifndef _STLP_INTERNAL_SLIST_H
  23. # include <stl/_slist.h>
  24. #endif
  25.  
  26. # undef slist
  27. # define  slist  __WORKAROUND_DBG_RENAME(slist)
  28. # if defined (_STLP_NESTED_TYPE_PARAM_BUG) 
  29. #  define size_type          size_t
  30. # endif
  31.  
  32. _STLP_BEGIN_NAMESPACE
  33.  
  34. template <class _Tp, class _Alloc> 
  35. _Slist_node_base*
  36. _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
  37.                                         _Slist_node_base* __last_node) {
  38.   _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
  39.   while (__cur != __last_node) {
  40.     _Slist_node<_Tp>* __tmp = __cur;
  41.     __cur = (_Slist_node<_Tp>*) __cur->_M_next;
  42.     _Destroy(&__tmp->_M_data);
  43.     _M_head.deallocate(__tmp,1);
  44.   }
  45.   __before_first->_M_next = __last_node;
  46.   return __last_node;
  47. }
  48.  
  49. template <class _Tp, class _Alloc>
  50. slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x)
  51. {
  52.   if (&__x != this) {
  53.     _Node_base* __p1 = &this->_M_head._M_data;
  54.     _Node* __n1 = (_Node*) this->_M_head._M_data._M_next;
  55.     const _Node* __n2 = (const _Node*) __x._M_head._M_data._M_next;
  56.     while (__n1 && __n2) {
  57.       __n1->_M_data = __n2->_M_data;
  58.       __p1 = __n1;
  59.       __n1 = (_Node*) __n1->_M_next;
  60.       __n2 = (const _Node*) __n2->_M_next;
  61.     }
  62.     if (__n2 == 0)
  63.       this->_M_erase_after(__p1, 0);
  64.     else
  65.       _M_insert_after_range(__p1, const_iterator((_Node*)__n2), 
  66.                                   const_iterator(0));
  67.   }
  68.   return *this;
  69. }
  70.  
  71. template <class _Tp, class _Alloc>
  72. void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
  73.   _Node_base* __prev = &this->_M_head._M_data;
  74.   _Node* __node = (_Node*) this->_M_head._M_data._M_next;
  75.   for ( ; __node != 0 && __n > 0 ; --__n) {
  76.     __node->_M_data = __val;
  77.     __prev = __node;
  78.     __node = (_Node*) __node->_M_next;
  79.   }
  80.   if (__n > 0)
  81.     _M_insert_after_fill(__prev, __n, __val);
  82.   else
  83.     this->_M_erase_after(__prev, 0);
  84. }
  85.  
  86.  
  87. template <class _Tp, class _Alloc>
  88. void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x)
  89. {
  90.   _Node_base* __cur = &this->_M_head._M_data;
  91.   while (__cur->_M_next != 0 && __len > 0) {
  92.     --__len;
  93.     __cur = __cur->_M_next;
  94.   }
  95.   if (__cur->_M_next) 
  96.     this->_M_erase_after(__cur, 0);
  97.   else
  98.     _M_insert_after_fill(__cur, __len, __x);
  99. }
  100.  
  101. template <class _Tp, class _Alloc>
  102. void slist<_Tp,_Alloc>::remove(const _Tp& __val)
  103. {
  104.   _Node_base* __cur = &this->_M_head._M_data;
  105.   while (__cur && __cur->_M_next) {
  106.     if (((_Node*) __cur->_M_next)->_M_data == __val)
  107.       this->_M_erase_after(__cur);
  108.     else
  109.       __cur = __cur->_M_next;
  110.   }
  111. }
  112.  
  113. template <class _Tp, class _Alloc> 
  114. void slist<_Tp,_Alloc>::unique()
  115. {
  116.   _Node_base* __cur = this->_M_head._M_data._M_next;
  117.   if (__cur) {
  118.     while (__cur->_M_next) {
  119.       if (((_Node*)__cur)->_M_data == 
  120.           ((_Node*)(__cur->_M_next))->_M_data)
  121.         this->_M_erase_after(__cur);
  122.       else
  123.         __cur = __cur->_M_next;
  124.     }
  125.   }
  126. }
  127.  
  128. template <class _Tp, class _Alloc>
  129. void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x)
  130. {
  131.   _Node_base* __n1 = &this->_M_head._M_data;
  132.   while (__n1->_M_next && __x._M_head._M_data._M_next) {
  133.     if (((_Node*) __x._M_head._M_data._M_next)->_M_data < 
  134.         ((_Node*)       __n1->_M_next)->_M_data) 
  135.       _Sl_global_inst::__splice_after(__n1, &__x._M_head._M_data, __x._M_head._M_data._M_next);
  136.     __n1 = __n1->_M_next;
  137.   }
  138.   if (__x._M_head._M_data._M_next) {
  139.     __n1->_M_next = __x._M_head._M_data._M_next;
  140.     __x._M_head._M_data._M_next = 0;
  141.   }
  142. }
  143.  
  144. template <class _Tp, class _Alloc>
  145. void slist<_Tp,_Alloc>::sort()
  146. {
  147.   if (this->_M_head._M_data._M_next && this->_M_head._M_data._M_next->_M_next) {
  148.     _Self __carry;
  149.     _Self __counter[64];
  150.     int __fill = 0;
  151.     while (!empty()) {
  152.       _Sl_global_inst::__splice_after(&__carry._M_head._M_data, &this->_M_head._M_data, this->_M_head._M_data._M_next);
  153.       int __i = 0;
  154.       while (__i < __fill && !__counter[__i].empty()) {
  155.         __counter[__i].merge(__carry);
  156.         __carry.swap(__counter[__i]);
  157.         ++__i;
  158.       }
  159.       __carry.swap(__counter[__i]);
  160.       if (__i == __fill)
  161.         ++__fill;
  162.     }
  163.  
  164.     for (int __i = 1; __i < __fill; ++__i)
  165.       __counter[__i].merge(__counter[__i-1]);
  166.     this->swap(__counter[__fill-1]);
  167.   }
  168. }
  169.  
  170. # undef slist
  171. # undef size_type
  172.  
  173. _STLP_END_NAMESPACE
  174.  
  175. #endif /*  _STLP_SLIST_C */
  176.  
  177. // Local Variables:
  178. // mode:C++
  179. // End:
  180.