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
42 template <class _Tp, class _Alloc>
43 _Slist_node_base*
_M_erase_after(_Slist_node_base * __before_first,_Slist_node_base * __last_node)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 */
69 template <class _Tp, class _Alloc>
70 slist<_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
90 template <class _Tp, class _Alloc>
_M_fill_assign(size_type __n,const _Tp & __val)91 void 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
105 template <class _Tp, class _Alloc>
resize(size_type __len,const _Tp & __x)106 void 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
118 template <class _Tp, class _Alloc>
remove(const _Tp & __val)119 void 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
133 template <class _Tp, class _Alloc, class _BinaryPredicate>
_Slist_unique(slist<_Tp,_Alloc> & __that,_BinaryPredicate __pred)134 void _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
147 template <class _Tp, class _Alloc, class _StrictWeakOrdering>
_Slist_merge(slist<_Tp,_Alloc> & __that,slist<_Tp,_Alloc> & __x,_StrictWeakOrdering __comp)148 void _Slist_merge(slist<_Tp, _Alloc>& __that, slist<_Tp, _Alloc>& __x,
149 _StrictWeakOrdering __comp) {
150 typedef _Slist_node<_Tp> _Node;
151 typedef _STLP_PRIV _Slist_node_base _Node_base;
152 if (__that.get_allocator() == __x.get_allocator()) {
153 typename slist<_Tp, _Alloc>::iterator __ite(__that.before_begin());
154 while (__ite._M_node->_M_next && !__x.empty()) {
155 if (__comp(__x.front(), __STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data)) {
156 _STLP_VERBOSE_ASSERT(!__comp(__STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data, __x.front()),
157 _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
158 __that.splice_after(__ite, __x, __x.before_begin());
159 }
160 ++__ite;
161 }
162 if (!__x.empty()) {
163 __that.splice_after(__ite, __x);
164 }
165 }
166 else {
167 typename slist<_Tp, _Alloc>::iterator __i1(__that.before_begin()), __i2(__x.begin());
168 while (__i1._M_node->_M_next && __i2._M_node) {
169 if (__comp(__STATIC_CAST(_Node*, __i1._M_node->_M_next)->_M_data, *__i2)) {
170 _STLP_VERBOSE_ASSERT(!__comp(*__i2, __STATIC_CAST(_Node*, __i1._M_node->_M_next)->_M_data),
171 _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
172 ++__i1;
173 }
174 else {
175 __i1 = __that.insert_after(__i1, *(__i2++));
176 }
177 }
178 __that.insert_after(__i1, __i2, __x.end());
179 __x.clear();
180 }
181 }
182
183 template <class _Tp, class _Alloc, class _StrictWeakOrdering>
_Slist_sort(slist<_Tp,_Alloc> & __that,_StrictWeakOrdering __comp)184 void _Slist_sort(slist<_Tp, _Alloc>& __that, _StrictWeakOrdering __comp) {
185 if (!__that.begin()._M_node || !__that.begin()._M_node->_M_next)
186 return;
187
188 slist<_Tp, _Alloc> __carry(__that.get_allocator());
189 const int NB = 64;
190 _STLP_PRIV _CArray<slist<_Tp, _Alloc>, NB> __counter(__carry);
191 int __fill = 0;
192 while (!__that.empty()) {
193 __carry.splice_after(__carry.before_begin(), __that, __that.before_begin());
194 int __i = 0;
195 while (__i < __fill && !__counter[__i].empty()) {
196 _STLP_PRIV _Slist_merge(__counter[__i], __carry, __comp);
197 __carry.swap(__counter[__i]);
198 ++__i;
199 }
200 __carry.swap(__counter[__i]);
201 if (__i == __fill) {
202 ++__fill;
203 if (__fill >= NB) {
204 //Looks like the slist has too many elements to be sorted with this algorithm:
205 __stl_throw_overflow_error("slist::sort");
206 }
207 }
208 }
209
210 for (int __i = 1; __i < __fill; ++__i)
211 _STLP_PRIV _Slist_merge(__counter[__i], __counter[__i - 1], __comp);
212 __that.swap(__counter[__fill-1]);
213 }
214
215 #if defined (slist)
216 # undef slist
217 #endif
218
219 _STLP_MOVE_TO_STD_NAMESPACE
220
221 _STLP_END_NAMESPACE
222
223 #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
224 # undef size_type
225 #endif
226
227 #endif /* _STLP_SLIST_C */
228
229 // Local Variables:
230 // mode:C++
231 // End:
232