1/* 2 * 3 * Copyright (c) 1994 4 * Hewlett-Packard Company 5 * 6 * Copyright (c) 1996,1997 7 * Silicon Graphics Computer Systems, Inc. 8 * 9 * Copyright (c) 1997 10 * Moscow Center for SPARC Technology 11 * 12 * Copyright (c) 1999 13 * Boris Fomitchev 14 * 15 * This material is provided "as is", with absolutely no warranty expressed 16 * or implied. Any use is at your own risk. 17 * 18 * Permission to use or copy this software for any purpose is hereby granted 19 * without fee, provided the above notices are retained on all copies. 20 * Permission to modify the code and to distribute modified code is granted, 21 * provided the above notices are retained, and a notice that the code was 22 * modified is included with the above copyright notice. 23 * 24 */ 25 26/* NOTE: This is an internal header file, included by other STL headers. 27 * You should not attempt to use it directly. 28 */ 29 30#ifndef _STLP_INTERNAL_ALGO_H 31#define _STLP_INTERNAL_ALGO_H 32 33#ifndef _STLP_INTERNAL_ALGOBASE_H 34# include <stl/_algobase.h> 35#endif 36 37#ifndef _STLP_INTERNAL_HEAP_H 38# include <stl/_heap.h> 39#endif 40 41#ifndef _STLP_INTERNAL_ITERATOR_H 42# include <stl/_iterator.h> 43#endif 44 45#ifndef _STLP_INTERNAL_FUNCTION_BASE_H 46# include <stl/_function_base.h> 47#endif 48 49#if defined (__SUNPRO_CC) && !defined (_STLP_INTERNAL_CSTDIO) 50// remove() conflict 51# include <stl/_cstdio.h> 52#endif 53 54_STLP_BEGIN_NAMESPACE 55 56// for_each. Apply a function to every element of a range. 57template <class _InputIter, class _Function> 58_STLP_INLINE_LOOP _Function 59for_each(_InputIter __first, _InputIter __last, _Function __f) { 60 for ( ; __first != __last; ++__first) 61 __f(*__first); 62 return __f; 63} 64 65// count_if 66template <class _InputIter, class _Predicate> 67_STLP_INLINE_LOOP _STLP_DIFFERENCE_TYPE(_InputIter) 68count_if(_InputIter __first, _InputIter __last, _Predicate __pred) { 69 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 70 _STLP_DIFFERENCE_TYPE(_InputIter) __n = 0; 71 for ( ; __first != __last; ++__first) { 72 if (__pred(*__first)) 73 ++__n; 74 } 75 return __n; 76} 77 78// adjacent_find. 79 80template <class _ForwardIter, class _BinaryPredicate> 81_STLP_INLINE_LOOP _ForwardIter 82adjacent_find(_ForwardIter __first, _ForwardIter __last, 83 _BinaryPredicate __binary_pred) { 84 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 85 if (__first == __last) 86 return __last; 87 _ForwardIter __next = __first; 88 while(++__next != __last) { 89 if (__binary_pred(*__first, *__next)) 90 return __first; 91 __first = __next; 92 } 93 return __last; 94} 95 96template <class _ForwardIter> 97_STLP_INLINE_LOOP _ForwardIter 98adjacent_find(_ForwardIter __first, _ForwardIter __last) { 99 return adjacent_find(__first, __last, 100 _STLP_PRIV __equal_to(_STLP_VALUE_TYPE(__first, _ForwardIter))); 101} 102 103#if !defined (_STLP_NO_ANACHRONISMS) 104template <class _InputIter, class _Tp, class _Size> 105_STLP_INLINE_LOOP void 106count(_InputIter __first, _InputIter __last, const _Tp& __val, _Size& __n) { 107 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 108 for ( ; __first != __last; ++__first) 109 if (*__first == __val) 110 ++__n; 111} 112 113template <class _InputIter, class _Predicate, class _Size> 114_STLP_INLINE_LOOP void 115count_if(_InputIter __first, _InputIter __last, _Predicate __pred, _Size& __n) { 116 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 117 for ( ; __first != __last; ++__first) 118 if (__pred(*__first)) 119 ++__n; 120} 121#endif 122 123template <class _ForwardIter1, class _ForwardIter2> 124_ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1, 125 _ForwardIter2 __first2, _ForwardIter2 __last2); 126 127// search_n. Search for __count consecutive copies of __val. 128template <class _ForwardIter, class _Integer, class _Tp> 129_ForwardIter search_n(_ForwardIter __first, _ForwardIter __last, 130 _Integer __count, const _Tp& __val); 131template <class _ForwardIter, class _Integer, class _Tp, class _BinaryPred> 132_ForwardIter search_n(_ForwardIter __first, _ForwardIter __last, 133 _Integer __count, const _Tp& __val, _BinaryPred __binary_pred); 134 135template <class _InputIter, class _ForwardIter> 136inline _InputIter find_first_of(_InputIter __first1, _InputIter __last1, 137 _ForwardIter __first2, _ForwardIter __last2) { 138 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1)) 139 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2)) 140 return _STLP_PRIV __find_first_of(__first1, __last1, __first2, __last2); 141} 142 143template <class _InputIter, class _ForwardIter, class _BinaryPredicate> 144inline _InputIter 145find_first_of(_InputIter __first1, _InputIter __last1, 146 _ForwardIter __first2, _ForwardIter __last2, _BinaryPredicate __comp) { 147 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1)) 148 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2)) 149 return _STLP_PRIV __find_first_of(__first1, __last1, __first2, __last2, __comp); 150} 151 152template <class _ForwardIter1, class _ForwardIter2> 153_ForwardIter1 154find_end(_ForwardIter1 __first1, _ForwardIter1 __last1, 155 _ForwardIter2 __first2, _ForwardIter2 __last2); 156 157// swap_ranges 158template <class _ForwardIter1, class _ForwardIter2> 159_STLP_INLINE_LOOP _ForwardIter2 160swap_ranges(_ForwardIter1 __first1, _ForwardIter1 __last1, _ForwardIter2 __first2) { 161 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1)) 162 for ( ; __first1 != __last1; ++__first1, ++__first2) 163 iter_swap(__first1, __first2); 164 return __first2; 165} 166 167// transform 168template <class _InputIter, class _OutputIter, class _UnaryOperation> 169_STLP_INLINE_LOOP _OutputIter 170transform(_InputIter __first, _InputIter __last, _OutputIter __result, _UnaryOperation __opr) { 171 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 172 for ( ; __first != __last; ++__first, ++__result) 173 *__result = __opr(*__first); 174 return __result; 175} 176template <class _InputIter1, class _InputIter2, class _OutputIter, class _BinaryOperation> 177_STLP_INLINE_LOOP _OutputIter 178transform(_InputIter1 __first1, _InputIter1 __last1, 179 _InputIter2 __first2, _OutputIter __result,_BinaryOperation __binary_op) { 180 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1)) 181 for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result) 182 *__result = __binary_op(*__first1, *__first2); 183 return __result; 184} 185 186// replace_if, replace_copy, replace_copy_if 187 188template <class _ForwardIter, class _Predicate, class _Tp> 189_STLP_INLINE_LOOP void 190replace_if(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, const _Tp& __new_value) { 191 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 192 for ( ; __first != __last; ++__first) 193 if (__pred(*__first)) 194 *__first = __new_value; 195} 196 197template <class _InputIter, class _OutputIter, class _Tp> 198_STLP_INLINE_LOOP _OutputIter 199replace_copy(_InputIter __first, _InputIter __last,_OutputIter __result, 200 const _Tp& __old_value, const _Tp& __new_value) { 201 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 202 for ( ; __first != __last; ++__first, ++__result) 203 *__result = *__first == __old_value ? __new_value : *__first; 204 return __result; 205} 206 207template <class _Iterator, class _OutputIter, class _Predicate, class _Tp> 208_STLP_INLINE_LOOP _OutputIter 209replace_copy_if(_Iterator __first, _Iterator __last, 210 _OutputIter __result, 211 _Predicate __pred, const _Tp& __new_value) { 212 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 213 for ( ; __first != __last; ++__first, ++__result) 214 *__result = __pred(*__first) ? __new_value : *__first; 215 return __result; 216} 217 218// generate and generate_n 219 220template <class _ForwardIter, class _Generator> 221_STLP_INLINE_LOOP void 222generate(_ForwardIter __first, _ForwardIter __last, _Generator __gen) { 223 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 224 for ( ; __first != __last; ++__first) 225 *__first = __gen(); 226} 227 228template <class _OutputIter, class _Size, class _Generator> 229_STLP_INLINE_LOOP void 230generate_n(_OutputIter __first, _Size __n, _Generator __gen) { 231 for ( ; __n > 0; --__n, ++__first) 232 *__first = __gen(); 233} 234 235// remove, remove_if, remove_copy, remove_copy_if 236 237template <class _InputIter, class _OutputIter, class _Tp> 238_STLP_INLINE_LOOP _OutputIter 239remove_copy(_InputIter __first, _InputIter __last,_OutputIter __result, const _Tp& __val) { 240 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 241 for ( ; __first != __last; ++__first) { 242 if (!(*__first == __val)) { 243 *__result = *__first; 244 ++__result; 245 } 246 } 247 return __result; 248} 249 250template <class _InputIter, class _OutputIter, class _Predicate> 251_STLP_INLINE_LOOP _OutputIter 252remove_copy_if(_InputIter __first, _InputIter __last, _OutputIter __result, _Predicate __pred) { 253 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 254 for ( ; __first != __last; ++__first) { 255 if (!__pred(*__first)) { 256 *__result = *__first; 257 ++__result; 258 } 259 } 260 return __result; 261} 262 263template <class _ForwardIter, class _Tp> 264_STLP_INLINE_LOOP _ForwardIter 265remove(_ForwardIter __first, _ForwardIter __last, const _Tp& __val) { 266 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 267 __first = find(__first, __last, __val); 268 if (__first == __last) 269 return __first; 270 else { 271 _ForwardIter __next = __first; 272 return remove_copy(++__next, __last, __first, __val); 273 } 274} 275 276template <class _ForwardIter, class _Predicate> 277_STLP_INLINE_LOOP _ForwardIter 278remove_if(_ForwardIter __first, _ForwardIter __last, _Predicate __pred) { 279 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 280 __first = find_if(__first, __last, __pred); 281 if ( __first == __last ) 282 return __first; 283 else { 284 _ForwardIter __next = __first; 285 return remove_copy_if(++__next, __last, __first, __pred); 286 } 287} 288 289// unique and unique_copy 290template <class _InputIter, class _OutputIter> 291_OutputIter unique_copy(_InputIter __first, _InputIter __last, _OutputIter __result); 292 293template <class _InputIter, class _OutputIter, class _BinaryPredicate> 294_OutputIter unique_copy(_InputIter __first, _InputIter __last,_OutputIter __result, 295 _BinaryPredicate __binary_pred); 296 297template <class _ForwardIter> 298inline _ForwardIter unique(_ForwardIter __first, _ForwardIter __last) { 299 __first = adjacent_find(__first, __last); 300 return unique_copy(__first, __last, __first); 301} 302 303template <class _ForwardIter, class _BinaryPredicate> 304inline _ForwardIter unique(_ForwardIter __first, _ForwardIter __last, 305 _BinaryPredicate __binary_pred) { 306 __first = adjacent_find(__first, __last, __binary_pred); 307 return unique_copy(__first, __last, __first, __binary_pred); 308} 309 310// reverse and reverse_copy, and their auxiliary functions 311 312_STLP_MOVE_TO_PRIV_NAMESPACE 313 314template <class _BidirectionalIter> 315_STLP_INLINE_LOOP void 316__reverse(_BidirectionalIter __first, _BidirectionalIter __last, const bidirectional_iterator_tag &) { 317 for (; __first != __last && __first != --__last; ++__first) 318 _STLP_STD::iter_swap(__first,__last); 319} 320 321template <class _RandomAccessIter> 322_STLP_INLINE_LOOP void 323__reverse(_RandomAccessIter __first, _RandomAccessIter __last, const random_access_iterator_tag &) { 324 for (; __first < __last; ++__first) 325 _STLP_STD::iter_swap(__first, --__last); 326} 327 328_STLP_MOVE_TO_STD_NAMESPACE 329 330template <class _BidirectionalIter> 331inline void 332reverse(_BidirectionalIter __first, _BidirectionalIter __last) { 333 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 334 _STLP_PRIV __reverse(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _BidirectionalIter)); 335} 336 337template <class _BidirectionalIter, class _OutputIter> 338_STLP_INLINE_LOOP 339_OutputIter reverse_copy(_BidirectionalIter __first, 340 _BidirectionalIter __last, 341 _OutputIter __result) { 342 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 343 while (__first != __last) { 344 --__last; 345 *__result = *__last; 346 ++__result; 347 } 348 return __result; 349} 350 351template <class _ForwardIter> 352void rotate(_ForwardIter __first, _ForwardIter __middle, _ForwardIter __last); 353 354template <class _ForwardIter, class _OutputIter> 355inline _OutputIter rotate_copy(_ForwardIter __first, _ForwardIter __middle, 356 _ForwardIter __last, _OutputIter __result) { 357 return _STLP_STD::copy(__first, __middle, copy(__middle, __last, __result)); 358} 359 360// random_shuffle 361 362template <class _RandomAccessIter> 363void random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last); 364 365template <class _RandomAccessIter, class _RandomNumberGenerator> 366void random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last, 367 _RandomNumberGenerator& __rand); 368 369#if !defined (_STLP_NO_EXTENSIONS) 370// random_sample and random_sample_n (extensions, not part of the standard). 371 372template <class _ForwardIter, class _OutputIter, class _Distance> 373_OutputIter random_sample_n(_ForwardIter __first, _ForwardIter __last, 374 _OutputIter __out_ite, const _Distance __n); 375 376template <class _ForwardIter, class _OutputIter, class _Distance, 377 class _RandomNumberGenerator> 378_OutputIter random_sample_n(_ForwardIter __first, _ForwardIter __last, 379 _OutputIter __out_ite, const _Distance __n, 380 _RandomNumberGenerator& __rand); 381 382template <class _InputIter, class _RandomAccessIter> 383_RandomAccessIter 384random_sample(_InputIter __first, _InputIter __last, 385 _RandomAccessIter __out_first, _RandomAccessIter __out_last); 386 387template <class _InputIter, class _RandomAccessIter, 388 class _RandomNumberGenerator> 389_RandomAccessIter 390random_sample(_InputIter __first, _InputIter __last, 391 _RandomAccessIter __out_first, _RandomAccessIter __out_last, 392 _RandomNumberGenerator& __rand); 393 394#endif /* _STLP_NO_EXTENSIONS */ 395 396// partition, stable_partition, and their auxiliary functions 397 398template <class _ForwardIter, class _Predicate> 399_ForwardIter partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred); 400 401template <class _ForwardIter, class _Predicate> 402_ForwardIter 403stable_partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred); 404 405// sort() and its auxiliary functions. 406_STLP_MOVE_TO_PRIV_NAMESPACE 407 408template <class _Size> 409inline _Size __lg(_Size __n) { 410 _Size __k; 411 for (__k = 0; __n != 1; __n >>= 1) ++__k; 412 return __k; 413} 414 415_STLP_MOVE_TO_STD_NAMESPACE 416 417template <class _RandomAccessIter> 418void sort(_RandomAccessIter __first, _RandomAccessIter __last); 419template <class _RandomAccessIter, class _Compare> 420void sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp); 421 422// stable_sort() and its auxiliary functions. 423template <class _RandomAccessIter> 424void stable_sort(_RandomAccessIter __first, 425 _RandomAccessIter __last); 426 427template <class _RandomAccessIter, class _Compare> 428void stable_sort(_RandomAccessIter __first, 429 _RandomAccessIter __last, _Compare __comp); 430 431// partial_sort, partial_sort_copy, and auxiliary functions. 432 433template <class _RandomAccessIter> 434void partial_sort(_RandomAccessIter __first, _RandomAccessIter __middle, 435 _RandomAccessIter __last); 436 437template <class _RandomAccessIter, class _Compare> 438void partial_sort(_RandomAccessIter __first,_RandomAccessIter __middle, 439 _RandomAccessIter __last, _Compare __comp); 440 441template <class _InputIter, class _RandomAccessIter> 442_RandomAccessIter 443partial_sort_copy(_InputIter __first, _InputIter __last, 444 _RandomAccessIter __result_first, _RandomAccessIter __result_last); 445 446template <class _InputIter, class _RandomAccessIter, class _Compare> 447_RandomAccessIter 448partial_sort_copy(_InputIter __first, _InputIter __last, 449 _RandomAccessIter __result_first, 450 _RandomAccessIter __result_last, _Compare __comp); 451 452// nth_element() and its auxiliary functions. 453template <class _RandomAccessIter> 454void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth, 455 _RandomAccessIter __last); 456 457template <class _RandomAccessIter, class _Compare> 458void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth, 459 _RandomAccessIter __last, _Compare __comp); 460 461// auxiliary class for lower_bound, etc. 462_STLP_MOVE_TO_PRIV_NAMESPACE 463 464template <class _T1, class _T2> 465struct __less_2 { 466 bool operator() (const _T1& __x, const _T2& __y) const { return __x < __y ; } 467}; 468 469template <class _T1, class _T2> 470__less_2<_T1,_T2> __less2(_T1*, _T2* ) { return __less_2<_T1, _T2>(); } 471 472#if defined (_STLP_FUNCTION_PARTIAL_ORDER) 473template <class _Tp> 474less<_Tp> __less2(_Tp*, _Tp* ) { return less<_Tp>(); } 475#endif 476 477_STLP_MOVE_TO_STD_NAMESPACE 478 479// Binary search (lower_bound, upper_bound, equal_range, binary_search). 480template <class _ForwardIter, class _Tp> 481inline _ForwardIter lower_bound(_ForwardIter __first, _ForwardIter __last, 482 const _Tp& __val) { 483 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 484 return _STLP_PRIV __lower_bound(__first, __last, __val, 485 _STLP_PRIV __less2(_STLP_VALUE_TYPE(__first, _ForwardIter), (_Tp*)0), 486 _STLP_PRIV __less2((_Tp*)0, _STLP_VALUE_TYPE(__first, _ForwardIter)), 487 _STLP_DISTANCE_TYPE(__first, _ForwardIter)); 488} 489 490template <class _ForwardIter, class _Tp, class _Compare> 491inline _ForwardIter lower_bound(_ForwardIter __first, _ForwardIter __last, 492 const _Tp& __val, _Compare __comp) { 493 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 494 return _STLP_PRIV __lower_bound(__first, __last, __val, __comp, __comp, 495 _STLP_DISTANCE_TYPE(__first, _ForwardIter)); 496} 497 498_STLP_MOVE_TO_PRIV_NAMESPACE 499 500template <class _ForwardIter, class _Tp, class _Compare1, class _Compare2, class _Distance> 501_ForwardIter __upper_bound(_ForwardIter __first, _ForwardIter __last, const _Tp& __val, 502 _Compare1 __comp1, _Compare2 __comp2, _Distance*); 503 504_STLP_MOVE_TO_STD_NAMESPACE 505 506template <class _ForwardIter, class _Tp> 507inline _ForwardIter upper_bound(_ForwardIter __first, _ForwardIter __last, 508 const _Tp& __val) { 509 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 510 return _STLP_PRIV __upper_bound(__first, __last, __val, 511 _STLP_PRIV __less2(_STLP_VALUE_TYPE(__first, _ForwardIter), (_Tp*)0), 512 _STLP_PRIV __less2((_Tp*)0, _STLP_VALUE_TYPE(__first, _ForwardIter)), 513 _STLP_DISTANCE_TYPE(__first, _ForwardIter)); 514} 515 516template <class _ForwardIter, class _Tp, class _Compare> 517inline _ForwardIter upper_bound(_ForwardIter __first, _ForwardIter __last, 518 const _Tp& __val, _Compare __comp) { 519 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 520 return _STLP_PRIV __upper_bound(__first, __last, __val, __comp, __comp, 521 _STLP_DISTANCE_TYPE(__first, _ForwardIter)); 522} 523 524_STLP_MOVE_TO_PRIV_NAMESPACE 525 526template <class _ForwardIter, class _Tp, class _Compare1, class _Compare2, class _Distance> 527pair<_ForwardIter, _ForwardIter> 528__equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val, 529 _Compare1 __comp1, _Compare2 __comp2, _Distance*); 530 531_STLP_MOVE_TO_STD_NAMESPACE 532 533template <class _ForwardIter, class _Tp> 534inline pair<_ForwardIter, _ForwardIter> 535equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val) { 536 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 537 return _STLP_PRIV __equal_range(__first, __last, __val, 538 _STLP_PRIV __less2(_STLP_VALUE_TYPE(__first, _ForwardIter), (_Tp*)0), 539 _STLP_PRIV __less2((_Tp*)0, _STLP_VALUE_TYPE(__first, _ForwardIter)), 540 _STLP_DISTANCE_TYPE(__first, _ForwardIter)); 541} 542 543template <class _ForwardIter, class _Tp, class _Compare> 544inline pair<_ForwardIter, _ForwardIter> 545equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val, 546 _Compare __comp) { 547 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 548 return _STLP_PRIV __equal_range(__first, __last, __val, __comp, __comp, 549 _STLP_DISTANCE_TYPE(__first, _ForwardIter)); 550} 551 552template <class _ForwardIter, class _Tp> 553inline bool binary_search(_ForwardIter __first, _ForwardIter __last, 554 const _Tp& __val) { 555 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 556 _ForwardIter __i = _STLP_PRIV __lower_bound(__first, __last, __val, 557 _STLP_PRIV __less2(_STLP_VALUE_TYPE(__first, _ForwardIter), (_Tp*)0), 558 _STLP_PRIV __less2((_Tp*)0, _STLP_VALUE_TYPE(__first, _ForwardIter)), 559 _STLP_DISTANCE_TYPE(__first, _ForwardIter)); 560 return __i != __last && !(__val < *__i); 561} 562 563template <class _ForwardIter, class _Tp, class _Compare> 564inline bool binary_search(_ForwardIter __first, _ForwardIter __last, 565 const _Tp& __val, 566 _Compare __comp) { 567 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) 568 _ForwardIter __i = _STLP_PRIV __lower_bound(__first, __last, __val, __comp, __comp, 569 _STLP_DISTANCE_TYPE(__first, _ForwardIter)); 570 return __i != __last && !__comp(__val, *__i); 571} 572 573// merge, with and without an explicitly supplied comparison function. 574 575template <class _InputIter1, class _InputIter2, class _OutputIter> 576_OutputIter merge(_InputIter1 __first1, _InputIter1 __last1, 577 _InputIter2 __first2, _InputIter2 __last2, 578 _OutputIter __result); 579 580template <class _InputIter1, class _InputIter2, class _OutputIter, 581 class _Compare> 582_OutputIter merge(_InputIter1 __first1, _InputIter1 __last1, 583 _InputIter2 __first2, _InputIter2 __last2, 584 _OutputIter __result, _Compare __comp); 585 586 587// inplace_merge and its auxiliary functions. 588 589 590template <class _BidirectionalIter> 591void inplace_merge(_BidirectionalIter __first, 592 _BidirectionalIter __middle, 593 _BidirectionalIter __last) ; 594 595template <class _BidirectionalIter, class _Compare> 596void inplace_merge(_BidirectionalIter __first, 597 _BidirectionalIter __middle, 598 _BidirectionalIter __last, _Compare __comp); 599 600// Set algorithms: includes, set_union, set_intersection, set_difference, 601// set_symmetric_difference. All of these algorithms have the precondition 602// that their input ranges are sorted and the postcondition that their output 603// ranges are sorted. 604 605template <class _InputIter1, class _InputIter2> 606bool includes(_InputIter1 __first1, _InputIter1 __last1, 607 _InputIter2 __first2, _InputIter2 __last2); 608 609template <class _InputIter1, class _InputIter2, class _Compare> 610bool includes(_InputIter1 __first1, _InputIter1 __last1, 611 _InputIter2 __first2, _InputIter2 __last2, _Compare __comp); 612 613template <class _InputIter1, class _InputIter2, class _OutputIter> 614_OutputIter set_union(_InputIter1 __first1, _InputIter1 __last1, 615 _InputIter2 __first2, _InputIter2 __last2, 616 _OutputIter __result); 617 618template <class _InputIter1, class _InputIter2, class _OutputIter, 619 class _Compare> 620_OutputIter set_union(_InputIter1 __first1, _InputIter1 __last1, 621 _InputIter2 __first2, _InputIter2 __last2, 622 _OutputIter __result, _Compare __comp); 623 624template <class _InputIter1, class _InputIter2, class _OutputIter> 625_OutputIter set_intersection(_InputIter1 __first1, _InputIter1 __last1, 626 _InputIter2 __first2, _InputIter2 __last2, 627 _OutputIter __result); 628 629template <class _InputIter1, class _InputIter2, class _OutputIter, 630 class _Compare> 631_OutputIter set_intersection(_InputIter1 __first1, _InputIter1 __last1, 632 _InputIter2 __first2, _InputIter2 __last2, 633 _OutputIter __result, _Compare __comp); 634 635 636 637template <class _InputIter1, class _InputIter2, class _OutputIter> 638_OutputIter set_difference(_InputIter1 __first1, _InputIter1 __last1, 639 _InputIter2 __first2, _InputIter2 __last2, 640 _OutputIter __result); 641 642template <class _InputIter1, class _InputIter2, class _OutputIter, 643 class _Compare> 644_OutputIter set_difference(_InputIter1 __first1, _InputIter1 __last1, 645 _InputIter2 __first2, _InputIter2 __last2, 646 _OutputIter __result, _Compare __comp); 647 648template <class _InputIter1, class _InputIter2, class _OutputIter> 649_OutputIter 650set_symmetric_difference(_InputIter1 __first1, _InputIter1 __last1, 651 _InputIter2 __first2, _InputIter2 __last2, 652 _OutputIter __result); 653 654 655template <class _InputIter1, class _InputIter2, class _OutputIter, 656 class _Compare> 657_OutputIter 658set_symmetric_difference(_InputIter1 __first1, _InputIter1 __last1, 659 _InputIter2 __first2, _InputIter2 __last2, 660 _OutputIter __result, 661 _Compare __comp); 662 663 664// min_element and max_element, with and without an explicitly supplied 665// comparison function. 666 667template <class _ForwardIter> 668_ForwardIter max_element(_ForwardIter __first, _ForwardIter __last); 669template <class _ForwardIter, class _Compare> 670_ForwardIter max_element(_ForwardIter __first, _ForwardIter __last, 671 _Compare __comp); 672 673template <class _ForwardIter> 674_ForwardIter min_element(_ForwardIter __first, _ForwardIter __last); 675 676template <class _ForwardIter, class _Compare> 677_ForwardIter min_element(_ForwardIter __first, _ForwardIter __last, 678 _Compare __comp); 679 680// next_permutation and prev_permutation, with and without an explicitly 681// supplied comparison function. 682 683template <class _BidirectionalIter> 684bool next_permutation(_BidirectionalIter __first, _BidirectionalIter __last); 685 686template <class _BidirectionalIter, class _Compare> 687bool next_permutation(_BidirectionalIter __first, _BidirectionalIter __last, 688 _Compare __comp); 689 690 691template <class _BidirectionalIter> 692bool prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last); 693 694 695template <class _BidirectionalIter, class _Compare> 696bool prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last, 697 _Compare __comp); 698 699#if !defined (_STLP_NO_EXTENSIONS) 700// is_heap, a predicate testing whether or not a range is 701// a heap. This function is an extension, not part of the C++ 702// standard. 703 704template <class _RandomAccessIter> 705bool is_heap(_RandomAccessIter __first, _RandomAccessIter __last); 706 707template <class _RandomAccessIter, class _StrictWeakOrdering> 708bool is_heap(_RandomAccessIter __first, _RandomAccessIter __last, 709 _StrictWeakOrdering __comp); 710 711// is_sorted, a predicated testing whether a range is sorted in 712// nondescending order. This is an extension, not part of the C++ 713// standard. 714_STLP_MOVE_TO_PRIV_NAMESPACE 715 716template <class _ForwardIter, class _StrictWeakOrdering> 717bool __is_sorted(_ForwardIter __first, _ForwardIter __last, 718 _StrictWeakOrdering __comp); 719 720_STLP_MOVE_TO_STD_NAMESPACE 721template <class _ForwardIter> 722inline bool is_sorted(_ForwardIter __first, _ForwardIter __last) { 723 return _STLP_PRIV __is_sorted(__first, __last, 724 _STLP_PRIV __less(_STLP_VALUE_TYPE(__first, _ForwardIter))); 725} 726 727template <class _ForwardIter, class _StrictWeakOrdering> 728inline bool is_sorted(_ForwardIter __first, _ForwardIter __last, 729 _StrictWeakOrdering __comp) { 730 return _STLP_PRIV __is_sorted(__first, __last, __comp); 731} 732#endif 733 734_STLP_END_NAMESPACE 735 736#if !defined (_STLP_LINK_TIME_INSTANTIATION) 737# include <stl/_algo.c> 738#endif 739 740#endif /* _STLP_INTERNAL_ALGO_H */ 741 742// Local Variables: 743// mode:C++ 744// End: 745 746