5 * Hewlett-Packard Company
7 * Copyright (c) 1996,1997
8 * Silicon Graphics Computer Systems, Inc.
11 * Moscow Center for SPARC Technology
16 * This material is provided "as is", with absolutely no warranty expressed
17 * or implied. Any use is at your own risk.
19 * Permission to use or copy this software for any purpose is hereby granted
20 * without fee, provided the above notices are retained on all copies.
21 * Permission to modify the code and to distribute modified code is granted,
22 * provided the above notices are retained, and a notice that the code was
23 * modified is included with the above copyright notice.
29 #ifndef _STLP_INTERNAL_LIST_H
30 # include <stl/_list.h>
33 #ifndef _STLP_CARRAY_H
34 # include <stl/_carray.h>
37 #ifndef _STLP_RANGE_ERRORS_H
38 # include <stl/_range_errors.h>
43 _STLP_MOVE_TO_PRIV_NAMESPACE
45 #if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
46 template <class _Dummy>
48 _List_global<_Dummy>::_Transfer(_List_node_base* __position,
49 _List_node_base* __first, _List_node_base* __last) {
50 if (__position != __last) {
51 // Remove [first, last) from its old position.
52 __last->_M_prev->_M_next = __position;
53 __first->_M_prev->_M_next = __last;
54 __position->_M_prev->_M_next = __first;
56 // Splice [first, last) into its new position.
57 _Node_base* __tmp = __position->_M_prev;
58 __position->_M_prev = __last->_M_prev;
59 __last->_M_prev = __first->_M_prev;
60 __first->_M_prev = __tmp;
63 #endif /* _STLP_EXPOSE_GLOBALS_IMPLEMENTATION */
65 template <class _Tp, class _Alloc>
66 void _List_base<_Tp,_Alloc>::clear() {
67 _Node* __cur = __STATIC_CAST(_Node*, _M_node._M_data._M_next);
68 while (__cur != &(_M_node._M_data)) {
70 __cur = __STATIC_CAST(_Node*, __cur->_M_next);
71 _STLP_STD::_Destroy(&__tmp->_M_data);
72 this->_M_node.deallocate(__tmp, 1);
74 _M_node._M_data._M_next = &_M_node._M_data;
75 _M_node._M_data._M_prev = &_M_node._M_data;
78 #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
79 # define size_type size_t
82 #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
83 # define list _STLP_PTR_IMPL_NAME(list)
84 #elif defined (_STLP_DEBUG)
85 # define list _STLP_NON_DBG_NAME(list)
87 _STLP_MOVE_TO_STD_NAMESPACE
90 template <class _Tp, class _Alloc>
91 void list<_Tp, _Alloc>::resize(size_type __new_size, const _Tp& __x) {
92 iterator __i = begin();
94 for ( ; __i != end() && __len < __new_size; ++__i, ++__len);
96 if (__len == __new_size)
99 insert(end(), __new_size - __len, __x);
102 template <class _Tp, class _Alloc>
103 list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(const list<_Tp, _Alloc>& __x) {
105 iterator __first1 = begin();
106 iterator __last1 = end();
107 const_iterator __first2 = __x.begin();
108 const_iterator __last2 = __x.end();
109 while (__first1 != __last1 && __first2 != __last2)
110 *__first1++ = *__first2++;
111 if (__first2 == __last2)
112 erase(__first1, __last1);
114 insert(__last1, __first2, __last2);
119 template <class _Tp, class _Alloc>
120 void list<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
121 iterator __i = begin();
122 for ( ; __i != end() && __n > 0; ++__i, --__n)
125 insert(end(), __n, __val);
131 _STLP_MOVE_TO_PRIV_NAMESPACE
134 template <class _Tp, class _Alloc, class _Predicate>
135 void _S_remove_if(list<_Tp, _Alloc>& __that, _Predicate __pred) {
136 typedef typename list<_Tp, _Alloc>::iterator _Literator;
137 _Literator __first = __that.begin();
138 _Literator __last = __that.end();
139 while (__first != __last) {
140 _Literator __next = __first;
142 if (__pred(*__first)) __that.erase(__first);
147 template <class _Tp, class _Alloc, class _BinaryPredicate>
148 void _S_unique(list<_Tp, _Alloc>& __that, _BinaryPredicate __binary_pred) {
149 typedef typename list<_Tp, _Alloc>::iterator _Literator;
150 _Literator __first = __that.begin();
151 _Literator __last = __that.end();
152 if (__first == __last) return;
153 _Literator __next = __first;
154 while (++__next != __last) {
155 if (__binary_pred(*__first, *__next))
156 __that.erase(__next);
163 template <class _Tp, class _Alloc, class _StrictWeakOrdering>
164 void _S_merge(list<_Tp, _Alloc>& __that, list<_Tp, _Alloc>& __x,
165 _StrictWeakOrdering __comp) {
166 typedef typename list<_Tp, _Alloc>::iterator _Literator;
167 _Literator __first1 = __that.begin();
168 _Literator __last1 = __that.end();
169 _Literator __first2 = __x.begin();
170 _Literator __last2 = __x.end();
171 if (__that.get_allocator() == __x.get_allocator()) {
172 while (__first1 != __last1 && __first2 != __last2) {
173 if (__comp(*__first2, *__first1)) {
174 _STLP_VERBOSE_ASSERT(!__comp(*__first1, *__first2), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
175 _Literator __next = __first2;
176 _List_global_inst::_Transfer(__first1._M_node, __first2._M_node, (++__next)._M_node);
182 if (__first2 != __last2)
183 _List_global_inst::_Transfer(__last1._M_node, __first2._M_node, __last2._M_node);
186 while (__first1 != __last1 && __first2 != __last2) {
187 if (__comp(*__first2, *__first1)) {
188 _STLP_VERBOSE_ASSERT(!__comp(*__first1, *__first2), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
189 __first1 = __that.insert(__first1, *__first2);
194 if (__first2 != __last2) {
195 __that.insert(__first1, __first2, __last2);
201 template <class _Tp, class _Alloc, class _StrictWeakOrdering>
202 void _S_sort(list<_Tp, _Alloc>& __that, _StrictWeakOrdering __comp) {
203 // Do nothing if the list has length 0 or 1.
204 if (__that._M_node._M_data._M_next == &__that._M_node._M_data ||
205 __that._M_node._M_data._M_next->_M_next == &__that._M_node._M_data)
208 list<_Tp, _Alloc> __carry(__that.get_allocator());
210 _STLP_PRIV _CArray<list<_Tp, _Alloc>, NB> __counter(__carry);
212 while (!__that.empty()) {
213 __carry.splice(__carry.begin(), __that, __that.begin());
215 while (__i < __fill && !__counter[__i].empty()) {
216 _S_merge(__counter[__i], __carry, __comp);
217 __carry.swap(__counter[__i++]);
219 __carry.swap(__counter[__i]);
223 //Looks like the list has too many elements to be sorted with this algorithm:
224 __stl_throw_overflow_error("list::sort");
229 for (int __i = 1; __i < __fill; ++__i)
230 _S_merge(__counter[__i], __counter[__i - 1], __comp);
231 __that.swap(__counter[__fill - 1]);
238 _STLP_MOVE_TO_STD_NAMESPACE
242 #endif /* _STLP_LIST_C */