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