]> git.buserror.net Git - polintos/scott/priv.git/blob - include/c++/stl/stl/_list.c
Add STLport 5.1.4
[polintos/scott/priv.git] / include / c++ / stl / stl / _list.c
1 /*
2  *
3  *
4  * Copyright (c) 1994
5  * Hewlett-Packard Company
6  *
7  * Copyright (c) 1996,1997
8  * Silicon Graphics Computer Systems, Inc.
9  *
10  * Copyright (c) 1997
11  * Moscow Center for SPARC Technology
12  *
13  * Copyright (c) 1999
14  * Boris Fomitchev
15  *
16  * This material is provided "as is", with absolutely no warranty expressed
17  * or implied. Any use is at your own risk.
18  *
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.
24  *
25  */
26 #ifndef _STLP_LIST_C
27 #define _STLP_LIST_C
28
29 #ifndef _STLP_INTERNAL_LIST_H
30 #  include <stl/_list.h>
31 #endif
32
33 #ifndef _STLP_CARRAY_H
34 #  include <stl/_carray.h>
35 #endif
36
37 #ifndef _STLP_RANGE_ERRORS_H
38 #  include <stl/_range_errors.h>
39 #endif
40
41 _STLP_BEGIN_NAMESPACE
42
43 _STLP_MOVE_TO_PRIV_NAMESPACE
44
45 #if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
46 template <class _Dummy>
47 void _STLP_CALL
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;
55
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;
61   }
62 }
63 #endif /* _STLP_EXPOSE_GLOBALS_IMPLEMENTATION */
64
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)) {
69     _Node* __tmp = __cur;
70     __cur = __STATIC_CAST(_Node*, __cur->_M_next);
71     _STLP_STD::_Destroy(&__tmp->_M_data);
72     this->_M_node.deallocate(__tmp, 1);
73   }
74   _M_node._M_data._M_next = &_M_node._M_data;
75   _M_node._M_data._M_prev = &_M_node._M_data;
76 }
77
78 #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
79 #  define size_type size_t
80 #endif
81
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)
86 #else
87 _STLP_MOVE_TO_STD_NAMESPACE
88 #endif
89
90 template <class _Tp, class _Alloc>
91 void list<_Tp, _Alloc>::resize(size_type __new_size, const _Tp& __x) {
92   iterator __i = begin();
93   size_type __len = 0;
94   for ( ; __i != end() && __len < __new_size; ++__i, ++__len);
95
96   if (__len == __new_size)
97     erase(__i, end());
98   else // __i == end()
99     insert(end(), __new_size - __len, __x);
100 }
101
102 template <class _Tp, class _Alloc>
103 list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(const list<_Tp, _Alloc>& __x) {
104   if (this != &__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);
113     else
114       insert(__last1, __first2, __last2);
115   }
116   return *this;
117 }
118
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)
123     *__i = __val;
124   if (__n > 0)
125     insert(end(), __n, __val);
126   else
127     erase(__i, end());
128 }
129
130 #if !defined (list)
131 _STLP_MOVE_TO_PRIV_NAMESPACE
132 #endif
133
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;
141     ++__next;
142     if (__pred(*__first)) __that.erase(__first);
143     __first = __next;
144   }
145 }
146
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);
157     else
158       __first = __next;
159     __next = __first;
160   }
161 }
162
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);
177         __first2 = __next;
178       }
179       else
180         ++__first1;
181     }
182     if (__first2 != __last2)
183       _List_global_inst::_Transfer(__last1._M_node, __first2._M_node, __last2._M_node);
184   }
185   else {
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);
190       }
191       else
192         ++__first1;
193     }
194     if (__first2 != __last2) {
195       __that.insert(__first1, __first2, __last2);
196     }
197     __x.clear();
198   }
199 }
200
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)
206     return;
207
208   list<_Tp, _Alloc> __carry(__that.get_allocator());
209   const int NB = 64;
210   _STLP_PRIV _CArray<list<_Tp, _Alloc>, NB> __counter(__carry);
211   int __fill = 0;
212   while (!__that.empty()) {
213     __carry.splice(__carry.begin(), __that, __that.begin());
214     int __i = 0;
215     while (__i < __fill && !__counter[__i].empty()) {
216       _S_merge(__counter[__i], __carry, __comp);
217       __carry.swap(__counter[__i++]);
218     }
219     __carry.swap(__counter[__i]);
220     if (__i == __fill) {
221       ++__fill;
222       if (__fill >= NB) {
223         //Looks like the list has too many elements to be sorted with this algorithm:
224         __stl_throw_overflow_error("list::sort");
225       }
226     }
227   }
228
229   for (int __i = 1; __i < __fill; ++__i)
230     _S_merge(__counter[__i], __counter[__i - 1], __comp);
231   __that.swap(__counter[__fill - 1]);
232 }
233
234 #if defined (list)
235 #  undef list
236 #endif
237
238 _STLP_MOVE_TO_STD_NAMESPACE
239
240 _STLP_END_NAMESPACE
241
242 #endif /*  _STLP_LIST_C */
243
244 // Local Variables:
245 // mode:C++
246 // End: