williamr@4
|
1 |
/*
|
williamr@4
|
2 |
*
|
williamr@4
|
3 |
* Copyright (c) 1994
|
williamr@4
|
4 |
* Hewlett-Packard Company
|
williamr@4
|
5 |
*
|
williamr@4
|
6 |
* Copyright (c) 1996,1997
|
williamr@4
|
7 |
* Silicon Graphics Computer Systems, Inc.
|
williamr@4
|
8 |
*
|
williamr@4
|
9 |
* Copyright (c) 1997
|
williamr@4
|
10 |
* Moscow Center for SPARC Technology
|
williamr@4
|
11 |
*
|
williamr@4
|
12 |
* Copyright (c) 1999
|
williamr@4
|
13 |
* Boris Fomitchev
|
williamr@4
|
14 |
*
|
williamr@4
|
15 |
* This material is provided "as is", with absolutely no warranty expressed
|
williamr@4
|
16 |
* or implied. Any use is at your own risk.
|
williamr@4
|
17 |
*
|
williamr@4
|
18 |
* Permission to use or copy this software for any purpose is hereby granted
|
williamr@4
|
19 |
* without fee, provided the above notices are retained on all copies.
|
williamr@4
|
20 |
* Permission to modify the code and to distribute modified code is granted,
|
williamr@4
|
21 |
* provided the above notices are retained, and a notice that the code was
|
williamr@4
|
22 |
* modified is included with the above copyright notice.
|
williamr@4
|
23 |
*
|
williamr@4
|
24 |
*/
|
williamr@4
|
25 |
|
williamr@4
|
26 |
#ifndef _STLP_ALGO_C
|
williamr@4
|
27 |
# define _STLP_ALGO_C
|
williamr@4
|
28 |
|
williamr@4
|
29 |
# if !defined (_STLP_INTERNAL_ALGO_H)
|
williamr@4
|
30 |
# include <stl/_algo.h>
|
williamr@4
|
31 |
# endif
|
williamr@4
|
32 |
|
williamr@4
|
33 |
_STLP_BEGIN_NAMESPACE
|
williamr@4
|
34 |
|
williamr@4
|
35 |
template <class _BidirectionalIter, class _Distance, class _Compare>
|
williamr@4
|
36 |
void __merge_without_buffer(_BidirectionalIter __first,
|
williamr@4
|
37 |
_BidirectionalIter __middle,
|
williamr@4
|
38 |
_BidirectionalIter __last,
|
williamr@4
|
39 |
_Distance __len1, _Distance __len2,
|
williamr@4
|
40 |
_Compare __comp);
|
williamr@4
|
41 |
|
williamr@4
|
42 |
|
williamr@4
|
43 |
template <class _BidirectionalIter1, class _BidirectionalIter2,
|
williamr@4
|
44 |
class _BidirectionalIter3, class _Compare>
|
williamr@4
|
45 |
_BidirectionalIter3 __merge_backward(_BidirectionalIter1 __first1,
|
williamr@4
|
46 |
_BidirectionalIter1 __last1,
|
williamr@4
|
47 |
_BidirectionalIter2 __first2,
|
williamr@4
|
48 |
_BidirectionalIter2 __last2,
|
williamr@4
|
49 |
_BidirectionalIter3 __result,
|
williamr@4
|
50 |
_Compare __comp);
|
williamr@4
|
51 |
|
williamr@4
|
52 |
template <class _Tp>
|
williamr@4
|
53 |
# if !(defined (__SUNPRO_CC) && (__SUNPRO_CC < 0x420 ))
|
williamr@4
|
54 |
inline
|
williamr@4
|
55 |
# endif
|
williamr@4
|
56 |
const _Tp& __median(const _Tp& __a, const _Tp& __b, const _Tp& __c) {
|
williamr@4
|
57 |
if (__a < __b)
|
williamr@4
|
58 |
if (__b < __c)
|
williamr@4
|
59 |
return __b;
|
williamr@4
|
60 |
else if (__a < __c)
|
williamr@4
|
61 |
return __c;
|
williamr@4
|
62 |
else
|
williamr@4
|
63 |
return __a;
|
williamr@4
|
64 |
else if (__a < __c)
|
williamr@4
|
65 |
return __a;
|
williamr@4
|
66 |
else if (__b < __c)
|
williamr@4
|
67 |
return __c;
|
williamr@4
|
68 |
else
|
williamr@4
|
69 |
return __b;
|
williamr@4
|
70 |
}
|
williamr@4
|
71 |
|
williamr@4
|
72 |
template <class _Tp, class _Compare>
|
williamr@4
|
73 |
# if !(defined (__SUNPRO_CC) && (__SUNPRO_CC < 0x420 ))
|
williamr@4
|
74 |
inline
|
williamr@4
|
75 |
# endif
|
williamr@4
|
76 |
const _Tp&
|
williamr@4
|
77 |
__median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp) {
|
williamr@4
|
78 |
if (__comp(__a, __b))
|
williamr@4
|
79 |
if (__comp(__b, __c))
|
williamr@4
|
80 |
return __b;
|
williamr@4
|
81 |
else if (__comp(__a, __c))
|
williamr@4
|
82 |
return __c;
|
williamr@4
|
83 |
else
|
williamr@4
|
84 |
return __a;
|
williamr@4
|
85 |
else if (__comp(__a, __c))
|
williamr@4
|
86 |
return __a;
|
williamr@4
|
87 |
else if (__comp(__b, __c))
|
williamr@4
|
88 |
return __c;
|
williamr@4
|
89 |
else
|
williamr@4
|
90 |
return __b;
|
williamr@4
|
91 |
}
|
williamr@4
|
92 |
|
williamr@4
|
93 |
template <class _ForwardIter1, class _ForwardIter2>
|
williamr@4
|
94 |
_ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,
|
williamr@4
|
95 |
_ForwardIter2 __first2, _ForwardIter2 __last2)
|
williamr@4
|
96 |
{
|
williamr@4
|
97 |
_STLP_DEBUG_CHECK(__check_range(__first1, __last1))
|
williamr@4
|
98 |
_STLP_DEBUG_CHECK(__check_range(__first2, __last2))
|
williamr@4
|
99 |
// Test for empty ranges
|
williamr@4
|
100 |
if (__first1 == __last1 || __first2 == __last2)
|
williamr@4
|
101 |
return __first1;
|
williamr@4
|
102 |
|
williamr@4
|
103 |
// Test for a pattern of length 1.
|
williamr@4
|
104 |
_ForwardIter2 __tmp(__first2);
|
williamr@4
|
105 |
++__tmp;
|
williamr@4
|
106 |
if (__tmp == __last2)
|
williamr@4
|
107 |
return find(__first1, __last1, *__first2);
|
williamr@4
|
108 |
|
williamr@4
|
109 |
// General case.
|
williamr@4
|
110 |
_ForwardIter2 __p1 = __first2;
|
williamr@4
|
111 |
++__p1;
|
williamr@4
|
112 |
|
williamr@4
|
113 |
_ForwardIter1 __current = __first1;
|
williamr@4
|
114 |
|
williamr@4
|
115 |
while (__first1 != __last1) {
|
williamr@4
|
116 |
__first1 = find(__first1, __last1, *__first2);
|
williamr@4
|
117 |
if (__first1 == __last1)
|
williamr@4
|
118 |
return __last1;
|
williamr@4
|
119 |
|
williamr@4
|
120 |
_ForwardIter2 __p = __p1;
|
williamr@4
|
121 |
__current = __first1;
|
williamr@4
|
122 |
if (++__current == __last1)
|
williamr@4
|
123 |
return __last1;
|
williamr@4
|
124 |
|
williamr@4
|
125 |
while (*__current == *__p) {
|
williamr@4
|
126 |
if (++__p == __last2)
|
williamr@4
|
127 |
return __first1;
|
williamr@4
|
128 |
if (++__current == __last1)
|
williamr@4
|
129 |
return __last1;
|
williamr@4
|
130 |
}
|
williamr@4
|
131 |
|
williamr@4
|
132 |
++__first1;
|
williamr@4
|
133 |
}
|
williamr@4
|
134 |
return __first1;
|
williamr@4
|
135 |
}
|
williamr@4
|
136 |
|
williamr@4
|
137 |
// search_n. Search for __count consecutive copies of __val.
|
williamr@4
|
138 |
|
williamr@4
|
139 |
template <class _ForwardIter, class _Integer, class _Tp>
|
williamr@4
|
140 |
_ForwardIter search_n(_ForwardIter __first, _ForwardIter __last,
|
williamr@4
|
141 |
_Integer __count, const _Tp& __val) {
|
williamr@4
|
142 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
143 |
if (__count <= 0)
|
williamr@4
|
144 |
return __first;
|
williamr@4
|
145 |
else {
|
williamr@4
|
146 |
__first = find(__first, __last, __val);
|
williamr@4
|
147 |
while (__first != __last) {
|
williamr@4
|
148 |
_Integer __n = __count - 1;
|
williamr@4
|
149 |
_ForwardIter __i = __first;
|
williamr@4
|
150 |
++__i;
|
williamr@4
|
151 |
while (__i != __last && __n != 0 && *__i == __val) {
|
williamr@4
|
152 |
++__i;
|
williamr@4
|
153 |
--__n;
|
williamr@4
|
154 |
}
|
williamr@4
|
155 |
if (__n == 0)
|
williamr@4
|
156 |
return __first;
|
williamr@4
|
157 |
else
|
williamr@4
|
158 |
__first = find(__i, __last, __val);
|
williamr@4
|
159 |
}
|
williamr@4
|
160 |
return __last;
|
williamr@4
|
161 |
}
|
williamr@4
|
162 |
}
|
williamr@4
|
163 |
|
williamr@4
|
164 |
template <class _ForwardIter, class _Integer, class _Tp, class _BinaryPred>
|
williamr@4
|
165 |
_ForwardIter search_n(_ForwardIter __first, _ForwardIter __last,
|
williamr@4
|
166 |
_Integer __count, const _Tp& __val,
|
williamr@4
|
167 |
_BinaryPred __binary_pred) {
|
williamr@4
|
168 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
169 |
if (__count <= 0)
|
williamr@4
|
170 |
return __first;
|
williamr@4
|
171 |
else {
|
williamr@4
|
172 |
while (__first != __last) {
|
williamr@4
|
173 |
if (__binary_pred(*__first, __val))
|
williamr@4
|
174 |
break;
|
williamr@4
|
175 |
++__first;
|
williamr@4
|
176 |
}
|
williamr@4
|
177 |
while (__first != __last) {
|
williamr@4
|
178 |
_Integer __n = __count - 1;
|
williamr@4
|
179 |
_ForwardIter __i = __first;
|
williamr@4
|
180 |
++__i;
|
williamr@4
|
181 |
while (__i != __last && __n != 0 && __binary_pred(*__i, __val)) {
|
williamr@4
|
182 |
++__i;
|
williamr@4
|
183 |
--__n;
|
williamr@4
|
184 |
}
|
williamr@4
|
185 |
if (__n == 0)
|
williamr@4
|
186 |
return __first;
|
williamr@4
|
187 |
else {
|
williamr@4
|
188 |
while (__i != __last) {
|
williamr@4
|
189 |
if (__binary_pred(*__i, __val))
|
williamr@4
|
190 |
break;
|
williamr@4
|
191 |
++__i;
|
williamr@4
|
192 |
}
|
williamr@4
|
193 |
__first = __i;
|
williamr@4
|
194 |
}
|
williamr@4
|
195 |
}
|
williamr@4
|
196 |
return __last;
|
williamr@4
|
197 |
}
|
williamr@4
|
198 |
}
|
williamr@4
|
199 |
|
williamr@4
|
200 |
template <class _ForwardIter1, class _ForwardIter2>
|
williamr@4
|
201 |
_ForwardIter1
|
williamr@4
|
202 |
find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
|
williamr@4
|
203 |
_ForwardIter2 __first2, _ForwardIter2 __last2)
|
williamr@4
|
204 |
{
|
williamr@4
|
205 |
_STLP_DEBUG_CHECK(__check_range(__first1, __last1))
|
williamr@4
|
206 |
_STLP_DEBUG_CHECK(__check_range(__first2, __last2))
|
williamr@4
|
207 |
return __find_end(__first1, __last1, __first2, __last2,
|
williamr@4
|
208 |
# if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
williamr@4
|
209 |
_STLP_ITERATOR_CATEGORY(__first1, _ForwardIter1),
|
williamr@4
|
210 |
_STLP_ITERATOR_CATEGORY(__first2, _ForwardIter2),
|
williamr@4
|
211 |
# else
|
williamr@4
|
212 |
forward_iterator_tag(),
|
williamr@4
|
213 |
forward_iterator_tag(),
|
williamr@4
|
214 |
# endif
|
williamr@4
|
215 |
__equal_to(_STLP_VALUE_TYPE(__first1, _ForwardIter1))
|
williamr@4
|
216 |
);
|
williamr@4
|
217 |
}
|
williamr@4
|
218 |
|
williamr@4
|
219 |
// unique and unique_copy
|
williamr@4
|
220 |
template <class _InputIterator, class _OutputIterator, class _BinaryPredicate,
|
williamr@4
|
221 |
class _Tp>
|
williamr@4
|
222 |
_STLP_INLINE_LOOP _OutputIterator
|
williamr@4
|
223 |
__unique_copy(_InputIterator __first, _InputIterator __last,
|
williamr@4
|
224 |
_OutputIterator __result,
|
williamr@4
|
225 |
_BinaryPredicate __binary_pred, _Tp*) {
|
williamr@4
|
226 |
_Tp __val = *__first;
|
williamr@4
|
227 |
_STLP_PUSH_STACK_ITEM(_Tp, &__val)
|
williamr@4
|
228 |
*__result = __val;
|
williamr@4
|
229 |
while (++__first != __last)
|
williamr@4
|
230 |
if (!__binary_pred(__val, *__first)) {
|
williamr@4
|
231 |
__val = *__first;
|
williamr@4
|
232 |
*++__result = __val;
|
williamr@4
|
233 |
}
|
williamr@4
|
234 |
return ++__result;
|
williamr@4
|
235 |
}
|
williamr@4
|
236 |
|
williamr@4
|
237 |
template <class _InputIter, class _OutputIter, class _BinaryPredicate>
|
williamr@4
|
238 |
inline _OutputIter
|
williamr@4
|
239 |
__unique_copy(_InputIter __first, _InputIter __last,_OutputIter __result,
|
williamr@4
|
240 |
_BinaryPredicate __binary_pred, const output_iterator_tag &) {
|
williamr@4
|
241 |
return __unique_copy(__first, __last, __result, __binary_pred, _STLP_VALUE_TYPE(__first, _InputIter));
|
williamr@4
|
242 |
}
|
williamr@4
|
243 |
|
williamr@4
|
244 |
template <class _InputIter, class _ForwardIter, class _BinaryPredicate>
|
williamr@4
|
245 |
_STLP_INLINE_LOOP _ForwardIter
|
williamr@4
|
246 |
__unique_copy(_InputIter __first, _InputIter __last, _ForwardIter __result,
|
williamr@4
|
247 |
_BinaryPredicate __binary_pred, const forward_iterator_tag &) {
|
williamr@4
|
248 |
*__result = *__first;
|
williamr@4
|
249 |
while (++__first != __last)
|
williamr@4
|
250 |
if (!__binary_pred(*__result, *__first)) *++__result = *__first;
|
williamr@4
|
251 |
return ++__result;
|
williamr@4
|
252 |
}
|
williamr@4
|
253 |
|
williamr@4
|
254 |
# if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
|
williamr@4
|
255 |
template <class _InputIterator, class _BidirectionalIterator, class _BinaryPredicate>
|
williamr@4
|
256 |
inline _BidirectionalIterator
|
williamr@4
|
257 |
__unique_copy(_InputIterator __first, _InputIterator __last,
|
williamr@4
|
258 |
_BidirectionalIterator __result, _BinaryPredicate __binary_pred,
|
williamr@4
|
259 |
const bidirectional_iterator_tag &) {
|
williamr@4
|
260 |
return __unique_copy(__first, __last, __result, __binary_pred, forward_iterator_tag());
|
williamr@4
|
261 |
}
|
williamr@4
|
262 |
|
williamr@4
|
263 |
template <class _InputIterator, class _RandomAccessIterator, class _BinaryPredicate>
|
williamr@4
|
264 |
inline _RandomAccessIterator
|
williamr@4
|
265 |
__unique_copy(_InputIterator __first, _InputIterator __last,
|
williamr@4
|
266 |
_RandomAccessIterator __result, _BinaryPredicate __binary_pred,
|
williamr@4
|
267 |
const random_access_iterator_tag &) {
|
williamr@4
|
268 |
return __unique_copy(__first, __last, __result, __binary_pred, forward_iterator_tag());
|
williamr@4
|
269 |
}
|
williamr@4
|
270 |
# endif /* _STLP_NONTEMPL_BASE_MATCH_BUG */
|
williamr@4
|
271 |
|
williamr@4
|
272 |
|
williamr@4
|
273 |
template <class _InputIter, class _OutputIter>
|
williamr@4
|
274 |
_OutputIter
|
williamr@4
|
275 |
unique_copy(_InputIter __first, _InputIter __last, _OutputIter __result) {
|
williamr@4
|
276 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
277 |
if (__first == __last) return __result;
|
williamr@4
|
278 |
return __unique_copy(__first, __last, __result, __equal_to(_STLP_VALUE_TYPE(__first, _InputIter)),
|
williamr@4
|
279 |
_STLP_ITERATOR_CATEGORY(__result, _OutputIter));
|
williamr@4
|
280 |
}
|
williamr@4
|
281 |
|
williamr@4
|
282 |
template <class _InputIter, class _OutputIter, class _BinaryPredicate>
|
williamr@4
|
283 |
_OutputIter
|
williamr@4
|
284 |
unique_copy(_InputIter __first, _InputIter __last,_OutputIter __result,
|
williamr@4
|
285 |
_BinaryPredicate __binary_pred) {
|
williamr@4
|
286 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
287 |
if (__first == __last) return __result;
|
williamr@4
|
288 |
return __unique_copy(__first, __last, __result, __binary_pred,
|
williamr@4
|
289 |
_STLP_ITERATOR_CATEGORY(__result, _OutputIter));
|
williamr@4
|
290 |
}
|
williamr@4
|
291 |
|
williamr@4
|
292 |
// rotate and rotate_copy, and their auxiliary functions
|
williamr@4
|
293 |
|
williamr@4
|
294 |
template <class _ForwardIter, class _Distance>
|
williamr@4
|
295 |
_ForwardIter __rotate(_ForwardIter __first,
|
williamr@4
|
296 |
_ForwardIter __middle,
|
williamr@4
|
297 |
_ForwardIter __last,
|
williamr@4
|
298 |
_Distance*,
|
williamr@4
|
299 |
const forward_iterator_tag &) {
|
williamr@4
|
300 |
if (__first == __middle)
|
williamr@4
|
301 |
return __last;
|
williamr@4
|
302 |
if (__last == __middle)
|
williamr@4
|
303 |
return __first;
|
williamr@4
|
304 |
|
williamr@4
|
305 |
_ForwardIter __first2 = __middle;
|
williamr@4
|
306 |
do {
|
williamr@4
|
307 |
swap(*__first++, *__first2++);
|
williamr@4
|
308 |
if (__first == __middle)
|
williamr@4
|
309 |
__middle = __first2;
|
williamr@4
|
310 |
} while (__first2 != __last);
|
williamr@4
|
311 |
|
williamr@4
|
312 |
_ForwardIter __new_middle = __first;
|
williamr@4
|
313 |
|
williamr@4
|
314 |
__first2 = __middle;
|
williamr@4
|
315 |
|
williamr@4
|
316 |
while (__first2 != __last) {
|
williamr@4
|
317 |
swap (*__first++, *__first2++);
|
williamr@4
|
318 |
if (__first == __middle)
|
williamr@4
|
319 |
__middle = __first2;
|
williamr@4
|
320 |
else if (__first2 == __last)
|
williamr@4
|
321 |
__first2 = __middle;
|
williamr@4
|
322 |
}
|
williamr@4
|
323 |
|
williamr@4
|
324 |
return __new_middle;
|
williamr@4
|
325 |
}
|
williamr@4
|
326 |
|
williamr@4
|
327 |
template <class _BidirectionalIter, class _Distance>
|
williamr@4
|
328 |
_BidirectionalIter __rotate(_BidirectionalIter __first,
|
williamr@4
|
329 |
_BidirectionalIter __middle,
|
williamr@4
|
330 |
_BidirectionalIter __last,
|
williamr@4
|
331 |
_Distance*,
|
williamr@4
|
332 |
const bidirectional_iterator_tag &) {
|
williamr@4
|
333 |
if (__first == __middle)
|
williamr@4
|
334 |
return __last;
|
williamr@4
|
335 |
if (__last == __middle)
|
williamr@4
|
336 |
return __first;
|
williamr@4
|
337 |
|
williamr@4
|
338 |
__reverse(__first, __middle, bidirectional_iterator_tag());
|
williamr@4
|
339 |
__reverse(__middle, __last, bidirectional_iterator_tag());
|
williamr@4
|
340 |
|
williamr@4
|
341 |
while (__first != __middle && __middle != __last)
|
williamr@4
|
342 |
swap (*__first++, *--__last);
|
williamr@4
|
343 |
|
williamr@4
|
344 |
if (__first == __middle) {
|
williamr@4
|
345 |
__reverse(__middle, __last, bidirectional_iterator_tag());
|
williamr@4
|
346 |
return __last;
|
williamr@4
|
347 |
}
|
williamr@4
|
348 |
else {
|
williamr@4
|
349 |
__reverse(__first, __middle, bidirectional_iterator_tag());
|
williamr@4
|
350 |
return __first;
|
williamr@4
|
351 |
}
|
williamr@4
|
352 |
}
|
williamr@4
|
353 |
|
williamr@4
|
354 |
template <class _RandomAccessIter, class _Distance, class _Tp>
|
williamr@4
|
355 |
_RandomAccessIter __rotate(_RandomAccessIter __first,
|
williamr@4
|
356 |
_RandomAccessIter __middle,
|
williamr@4
|
357 |
_RandomAccessIter __last,
|
williamr@4
|
358 |
_Distance *, _Tp *) {
|
williamr@4
|
359 |
|
williamr@4
|
360 |
_Distance __n = __last - __first;
|
williamr@4
|
361 |
_Distance __k = __middle - __first;
|
williamr@4
|
362 |
_Distance __l = __n - __k;
|
williamr@4
|
363 |
_RandomAccessIter __result = __first + (__last - __middle);
|
williamr@4
|
364 |
|
williamr@4
|
365 |
if (__k==0) /* __first == middle */
|
williamr@4
|
366 |
return __last;
|
williamr@4
|
367 |
|
williamr@4
|
368 |
if (__k == __l) {
|
williamr@4
|
369 |
swap_ranges(__first, __middle, __middle);
|
williamr@4
|
370 |
return __result;
|
williamr@4
|
371 |
}
|
williamr@4
|
372 |
|
williamr@4
|
373 |
_Distance __d = __gcd(__n, __k);
|
williamr@4
|
374 |
|
williamr@4
|
375 |
for (_Distance __i = 0; __i < __d; __i++) {
|
williamr@4
|
376 |
_Tp __tmp = *__first;
|
williamr@4
|
377 |
_STLP_PUSH_STACK_ITEM(_Tp, &__tmp)
|
williamr@4
|
378 |
_RandomAccessIter __p = __first;
|
williamr@4
|
379 |
|
williamr@4
|
380 |
if (__k < __l) {
|
williamr@4
|
381 |
for (_Distance __j = 0; __j < __l/__d; __j++) {
|
williamr@4
|
382 |
if (__p > __first + __l) {
|
williamr@4
|
383 |
*__p = *(__p - __l);
|
williamr@4
|
384 |
__p -= __l;
|
williamr@4
|
385 |
}
|
williamr@4
|
386 |
|
williamr@4
|
387 |
*__p = *(__p + __k);
|
williamr@4
|
388 |
__p += __k;
|
williamr@4
|
389 |
}
|
williamr@4
|
390 |
}
|
williamr@4
|
391 |
|
williamr@4
|
392 |
else {
|
williamr@4
|
393 |
for (_Distance __j = 0; __j < __k/__d - 1; __j ++) {
|
williamr@4
|
394 |
if (__p < __last - __k) {
|
williamr@4
|
395 |
*__p = *(__p + __k);
|
williamr@4
|
396 |
__p += __k;
|
williamr@4
|
397 |
}
|
williamr@4
|
398 |
|
williamr@4
|
399 |
*__p = * (__p - __l);
|
williamr@4
|
400 |
__p -= __l;
|
williamr@4
|
401 |
}
|
williamr@4
|
402 |
}
|
williamr@4
|
403 |
|
williamr@4
|
404 |
*__p = __tmp;
|
williamr@4
|
405 |
++__first;
|
williamr@4
|
406 |
}
|
williamr@4
|
407 |
|
williamr@4
|
408 |
return __result;
|
williamr@4
|
409 |
}
|
williamr@4
|
410 |
|
williamr@4
|
411 |
template <class _RandomAccessIter, class _Distance>
|
williamr@4
|
412 |
inline _RandomAccessIter
|
williamr@4
|
413 |
__rotate(_RandomAccessIter __first, _RandomAccessIter __middle, _RandomAccessIter __last,
|
williamr@4
|
414 |
_Distance * __dis, const random_access_iterator_tag &) {
|
williamr@4
|
415 |
return __rotate(__first, __middle, __last,
|
williamr@4
|
416 |
__dis, _STLP_VALUE_TYPE(__first, _RandomAccessIter));
|
williamr@4
|
417 |
}
|
williamr@4
|
418 |
|
williamr@4
|
419 |
template <class _ForwardIter>
|
williamr@4
|
420 |
_ForwardIter
|
williamr@4
|
421 |
rotate(_ForwardIter __first, _ForwardIter __middle, _ForwardIter __last) {
|
williamr@4
|
422 |
_STLP_DEBUG_CHECK(__check_range(__first, __middle))
|
williamr@4
|
423 |
_STLP_DEBUG_CHECK(__check_range(__middle, __last))
|
williamr@4
|
424 |
return __rotate(__first, __middle, __last,
|
williamr@4
|
425 |
_STLP_DISTANCE_TYPE(__first, _ForwardIter),
|
williamr@4
|
426 |
_STLP_ITERATOR_CATEGORY(__first, _ForwardIter));
|
williamr@4
|
427 |
}
|
williamr@4
|
428 |
|
williamr@4
|
429 |
// Return a random number in the range [0, __n). This function encapsulates
|
williamr@4
|
430 |
// whether we're using rand (part of the standard C library) or lrand48
|
williamr@4
|
431 |
// (not standard, but a much better choice whenever it's available).
|
williamr@4
|
432 |
|
williamr@4
|
433 |
template <class _Distance>
|
williamr@4
|
434 |
inline _Distance __random_number(_Distance __n) {
|
williamr@4
|
435 |
#ifdef _STLP_NO_DRAND48
|
williamr@4
|
436 |
return rand() % __n;
|
williamr@4
|
437 |
#else
|
williamr@4
|
438 |
return lrand48() % __n;
|
williamr@4
|
439 |
#endif
|
williamr@4
|
440 |
}
|
williamr@4
|
441 |
|
williamr@4
|
442 |
template <class _RandomAccessIter>
|
williamr@4
|
443 |
void random_shuffle(_RandomAccessIter __first,
|
williamr@4
|
444 |
_RandomAccessIter __last) {
|
williamr@4
|
445 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
446 |
if (__first == __last) return;
|
williamr@4
|
447 |
for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i)
|
williamr@4
|
448 |
iter_swap(__i, __first + __random_number((__i - __first) + 1));
|
williamr@4
|
449 |
}
|
williamr@4
|
450 |
|
williamr@4
|
451 |
template <class _RandomAccessIter, class _RandomNumberGenerator>
|
williamr@4
|
452 |
void random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last,
|
williamr@4
|
453 |
_RandomNumberGenerator& __rand) {
|
williamr@4
|
454 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
455 |
if (__first == __last) return;
|
williamr@4
|
456 |
for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i)
|
williamr@4
|
457 |
iter_swap(__i, __first + __rand((__i - __first) + 1));
|
williamr@4
|
458 |
}
|
williamr@4
|
459 |
|
williamr@4
|
460 |
# ifndef _STLP_NO_EXTENSIONS
|
williamr@4
|
461 |
|
williamr@4
|
462 |
// random_sample and random_sample_n (extensions, not part of the standard).
|
williamr@4
|
463 |
|
williamr@4
|
464 |
template <class _ForwardIter, class _OutputIter, class _Distance>
|
williamr@4
|
465 |
_OutputIter random_sample_n(_ForwardIter __first, _ForwardIter __last,
|
williamr@4
|
466 |
_OutputIter __stl_out, const _Distance __n)
|
williamr@4
|
467 |
{
|
williamr@4
|
468 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
469 |
_Distance __remaining = distance(__first, __last);
|
williamr@4
|
470 |
_Distance __m = (min) (__n, __remaining);
|
williamr@4
|
471 |
|
williamr@4
|
472 |
while (__m > 0) {
|
williamr@4
|
473 |
if (__random_number(__remaining) < __m) {
|
williamr@4
|
474 |
*__stl_out = *__first;
|
williamr@4
|
475 |
++__stl_out;
|
williamr@4
|
476 |
--__m;
|
williamr@4
|
477 |
}
|
williamr@4
|
478 |
|
williamr@4
|
479 |
--__remaining;
|
williamr@4
|
480 |
++__first;
|
williamr@4
|
481 |
}
|
williamr@4
|
482 |
return __stl_out;
|
williamr@4
|
483 |
}
|
williamr@4
|
484 |
|
williamr@4
|
485 |
|
williamr@4
|
486 |
template <class _ForwardIter, class _OutputIter, class _Distance,
|
williamr@4
|
487 |
class _RandomNumberGenerator>
|
williamr@4
|
488 |
_OutputIter random_sample_n(_ForwardIter __first, _ForwardIter __last,
|
williamr@4
|
489 |
_OutputIter __stl_out, const _Distance __n,
|
williamr@4
|
490 |
_RandomNumberGenerator& __rand)
|
williamr@4
|
491 |
{
|
williamr@4
|
492 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
493 |
_Distance __remaining = distance(__first, __last);
|
williamr@4
|
494 |
_Distance __m = (min) (__n, __remaining);
|
williamr@4
|
495 |
|
williamr@4
|
496 |
while (__m > 0) {
|
williamr@4
|
497 |
if (__rand(__remaining) < __m) {
|
williamr@4
|
498 |
*__stl_out = *__first;
|
williamr@4
|
499 |
++__stl_out;
|
williamr@4
|
500 |
--__m;
|
williamr@4
|
501 |
}
|
williamr@4
|
502 |
|
williamr@4
|
503 |
--__remaining;
|
williamr@4
|
504 |
++__first;
|
williamr@4
|
505 |
}
|
williamr@4
|
506 |
return __stl_out;
|
williamr@4
|
507 |
}
|
williamr@4
|
508 |
|
williamr@4
|
509 |
template <class _InputIter, class _RandomAccessIter, class _Distance>
|
williamr@4
|
510 |
_RandomAccessIter __random_sample(_InputIter __first, _InputIter __last,
|
williamr@4
|
511 |
_RandomAccessIter __stl_out,
|
williamr@4
|
512 |
const _Distance __n)
|
williamr@4
|
513 |
{
|
williamr@4
|
514 |
_Distance __m = 0;
|
williamr@4
|
515 |
_Distance __t = __n;
|
williamr@4
|
516 |
for ( ; __first != __last && __m < __n; ++__m, ++__first)
|
williamr@4
|
517 |
__stl_out[__m] = *__first;
|
williamr@4
|
518 |
|
williamr@4
|
519 |
while (__first != __last) {
|
williamr@4
|
520 |
++__t;
|
williamr@4
|
521 |
_Distance __M = __random_number(__t);
|
williamr@4
|
522 |
if (__M < __n)
|
williamr@4
|
523 |
__stl_out[__M] = *__first;
|
williamr@4
|
524 |
++__first;
|
williamr@4
|
525 |
}
|
williamr@4
|
526 |
|
williamr@4
|
527 |
return __stl_out + __m;
|
williamr@4
|
528 |
}
|
williamr@4
|
529 |
|
williamr@4
|
530 |
template <class _InputIter, class _RandomAccessIter,
|
williamr@4
|
531 |
class _RandomNumberGenerator, class _Distance>
|
williamr@4
|
532 |
_RandomAccessIter __random_sample(_InputIter __first, _InputIter __last,
|
williamr@4
|
533 |
_RandomAccessIter __stl_out,
|
williamr@4
|
534 |
_RandomNumberGenerator& __rand,
|
williamr@4
|
535 |
const _Distance __n)
|
williamr@4
|
536 |
{
|
williamr@4
|
537 |
_Distance __m = 0;
|
williamr@4
|
538 |
_Distance __t = __n;
|
williamr@4
|
539 |
for ( ; __first != __last && __m < __n; ++__m, ++__first)
|
williamr@4
|
540 |
__stl_out[__m] = *__first;
|
williamr@4
|
541 |
|
williamr@4
|
542 |
while (__first != __last) {
|
williamr@4
|
543 |
++__t;
|
williamr@4
|
544 |
_Distance __M = __rand(__t);
|
williamr@4
|
545 |
if (__M < __n)
|
williamr@4
|
546 |
__stl_out[__M] = *__first;
|
williamr@4
|
547 |
++__first;
|
williamr@4
|
548 |
}
|
williamr@4
|
549 |
|
williamr@4
|
550 |
return __stl_out + __m;
|
williamr@4
|
551 |
}
|
williamr@4
|
552 |
|
williamr@4
|
553 |
template <class _InputIter, class _RandomAccessIter>
|
williamr@4
|
554 |
_RandomAccessIter
|
williamr@4
|
555 |
random_sample(_InputIter __first, _InputIter __last,
|
williamr@4
|
556 |
_RandomAccessIter __out_first, _RandomAccessIter __out_last)
|
williamr@4
|
557 |
{
|
williamr@4
|
558 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
559 |
_STLP_DEBUG_CHECK(__check_range(__out_first, __out_last))
|
williamr@4
|
560 |
return __random_sample(__first, __last,
|
williamr@4
|
561 |
__out_first, __out_last - __out_first);
|
williamr@4
|
562 |
}
|
williamr@4
|
563 |
|
williamr@4
|
564 |
template <class _InputIter, class _RandomAccessIter, class _RandomNumberGenerator>
|
williamr@4
|
565 |
_RandomAccessIter
|
williamr@4
|
566 |
random_sample(_InputIter __first, _InputIter __last,
|
williamr@4
|
567 |
_RandomAccessIter __out_first, _RandomAccessIter __out_last,
|
williamr@4
|
568 |
_RandomNumberGenerator& __rand)
|
williamr@4
|
569 |
{
|
williamr@4
|
570 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
571 |
_STLP_DEBUG_CHECK(__check_range(__out_first, __out_last))
|
williamr@4
|
572 |
return __random_sample(__first, __last,
|
williamr@4
|
573 |
__out_first, __rand,
|
williamr@4
|
574 |
__out_last - __out_first);
|
williamr@4
|
575 |
}
|
williamr@4
|
576 |
|
williamr@4
|
577 |
# endif /* _STLP_NO_EXTENSIONS */
|
williamr@4
|
578 |
|
williamr@4
|
579 |
// partition, stable_partition, and their auxiliary functions
|
williamr@4
|
580 |
|
williamr@4
|
581 |
template <class _ForwardIter, class _Predicate>
|
williamr@4
|
582 |
_STLP_INLINE_LOOP _ForwardIter __partition(_ForwardIter __first,
|
williamr@4
|
583 |
_ForwardIter __last,
|
williamr@4
|
584 |
_Predicate __pred,
|
williamr@4
|
585 |
const forward_iterator_tag &) {
|
williamr@4
|
586 |
if (__first == __last) return __first;
|
williamr@4
|
587 |
|
williamr@4
|
588 |
while (__pred(*__first))
|
williamr@4
|
589 |
if (++__first == __last) return __first;
|
williamr@4
|
590 |
|
williamr@4
|
591 |
_ForwardIter __next = __first;
|
williamr@4
|
592 |
|
williamr@4
|
593 |
while (++__next != __last)
|
williamr@4
|
594 |
if (__pred(*__next)) {
|
williamr@4
|
595 |
swap(*__first, *__next);
|
williamr@4
|
596 |
++__first;
|
williamr@4
|
597 |
}
|
williamr@4
|
598 |
return __first;
|
williamr@4
|
599 |
}
|
williamr@4
|
600 |
|
williamr@4
|
601 |
/* bug fix- start*/
|
williamr@4
|
602 |
|
williamr@4
|
603 |
template <class _ForwardIter>
|
williamr@4
|
604 |
_ForwardIter
|
williamr@4
|
605 |
__rotate(_ForwardIter __first, _ForwardIter __middle, _ForwardIter __last) {
|
williamr@4
|
606 |
_STLP_DEBUG_CHECK(__check_range(__first, __middle))
|
williamr@4
|
607 |
_STLP_DEBUG_CHECK(__check_range(__middle, __last))
|
williamr@4
|
608 |
return __rotate_aux(__first, __middle, __last,
|
williamr@4
|
609 |
_STLP_DISTANCE_TYPE(__first, _ForwardIter),
|
williamr@4
|
610 |
_STLP_ITERATOR_CATEGORY(__first, _ForwardIter));
|
williamr@4
|
611 |
}
|
williamr@4
|
612 |
|
williamr@4
|
613 |
template <class _ForwardIter, class _Distance>
|
williamr@4
|
614 |
_ForwardIter __rotate_aux(_ForwardIter __first,
|
williamr@4
|
615 |
_ForwardIter __middle,
|
williamr@4
|
616 |
_ForwardIter __last,
|
williamr@4
|
617 |
_Distance*,
|
williamr@4
|
618 |
const forward_iterator_tag &) {
|
williamr@4
|
619 |
if (__first == __middle)
|
williamr@4
|
620 |
return __last;
|
williamr@4
|
621 |
if (__last == __middle)
|
williamr@4
|
622 |
return __first;
|
williamr@4
|
623 |
|
williamr@4
|
624 |
_ForwardIter __first2 = __middle;
|
williamr@4
|
625 |
do {
|
williamr@4
|
626 |
swap(*__first++, *__first2++);
|
williamr@4
|
627 |
if (__first == __middle)
|
williamr@4
|
628 |
__middle = __first2;
|
williamr@4
|
629 |
} while (__first2 != __last);
|
williamr@4
|
630 |
|
williamr@4
|
631 |
_ForwardIter __new_middle = __first;
|
williamr@4
|
632 |
|
williamr@4
|
633 |
__first2 = __middle;
|
williamr@4
|
634 |
|
williamr@4
|
635 |
while (__first2 != __last) {
|
williamr@4
|
636 |
swap (*__first++, *__first2++);
|
williamr@4
|
637 |
if (__first == __middle)
|
williamr@4
|
638 |
__middle = __first2;
|
williamr@4
|
639 |
else if (__first2 == __last)
|
williamr@4
|
640 |
__first2 = __middle;
|
williamr@4
|
641 |
}
|
williamr@4
|
642 |
|
williamr@4
|
643 |
return __new_middle;
|
williamr@4
|
644 |
}
|
williamr@4
|
645 |
|
williamr@4
|
646 |
|
williamr@4
|
647 |
template <class _ForwardIter, class _Pointer, class _Predicate,
|
williamr@4
|
648 |
class _Distance>
|
williamr@4
|
649 |
_ForwardIter __stable_partition_adaptive(_ForwardIter __first,
|
williamr@4
|
650 |
_ForwardIter __last,
|
williamr@4
|
651 |
_Predicate __pred, _Distance __len,
|
williamr@4
|
652 |
_Pointer __buffer, _Distance __buffer_size,
|
williamr@4
|
653 |
bool __pred_of_first, bool __pred_of_before_last) {
|
williamr@4
|
654 |
if (__len <= __buffer_size) {
|
williamr@4
|
655 |
_ForwardIter __result1 = __first;
|
williamr@4
|
656 |
_Pointer __result2 = __buffer;
|
williamr@4
|
657 |
if ((__first != __last) && (!__pred_of_first || __pred(*__first))) {
|
williamr@4
|
658 |
*__result2 = *__first;
|
williamr@4
|
659 |
++__result2; ++__first; --__len;
|
williamr@4
|
660 |
}
|
williamr@4
|
661 |
for (; __first != __last ; ++__first, --__len) {
|
williamr@4
|
662 |
if (((__len == 1) && (__pred_of_before_last || __pred(*__first))) ||
|
williamr@4
|
663 |
((__len != 1) && __pred(*__first))){
|
williamr@4
|
664 |
*__result1 = *__first;
|
williamr@4
|
665 |
++__result1;
|
williamr@4
|
666 |
}
|
williamr@4
|
667 |
else {
|
williamr@4
|
668 |
*__result2 = *__first;
|
williamr@4
|
669 |
++__result2;
|
williamr@4
|
670 |
}
|
williamr@4
|
671 |
}
|
williamr@4
|
672 |
copy(__buffer, __result2, __result1);
|
williamr@4
|
673 |
return __result1;
|
williamr@4
|
674 |
}
|
williamr@4
|
675 |
else {
|
williamr@4
|
676 |
_ForwardIter __middle = __first;
|
williamr@4
|
677 |
_Distance __half_len = __len / 2;
|
williamr@4
|
678 |
advance(__middle, __half_len);
|
williamr@4
|
679 |
return __rotate(__stable_partition_adaptive(
|
williamr@4
|
680 |
__first, __middle, __pred,
|
williamr@4
|
681 |
__half_len, __buffer, __buffer_size,
|
williamr@4
|
682 |
__pred_of_first, false),
|
williamr@4
|
683 |
__middle,
|
williamr@4
|
684 |
__stable_partition_adaptive(
|
williamr@4
|
685 |
__middle, __last, __pred,
|
williamr@4
|
686 |
__len - __half_len, __buffer, __buffer_size,
|
williamr@4
|
687 |
true, __pred_of_before_last));
|
williamr@4
|
688 |
}
|
williamr@4
|
689 |
}
|
williamr@4
|
690 |
|
williamr@4
|
691 |
|
williamr@4
|
692 |
template <class _ForwardIter, class _Predicate, class _Distance>
|
williamr@4
|
693 |
_ForwardIter __inplace_stable_partition(_ForwardIter __first,
|
williamr@4
|
694 |
_ForwardIter __last,
|
williamr@4
|
695 |
_Predicate __pred, _Distance __len,
|
williamr@4
|
696 |
bool __pred_of_first, bool __pred_of_before_last) {
|
williamr@4
|
697 |
if (__len == 1)
|
williamr@4
|
698 |
return (__pred_of_first && (__pred_of_before_last || __pred(*__first))) ? __last : __first;
|
williamr@4
|
699 |
_ForwardIter __middle = __first;
|
williamr@4
|
700 |
_Distance __half_len = __len / 2;
|
williamr@4
|
701 |
advance(__middle, __half_len);
|
williamr@4
|
702 |
return __rotate(__inplace_stable_partition(__first, __middle, __pred, __half_len, __pred_of_first, false),
|
williamr@4
|
703 |
__middle,
|
williamr@4
|
704 |
__inplace_stable_partition(__middle, __last, __pred, __len - __half_len, true, __pred_of_before_last));
|
williamr@4
|
705 |
}
|
williamr@4
|
706 |
|
williamr@4
|
707 |
|
williamr@4
|
708 |
|
williamr@4
|
709 |
template <class _ForwardIter, class _Predicate>
|
williamr@4
|
710 |
_ForwardIter
|
williamr@4
|
711 |
stable_partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred) {
|
williamr@4
|
712 |
_STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
|
williamr@4
|
713 |
for (;;) {
|
williamr@4
|
714 |
if (__first == __last)
|
williamr@4
|
715 |
return __first;
|
williamr@4
|
716 |
else if (__pred(*__first))
|
williamr@4
|
717 |
++__first;
|
williamr@4
|
718 |
else
|
williamr@4
|
719 |
break;
|
williamr@4
|
720 |
}
|
williamr@4
|
721 |
return __stable_partition_aux(__first, __last, __pred,
|
williamr@4
|
722 |
_STLP_ITERATOR_CATEGORY(__first, _ForwardIter));
|
williamr@4
|
723 |
}
|
williamr@4
|
724 |
|
williamr@4
|
725 |
|
williamr@4
|
726 |
template <class _ForwardIter, class _Predicate, class _Tp, class _Distance>
|
williamr@4
|
727 |
inline _ForwardIter
|
williamr@4
|
728 |
__stable_partition_aux_aux(_ForwardIter __first, _ForwardIter __last,
|
williamr@4
|
729 |
_Predicate __pred, _Tp*, _Distance*, bool __pred_of_before_last = false) {
|
williamr@4
|
730 |
_Temporary_buffer<_ForwardIter, _Tp> __buf(__first, __last);
|
williamr@4
|
731 |
return (__buf.size() > 0) ?
|
williamr@4
|
732 |
__stable_partition_adaptive(__first, __last, __pred,
|
williamr@4
|
733 |
_Distance(__buf.requested_size()),
|
williamr@4
|
734 |
__buf.begin(), __buf.size(),
|
williamr@4
|
735 |
false, __pred_of_before_last) :
|
williamr@4
|
736 |
__inplace_stable_partition(__first, __last, __pred,
|
williamr@4
|
737 |
_Distance(__buf.requested_size()),
|
williamr@4
|
738 |
false, __pred_of_before_last);
|
williamr@4
|
739 |
|
williamr@4
|
740 |
}
|
williamr@4
|
741 |
|
williamr@4
|
742 |
template <class _ForwardIter, class _Predicate>
|
williamr@4
|
743 |
_ForwardIter
|
williamr@4
|
744 |
__stable_partition_aux(_ForwardIter __first, _ForwardIter __last, _Predicate __pred,
|
williamr@4
|
745 |
const forward_iterator_tag &) {
|
williamr@4
|
746 |
return __stable_partition_aux_aux(__first, __last, __pred,
|
williamr@4
|
747 |
_STLP_VALUE_TYPE(__first, _ForwardIter),
|
williamr@4
|
748 |
_STLP_DISTANCE_TYPE(__first, _ForwardIter));
|
williamr@4
|
749 |
}
|
williamr@4
|
750 |
|
williamr@4
|
751 |
|
williamr@4
|
752 |
/* bug fix- end*/
|
williamr@4
|
753 |
|
williamr@4
|
754 |
|
williamr@4
|
755 |
|
williamr@4
|
756 |
template <class _BidirectionalIter, class _Predicate>
|
williamr@4
|
757 |
_STLP_INLINE_LOOP _BidirectionalIter __partition(_BidirectionalIter __first,
|
williamr@4
|
758 |
_BidirectionalIter __last,
|
williamr@4
|
759 |
_Predicate __pred,
|
williamr@4
|
760 |
const bidirectional_iterator_tag &) {
|
williamr@4
|
761 |
while (true) {
|
williamr@4
|
762 |
while (true)
|
williamr@4
|
763 |
if (__first == __last)
|
williamr@4
|
764 |
return __first;
|
williamr@4
|
765 |
else if (__pred(*__first))
|
williamr@4
|
766 |
++__first;
|
williamr@4
|
767 |
else
|
williamr@4
|
768 |
break;
|
williamr@4
|
769 |
--__last;
|
williamr@4
|
770 |
while (true)
|
williamr@4
|
771 |
if (__first == __last)
|
williamr@4
|
772 |
return __first;
|
williamr@4
|
773 |
else if (!__pred(*__last))
|
williamr@4
|
774 |
--__last;
|
williamr@4
|
775 |
else
|
williamr@4
|
776 |
break;
|
williamr@4
|
777 |
iter_swap(__first, __last);
|
williamr@4
|
778 |
++__first;
|
williamr@4
|
779 |
}
|
williamr@4
|
780 |
}
|
williamr@4
|
781 |
|
williamr@4
|
782 |
# if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
|
williamr@4
|
783 |
template <class _BidirectionalIter, class _Predicate>
|
williamr@4
|
784 |
inline
|
williamr@4
|
785 |
_BidirectionalIter __partition(_BidirectionalIter __first,
|
williamr@4
|
786 |
_BidirectionalIter __last,
|
williamr@4
|
787 |
_Predicate __pred,
|
williamr@4
|
788 |
const random_access_iterator_tag &) {
|
williamr@4
|
789 |
return __partition(__first, __last, __pred, bidirectional_iterator_tag());
|
williamr@4
|
790 |
}
|
williamr@4
|
791 |
# endif
|
williamr@4
|
792 |
|
williamr@4
|
793 |
template <class _ForwardIter, class _Predicate>
|
williamr@4
|
794 |
_ForwardIter partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred) {
|
williamr@4
|
795 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
796 |
return __partition(__first, __last, __pred, _STLP_ITERATOR_CATEGORY(__first, _ForwardIter));
|
williamr@4
|
797 |
}
|
williamr@4
|
798 |
|
williamr@4
|
799 |
/*
|
williamr@4
|
800 |
template <class _ForwardIter, class _Predicate, class _Distance>
|
williamr@4
|
801 |
_ForwardIter __inplace_stable_partition(_ForwardIter __first,
|
williamr@4
|
802 |
_ForwardIter __last,
|
williamr@4
|
803 |
_Predicate __pred, _Distance __len) {
|
williamr@4
|
804 |
if (__len == 1)
|
williamr@4
|
805 |
return __pred(*__first) ? __last : __first;
|
williamr@4
|
806 |
_ForwardIter __middle = __first;
|
williamr@4
|
807 |
advance(__middle, __len / 2);
|
williamr@4
|
808 |
return rotate(__inplace_stable_partition(__first, __middle, __pred,
|
williamr@4
|
809 |
__len / 2),
|
williamr@4
|
810 |
__middle,
|
williamr@4
|
811 |
__inplace_stable_partition(__middle, __last, __pred,
|
williamr@4
|
812 |
__len - __len / 2));
|
williamr@4
|
813 |
}
|
williamr@4
|
814 |
|
williamr@4
|
815 |
|
williamr@4
|
816 |
template <class _ForwardIter, class _Pointer, class _Predicate,
|
williamr@4
|
817 |
class _Distance>
|
williamr@4
|
818 |
_ForwardIter __stable_partition_adaptive(_ForwardIter __first,
|
williamr@4
|
819 |
_ForwardIter __last,
|
williamr@4
|
820 |
_Predicate __pred, _Distance __len,
|
williamr@4
|
821 |
_Pointer __buffer,
|
williamr@4
|
822 |
_Distance __buffer_size)
|
williamr@4
|
823 |
{
|
williamr@4
|
824 |
if (__len <= __buffer_size) {
|
williamr@4
|
825 |
_ForwardIter __result1 = __first;
|
williamr@4
|
826 |
_Pointer __result2 = __buffer;
|
williamr@4
|
827 |
for ( ; __first != __last ; ++__first)
|
williamr@4
|
828 |
if (__pred(*__first)) {
|
williamr@4
|
829 |
*__result1 = *__first;
|
williamr@4
|
830 |
++__result1;
|
williamr@4
|
831 |
}
|
williamr@4
|
832 |
else {
|
williamr@4
|
833 |
*__result2 = *__first;
|
williamr@4
|
834 |
++__result2;
|
williamr@4
|
835 |
}
|
williamr@4
|
836 |
copy(__buffer, __result2, __result1);
|
williamr@4
|
837 |
return __result1;
|
williamr@4
|
838 |
}
|
williamr@4
|
839 |
else {
|
williamr@4
|
840 |
_ForwardIter __middle = __first;
|
williamr@4
|
841 |
advance(__middle, __len / 2);
|
williamr@4
|
842 |
return rotate(__stable_partition_adaptive(
|
williamr@4
|
843 |
__first, __middle, __pred,
|
williamr@4
|
844 |
_Distance(__len / 2), __buffer, __buffer_size),
|
williamr@4
|
845 |
__middle,
|
williamr@4
|
846 |
__stable_partition_adaptive(
|
williamr@4
|
847 |
__middle, __last, __pred,
|
williamr@4
|
848 |
_Distance(__len - __len / 2), __buffer, __buffer_size));
|
williamr@4
|
849 |
}
|
williamr@4
|
850 |
}
|
williamr@4
|
851 |
*/ //bug fix
|
williamr@4
|
852 |
template <class _ForwardIter, class _Predicate, class _Tp, class _Distance>
|
williamr@4
|
853 |
inline _ForwardIter
|
williamr@4
|
854 |
__stable_partition_aux(_ForwardIter __first, _ForwardIter __last,
|
williamr@4
|
855 |
_Predicate __pred, _Tp*, _Distance*)
|
williamr@4
|
856 |
{
|
williamr@4
|
857 |
typedef _Temporary_buffer<_ForwardIter, _Tp> _TmpBuf;
|
williamr@4
|
858 |
_Temporary_buffer<_ForwardIter, _Tp> __buf(__first, __last);
|
williamr@4
|
859 |
_STLP_PUSH_STACK_ITEM(_TmpBuf, &__buf);
|
williamr@4
|
860 |
|
williamr@4
|
861 |
_STLP_MPWFIX_TRY //*TY 06/01/2000 - they forget to call dtor for _Temporary_buffer if no try/catch block is present
|
williamr@4
|
862 |
return (__buf.size() > 0) ?
|
williamr@4
|
863 |
__stable_partition_adaptive(__first, __last, __pred,
|
williamr@4
|
864 |
_Distance(__buf.requested_size()),
|
williamr@4
|
865 |
__buf.begin(), _Distance(__buf.size())) :
|
williamr@4
|
866 |
__inplace_stable_partition(__first, __last, __pred,
|
williamr@4
|
867 |
_Distance(__buf.requested_size()));
|
williamr@4
|
868 |
_STLP_MPWFIX_CATCH //*TY 06/01/2000 - they forget to call dtor for _Temporary_buffer if no try/catch block is present
|
williamr@4
|
869 |
}
|
williamr@4
|
870 |
/*
|
williamr@4
|
871 |
template <class _ForwardIter, class _Predicate>
|
williamr@4
|
872 |
_ForwardIter
|
williamr@4
|
873 |
stable_partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred) {
|
williamr@4
|
874 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
875 |
if (__first == __last)
|
williamr@4
|
876 |
return __first;
|
williamr@4
|
877 |
else
|
williamr@4
|
878 |
return __stable_partition_aux(__first, __last, __pred,
|
williamr@4
|
879 |
_STLP_VALUE_TYPE(__first, _ForwardIter),
|
williamr@4
|
880 |
_STLP_DISTANCE_TYPE(__first, _ForwardIter));
|
williamr@4
|
881 |
}
|
williamr@4
|
882 |
*/ //bug fix
|
williamr@4
|
883 |
template <class _RandomAccessIter, class _Tp, class _Compare>
|
williamr@4
|
884 |
_RandomAccessIter __unguarded_partition(_RandomAccessIter __first,
|
williamr@4
|
885 |
_RandomAccessIter __last,
|
williamr@4
|
886 |
_Tp __pivot, _Compare __comp)
|
williamr@4
|
887 |
{
|
williamr@4
|
888 |
_STLP_PUSH_STACK_ITEM(_Tp, &__pivot)
|
williamr@4
|
889 |
while (true) {
|
williamr@4
|
890 |
while (__comp(*__first, __pivot))
|
williamr@4
|
891 |
++__first;
|
williamr@4
|
892 |
--__last;
|
williamr@4
|
893 |
while (__comp(__pivot, *__last))
|
williamr@4
|
894 |
--__last;
|
williamr@4
|
895 |
if (!(__first < __last))
|
williamr@4
|
896 |
return __first;
|
williamr@4
|
897 |
iter_swap(__first, __last);
|
williamr@4
|
898 |
++__first;
|
williamr@4
|
899 |
}
|
williamr@4
|
900 |
}
|
williamr@4
|
901 |
|
williamr@4
|
902 |
// sort() and its auxiliary functions.
|
williamr@4
|
903 |
|
williamr@4
|
904 |
# define __stl_threshold 16
|
williamr@4
|
905 |
|
williamr@4
|
906 |
template <class _RandomAccessIter, class _Tp, class _Compare>
|
williamr@4
|
907 |
void __unguarded_linear_insert(_RandomAccessIter __last, _Tp __val,
|
williamr@4
|
908 |
_Compare __comp) {
|
williamr@4
|
909 |
_STLP_PUSH_STACK_ITEM(_Tp, &__val)
|
williamr@4
|
910 |
_RandomAccessIter __next = __last;
|
williamr@4
|
911 |
--__next;
|
williamr@4
|
912 |
while (__comp(__val, *__next)) {
|
williamr@4
|
913 |
*__last = *__next;
|
williamr@4
|
914 |
__last = __next;
|
williamr@4
|
915 |
--__next;
|
williamr@4
|
916 |
}
|
williamr@4
|
917 |
*__last = __val;
|
williamr@4
|
918 |
}
|
williamr@4
|
919 |
|
williamr@4
|
920 |
template <class _RandomAccessIter, class _Tp, class _Compare>
|
williamr@4
|
921 |
inline void __linear_insert(_RandomAccessIter __first,
|
williamr@4
|
922 |
_RandomAccessIter __last, _Tp __val, _Compare __comp) {
|
williamr@4
|
923 |
_STLP_PUSH_STACK_ITEM(_Tp, &__val)
|
williamr@4
|
924 |
if (__comp(__val, *__first)) {
|
williamr@4
|
925 |
copy_backward(__first, __last, __last + 1);
|
williamr@4
|
926 |
*__first = __val;
|
williamr@4
|
927 |
}
|
williamr@4
|
928 |
else
|
williamr@4
|
929 |
__unguarded_linear_insert(__last, __val, __comp);
|
williamr@4
|
930 |
}
|
williamr@4
|
931 |
|
williamr@4
|
932 |
template <class _RandomAccessIter, class _Compare>
|
williamr@4
|
933 |
void __insertion_sort(_RandomAccessIter __first,
|
williamr@4
|
934 |
_RandomAccessIter __last, _Compare __comp) {
|
williamr@4
|
935 |
if (__first == __last) return;
|
williamr@4
|
936 |
for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i)
|
williamr@4
|
937 |
__linear_insert(__first, __i, *__i, __comp); //*TY 12/26/1998 - supply *__i as __val
|
williamr@4
|
938 |
}
|
williamr@4
|
939 |
|
williamr@4
|
940 |
template <class _RandomAccessIter, class _Tp, class _Compare>
|
williamr@4
|
941 |
void __unguarded_insertion_sort_aux(_RandomAccessIter __first,
|
williamr@4
|
942 |
_RandomAccessIter __last,
|
williamr@4
|
943 |
_Tp*, _Compare __comp) {
|
williamr@4
|
944 |
for (_RandomAccessIter __i = __first; __i != __last; ++__i)
|
williamr@4
|
945 |
__unguarded_linear_insert(__i, _Tp(*__i), __comp);
|
williamr@4
|
946 |
}
|
williamr@4
|
947 |
|
williamr@4
|
948 |
template <class _RandomAccessIter, class _Compare>
|
williamr@4
|
949 |
inline void __unguarded_insertion_sort(_RandomAccessIter __first,
|
williamr@4
|
950 |
_RandomAccessIter __last,
|
williamr@4
|
951 |
_Compare __comp) {
|
williamr@4
|
952 |
__unguarded_insertion_sort_aux(__first, __last, _STLP_VALUE_TYPE(__first, _RandomAccessIter), __comp);
|
williamr@4
|
953 |
}
|
williamr@4
|
954 |
|
williamr@4
|
955 |
template <class _RandomAccessIter, class _Compare>
|
williamr@4
|
956 |
void __final_insertion_sort(_RandomAccessIter __first,
|
williamr@4
|
957 |
_RandomAccessIter __last, _Compare __comp) {
|
williamr@4
|
958 |
if (__last - __first > __stl_threshold) {
|
williamr@4
|
959 |
__insertion_sort(__first, __first + __stl_threshold, __comp);
|
williamr@4
|
960 |
__unguarded_insertion_sort(__first + __stl_threshold, __last, __comp);
|
williamr@4
|
961 |
}
|
williamr@4
|
962 |
else
|
williamr@4
|
963 |
__insertion_sort(__first, __last, __comp);
|
williamr@4
|
964 |
}
|
williamr@4
|
965 |
|
williamr@4
|
966 |
template <class _RandomAccessIter, class _Tp, class _Size, class _Compare>
|
williamr@4
|
967 |
void __introsort_loop(_RandomAccessIter __first,
|
williamr@4
|
968 |
_RandomAccessIter __last, _Tp*,
|
williamr@4
|
969 |
_Size __depth_limit, _Compare __comp)
|
williamr@4
|
970 |
{
|
williamr@4
|
971 |
while (__last - __first > __stl_threshold) {
|
williamr@4
|
972 |
if (__depth_limit == 0) {
|
williamr@4
|
973 |
partial_sort(__first, __last, __last, __comp);
|
williamr@4
|
974 |
return;
|
williamr@4
|
975 |
}
|
williamr@4
|
976 |
--__depth_limit;
|
williamr@4
|
977 |
_RandomAccessIter __cut =
|
williamr@4
|
978 |
__unguarded_partition(__first, __last,
|
williamr@4
|
979 |
_Tp(__median(*__first,
|
williamr@4
|
980 |
*(__first + (__last - __first)/2),
|
williamr@4
|
981 |
*(__last - 1), __comp)),
|
williamr@4
|
982 |
__comp);
|
williamr@4
|
983 |
__introsort_loop(__cut, __last, (_Tp*) 0, __depth_limit, __comp);
|
williamr@4
|
984 |
__last = __cut;
|
williamr@4
|
985 |
}
|
williamr@4
|
986 |
}
|
williamr@4
|
987 |
|
williamr@4
|
988 |
template <class _RandomAccessIter>
|
williamr@4
|
989 |
void sort(_RandomAccessIter __first, _RandomAccessIter __last) {
|
williamr@4
|
990 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
991 |
if (__first != __last) {
|
williamr@4
|
992 |
__introsort_loop(__first, __last,
|
williamr@4
|
993 |
_STLP_VALUE_TYPE(__first, _RandomAccessIter),
|
williamr@4
|
994 |
__lg(__last - __first) * 2, __less(_STLP_VALUE_TYPE(__first, _RandomAccessIter)) );
|
williamr@4
|
995 |
__final_insertion_sort(__first, __last, __less(_STLP_VALUE_TYPE(__first, _RandomAccessIter)));
|
williamr@4
|
996 |
}
|
williamr@4
|
997 |
}
|
williamr@4
|
998 |
|
williamr@4
|
999 |
template <class _RandomAccessIter, class _Compare>
|
williamr@4
|
1000 |
void sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp) {
|
williamr@4
|
1001 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1002 |
if (__first != __last) {
|
williamr@4
|
1003 |
__introsort_loop(__first, __last,
|
williamr@4
|
1004 |
_STLP_VALUE_TYPE(__first, _RandomAccessIter),
|
williamr@4
|
1005 |
__lg(__last - __first) * 2,
|
williamr@4
|
1006 |
__comp);
|
williamr@4
|
1007 |
__final_insertion_sort(__first, __last, __comp);
|
williamr@4
|
1008 |
}
|
williamr@4
|
1009 |
}
|
williamr@4
|
1010 |
|
williamr@4
|
1011 |
// stable_sort() and its auxiliary functions.
|
williamr@4
|
1012 |
|
williamr@4
|
1013 |
template <class _RandomAccessIter, class _Compare>
|
williamr@4
|
1014 |
void __inplace_stable_sort(_RandomAccessIter __first,
|
williamr@4
|
1015 |
_RandomAccessIter __last, _Compare __comp) {
|
williamr@4
|
1016 |
if (__last - __first < 15) {
|
williamr@4
|
1017 |
__insertion_sort(__first, __last, __comp);
|
williamr@4
|
1018 |
return;
|
williamr@4
|
1019 |
}
|
williamr@4
|
1020 |
_RandomAccessIter __middle = __first + (__last - __first) / 2;
|
williamr@4
|
1021 |
__inplace_stable_sort(__first, __middle, __comp);
|
williamr@4
|
1022 |
__inplace_stable_sort(__middle, __last, __comp);
|
williamr@4
|
1023 |
__merge_without_buffer(__first, __middle, __last,
|
williamr@4
|
1024 |
__middle - __first,
|
williamr@4
|
1025 |
__last - __middle,
|
williamr@4
|
1026 |
__comp);
|
williamr@4
|
1027 |
}
|
williamr@4
|
1028 |
|
williamr@4
|
1029 |
template <class _RandomAccessIter1, class _RandomAccessIter2,
|
williamr@4
|
1030 |
class _Distance, class _Compare>
|
williamr@4
|
1031 |
void __merge_sort_loop(_RandomAccessIter1 __first,
|
williamr@4
|
1032 |
_RandomAccessIter1 __last,
|
williamr@4
|
1033 |
_RandomAccessIter2 __result, _Distance __step_size,
|
williamr@4
|
1034 |
_Compare __comp) {
|
williamr@4
|
1035 |
_Distance __two_step = 2 * __step_size;
|
williamr@4
|
1036 |
|
williamr@4
|
1037 |
while (__last - __first >= __two_step) {
|
williamr@4
|
1038 |
__result = merge(__first, __first + __step_size,
|
williamr@4
|
1039 |
__first + __step_size, __first + __two_step,
|
williamr@4
|
1040 |
__result,
|
williamr@4
|
1041 |
__comp);
|
williamr@4
|
1042 |
__first += __two_step;
|
williamr@4
|
1043 |
}
|
williamr@4
|
1044 |
__step_size = (min) (_Distance(__last - __first), __step_size);
|
williamr@4
|
1045 |
|
williamr@4
|
1046 |
merge(__first, __first + __step_size,
|
williamr@4
|
1047 |
__first + __step_size, __last,
|
williamr@4
|
1048 |
__result,
|
williamr@4
|
1049 |
__comp);
|
williamr@4
|
1050 |
}
|
williamr@4
|
1051 |
|
williamr@4
|
1052 |
const int __stl_chunk_size = 7;
|
williamr@4
|
1053 |
|
williamr@4
|
1054 |
template <class _RandomAccessIter, class _Distance, class _Compare>
|
williamr@4
|
1055 |
void __chunk_insertion_sort(_RandomAccessIter __first,
|
williamr@4
|
1056 |
_RandomAccessIter __last,
|
williamr@4
|
1057 |
_Distance __chunk_size, _Compare __comp)
|
williamr@4
|
1058 |
{
|
williamr@4
|
1059 |
while (__last - __first >= __chunk_size) {
|
williamr@4
|
1060 |
__insertion_sort(__first, __first + __chunk_size, __comp);
|
williamr@4
|
1061 |
__first += __chunk_size;
|
williamr@4
|
1062 |
}
|
williamr@4
|
1063 |
__insertion_sort(__first, __last, __comp);
|
williamr@4
|
1064 |
}
|
williamr@4
|
1065 |
|
williamr@4
|
1066 |
template <class _RandomAccessIter, class _Pointer, class _Distance,
|
williamr@4
|
1067 |
class _Compare>
|
williamr@4
|
1068 |
void __merge_sort_with_buffer(_RandomAccessIter __first,
|
williamr@4
|
1069 |
_RandomAccessIter __last, _Pointer __buffer,
|
williamr@4
|
1070 |
_Distance*, _Compare __comp) {
|
williamr@4
|
1071 |
_Distance __len = __last - __first;
|
williamr@4
|
1072 |
_Pointer __buffer_last = __buffer + __len;
|
williamr@4
|
1073 |
|
williamr@4
|
1074 |
_Distance __step_size = __stl_chunk_size;
|
williamr@4
|
1075 |
__chunk_insertion_sort(__first, __last, __step_size, __comp);
|
williamr@4
|
1076 |
|
williamr@4
|
1077 |
while (__step_size < __len) {
|
williamr@4
|
1078 |
__merge_sort_loop(__first, __last, __buffer, __step_size, __comp);
|
williamr@4
|
1079 |
__step_size *= 2;
|
williamr@4
|
1080 |
__merge_sort_loop(__buffer, __buffer_last, __first, __step_size, __comp);
|
williamr@4
|
1081 |
__step_size *= 2;
|
williamr@4
|
1082 |
}
|
williamr@4
|
1083 |
}
|
williamr@4
|
1084 |
|
williamr@4
|
1085 |
template <class _BidirectionalIter1, class _BidirectionalIter2,
|
williamr@4
|
1086 |
class _Distance>
|
williamr@4
|
1087 |
_BidirectionalIter1 __rotate_adaptive(_BidirectionalIter1 __first,
|
williamr@4
|
1088 |
_BidirectionalIter1 __middle,
|
williamr@4
|
1089 |
_BidirectionalIter1 __last,
|
williamr@4
|
1090 |
_Distance __len1, _Distance __len2,
|
williamr@4
|
1091 |
_BidirectionalIter2 __buffer,
|
williamr@4
|
1092 |
_Distance __buffer_size) {
|
williamr@4
|
1093 |
if (__len1 > __len2 && __len2 <= __buffer_size) {
|
williamr@4
|
1094 |
_BidirectionalIter2 __buffer_end = copy(__middle, __last, __buffer);
|
williamr@4
|
1095 |
copy_backward(__first, __middle, __last);
|
williamr@4
|
1096 |
return copy(__buffer, __buffer_end, __first);
|
williamr@4
|
1097 |
}
|
williamr@4
|
1098 |
else if (__len1 <= __buffer_size) {
|
williamr@4
|
1099 |
_BidirectionalIter2 __buffer_end = copy(__first, __middle, __buffer);
|
williamr@4
|
1100 |
copy(__middle, __last, __first);
|
williamr@4
|
1101 |
return copy_backward(__buffer, __buffer_end, __last);
|
williamr@4
|
1102 |
}
|
williamr@4
|
1103 |
else
|
williamr@4
|
1104 |
return rotate(__first, __middle, __last);
|
williamr@4
|
1105 |
}
|
williamr@4
|
1106 |
|
williamr@4
|
1107 |
template <class _BidirectionalIter, class _Distance, class _Pointer,
|
williamr@4
|
1108 |
class _Compare>
|
williamr@4
|
1109 |
void __merge_adaptive(_BidirectionalIter __first,
|
williamr@4
|
1110 |
_BidirectionalIter __middle,
|
williamr@4
|
1111 |
_BidirectionalIter __last,
|
williamr@4
|
1112 |
_Distance __len1, _Distance __len2,
|
williamr@4
|
1113 |
_Pointer __buffer, _Distance __buffer_size,
|
williamr@4
|
1114 |
_Compare __comp) {
|
williamr@4
|
1115 |
if (__len1 <= __len2 && __len1 <= __buffer_size) {
|
williamr@4
|
1116 |
_Pointer __buffer_end = copy(__first, __middle, __buffer);
|
williamr@4
|
1117 |
merge(__buffer, __buffer_end, __middle, __last, __first, __comp);
|
williamr@4
|
1118 |
}
|
williamr@4
|
1119 |
else if (__len2 <= __buffer_size) {
|
williamr@4
|
1120 |
_Pointer __buffer_end = copy(__middle, __last, __buffer);
|
williamr@4
|
1121 |
__merge_backward(__first, __middle, __buffer, __buffer_end, __last,
|
williamr@4
|
1122 |
__comp);
|
williamr@4
|
1123 |
}
|
williamr@4
|
1124 |
else {
|
williamr@4
|
1125 |
_BidirectionalIter __first_cut = __first;
|
williamr@4
|
1126 |
_BidirectionalIter __second_cut = __middle;
|
williamr@4
|
1127 |
_Distance __len11 = 0;
|
williamr@4
|
1128 |
_Distance __len22 = 0;
|
williamr@4
|
1129 |
if (__len1 > __len2) {
|
williamr@4
|
1130 |
__len11 = __len1 / 2;
|
williamr@4
|
1131 |
advance(__first_cut, __len11);
|
williamr@4
|
1132 |
__second_cut = lower_bound(__middle, __last, *__first_cut, __comp);
|
williamr@4
|
1133 |
__len22 += distance(__middle, __second_cut);
|
williamr@4
|
1134 |
}
|
williamr@4
|
1135 |
else {
|
williamr@4
|
1136 |
__len22 = __len2 / 2;
|
williamr@4
|
1137 |
advance(__second_cut, __len22);
|
williamr@4
|
1138 |
__first_cut = upper_bound(__first, __middle, *__second_cut, __comp);
|
williamr@4
|
1139 |
__len11 += distance(__first, __first_cut);
|
williamr@4
|
1140 |
}
|
williamr@4
|
1141 |
_BidirectionalIter __new_middle =
|
williamr@4
|
1142 |
__rotate_adaptive(__first_cut, __middle, __second_cut, __len1 - __len11,
|
williamr@4
|
1143 |
__len22, __buffer, __buffer_size);
|
williamr@4
|
1144 |
__merge_adaptive(__first, __first_cut, __new_middle, __len11,
|
williamr@4
|
1145 |
__len22, __buffer, __buffer_size, __comp);
|
williamr@4
|
1146 |
__merge_adaptive(__new_middle, __second_cut, __last, __len1 - __len11,
|
williamr@4
|
1147 |
__len2 - __len22, __buffer, __buffer_size, __comp);
|
williamr@4
|
1148 |
}
|
williamr@4
|
1149 |
}
|
williamr@4
|
1150 |
|
williamr@4
|
1151 |
template <class _RandomAccessIter, class _Pointer, class _Distance,
|
williamr@4
|
1152 |
class _Compare>
|
williamr@4
|
1153 |
void __stable_sort_adaptive(_RandomAccessIter __first,
|
williamr@4
|
1154 |
_RandomAccessIter __last, _Pointer __buffer,
|
williamr@4
|
1155 |
_Distance __buffer_size, _Compare __comp) {
|
williamr@4
|
1156 |
_Distance __len = (__last - __first + 1) / 2;
|
williamr@4
|
1157 |
_RandomAccessIter __middle = __first + __len;
|
williamr@4
|
1158 |
if (__len > __buffer_size) {
|
williamr@4
|
1159 |
__stable_sort_adaptive(__first, __middle, __buffer, __buffer_size,
|
williamr@4
|
1160 |
__comp);
|
williamr@4
|
1161 |
__stable_sort_adaptive(__middle, __last, __buffer, __buffer_size,
|
williamr@4
|
1162 |
__comp);
|
williamr@4
|
1163 |
}
|
williamr@4
|
1164 |
else {
|
williamr@4
|
1165 |
__merge_sort_with_buffer(__first, __middle, __buffer, (_Distance*)0,
|
williamr@4
|
1166 |
__comp);
|
williamr@4
|
1167 |
__merge_sort_with_buffer(__middle, __last, __buffer, (_Distance*)0,
|
williamr@4
|
1168 |
__comp);
|
williamr@4
|
1169 |
}
|
williamr@4
|
1170 |
__merge_adaptive(__first, __middle, __last, _Distance(__middle - __first),
|
williamr@4
|
1171 |
_Distance(__last - __middle), __buffer, __buffer_size,
|
williamr@4
|
1172 |
__comp);
|
williamr@4
|
1173 |
}
|
williamr@4
|
1174 |
|
williamr@4
|
1175 |
template <class _RandomAccessIter, class _Tp, class _Distance, class _Compare>
|
williamr@4
|
1176 |
void __stable_sort_aux(_RandomAccessIter __first,
|
williamr@4
|
1177 |
_RandomAccessIter __last, _Tp*, _Distance*,
|
williamr@4
|
1178 |
_Compare __comp) {
|
williamr@4
|
1179 |
|
williamr@4
|
1180 |
typedef _Temporary_buffer<_RandomAccessIter, _Tp> _TmpBuf;
|
williamr@4
|
1181 |
_TmpBuf __buf(__first, __last);
|
williamr@4
|
1182 |
_STLP_PUSH_STACK_ITEM(_TmpBuf, &__buf);
|
williamr@4
|
1183 |
|
williamr@4
|
1184 |
if (__buf.begin() == 0)
|
williamr@4
|
1185 |
__inplace_stable_sort(__first, __last, __comp);
|
williamr@4
|
1186 |
else
|
williamr@4
|
1187 |
__stable_sort_adaptive(__first, __last, __buf.begin(),
|
williamr@4
|
1188 |
_Distance(__buf.size()),
|
williamr@4
|
1189 |
__comp);
|
williamr@4
|
1190 |
}
|
williamr@4
|
1191 |
|
williamr@4
|
1192 |
template <class _RandomAccessIter>
|
williamr@4
|
1193 |
void stable_sort(_RandomAccessIter __first,
|
williamr@4
|
1194 |
_RandomAccessIter __last) {
|
williamr@4
|
1195 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1196 |
__stable_sort_aux(__first, __last,
|
williamr@4
|
1197 |
_STLP_VALUE_TYPE(__first, _RandomAccessIter),
|
williamr@4
|
1198 |
_STLP_DISTANCE_TYPE(__first, _RandomAccessIter),
|
williamr@4
|
1199 |
__less(_STLP_VALUE_TYPE(__first, _RandomAccessIter)));
|
williamr@4
|
1200 |
}
|
williamr@4
|
1201 |
|
williamr@4
|
1202 |
template <class _RandomAccessIter, class _Compare>
|
williamr@4
|
1203 |
void stable_sort(_RandomAccessIter __first,
|
williamr@4
|
1204 |
_RandomAccessIter __last, _Compare __comp) {
|
williamr@4
|
1205 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1206 |
__stable_sort_aux(__first, __last,
|
williamr@4
|
1207 |
_STLP_VALUE_TYPE(__first, _RandomAccessIter),
|
williamr@4
|
1208 |
_STLP_DISTANCE_TYPE(__first, _RandomAccessIter),
|
williamr@4
|
1209 |
__comp);
|
williamr@4
|
1210 |
}
|
williamr@4
|
1211 |
|
williamr@4
|
1212 |
// partial_sort, partial_sort_copy, and auxiliary functions.
|
williamr@4
|
1213 |
|
williamr@4
|
1214 |
template <class _RandomAccessIter, class _Tp, class _Compare>
|
williamr@4
|
1215 |
void __partial_sort(_RandomAccessIter __first, _RandomAccessIter __middle,
|
williamr@4
|
1216 |
_RandomAccessIter __last, _Tp*, _Compare __comp) {
|
williamr@4
|
1217 |
make_heap(__first, __middle, __comp);
|
williamr@4
|
1218 |
for (_RandomAccessIter __i = __middle; __i < __last; ++__i)
|
williamr@4
|
1219 |
if (__comp(*__i, *__first))
|
williamr@4
|
1220 |
__pop_heap(__first, __middle, __i, _Tp(*__i), __comp,
|
williamr@4
|
1221 |
_STLP_DISTANCE_TYPE(__first, _RandomAccessIter));
|
williamr@4
|
1222 |
sort_heap(__first, __middle, __comp);
|
williamr@4
|
1223 |
}
|
williamr@4
|
1224 |
|
williamr@4
|
1225 |
|
williamr@4
|
1226 |
template <class _RandomAccessIter>
|
williamr@4
|
1227 |
void
|
williamr@4
|
1228 |
partial_sort(_RandomAccessIter __first,_RandomAccessIter __middle, _RandomAccessIter __last) {
|
williamr@4
|
1229 |
_STLP_DEBUG_CHECK(__check_range(__first, __middle))
|
williamr@4
|
1230 |
_STLP_DEBUG_CHECK(__check_range(__middle, __last))
|
williamr@4
|
1231 |
__partial_sort(__first, __middle, __last, _STLP_VALUE_TYPE(__first, _RandomAccessIter),
|
williamr@4
|
1232 |
__less(_STLP_VALUE_TYPE(__first, _RandomAccessIter)));
|
williamr@4
|
1233 |
}
|
williamr@4
|
1234 |
|
williamr@4
|
1235 |
template <class _RandomAccessIter, class _Compare>
|
williamr@4
|
1236 |
void partial_sort(_RandomAccessIter __first,_RandomAccessIter __middle,
|
williamr@4
|
1237 |
_RandomAccessIter __last, _Compare __comp) {
|
williamr@4
|
1238 |
_STLP_DEBUG_CHECK(__check_range(__first, __middle))
|
williamr@4
|
1239 |
_STLP_DEBUG_CHECK(__check_range(__middle, __last))
|
williamr@4
|
1240 |
__partial_sort(__first, __middle, __last, _STLP_VALUE_TYPE(__first, _RandomAccessIter), __comp);
|
williamr@4
|
1241 |
}
|
williamr@4
|
1242 |
|
williamr@4
|
1243 |
template <class _InputIter, class _RandomAccessIter, class _Compare,
|
williamr@4
|
1244 |
class _Distance, class _Tp>
|
williamr@4
|
1245 |
_RandomAccessIter __partial_sort_copy(_InputIter __first,
|
williamr@4
|
1246 |
_InputIter __last,
|
williamr@4
|
1247 |
_RandomAccessIter __result_first,
|
williamr@4
|
1248 |
_RandomAccessIter __result_last,
|
williamr@4
|
1249 |
_Compare __comp, _Distance*, _Tp*) {
|
williamr@4
|
1250 |
if (__result_first == __result_last) return __result_last;
|
williamr@4
|
1251 |
_RandomAccessIter __result_real_last = __result_first;
|
williamr@4
|
1252 |
while(__first != __last && __result_real_last != __result_last) {
|
williamr@4
|
1253 |
*__result_real_last = *__first;
|
williamr@4
|
1254 |
++__result_real_last;
|
williamr@4
|
1255 |
++__first;
|
williamr@4
|
1256 |
}
|
williamr@4
|
1257 |
make_heap(__result_first, __result_real_last, __comp);
|
williamr@4
|
1258 |
while (__first != __last) {
|
williamr@4
|
1259 |
if (__comp(*__first, *__result_first))
|
williamr@4
|
1260 |
__adjust_heap(__result_first, _Distance(0),
|
williamr@4
|
1261 |
_Distance(__result_real_last - __result_first),
|
williamr@4
|
1262 |
_Tp(*__first),
|
williamr@4
|
1263 |
__comp);
|
williamr@4
|
1264 |
++__first;
|
williamr@4
|
1265 |
}
|
williamr@4
|
1266 |
sort_heap(__result_first, __result_real_last, __comp);
|
williamr@4
|
1267 |
return __result_real_last;
|
williamr@4
|
1268 |
}
|
williamr@4
|
1269 |
|
williamr@4
|
1270 |
template <class _InputIter, class _RandomAccessIter>
|
williamr@4
|
1271 |
_RandomAccessIter
|
williamr@4
|
1272 |
partial_sort_copy(_InputIter __first, _InputIter __last,
|
williamr@4
|
1273 |
_RandomAccessIter __result_first, _RandomAccessIter __result_last) {
|
williamr@4
|
1274 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1275 |
_STLP_DEBUG_CHECK(__check_range(__result_first, __result_last))
|
williamr@4
|
1276 |
return __partial_sort_copy(__first, __last, __result_first, __result_last,
|
williamr@4
|
1277 |
__less(_STLP_VALUE_TYPE(__first, _InputIter)),
|
williamr@4
|
1278 |
_STLP_DISTANCE_TYPE(__result_first, _RandomAccessIter),
|
williamr@4
|
1279 |
_STLP_VALUE_TYPE(__first, _InputIter));
|
williamr@4
|
1280 |
}
|
williamr@4
|
1281 |
|
williamr@4
|
1282 |
template <class _InputIter, class _RandomAccessIter, class _Compare>
|
williamr@4
|
1283 |
_RandomAccessIter
|
williamr@4
|
1284 |
partial_sort_copy(_InputIter __first, _InputIter __last,
|
williamr@4
|
1285 |
_RandomAccessIter __result_first,
|
williamr@4
|
1286 |
_RandomAccessIter __result_last, _Compare __comp) {
|
williamr@4
|
1287 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1288 |
_STLP_DEBUG_CHECK(__check_range(__result_first, __result_last))
|
williamr@4
|
1289 |
return __partial_sort_copy(__first, __last, __result_first, __result_last,
|
williamr@4
|
1290 |
__comp,
|
williamr@4
|
1291 |
_STLP_DISTANCE_TYPE(__result_first, _RandomAccessIter),
|
williamr@4
|
1292 |
_STLP_VALUE_TYPE(__first, _InputIter));
|
williamr@4
|
1293 |
}
|
williamr@4
|
1294 |
|
williamr@4
|
1295 |
// nth_element() and its auxiliary functions.
|
williamr@4
|
1296 |
|
williamr@4
|
1297 |
template <class _RandomAccessIter, class _Tp, class _Compare>
|
williamr@4
|
1298 |
void __nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,
|
williamr@4
|
1299 |
_RandomAccessIter __last, _Tp*, _Compare __comp) {
|
williamr@4
|
1300 |
while (__last - __first > 3) {
|
williamr@4
|
1301 |
_RandomAccessIter __cut =
|
williamr@4
|
1302 |
__unguarded_partition(__first, __last,
|
williamr@4
|
1303 |
_Tp(__median(*__first,
|
williamr@4
|
1304 |
*(__first + (__last - __first)/2),
|
williamr@4
|
1305 |
*(__last - 1),
|
williamr@4
|
1306 |
__comp)),
|
williamr@4
|
1307 |
__comp);
|
williamr@4
|
1308 |
if (__cut <= __nth)
|
williamr@4
|
1309 |
__first = __cut;
|
williamr@4
|
1310 |
else
|
williamr@4
|
1311 |
__last = __cut;
|
williamr@4
|
1312 |
}
|
williamr@4
|
1313 |
__insertion_sort(__first, __last, __comp);
|
williamr@4
|
1314 |
}
|
williamr@4
|
1315 |
|
williamr@4
|
1316 |
|
williamr@4
|
1317 |
template <class _RandomAccessIter>
|
williamr@4
|
1318 |
void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,
|
williamr@4
|
1319 |
_RandomAccessIter __last) {
|
williamr@4
|
1320 |
_STLP_DEBUG_CHECK(__check_range(__first, __nth))
|
williamr@4
|
1321 |
_STLP_DEBUG_CHECK(__check_range(__nth, __last))
|
williamr@4
|
1322 |
__nth_element(__first, __nth, __last, _STLP_VALUE_TYPE(__first, _RandomAccessIter),
|
williamr@4
|
1323 |
__less(_STLP_VALUE_TYPE(__first, _RandomAccessIter)));
|
williamr@4
|
1324 |
}
|
williamr@4
|
1325 |
|
williamr@4
|
1326 |
template <class _RandomAccessIter, class _Compare>
|
williamr@4
|
1327 |
void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,
|
williamr@4
|
1328 |
_RandomAccessIter __last, _Compare __comp) {
|
williamr@4
|
1329 |
_STLP_DEBUG_CHECK(__check_range(__first, __nth))
|
williamr@4
|
1330 |
_STLP_DEBUG_CHECK(__check_range(__nth, __last))
|
williamr@4
|
1331 |
__nth_element(__first, __nth, __last, _STLP_VALUE_TYPE(__first, _RandomAccessIter), __comp);
|
williamr@4
|
1332 |
}
|
williamr@4
|
1333 |
|
williamr@4
|
1334 |
// Binary search (lower_bound, upper_bound, equal_range, binary_search).
|
williamr@4
|
1335 |
|
williamr@4
|
1336 |
template <class _ForwardIter, class _Tp, class _Compare, class _Distance>
|
williamr@4
|
1337 |
_ForwardIter __upper_bound(_ForwardIter __first, _ForwardIter __last,
|
williamr@4
|
1338 |
const _Tp& __val, _Compare __comp, _Distance*)
|
williamr@4
|
1339 |
{
|
williamr@4
|
1340 |
_Distance __len = distance(__first, __last);
|
williamr@4
|
1341 |
_Distance __half;
|
williamr@4
|
1342 |
|
williamr@4
|
1343 |
while (__len > 0) {
|
williamr@4
|
1344 |
__half = __len >> 1;
|
williamr@4
|
1345 |
_ForwardIter __middle = __first;
|
williamr@4
|
1346 |
advance(__middle, __half);
|
williamr@4
|
1347 |
if (__comp(__val, *__middle))
|
williamr@4
|
1348 |
__len = __half;
|
williamr@4
|
1349 |
else {
|
williamr@4
|
1350 |
__first = __middle;
|
williamr@4
|
1351 |
++__first;
|
williamr@4
|
1352 |
__len = __len - __half - 1;
|
williamr@4
|
1353 |
}
|
williamr@4
|
1354 |
}
|
williamr@4
|
1355 |
return __first;
|
williamr@4
|
1356 |
}
|
williamr@4
|
1357 |
|
williamr@4
|
1358 |
template <class _ForwardIter, class _Tp, class _Compare, class _Distance>
|
williamr@4
|
1359 |
pair<_ForwardIter, _ForwardIter>
|
williamr@4
|
1360 |
__equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
|
williamr@4
|
1361 |
_Compare __comp, _Distance*)
|
williamr@4
|
1362 |
{
|
williamr@4
|
1363 |
_Distance __len = distance(__first, __last);
|
williamr@4
|
1364 |
_Distance __half;
|
williamr@4
|
1365 |
|
williamr@4
|
1366 |
while (__len > 0) {
|
williamr@4
|
1367 |
__half = __len >> 1;
|
williamr@4
|
1368 |
_ForwardIter __middle = __first;
|
williamr@4
|
1369 |
advance(__middle, __half);
|
williamr@4
|
1370 |
if (__comp(*__middle, __val)) {
|
williamr@4
|
1371 |
__first = __middle;
|
williamr@4
|
1372 |
++__first;
|
williamr@4
|
1373 |
__len = __len - __half - 1;
|
williamr@4
|
1374 |
}
|
williamr@4
|
1375 |
else if (__comp(__val, *__middle))
|
williamr@4
|
1376 |
__len = __half;
|
williamr@4
|
1377 |
else {
|
williamr@4
|
1378 |
_ForwardIter __left = lower_bound(__first, __middle, __val, __comp);
|
williamr@4
|
1379 |
advance(__first, __len);
|
williamr@4
|
1380 |
_ForwardIter __right = upper_bound(++__middle, __first, __val, __comp);
|
williamr@4
|
1381 |
return pair<_ForwardIter, _ForwardIter>(__left, __right);
|
williamr@4
|
1382 |
}
|
williamr@4
|
1383 |
}
|
williamr@4
|
1384 |
return pair<_ForwardIter, _ForwardIter>(__first, __first);
|
williamr@4
|
1385 |
}
|
williamr@4
|
1386 |
|
williamr@4
|
1387 |
template <class _InputIter1, class _InputIter2, class _OutputIter>
|
williamr@4
|
1388 |
_OutputIter merge(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1389 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1390 |
_OutputIter __result) {
|
williamr@4
|
1391 |
_STLP_DEBUG_CHECK(__check_range(__first1, __last1))
|
williamr@4
|
1392 |
_STLP_DEBUG_CHECK(__check_range(__first2, __last2))
|
williamr@4
|
1393 |
while (__first1 != __last1 && __first2 != __last2) {
|
williamr@4
|
1394 |
if (*__first2 < *__first1) {
|
williamr@4
|
1395 |
*__result = *__first2;
|
williamr@4
|
1396 |
++__first2;
|
williamr@4
|
1397 |
}
|
williamr@4
|
1398 |
else {
|
williamr@4
|
1399 |
*__result = *__first1;
|
williamr@4
|
1400 |
++__first1;
|
williamr@4
|
1401 |
}
|
williamr@4
|
1402 |
++__result;
|
williamr@4
|
1403 |
}
|
williamr@4
|
1404 |
return copy(__first2, __last2, copy(__first1, __last1, __result));
|
williamr@4
|
1405 |
}
|
williamr@4
|
1406 |
|
williamr@4
|
1407 |
template <class _InputIter1, class _InputIter2, class _OutputIter,
|
williamr@4
|
1408 |
class _Compare>
|
williamr@4
|
1409 |
_OutputIter merge(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1410 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1411 |
_OutputIter __result, _Compare __comp) {
|
williamr@4
|
1412 |
_STLP_DEBUG_CHECK(__check_range(__first1, __last1))
|
williamr@4
|
1413 |
_STLP_DEBUG_CHECK(__check_range(__first2, __last2))
|
williamr@4
|
1414 |
while (__first1 != __last1 && __first2 != __last2) {
|
williamr@4
|
1415 |
if (__comp(*__first2, *__first1)) {
|
williamr@4
|
1416 |
*__result = *__first2;
|
williamr@4
|
1417 |
++__first2;
|
williamr@4
|
1418 |
}
|
williamr@4
|
1419 |
else {
|
williamr@4
|
1420 |
*__result = *__first1;
|
williamr@4
|
1421 |
++__first1;
|
williamr@4
|
1422 |
}
|
williamr@4
|
1423 |
++__result;
|
williamr@4
|
1424 |
}
|
williamr@4
|
1425 |
return copy(__first2, __last2, copy(__first1, __last1, __result));
|
williamr@4
|
1426 |
}
|
williamr@4
|
1427 |
|
williamr@4
|
1428 |
template <class _BidirectionalIter, class _Distance, class _Compare>
|
williamr@4
|
1429 |
void __merge_without_buffer(_BidirectionalIter __first,
|
williamr@4
|
1430 |
_BidirectionalIter __middle,
|
williamr@4
|
1431 |
_BidirectionalIter __last,
|
williamr@4
|
1432 |
_Distance __len1, _Distance __len2,
|
williamr@4
|
1433 |
_Compare __comp) {
|
williamr@4
|
1434 |
if (__len1 == 0 || __len2 == 0)
|
williamr@4
|
1435 |
return;
|
williamr@4
|
1436 |
if (__len1 + __len2 == 2) {
|
williamr@4
|
1437 |
if (__comp(*__middle, *__first))
|
williamr@4
|
1438 |
iter_swap(__first, __middle);
|
williamr@4
|
1439 |
return;
|
williamr@4
|
1440 |
}
|
williamr@4
|
1441 |
_BidirectionalIter __first_cut = __first;
|
williamr@4
|
1442 |
_BidirectionalIter __second_cut = __middle;
|
williamr@4
|
1443 |
_Distance __len11 = 0;
|
williamr@4
|
1444 |
_Distance __len22 = 0;
|
williamr@4
|
1445 |
if (__len1 > __len2) {
|
williamr@4
|
1446 |
__len11 = __len1 / 2;
|
williamr@4
|
1447 |
advance(__first_cut, __len11);
|
williamr@4
|
1448 |
__second_cut = lower_bound(__middle, __last, *__first_cut, __comp);
|
williamr@4
|
1449 |
__len22 += distance(__middle, __second_cut);
|
williamr@4
|
1450 |
}
|
williamr@4
|
1451 |
else {
|
williamr@4
|
1452 |
__len22 = __len2 / 2;
|
williamr@4
|
1453 |
advance(__second_cut, __len22);
|
williamr@4
|
1454 |
__first_cut = upper_bound(__first, __middle, *__second_cut, __comp);
|
williamr@4
|
1455 |
__len11 +=distance(__first, __first_cut);
|
williamr@4
|
1456 |
}
|
williamr@4
|
1457 |
_BidirectionalIter __new_middle
|
williamr@4
|
1458 |
= rotate(__first_cut, __middle, __second_cut);
|
williamr@4
|
1459 |
__merge_without_buffer(__first, __first_cut, __new_middle, __len11, __len22,
|
williamr@4
|
1460 |
__comp);
|
williamr@4
|
1461 |
__merge_without_buffer(__new_middle, __second_cut, __last, __len1 - __len11,
|
williamr@4
|
1462 |
__len2 - __len22, __comp);
|
williamr@4
|
1463 |
}
|
williamr@4
|
1464 |
|
williamr@4
|
1465 |
template <class _BidirectionalIter1, class _BidirectionalIter2,
|
williamr@4
|
1466 |
class _BidirectionalIter3, class _Compare>
|
williamr@4
|
1467 |
_BidirectionalIter3 __merge_backward(_BidirectionalIter1 __first1,
|
williamr@4
|
1468 |
_BidirectionalIter1 __last1,
|
williamr@4
|
1469 |
_BidirectionalIter2 __first2,
|
williamr@4
|
1470 |
_BidirectionalIter2 __last2,
|
williamr@4
|
1471 |
_BidirectionalIter3 __result,
|
williamr@4
|
1472 |
_Compare __comp) {
|
williamr@4
|
1473 |
if (__first1 == __last1)
|
williamr@4
|
1474 |
return copy_backward(__first2, __last2, __result);
|
williamr@4
|
1475 |
if (__first2 == __last2)
|
williamr@4
|
1476 |
return copy_backward(__first1, __last1, __result);
|
williamr@4
|
1477 |
--__last1;
|
williamr@4
|
1478 |
--__last2;
|
williamr@4
|
1479 |
while (true) {
|
williamr@4
|
1480 |
if (__comp(*__last2, *__last1)) {
|
williamr@4
|
1481 |
*--__result = *__last1;
|
williamr@4
|
1482 |
if (__first1 == __last1)
|
williamr@4
|
1483 |
return copy_backward(__first2, ++__last2, __result);
|
williamr@4
|
1484 |
--__last1;
|
williamr@4
|
1485 |
}
|
williamr@4
|
1486 |
else {
|
williamr@4
|
1487 |
*--__result = *__last2;
|
williamr@4
|
1488 |
if (__first2 == __last2)
|
williamr@4
|
1489 |
return copy_backward(__first1, ++__last1, __result);
|
williamr@4
|
1490 |
--__last2;
|
williamr@4
|
1491 |
}
|
williamr@4
|
1492 |
}
|
williamr@4
|
1493 |
}
|
williamr@4
|
1494 |
|
williamr@4
|
1495 |
template <class _BidirectionalIter, class _Tp,
|
williamr@4
|
1496 |
class _Distance, class _Compare>
|
williamr@4
|
1497 |
inline void __inplace_merge_aux(_BidirectionalIter __first,
|
williamr@4
|
1498 |
_BidirectionalIter __middle,
|
williamr@4
|
1499 |
_BidirectionalIter __last, _Tp*, _Distance*,
|
williamr@4
|
1500 |
_Compare __comp) {
|
williamr@4
|
1501 |
_Distance __len1 = distance(__first, __middle);
|
williamr@4
|
1502 |
_Distance __len2 = distance(__middle, __last);
|
williamr@4
|
1503 |
|
williamr@4
|
1504 |
typedef _Temporary_buffer<_BidirectionalIter, _Tp> _TmpBuf;
|
williamr@4
|
1505 |
_TmpBuf __buf(__first, __last);
|
williamr@4
|
1506 |
_STLP_PUSH_STACK_ITEM(_TmpBuf, &__buf);
|
williamr@4
|
1507 |
|
williamr@4
|
1508 |
if (__buf.begin() == 0)
|
williamr@4
|
1509 |
__merge_without_buffer(__first, __middle, __last, __len1, __len2, __comp);
|
williamr@4
|
1510 |
else
|
williamr@4
|
1511 |
__merge_adaptive(__first, __middle, __last, __len1, __len2,
|
williamr@4
|
1512 |
__buf.begin(), _Distance(__buf.size()),
|
williamr@4
|
1513 |
__comp);
|
williamr@4
|
1514 |
}
|
williamr@4
|
1515 |
|
williamr@4
|
1516 |
template <class _BidirectionalIter>
|
williamr@4
|
1517 |
void inplace_merge(_BidirectionalIter __first,
|
williamr@4
|
1518 |
_BidirectionalIter __middle,
|
williamr@4
|
1519 |
_BidirectionalIter __last) {
|
williamr@4
|
1520 |
_STLP_DEBUG_CHECK(__check_range(__first, __middle))
|
williamr@4
|
1521 |
_STLP_DEBUG_CHECK(__check_range(__middle, __last))
|
williamr@4
|
1522 |
if (__first == __middle || __middle == __last)
|
williamr@4
|
1523 |
return;
|
williamr@4
|
1524 |
__inplace_merge_aux(__first, __middle, __last,
|
williamr@4
|
1525 |
_STLP_VALUE_TYPE(__first, _BidirectionalIter), _STLP_DISTANCE_TYPE(__first, _BidirectionalIter),
|
williamr@4
|
1526 |
__less(_STLP_VALUE_TYPE(__first, _BidirectionalIter)));
|
williamr@4
|
1527 |
}
|
williamr@4
|
1528 |
|
williamr@4
|
1529 |
template <class _BidirectionalIter, class _Compare>
|
williamr@4
|
1530 |
void inplace_merge(_BidirectionalIter __first,
|
williamr@4
|
1531 |
_BidirectionalIter __middle,
|
williamr@4
|
1532 |
_BidirectionalIter __last, _Compare __comp) {
|
williamr@4
|
1533 |
_STLP_DEBUG_CHECK(__check_range(__first, __middle))
|
williamr@4
|
1534 |
_STLP_DEBUG_CHECK(__check_range(__middle, __last))
|
williamr@4
|
1535 |
if (__first == __middle || __middle == __last)
|
williamr@4
|
1536 |
return;
|
williamr@4
|
1537 |
__inplace_merge_aux(__first, __middle, __last,
|
williamr@4
|
1538 |
_STLP_VALUE_TYPE(__first, _BidirectionalIter), _STLP_DISTANCE_TYPE(__first, _BidirectionalIter),
|
williamr@4
|
1539 |
__comp);
|
williamr@4
|
1540 |
}
|
williamr@4
|
1541 |
|
williamr@4
|
1542 |
|
williamr@4
|
1543 |
template <class _InputIter1, class _InputIter2, class _Compare>
|
williamr@4
|
1544 |
bool __includes(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1545 |
_InputIter2 __first2, _InputIter2 __last2, _Compare __comp) {
|
williamr@4
|
1546 |
_STLP_DEBUG_CHECK(__check_range(__first1, __last1))
|
williamr@4
|
1547 |
_STLP_DEBUG_CHECK(__check_range(__first2, __last2))
|
williamr@4
|
1548 |
while (__first1 != __last1 && __first2 != __last2)
|
williamr@4
|
1549 |
if (__comp(*__first2, *__first1))
|
williamr@4
|
1550 |
return false;
|
williamr@4
|
1551 |
else if(__comp(*__first1, *__first2))
|
williamr@4
|
1552 |
++__first1;
|
williamr@4
|
1553 |
else
|
williamr@4
|
1554 |
++__first1, ++__first2;
|
williamr@4
|
1555 |
|
williamr@4
|
1556 |
return __first2 == __last2;
|
williamr@4
|
1557 |
}
|
williamr@4
|
1558 |
|
williamr@4
|
1559 |
template <class _InputIter1, class _InputIter2, class _Compare>
|
williamr@4
|
1560 |
bool includes(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1561 |
_InputIter2 __first2, _InputIter2 __last2, _Compare __comp) {
|
williamr@4
|
1562 |
return __includes(__first1, __last1, __first2, __last2, __comp);
|
williamr@4
|
1563 |
}
|
williamr@4
|
1564 |
|
williamr@4
|
1565 |
template <class _InputIter1, class _InputIter2>
|
williamr@4
|
1566 |
bool includes(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1567 |
_InputIter2 __first2, _InputIter2 __last2) {
|
williamr@4
|
1568 |
return __includes(__first1, __last1, __first2, __last2, __less(_STLP_VALUE_TYPE(__first1, _InputIter1)));
|
williamr@4
|
1569 |
}
|
williamr@4
|
1570 |
|
williamr@4
|
1571 |
template <class _InputIter1, class _InputIter2, class _OutputIter,
|
williamr@4
|
1572 |
class _Compare>
|
williamr@4
|
1573 |
_OutputIter __set_union(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1574 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1575 |
_OutputIter __result, _Compare __comp) {
|
williamr@4
|
1576 |
_STLP_DEBUG_CHECK(__check_range(__first1, __last1))
|
williamr@4
|
1577 |
_STLP_DEBUG_CHECK(__check_range(__first2, __last2))
|
williamr@4
|
1578 |
while (__first1 != __last1 && __first2 != __last2) {
|
williamr@4
|
1579 |
if (__comp(*__first1, *__first2)) {
|
williamr@4
|
1580 |
*__result = *__first1;
|
williamr@4
|
1581 |
++__first1;
|
williamr@4
|
1582 |
}
|
williamr@4
|
1583 |
else if (__comp(*__first2, *__first1)) {
|
williamr@4
|
1584 |
*__result = *__first2;
|
williamr@4
|
1585 |
++__first2;
|
williamr@4
|
1586 |
}
|
williamr@4
|
1587 |
else {
|
williamr@4
|
1588 |
*__result = *__first1;
|
williamr@4
|
1589 |
++__first1;
|
williamr@4
|
1590 |
++__first2;
|
williamr@4
|
1591 |
}
|
williamr@4
|
1592 |
++__result;
|
williamr@4
|
1593 |
}
|
williamr@4
|
1594 |
return copy(__first2, __last2, copy(__first1, __last1, __result));
|
williamr@4
|
1595 |
}
|
williamr@4
|
1596 |
|
williamr@4
|
1597 |
template <class _InputIter1, class _InputIter2, class _OutputIter>
|
williamr@4
|
1598 |
_OutputIter set_union(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1599 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1600 |
_OutputIter __result) {
|
williamr@4
|
1601 |
return __set_union(__first1, __last1, __first2, __last2, __result, __less(_STLP_VALUE_TYPE(__first1, _InputIter1)));
|
williamr@4
|
1602 |
}
|
williamr@4
|
1603 |
|
williamr@4
|
1604 |
template <class _InputIter1, class _InputIter2, class _OutputIter,
|
williamr@4
|
1605 |
class _Compare>
|
williamr@4
|
1606 |
_OutputIter set_union(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1607 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1608 |
_OutputIter __result, _Compare __comp) {
|
williamr@4
|
1609 |
return __set_union(__first1, __last1, __first2, __last2, __result, __comp);
|
williamr@4
|
1610 |
}
|
williamr@4
|
1611 |
|
williamr@4
|
1612 |
template <class _InputIter1, class _InputIter2, class _OutputIter,
|
williamr@4
|
1613 |
class _Compare>
|
williamr@4
|
1614 |
_OutputIter __set_intersection(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1615 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1616 |
_OutputIter __result, _Compare __comp) {
|
williamr@4
|
1617 |
_STLP_DEBUG_CHECK(__check_range(__first1, __last1))
|
williamr@4
|
1618 |
_STLP_DEBUG_CHECK(__check_range(__first2, __last2))
|
williamr@4
|
1619 |
while (__first1 != __last1 && __first2 != __last2)
|
williamr@4
|
1620 |
if (__comp(*__first1, *__first2))
|
williamr@4
|
1621 |
++__first1;
|
williamr@4
|
1622 |
else if (__comp(*__first2, *__first1))
|
williamr@4
|
1623 |
++__first2;
|
williamr@4
|
1624 |
else {
|
williamr@4
|
1625 |
*__result = *__first1;
|
williamr@4
|
1626 |
++__first1;
|
williamr@4
|
1627 |
++__first2;
|
williamr@4
|
1628 |
++__result;
|
williamr@4
|
1629 |
}
|
williamr@4
|
1630 |
return __result;
|
williamr@4
|
1631 |
}
|
williamr@4
|
1632 |
|
williamr@4
|
1633 |
template <class _InputIter1, class _InputIter2, class _OutputIter>
|
williamr@4
|
1634 |
_OutputIter set_intersection(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1635 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1636 |
_OutputIter __result) {
|
williamr@4
|
1637 |
return __set_intersection(__first1, __last1, __first2, __last2, __result, __less(_STLP_VALUE_TYPE(__first1, _InputIter1)));
|
williamr@4
|
1638 |
}
|
williamr@4
|
1639 |
|
williamr@4
|
1640 |
template <class _InputIter1, class _InputIter2, class _OutputIter,
|
williamr@4
|
1641 |
class _Compare>
|
williamr@4
|
1642 |
_OutputIter set_intersection(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1643 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1644 |
_OutputIter __result, _Compare __comp) {
|
williamr@4
|
1645 |
return __set_intersection(__first1, __last1, __first2, __last2, __result, __comp);
|
williamr@4
|
1646 |
}
|
williamr@4
|
1647 |
|
williamr@4
|
1648 |
template <class _InputIter1, class _InputIter2, class _OutputIter,
|
williamr@4
|
1649 |
class _Compare>
|
williamr@4
|
1650 |
_OutputIter __set_difference(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1651 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1652 |
_OutputIter __result, _Compare __comp) {
|
williamr@4
|
1653 |
_STLP_DEBUG_CHECK(__check_range(__first1, __last1))
|
williamr@4
|
1654 |
_STLP_DEBUG_CHECK(__check_range(__first2, __last2))
|
williamr@4
|
1655 |
while (__first1 != __last1 && __first2 != __last2)
|
williamr@4
|
1656 |
if (__comp(*__first1, *__first2)) {
|
williamr@4
|
1657 |
*__result = *__first1;
|
williamr@4
|
1658 |
++__first1;
|
williamr@4
|
1659 |
++__result;
|
williamr@4
|
1660 |
}
|
williamr@4
|
1661 |
else if (__comp(*__first2, *__first1))
|
williamr@4
|
1662 |
++__first2;
|
williamr@4
|
1663 |
else {
|
williamr@4
|
1664 |
++__first1;
|
williamr@4
|
1665 |
++__first2;
|
williamr@4
|
1666 |
}
|
williamr@4
|
1667 |
return copy(__first1, __last1, __result);
|
williamr@4
|
1668 |
}
|
williamr@4
|
1669 |
|
williamr@4
|
1670 |
template <class _InputIter1, class _InputIter2, class _OutputIter>
|
williamr@4
|
1671 |
_OutputIter set_difference(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1672 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1673 |
_OutputIter __result) {
|
williamr@4
|
1674 |
return __set_difference(__first1, __last1, __first2, __last2, __result,
|
williamr@4
|
1675 |
__less(_STLP_VALUE_TYPE(__first1, _InputIter1)));
|
williamr@4
|
1676 |
}
|
williamr@4
|
1677 |
|
williamr@4
|
1678 |
template <class _InputIter1, class _InputIter2, class _OutputIter,
|
williamr@4
|
1679 |
class _Compare>
|
williamr@4
|
1680 |
_OutputIter set_difference(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1681 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1682 |
_OutputIter __result, _Compare __comp) {
|
williamr@4
|
1683 |
return __set_difference(__first1, __last1, __first2, __last2, __result, __comp);
|
williamr@4
|
1684 |
}
|
williamr@4
|
1685 |
|
williamr@4
|
1686 |
template <class _InputIter1, class _InputIter2, class _OutputIter, class _Compare>
|
williamr@4
|
1687 |
_OutputIter
|
williamr@4
|
1688 |
__set_symmetric_difference(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1689 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1690 |
_OutputIter __result, _Compare __comp) {
|
williamr@4
|
1691 |
_STLP_DEBUG_CHECK(__check_range(__first1, __last1))
|
williamr@4
|
1692 |
_STLP_DEBUG_CHECK(__check_range(__first2, __last2))
|
williamr@4
|
1693 |
while (__first1 != __last1 && __first2 != __last2)
|
williamr@4
|
1694 |
if (__comp(*__first1, *__first2)) {
|
williamr@4
|
1695 |
*__result = *__first1;
|
williamr@4
|
1696 |
++__first1;
|
williamr@4
|
1697 |
++__result;
|
williamr@4
|
1698 |
}
|
williamr@4
|
1699 |
else if (__comp(*__first2, *__first1)) {
|
williamr@4
|
1700 |
*__result = *__first2;
|
williamr@4
|
1701 |
++__first2;
|
williamr@4
|
1702 |
++__result;
|
williamr@4
|
1703 |
}
|
williamr@4
|
1704 |
else {
|
williamr@4
|
1705 |
++__first1;
|
williamr@4
|
1706 |
++__first2;
|
williamr@4
|
1707 |
}
|
williamr@4
|
1708 |
return copy(__first2, __last2, copy(__first1, __last1, __result));
|
williamr@4
|
1709 |
}
|
williamr@4
|
1710 |
|
williamr@4
|
1711 |
template <class _InputIter1, class _InputIter2, class _OutputIter>
|
williamr@4
|
1712 |
_OutputIter
|
williamr@4
|
1713 |
set_symmetric_difference(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1714 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1715 |
_OutputIter __result) {
|
williamr@4
|
1716 |
return __set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
|
williamr@4
|
1717 |
__less(_STLP_VALUE_TYPE(__first1, _InputIter1)));
|
williamr@4
|
1718 |
}
|
williamr@4
|
1719 |
|
williamr@4
|
1720 |
template <class _InputIter1, class _InputIter2, class _OutputIter, class _Compare>
|
williamr@4
|
1721 |
_OutputIter
|
williamr@4
|
1722 |
set_symmetric_difference(_InputIter1 __first1, _InputIter1 __last1,
|
williamr@4
|
1723 |
_InputIter2 __first2, _InputIter2 __last2,
|
williamr@4
|
1724 |
_OutputIter __result,
|
williamr@4
|
1725 |
_Compare __comp) {
|
williamr@4
|
1726 |
return __set_symmetric_difference(__first1, __last1, __first2, __last2, __result, __comp);
|
williamr@4
|
1727 |
}
|
williamr@4
|
1728 |
|
williamr@4
|
1729 |
// min_element and max_element, with and without an explicitly supplied
|
williamr@4
|
1730 |
// comparison function.
|
williamr@4
|
1731 |
|
williamr@4
|
1732 |
template <class _ForwardIter, class _Compare>
|
williamr@4
|
1733 |
_ForwardIter max_element(_ForwardIter __first, _ForwardIter __last,
|
williamr@4
|
1734 |
_Compare __comp) {
|
williamr@4
|
1735 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1736 |
if (__first == __last) return __first;
|
williamr@4
|
1737 |
_ForwardIter __result = __first;
|
williamr@4
|
1738 |
while (++__first != __last)
|
williamr@4
|
1739 |
if (__comp(*__result, *__first)) __result = __first;
|
williamr@4
|
1740 |
return __result;
|
williamr@4
|
1741 |
}
|
williamr@4
|
1742 |
|
williamr@4
|
1743 |
template <class _ForwardIter>
|
williamr@4
|
1744 |
_ForwardIter max_element(_ForwardIter __first, _ForwardIter __last) {
|
williamr@4
|
1745 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1746 |
if (__first == __last) return __first;
|
williamr@4
|
1747 |
_ForwardIter __result = __first;
|
williamr@4
|
1748 |
while (++__first != __last)
|
williamr@4
|
1749 |
if (*__result < *__first)
|
williamr@4
|
1750 |
__result = __first;
|
williamr@4
|
1751 |
return __result;
|
williamr@4
|
1752 |
}
|
williamr@4
|
1753 |
|
williamr@4
|
1754 |
template <class _ForwardIter>
|
williamr@4
|
1755 |
_ForwardIter min_element(_ForwardIter __first, _ForwardIter __last) {
|
williamr@4
|
1756 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1757 |
if (__first == __last) return __first;
|
williamr@4
|
1758 |
_ForwardIter __result = __first;
|
williamr@4
|
1759 |
while (++__first != __last)
|
williamr@4
|
1760 |
if (*__first < *__result)
|
williamr@4
|
1761 |
__result = __first;
|
williamr@4
|
1762 |
return __result;
|
williamr@4
|
1763 |
}
|
williamr@4
|
1764 |
|
williamr@4
|
1765 |
template <class _ForwardIter, class _Compare>
|
williamr@4
|
1766 |
_ForwardIter min_element(_ForwardIter __first, _ForwardIter __last,
|
williamr@4
|
1767 |
_Compare __comp) {
|
williamr@4
|
1768 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1769 |
if (__first == __last) return __first;
|
williamr@4
|
1770 |
_ForwardIter __result = __first;
|
williamr@4
|
1771 |
while (++__first != __last)
|
williamr@4
|
1772 |
if (__comp(*__first, *__result)) __result = __first;
|
williamr@4
|
1773 |
return __result;
|
williamr@4
|
1774 |
}
|
williamr@4
|
1775 |
|
williamr@4
|
1776 |
// next_permutation and prev_permutation, with and without an explicitly
|
williamr@4
|
1777 |
// supplied comparison function.
|
williamr@4
|
1778 |
template <class _BidirectionalIter, class _Compare>
|
williamr@4
|
1779 |
bool __next_permutation(_BidirectionalIter __first, _BidirectionalIter __last,
|
williamr@4
|
1780 |
_Compare __comp) {
|
williamr@4
|
1781 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1782 |
if (__first == __last)
|
williamr@4
|
1783 |
return false;
|
williamr@4
|
1784 |
_BidirectionalIter __i = __first;
|
williamr@4
|
1785 |
++__i;
|
williamr@4
|
1786 |
if (__i == __last)
|
williamr@4
|
1787 |
return false;
|
williamr@4
|
1788 |
__i = __last;
|
williamr@4
|
1789 |
--__i;
|
williamr@4
|
1790 |
|
williamr@4
|
1791 |
for(;;) {
|
williamr@4
|
1792 |
_BidirectionalIter __ii = __i;
|
williamr@4
|
1793 |
--__i;
|
williamr@4
|
1794 |
if (__comp(*__i, *__ii)) {
|
williamr@4
|
1795 |
_BidirectionalIter __j = __last;
|
williamr@4
|
1796 |
while (!__comp(*__i, *--__j))
|
williamr@4
|
1797 |
{}
|
williamr@4
|
1798 |
iter_swap(__i, __j);
|
williamr@4
|
1799 |
reverse(__ii, __last);
|
williamr@4
|
1800 |
return true;
|
williamr@4
|
1801 |
}
|
williamr@4
|
1802 |
if (__i == __first) {
|
williamr@4
|
1803 |
reverse(__first, __last);
|
williamr@4
|
1804 |
return false;
|
williamr@4
|
1805 |
}
|
williamr@4
|
1806 |
}
|
williamr@4
|
1807 |
#if defined(_STLP_NEED_UNREACHABLE_RETURN)
|
williamr@4
|
1808 |
return 0;
|
williamr@4
|
1809 |
#endif
|
williamr@4
|
1810 |
}
|
williamr@4
|
1811 |
|
williamr@4
|
1812 |
template <class _BidirectionalIter>
|
williamr@4
|
1813 |
bool next_permutation(_BidirectionalIter __first, _BidirectionalIter __last) {
|
williamr@4
|
1814 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1815 |
return __next_permutation(__first, __last, __less(_STLP_VALUE_TYPE(__first, _BidirectionalIter)));
|
williamr@4
|
1816 |
}
|
williamr@4
|
1817 |
|
williamr@4
|
1818 |
template <class _BidirectionalIter, class _Compare>
|
williamr@4
|
1819 |
bool next_permutation(_BidirectionalIter __first, _BidirectionalIter __last,
|
williamr@4
|
1820 |
_Compare __comp) {
|
williamr@4
|
1821 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1822 |
return __next_permutation(__first, __last, __comp);
|
williamr@4
|
1823 |
}
|
williamr@4
|
1824 |
|
williamr@4
|
1825 |
template <class _BidirectionalIter, class _Compare>
|
williamr@4
|
1826 |
bool __prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last,
|
williamr@4
|
1827 |
_Compare __comp) {
|
williamr@4
|
1828 |
if (__first == __last)
|
williamr@4
|
1829 |
return false;
|
williamr@4
|
1830 |
_BidirectionalIter __i = __first;
|
williamr@4
|
1831 |
++__i;
|
williamr@4
|
1832 |
if (__i == __last)
|
williamr@4
|
1833 |
return false;
|
williamr@4
|
1834 |
__i = __last;
|
williamr@4
|
1835 |
--__i;
|
williamr@4
|
1836 |
|
williamr@4
|
1837 |
for(;;) {
|
williamr@4
|
1838 |
_BidirectionalIter __ii = __i;
|
williamr@4
|
1839 |
--__i;
|
williamr@4
|
1840 |
if (__comp(*__ii, *__i)) {
|
williamr@4
|
1841 |
_BidirectionalIter __j = __last;
|
williamr@4
|
1842 |
while (!__comp(*--__j, *__i))
|
williamr@4
|
1843 |
{}
|
williamr@4
|
1844 |
iter_swap(__i, __j);
|
williamr@4
|
1845 |
reverse(__ii, __last);
|
williamr@4
|
1846 |
return true;
|
williamr@4
|
1847 |
}
|
williamr@4
|
1848 |
if (__i == __first) {
|
williamr@4
|
1849 |
reverse(__first, __last);
|
williamr@4
|
1850 |
return false;
|
williamr@4
|
1851 |
}
|
williamr@4
|
1852 |
}
|
williamr@4
|
1853 |
#if defined(_STLP_NEED_UNREACHABLE_RETURN)
|
williamr@4
|
1854 |
return 0;
|
williamr@4
|
1855 |
#endif
|
williamr@4
|
1856 |
}
|
williamr@4
|
1857 |
|
williamr@4
|
1858 |
template <class _BidirectionalIter>
|
williamr@4
|
1859 |
bool prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last) {
|
williamr@4
|
1860 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1861 |
return __prev_permutation(__first, __last, __less(_STLP_VALUE_TYPE(__first, _BidirectionalIter)));
|
williamr@4
|
1862 |
}
|
williamr@4
|
1863 |
|
williamr@4
|
1864 |
template <class _BidirectionalIter, class _Compare>
|
williamr@4
|
1865 |
bool prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last,
|
williamr@4
|
1866 |
_Compare __comp) {
|
williamr@4
|
1867 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1868 |
return __prev_permutation(__first, __last, __comp);
|
williamr@4
|
1869 |
}
|
williamr@4
|
1870 |
|
williamr@4
|
1871 |
# ifndef _STLP_NO_EXTENSIONS
|
williamr@4
|
1872 |
|
williamr@4
|
1873 |
// is_heap, a predicate testing whether or not a range is
|
williamr@4
|
1874 |
// a heap. This function is an extension, not part of the C++
|
williamr@4
|
1875 |
// standard.
|
williamr@4
|
1876 |
|
williamr@4
|
1877 |
|
williamr@4
|
1878 |
template <class _RandomAccessIter, class _Distance, class _StrictWeakOrdering>
|
williamr@4
|
1879 |
bool __is_heap(_RandomAccessIter __first, _StrictWeakOrdering __comp,
|
williamr@4
|
1880 |
_Distance __n)
|
williamr@4
|
1881 |
{
|
williamr@4
|
1882 |
_Distance __parent = 0;
|
williamr@4
|
1883 |
for (_Distance __child = 1; __child < __n; ++__child) {
|
williamr@4
|
1884 |
if (__comp(__first[__parent], __first[__child]))
|
williamr@4
|
1885 |
return false;
|
williamr@4
|
1886 |
if ((__child & 1) == 0)
|
williamr@4
|
1887 |
++__parent;
|
williamr@4
|
1888 |
}
|
williamr@4
|
1889 |
return true;
|
williamr@4
|
1890 |
}
|
williamr@4
|
1891 |
|
williamr@4
|
1892 |
template <class _RandomAccessIter>
|
williamr@4
|
1893 |
bool is_heap(_RandomAccessIter __first, _RandomAccessIter __last)
|
williamr@4
|
1894 |
{
|
williamr@4
|
1895 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1896 |
return __is_heap(__first, __less(_STLP_VALUE_TYPE(__first, _RandomAccessIter)), __last - __first);
|
williamr@4
|
1897 |
}
|
williamr@4
|
1898 |
|
williamr@4
|
1899 |
template <class _RandomAccessIter, class _StrictWeakOrdering>
|
williamr@4
|
1900 |
bool is_heap(_RandomAccessIter __first, _RandomAccessIter __last,
|
williamr@4
|
1901 |
_StrictWeakOrdering __comp)
|
williamr@4
|
1902 |
{
|
williamr@4
|
1903 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1904 |
return __is_heap(__first, __comp, __last - __first);
|
williamr@4
|
1905 |
}
|
williamr@4
|
1906 |
|
williamr@4
|
1907 |
|
williamr@4
|
1908 |
template <class _ForwardIter, class _StrictWeakOrdering>
|
williamr@4
|
1909 |
bool __is_sorted(_ForwardIter __first, _ForwardIter __last,
|
williamr@4
|
1910 |
_StrictWeakOrdering __comp)
|
williamr@4
|
1911 |
{
|
williamr@4
|
1912 |
_STLP_DEBUG_CHECK(__check_range(__first, __last))
|
williamr@4
|
1913 |
if (__first == __last)
|
williamr@4
|
1914 |
return true;
|
williamr@4
|
1915 |
|
williamr@4
|
1916 |
_ForwardIter __next = __first;
|
williamr@4
|
1917 |
for (++__next; __next != __last; __first = __next, ++__next) {
|
williamr@4
|
1918 |
if (__comp(*__next, *__first))
|
williamr@4
|
1919 |
return false;
|
williamr@4
|
1920 |
}
|
williamr@4
|
1921 |
|
williamr@4
|
1922 |
return true;
|
williamr@4
|
1923 |
}
|
williamr@4
|
1924 |
|
williamr@4
|
1925 |
# endif /* _STLP_NO_EXTENSIONS */
|
williamr@4
|
1926 |
|
williamr@4
|
1927 |
_STLP_END_NAMESPACE
|
williamr@4
|
1928 |
|
williamr@4
|
1929 |
# undef __stl_threshold
|
williamr@4
|
1930 |
|
williamr@4
|
1931 |
#endif /* _STLP_ALGO_C */
|
williamr@4
|
1932 |
// Local Variables:
|
williamr@4
|
1933 |
// mode:C++
|
williamr@4
|
1934 |
// End:
|