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#ifndef _STLP_CARRAY_H
27#  include <stl/_carray.h>
28#endif
29
30#ifndef _STLP_RANGE_ERRORS_H
31#  include <stl/_range_errors.h>
32#endif
33
34#if defined (_STLP_NESTED_TYPE_PARAM_BUG)
35#  define size_type size_t
36#endif
37
38_STLP_BEGIN_NAMESPACE
39
40_STLP_MOVE_TO_PRIV_NAMESPACE
41
42template <class _Tp, class _Alloc>
43_Slist_node_base*
44_Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
45                                        _Slist_node_base* __last_node) {
46  _Slist_node_base* __cur = __before_first->_M_next;
47  while (__cur != __last_node) {
48    _Node* __tmp = __STATIC_CAST(_Node*, __cur);
49    __cur = __cur->_M_next;
50    _STLP_STD::_Destroy(&__tmp->_M_data);
51    _M_head.deallocate(__tmp,1);
52  }
53  __before_first->_M_next = __last_node;
54  return __last_node;
55}
56
57#if defined (_STLP_USE_PTR_SPECIALIZATIONS)
58#  define slist _STLP_PTR_IMPL_NAME(slist)
59#elif defined (_STLP_DEBUG)
60#  define slist _STLP_NON_DBG_NAME(slist)
61#else
62_STLP_MOVE_TO_STD_NAMESPACE
63#endif
64
65/* When building STLport lib Digital Mars Compiler complains on the _M_data assignment
66 * problem which would be perfertly right if we were using it. Hiding it during build
67 * fix this issue.
68 */
69template <class _Tp, class _Alloc>
70slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x) {
71  if (&__x != this) {
72    _Node_base* __p1 = &this->_M_head._M_data;
73    _Node_base* __n1 = this->_M_head._M_data._M_next;
74    const _Node_base* __n2 = __x._M_head._M_data._M_next;
75    while (__n1 && __n2) {
76      __STATIC_CAST(_Node*, __n1)->_M_data = __STATIC_CAST(const _Node*, __n2)->_M_data;
77      __p1 = __n1;
78      __n1 = __n1->_M_next;
79      __n2 = __n2->_M_next;
80    }
81    if (__n2 == 0)
82      this->_M_erase_after(__p1, 0);
83    else
84      _M_insert_after_range(__p1, const_iterator(__CONST_CAST(_Node_base*, __n2)),
85                                  const_iterator(0));
86  }
87  return *this;
88}
89
90template <class _Tp, class _Alloc>
91void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
92  _Node_base* __prev = &this->_M_head._M_data;
93  _Node_base* __node = this->_M_head._M_data._M_next;
94  for ( ; __node != 0 && __n > 0 ; --__n) {
95    __STATIC_CAST(_Node*, __node)->_M_data = __val;
96    __prev = __node;
97    __node = __node->_M_next;
98  }
99  if (__n > 0)
100    _M_insert_after_fill(__prev, __n, __val);
101  else
102    this->_M_erase_after(__prev, 0);
103}
104
105template <class _Tp, class _Alloc>
106void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x) {
107  _Node_base* __cur = &this->_M_head._M_data;
108  while (__cur->_M_next != 0 && __len > 0) {
109    --__len;
110    __cur = __cur->_M_next;
111  }
112  if (__cur->_M_next)
113    this->_M_erase_after(__cur, 0);
114  else
115    _M_insert_after_fill(__cur, __len, __x);
116}
117
118template <class _Tp, class _Alloc>
119void slist<_Tp,_Alloc>::remove(const _Tp& __val) {
120  _Node_base* __cur = &this->_M_head._M_data;
121  while (__cur && __cur->_M_next) {
122    if (__STATIC_CAST(_Node*, __cur->_M_next)->_M_data == __val)
123      this->_M_erase_after(__cur);
124    else
125      __cur = __cur->_M_next;
126  }
127}
128
129#if !defined (slist)
130_STLP_MOVE_TO_PRIV_NAMESPACE
131#endif
132
133template <class _Tp, class _Alloc, class _BinaryPredicate>
134void _Slist_unique(slist<_Tp, _Alloc>& __that, _BinaryPredicate __pred) {
135  typedef _Slist_node<_Tp> _Node;
136  typename slist<_Tp, _Alloc>::iterator __ite(__that.begin());
137  if (__ite != __that.end()) {
138    while (__ite._M_node->_M_next) {
139      if (__pred(*__ite, __STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data))
140        __that.erase_after(__ite);
141      else
142        ++__ite;
143    }
144  }
145}
146
147template <class _Tp, class _Alloc, class _StrictWeakOrdering>
148void _Slist_merge(slist<_Tp, _Alloc>& __that, slist<_Tp, _Alloc>& __x,
149                  _StrictWeakOrdering __comp) {
150  typedef _Slist_node<_Tp> _Node;
151  if (__that.get_allocator() == __x.get_allocator()) {
152    typename slist<_Tp, _Alloc>::iterator __ite(__that.before_begin());
153    while (__ite._M_node->_M_next && !__x.empty()) {
154      if (__comp(__x.front(), __STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data)) {
155        _STLP_VERBOSE_ASSERT(!__comp(__STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data, __x.front()),
156                             _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
157        __that.splice_after(__ite, __x, __x.before_begin());
158      }
159      ++__ite;
160    }
161    if (!__x.empty()) {
162      __that.splice_after(__ite, __x);
163    }
164  }
165  else {
166    typename slist<_Tp, _Alloc>::iterator __i1(__that.before_begin()), __i2(__x.begin());
167    while (__i1._M_node->_M_next && __i2._M_node) {
168      if (__comp(__STATIC_CAST(_Node*, __i1._M_node->_M_next)->_M_data, *__i2)) {
169        _STLP_VERBOSE_ASSERT(!__comp(*__i2, __STATIC_CAST(_Node*, __i1._M_node->_M_next)->_M_data),
170                             _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
171        ++__i1;
172      }
173      else {
174        __i1 = __that.insert_after(__i1, *(__i2++));
175      }
176    }
177    __that.insert_after(__i1, __i2, __x.end());
178    __x.clear();
179  }
180}
181
182template <class _Tp, class _Alloc, class _StrictWeakOrdering>
183void _Slist_sort(slist<_Tp, _Alloc>& __that, _StrictWeakOrdering __comp) {
184  if (!__that.begin()._M_node || !__that.begin()._M_node->_M_next)
185    return;
186
187  slist<_Tp, _Alloc> __carry(__that.get_allocator());
188  const int NB = 64;
189  _STLP_PRIV _CArray<slist<_Tp, _Alloc>, NB> __counter(__carry);
190  int __fill = 0;
191  while (!__that.empty()) {
192    __carry.splice_after(__carry.before_begin(), __that, __that.before_begin());
193    int __i = 0;
194    while (__i < __fill && !__counter[__i].empty()) {
195      _STLP_PRIV _Slist_merge(__counter[__i], __carry, __comp);
196      __carry.swap(__counter[__i]);
197      ++__i;
198    }
199    __carry.swap(__counter[__i]);
200    if (__i == __fill) {
201      ++__fill;
202      if (__fill >= NB) {
203        //Looks like the slist has too many elements to be sorted with this algorithm:
204        __stl_throw_overflow_error("slist::sort");
205      }
206    }
207  }
208
209  for (int __i = 1; __i < __fill; ++__i)
210    _STLP_PRIV _Slist_merge(__counter[__i], __counter[__i - 1], __comp);
211  __that.swap(__counter[__fill-1]);
212}
213
214#if defined (slist)
215#  undef slist
216#endif
217
218_STLP_MOVE_TO_STD_NAMESPACE
219
220_STLP_END_NAMESPACE
221
222#if defined (_STLP_NESTED_TYPE_PARAM_BUG)
223#  undef size_type
224#endif
225
226#endif /*  _STLP_SLIST_C */
227
228// Local Variables:
229// mode:C++
230// End:
231