ReactOS 0.4.15-dev-7942-gd23573b
_slist.c
Go to the documentation of this file.
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
39
41
42template <class _Tp, class _Alloc>
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
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>
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)),
86 }
87 return *this;
88}
89
90template <class _Tp, class _Alloc>
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>
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)
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>
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 }
182
183template <class _Tp, class _Alloc, class _StrictWeakOrdering>
184void _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
220
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:
_STLP_INLINE_LOOP _InputIter _Predicate __pred
Definition: _algo.h:68
return __n
Definition: _algo.h:75
void __fill(_ForwardIter __first, _ForwardIter __last, const _Tp &__val, const input_iterator_tag &, _Distance *)
Definition: _algobase.h:417
_STLP_INLINE_LOOP _InputIter const _Tp & __val
Definition: _algobase.h:656
#define _STLP_VERBOSE_ASSERT(expr, diagnostic)
Definition: _debug.h:439
#define _STLP_PRIV
Definition: _dm.h:70
_STLP_THROW_FUNCT_SPEC _STLP_CALL __stl_throw_overflow_error(const char *__msg)
Definition: _range_errors.c:78
void _Slist_merge(slist< _Tp, _Alloc > &__that, slist< _Tp, _Alloc > &__x, _StrictWeakOrdering __comp)
Definition: _slist.c:148
void _Slist_sort(slist< _Tp, _Alloc > &__that, _StrictWeakOrdering __comp)
Definition: _slist.c:184
_STLP_MOVE_TO_PRIV_NAMESPACE void _Slist_unique(slist< _Tp, _Alloc > &__that, _BinaryPredicate __pred)
Definition: _slist.c:134
_Slist_node_base * _M_erase_after(_Slist_node_base *__pos)
Definition: _slist.h:173
_Tp _M_data
Definition: _slist.h:61
Definition: _slist.h:57
_Self & operator=(const _Self &__x)
Definition: _slist.c:70
void swap(_Self &__x)
Definition: _slist.h:430
_STLP_PRIV _Slist_iterator< _Tp, _Nonconst_traits< _Tp > > iterator
Definition: _slist.h:239
void resize(size_type new_size, const value_type &__x=_Tp())
Definition: _slist.c:106
iterator end()
Definition: _slist.h:420
void remove(const _Tp &__val)
Definition: _slist.c:119
size_t size_type
Definition: _slist.h:235
void splice_after(iterator __pos, _Self &__x, iterator __before_first, iterator __before_last)
Definition: _slist.h:697
void clear()
Definition: _slist.h:691
void _M_fill_assign(size_type __n, const _Tp &__val)
Definition: _slist.c:91
iterator insert_after(iterator __pos, const value_type &__x=_Tp())
Definition: _slist.h:601
iterator before_begin()
Definition: _slist.h:412
_STLP_PRIV _Slist_node< _Tp > _Node
Definition: _slist.h:246
iterator erase_after(iterator __pos)
Definition: _slist.h:671
allocator_type get_allocator() const
Definition: _slist.h:277
reference front()
Definition: _slist.h:437
_STLP_PRIV _Slist_node_base _Node_base
Definition: _slist.h:247
_STLP_PRIV _Slist_iterator< _Tp, _Const_traits< _Tp > > const_iterator
Definition: _slist.h:240
bool empty() const
Definition: _slist.h:428
iterator begin()
Definition: _slist.h:416
static const WCHAR empty[]
Definition: main.c:47
#define _STLP_MOVE_TO_STD_NAMESPACE
Definition: features.h:525
#define __STATIC_CAST(__x, __y)
Definition: features.h:585
#define __CONST_CAST(__x, __y)
Definition: features.h:584
#define _STLP_BEGIN_NAMESPACE
Definition: features.h:501
#define _STLP_END_NAMESPACE
Definition: features.h:503
#define _STLP_MOVE_TO_PRIV_NAMESPACE
Definition: features.h:524
_Slist_node_base * _M_next
Definition: _slist_base.h:39