_algo.h

00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Copyright (c) 1996,1997
00007  * Silicon Graphics Computer Systems, Inc.
00008  *
00009  * Copyright (c) 1997
00010  * Moscow Center for SPARC Technology
00011  *
00012  * Copyright (c) 1999 
00013  * Boris Fomitchev
00014  *
00015  * This material is provided "as is", with absolutely no warranty expressed
00016  * or implied. Any use is at your own risk.
00017  *
00018  * Permission to use or copy this software for any purpose is hereby granted 
00019  * without fee, provided the above notices are retained on all copies.
00020  * Permission to modify the code and to distribute modified code is granted,
00021  * provided the above notices are retained, and a notice that the code was
00022  * modified is included with the above copyright notice.
00023  *
00024  */
00025 
00026 /* NOTE: This is an internal header file, included by other STL headers.
00027  *   You should not attempt to use it directly.
00028  */
00029 
00030 #ifndef _STLP_INTERNAL_ALGO_H
00031 #define _STLP_INTERNAL_ALGO_H
00032 
00033 # ifndef _STLP_INTERNAL_ALGOBASE_H
00034 #  include <stl/_algobase.h>
00035 # endif
00036 
00037 # ifndef _STLP_INTERNAL_TEMPBUF_H
00038 #  include <stl/_tempbuf.h>
00039 # endif
00040 
00041 # ifndef _STLP_INTERNAL_HEAP_H
00042 #  include <stl/_heap.h>
00043 # endif
00044 
00045 # ifndef _STLP_INTERNAL_ITERATOR_H
00046 #  include <stl/_iterator.h>
00047 # endif
00048 
00049 # ifndef _STLP_INTERNAL_FUNCTION_BASE_H
00050 #  include <stl/_function_base.h>
00051 # endif
00052 
00053 # ifdef __SUNPRO_CC
00054 // remove() conflict
00055 #  include <cstdio>
00056 # endif
00057 
00058 _STLP_BEGIN_NAMESPACE
00059 
00060 // for_each.  Apply a function to every element of a range.
00061 template <class _InputIter, class _Function>
00062 _STLP_INLINE_LOOP _Function 
00063 for_each(_InputIter __first, _InputIter __last, _Function __f) {
00064   for ( ; __first != __last; ++__first)
00065     __f(*__first);
00066   return __f;
00067 }
00068 
00069 // count_if
00070 template <class _InputIter, class _Predicate>
00071 _STLP_INLINE_LOOP _STLP_DIFFERENCE_TYPE(_InputIter)
00072 count_if(_InputIter __first, _InputIter __last, _Predicate __pred) {
00073   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00074 _STLP_DIFFERENCE_TYPE(_InputIter) __n = 0;
00075   for ( ; __first != __last; ++__first)
00076     if (__pred(*__first))
00077       ++__n;
00078   return __n;
00079 }
00080 
00081 // adjacent_find.
00082 template <class _ForwardIter>
00083 _STLP_INLINE_LOOP _ForwardIter 
00084 adjacent_find(_ForwardIter __first, _ForwardIter __last) {
00085   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00086     if (__first == __last)
00087       return __last;
00088   _ForwardIter __next = __first;
00089   while(++__next != __last) {
00090     if (*__first == *__next)
00091       return __first;
00092     __first = __next;
00093   }
00094   return __last;
00095 }
00096 
00097 template <class _ForwardIter, class _BinaryPredicate>
00098 _STLP_INLINE_LOOP _ForwardIter 
00099 adjacent_find(_ForwardIter __first, _ForwardIter __last,
00100               _BinaryPredicate __binary_pred) {
00101   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00102   if (__first == __last)
00103     return __last;
00104   _ForwardIter __next = __first;
00105   while(++__next != __last) {
00106     if (__binary_pred(*__first, *__next))
00107       return __first;
00108     __first = __next;
00109   }
00110   return __last;
00111 }
00112 
00113 # ifndef _STLP_NO_ANACHRONISMS
00114 template <class _InputIter, class _Tp, class _Size>
00115 _STLP_INLINE_LOOP void 
00116 count(_InputIter __first, _InputIter __last, const _Tp& __value, _Size& __n) {
00117   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00118     for ( ; __first != __last; ++__first)
00119       if (*__first == __value)
00120         ++__n;
00121 }
00122 
00123 template <class _InputIter, class _Predicate, class _Size>
00124 _STLP_INLINE_LOOP void 
00125 count_if(_InputIter __first, _InputIter __last, _Predicate __pred, _Size& __n) {
00126   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00127   for ( ; __first != __last; ++__first)
00128     if (__pred(*__first))
00129       ++__n;
00130 }
00131 # endif
00132 
00133 template <class _ForwardIter1, class _ForwardIter2>
00134 _ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,
00135                      _ForwardIter2 __first2, _ForwardIter2 __last2);
00136 
00137 // search_n.  Search for __count consecutive copies of __val.
00138 template <class _ForwardIter, class _Integer, class _Tp>
00139 _ForwardIter search_n(_ForwardIter __first, _ForwardIter __last,
00140                       _Integer __count, const _Tp& __val);
00141 template <class _ForwardIter, class _Integer, class _Tp, class _BinaryPred>
00142 _ForwardIter search_n(_ForwardIter __first, _ForwardIter __last,
00143                       _Integer __count, const _Tp& __val, _BinaryPred __binary_pred);
00144 
00145 template <class _InputIter, class _ForwardIter>
00146 inline _InputIter find_first_of(_InputIter __first1, _InputIter __last1,
00147                                 _ForwardIter __first2, _ForwardIter __last2) {
00148   _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
00149   _STLP_DEBUG_CHECK(__check_range(__first2, __last2))
00150   return __find_first_of(__first1, __last1, __first2, __last2,__equal_to(_STLP_VALUE_TYPE(__first1, _InputIter)));
00151 }
00152 
00153 template <class _InputIter, class _ForwardIter, class _BinaryPredicate>
00154 inline _InputIter 
00155 find_first_of(_InputIter __first1, _InputIter __last1,
00156               _ForwardIter __first2, _ForwardIter __last2,_BinaryPredicate __comp) {
00157   _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
00158   _STLP_DEBUG_CHECK(__check_range(__first2, __last2))
00159   return __find_first_of(__first1, __last1, __first2, __last2,__comp);
00160 }
00161 
00162 template <class _ForwardIter1, class _ForwardIter2>
00163 _ForwardIter1 
00164 find_end(_ForwardIter1 __first1, _ForwardIter1 __last1, 
00165          _ForwardIter2 __first2, _ForwardIter2 __last2);
00166 
00167 // swap_ranges
00168 template <class _ForwardIter1, class _ForwardIter2>
00169 _STLP_INLINE_LOOP _ForwardIter2 
00170 swap_ranges(_ForwardIter1 __first1, _ForwardIter1 __last1, _ForwardIter2 __first2) {
00171   _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
00172   for ( ; __first1 != __last1; ++__first1, ++__first2)
00173     iter_swap(__first1, __first2);
00174   return __first2;
00175 }
00176 
00177 // transform
00178 template <class _InputIter, class _OutputIter, class _UnaryOperation>
00179 _STLP_INLINE_LOOP _OutputIter 
00180 transform(_InputIter __first, _InputIter __last, _OutputIter __result, _UnaryOperation __opr) {
00181   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00182   for ( ; __first != __last; ++__first, ++__result)
00183     *__result = __opr(*__first);
00184   return __result;
00185 }
00186 template <class _InputIter1, class _InputIter2, class _OutputIter, class _BinaryOperation>
00187 _STLP_INLINE_LOOP _OutputIter 
00188 transform(_InputIter1 __first1, _InputIter1 __last1, 
00189           _InputIter2 __first2, _OutputIter __result,_BinaryOperation __binary_op) {
00190   _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
00191   for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)
00192     *__result = __binary_op(*__first1, *__first2);
00193   return __result;
00194 }
00195 
00196 // replace_if, replace_copy, replace_copy_if
00197 
00198 template <class _ForwardIter, class _Predicate, class _Tp>
00199 _STLP_INLINE_LOOP void 
00200 replace_if(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, const _Tp& __new_value) {
00201   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00202   for ( ; __first != __last; ++__first)
00203     if (__pred(*__first))
00204       *__first = __new_value;
00205 }
00206 
00207 template <class _InputIter, class _OutputIter, class _Tp>
00208 _STLP_INLINE_LOOP  _OutputIter 
00209 replace_copy(_InputIter __first, _InputIter __last,_OutputIter __result,
00210              const _Tp& __old_value, const _Tp& __new_value) {
00211   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00212   for ( ; __first != __last; ++__first, ++__result)
00213     *__result = *__first == __old_value ? __new_value : *__first;
00214   return __result;
00215 }
00216 
00217 template <class _Iterator, class _OutputIter, class _Predicate, class _Tp>
00218 _STLP_INLINE_LOOP _OutputIter 
00219 replace_copy_if(_Iterator __first, _Iterator __last,
00220                 _OutputIter __result,
00221                 _Predicate __pred, const _Tp& __new_value) {
00222   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00223   for ( ; __first != __last; ++__first, ++__result)
00224     *__result = __pred(*__first) ? __new_value : *__first;
00225   return __result;
00226 }
00227 
00228 // generate and generate_n
00229 
00230 template <class _ForwardIter, class _Generator>
00231 _STLP_INLINE_LOOP void 
00232 generate(_ForwardIter __first, _ForwardIter __last, _Generator __gen) {
00233   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00234   for ( ; __first != __last; ++__first)
00235     *__first = __gen();
00236 }
00237 
00238 template <class _OutputIter, class _Size, class _Generator>
00239 _STLP_INLINE_LOOP _OutputIter 
00240 generate_n(_OutputIter __first, _Size __n, _Generator __gen) {
00241   for ( ; __n > 0; --__n, ++__first)
00242     *__first = __gen();
00243   return __first;
00244 }
00245 
00246 // remove, remove_if, remove_copy, remove_copy_if
00247 
00248 template <class _InputIter, class _OutputIter, class _Tp>
00249 _STLP_INLINE_LOOP _OutputIter 
00250 remove_copy(_InputIter __first, _InputIter __last,_OutputIter __result, const _Tp& __value) {
00251   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00252   for ( ; __first != __last; ++__first)
00253     if (!(*__first == __value)) {
00254       *__result = *__first;
00255       ++__result;
00256     }
00257   return __result;
00258 }
00259 
00260 template <class _InputIter, class _OutputIter, class _Predicate>
00261 _STLP_INLINE_LOOP _OutputIter 
00262 remove_copy_if(_InputIter __first, _InputIter __last, _OutputIter __result, _Predicate __pred) {
00263   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00264   for ( ; __first != __last; ++__first)
00265     if (!__pred(*__first)) {
00266       *__result = *__first;
00267       ++__result;
00268     }
00269   return __result;
00270 }
00271 
00272 //DJO This is making abld fail with: macro 'remove' used with too many (3) args
00273 
00274 /*template <class _ForwardIter, class _Tp>
00275 _STLP_INLINE_LOOP _ForwardIter 
00276 remove(_ForwardIter __first, _ForwardIter __last, const _Tp& __value) {
00277   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00278   __first = find(__first, __last, __value);
00279   if (__first == __last)
00280     return __first;
00281   else { 
00282     _ForwardIter __next = __first;
00283     return remove_copy(++__next, __last, __first, __value);
00284   }
00285 }*/
00286 
00287 template <class _ForwardIter, class _Predicate>
00288 _STLP_INLINE_LOOP _ForwardIter 
00289 remove_if(_ForwardIter __first, _ForwardIter __last, _Predicate __pred) {
00290   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00291   __first = find_if(__first, __last, __pred);
00292   if ( __first == __last )
00293     return __first;
00294   else {
00295     _ForwardIter __next = __first;
00296     return remove_copy_if(++__next, __last, __first, __pred);
00297   }
00298 }
00299 
00300 // unique and unique_copy
00301 template <class _InputIter, class _OutputIter>
00302 _OutputIter unique_copy(_InputIter __first, _InputIter __last, _OutputIter __result);
00303 
00304 template <class _InputIter, class _OutputIter, class _BinaryPredicate>
00305 _OutputIter unique_copy(_InputIter __first, _InputIter __last,_OutputIter __result,
00306                         _BinaryPredicate __binary_pred);
00307 
00308 template <class _ForwardIter>
00309 inline _ForwardIter unique(_ForwardIter __first, _ForwardIter __last) {
00310   __first = adjacent_find(__first, __last);
00311   return unique_copy(__first, __last, __first);
00312 }
00313 
00314 template <class _ForwardIter, class _BinaryPredicate>
00315 inline _ForwardIter unique(_ForwardIter __first, _ForwardIter __last,
00316                     _BinaryPredicate __binary_pred) {
00317   __first = adjacent_find(__first, __last, __binary_pred);
00318   return unique_copy(__first, __last, __first, __binary_pred);
00319 }
00320 
00321 // reverse and reverse_copy, and their auxiliary functions
00322 
00323 template <class _BidirectionalIter>
00324 _STLP_INLINE_LOOP void 
00325 __reverse(_BidirectionalIter __first, _BidirectionalIter __last, const bidirectional_iterator_tag &) {
00326   while (true)
00327     if (__first == __last || __first == --__last)
00328       return;
00329     else
00330       iter_swap(__first++, __last);
00331 }
00332 
00333 
00334 template <class _RandomAccessIter>
00335 _STLP_INLINE_LOOP void 
00336 __reverse(_RandomAccessIter __first, _RandomAccessIter __last, const random_access_iterator_tag &) {
00337   for (; __first < __last; ++__first) iter_swap(__first, --__last);
00338 }
00339 
00340 template <class _BidirectionalIter>
00341 inline void 
00342 reverse(_BidirectionalIter __first, _BidirectionalIter __last) {
00343   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00344   __reverse(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _BidirectionalIter));
00345 }
00346 
00347 template <class _BidirectionalIter, class _OutputIter>
00348 _STLP_INLINE_LOOP
00349 _OutputIter reverse_copy(_BidirectionalIter __first,
00350                             _BidirectionalIter __last,
00351                             _OutputIter __result) {
00352   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00353   while (__first != __last) {
00354     --__last;
00355     *__result = *__last;
00356     ++__result;
00357   }
00358   return __result;
00359 }
00360 
00361 // rotate and rotate_copy, and their auxiliary functions
00362 
00363 template <class _EuclideanRingElement>
00364 _STLP_INLINE_LOOP
00365 _EuclideanRingElement __gcd(_EuclideanRingElement __m,
00366                             _EuclideanRingElement __n)
00367 {
00368   while (__n != 0) {
00369     _EuclideanRingElement __t = __m % __n;
00370     __m = __n;
00371     __n = __t;
00372   }
00373   return __m;
00374 }
00375 
00376 template <class _ForwardIter>
00377 _ForwardIter 
00378 rotate(_ForwardIter __first, _ForwardIter __middle, _ForwardIter __last);
00379 
00380 template <class _ForwardIter, class _OutputIter>
00381 inline _OutputIter rotate_copy(_ForwardIter __first, _ForwardIter __middle,
00382                                _ForwardIter __last, _OutputIter __result) {
00383   return copy(__first, __middle, copy(__middle, __last, __result));
00384 }
00385 
00386 // random_shuffle
00387 
00388 template <class _RandomAccessIter>
00389 void random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last);
00390 
00391 template <class _RandomAccessIter, class _RandomNumberGenerator>
00392 void random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last,
00393                     _RandomNumberGenerator& __rand);
00394 
00395 # ifndef _STLP_NO_EXTENSIONS
00396 // random_sample and random_sample_n (extensions, not part of the standard).
00397 
00398 template <class _ForwardIter, class _OutputIter, class _Distance>
00399 _OutputIter random_sample_n(_ForwardIter __first, _ForwardIter __last,
00400                             _OutputIter __out, const _Distance __n);
00401 
00402 template <class _ForwardIter, class _OutputIter, class _Distance,
00403           class _RandomNumberGenerator>
00404 _OutputIter random_sample_n(_ForwardIter __first, _ForwardIter __last,
00405                             _OutputIter __out, const _Distance __n,
00406                             _RandomNumberGenerator& __rand);
00407 
00408 template <class _InputIter, class _RandomAccessIter>
00409 _RandomAccessIter
00410 random_sample(_InputIter __first, _InputIter __last,
00411               _RandomAccessIter __out_first, _RandomAccessIter __out_last);
00412 
00413 template <class _InputIter, class _RandomAccessIter, 
00414           class _RandomNumberGenerator>
00415 _RandomAccessIter
00416 random_sample(_InputIter __first, _InputIter __last,
00417               _RandomAccessIter __out_first, _RandomAccessIter __out_last,
00418               _RandomNumberGenerator& __rand);
00419 
00420 # endif /* _STLP_NO_EXTENSIONS */
00421 
00422 // partition, stable_partition, and their auxiliary functions
00423 
00424 template <class _ForwardIter, class _Predicate>
00425 _ForwardIter partition(_ForwardIter __first, _ForwardIter __last, _Predicate   __pred);
00426 
00427 
00428 template <class _ForwardIter, class _Predicate>
00429 _ForwardIter 
00430 stable_partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred);
00431 
00432 // sort() and its auxiliary functions. 
00433 
00434 template <class _Size>
00435 inline _Size __lg(_Size __n) {
00436   _Size __k;
00437   for (__k = 0; __n != 1; __n >>= 1) ++__k;
00438   return __k;
00439 }
00440 
00441 template <class _RandomAccessIter>
00442 void sort(_RandomAccessIter __first, _RandomAccessIter __last);
00443 template <class _RandomAccessIter, class _Compare>
00444 void sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp);
00445 
00446 // stable_sort() and its auxiliary functions.
00447 template <class _RandomAccessIter>
00448 void stable_sort(_RandomAccessIter __first,
00449                  _RandomAccessIter __last);
00450 
00451 template <class _RandomAccessIter, class _Compare>
00452 void stable_sort(_RandomAccessIter __first,
00453                  _RandomAccessIter __last, _Compare __comp);
00454 
00455 // partial_sort, partial_sort_copy, and auxiliary functions.
00456 
00457 template <class _RandomAccessIter>
00458 void 
00459 partial_sort(_RandomAccessIter __first,_RandomAccessIter __middle, _RandomAccessIter __last);
00460 
00461 template <class _RandomAccessIter, class _Compare>
00462 void 
00463 partial_sort(_RandomAccessIter __first,_RandomAccessIter __middle, 
00464              _RandomAccessIter __last, _Compare __comp);
00465 
00466 template <class _InputIter, class _RandomAccessIter>
00467 _RandomAccessIter
00468 partial_sort_copy(_InputIter __first, _InputIter __last,
00469                   _RandomAccessIter __result_first, _RandomAccessIter __result_last);
00470 
00471 template <class _InputIter, class _RandomAccessIter, class _Compare>
00472 _RandomAccessIter
00473 partial_sort_copy(_InputIter __first, _InputIter __last,
00474                   _RandomAccessIter __result_first,
00475                   _RandomAccessIter __result_last, _Compare __comp);
00476 
00477 // nth_element() and its auxiliary functions.  
00478 
00479 template <class _RandomAccessIter>
00480 void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,
00481                  _RandomAccessIter __last);
00482 
00483 template <class _RandomAccessIter, class _Compare>
00484 void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,
00485                  _RandomAccessIter __last, _Compare __comp);
00486 
00487 // Binary search (lower_bound, upper_bound, equal_range, binary_search).
00488 
00489 template <class _ForwardIter, class _Tp>
00490 inline _ForwardIter lower_bound(_ForwardIter __first, _ForwardIter __last,
00491                                    const _Tp& __val) {
00492   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00493   return __lower_bound(__first, __last, __val, less<_Tp>(), _STLP_DISTANCE_TYPE(__first, _ForwardIter));
00494 }
00495 
00496 template <class _ForwardIter, class _Tp, class _Compare>
00497 inline _ForwardIter lower_bound(_ForwardIter __first, _ForwardIter __last,
00498                                 const _Tp& __val, _Compare __comp) {
00499   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00500   return __lower_bound(__first, __last, __val, __comp, _STLP_DISTANCE_TYPE(__first, _ForwardIter));
00501 }
00502 
00503 template <class _ForwardIter, class _Tp, class _Compare, class _Distance>
00504 _ForwardIter __upper_bound(_ForwardIter __first, _ForwardIter __last,
00505                            const _Tp& __val, _Compare __comp, _Distance*);
00506 
00507 template <class _ForwardIter, class _Tp>
00508 inline _ForwardIter upper_bound(_ForwardIter __first, _ForwardIter __last,
00509                                 const _Tp& __val) {
00510   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00511   return __upper_bound(__first, __last, __val, less<_Tp>(), 
00512                        _STLP_DISTANCE_TYPE(__first, _ForwardIter));
00513 }
00514 
00515 template <class _ForwardIter, class _Tp, class _Compare>
00516 inline _ForwardIter upper_bound(_ForwardIter __first, _ForwardIter __last,
00517                                 const _Tp& __val, _Compare __comp) {
00518   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00519   return __upper_bound(__first, __last, __val, __comp,
00520                        _STLP_DISTANCE_TYPE(__first, _ForwardIter));
00521 }
00522 
00523 template <class _ForwardIter, class _Tp, class _Compare, class _Distance>
00524 pair<_ForwardIter, _ForwardIter>
00525 __equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
00526               _Compare __comp, _Distance*);
00527 
00528 template <class _ForwardIter, class _Tp>
00529 inline pair<_ForwardIter, _ForwardIter>
00530 equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val) {
00531   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00532   return __equal_range(__first, __last, __val,  less<_Tp>(),
00533                        _STLP_DISTANCE_TYPE(__first, _ForwardIter));
00534 }
00535 
00536 template <class _ForwardIter, class _Tp, class _Compare>
00537 inline pair<_ForwardIter, _ForwardIter>
00538 equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
00539             _Compare __comp) {
00540   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00541   return __equal_range(__first, __last, __val, __comp,
00542                        _STLP_DISTANCE_TYPE(__first, _ForwardIter));
00543 } 
00544 
00545 template <class _ForwardIter, class _Tp>
00546 inline bool binary_search(_ForwardIter __first, _ForwardIter __last,
00547                    const _Tp& __val) {
00548   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00549   _ForwardIter __i = __lower_bound(__first, __last, __val, less<_Tp>(), _STLP_DISTANCE_TYPE(__first, _ForwardIter));
00550   return __i != __last && !(__val < *__i);
00551 }
00552 
00553 template <class _ForwardIter, class _Tp, class _Compare>
00554 inline bool binary_search(_ForwardIter __first, _ForwardIter __last,
00555                    const _Tp& __val,
00556                    _Compare __comp) {
00557   _STLP_DEBUG_CHECK(__check_range(__first, __last))
00558   _ForwardIter __i = __lower_bound(__first, __last, __val, __comp, _STLP_DISTANCE_TYPE(__first, _ForwardIter));
00559   return __i != __last && !__comp(__val, *__i);
00560 }
00561 
00562 // merge, with and without an explicitly supplied comparison function.
00563 
00564 template <class _InputIter1, class _InputIter2, class _OutputIter>
00565 _OutputIter merge(_InputIter1 __first1, _InputIter1 __last1,
00566                   _InputIter2 __first2, _InputIter2 __last2,
00567                   _OutputIter __result);
00568  
00569 template <class _InputIter1, class _InputIter2, class _OutputIter,
00570           class _Compare>
00571 _OutputIter merge(_InputIter1 __first1, _InputIter1 __last1,
00572                   _InputIter2 __first2, _InputIter2 __last2,
00573                   _OutputIter __result, _Compare __comp);
00574 
00575 
00576 // inplace_merge and its auxiliary functions. 
00577 
00578 
00579 template <class _BidirectionalIter>
00580 void inplace_merge(_BidirectionalIter __first,
00581                    _BidirectionalIter __middle,
00582                    _BidirectionalIter __last) ;
00583 
00584 template <class _BidirectionalIter, class _Compare>
00585 void inplace_merge(_BidirectionalIter __first,
00586                    _BidirectionalIter __middle,
00587                    _BidirectionalIter __last, _Compare __comp);
00588 
00589 // Set algorithms: includes, set_union, set_intersection, set_difference,
00590 // set_symmetric_difference.  All of these algorithms have the precondition
00591 // that their input ranges are sorted and the postcondition that their output
00592 // ranges are sorted.
00593 
00594 template <class _InputIter1, class _InputIter2>
00595 bool includes(_InputIter1 __first1, _InputIter1 __last1,
00596               _InputIter2 __first2, _InputIter2 __last2);
00597 
00598 template <class _InputIter1, class _InputIter2, class _Compare>
00599 bool includes(_InputIter1 __first1, _InputIter1 __last1,
00600               _InputIter2 __first2, _InputIter2 __last2, _Compare __comp);
00601  
00602 template <class _InputIter1, class _InputIter2, class _OutputIter>
00603 _OutputIter set_union(_InputIter1 __first1, _InputIter1 __last1,
00604                       _InputIter2 __first2, _InputIter2 __last2,
00605                       _OutputIter __result);
00606 
00607 template <class _InputIter1, class _InputIter2, class _OutputIter,
00608           class _Compare>
00609 _OutputIter set_union(_InputIter1 __first1, _InputIter1 __last1,
00610                       _InputIter2 __first2, _InputIter2 __last2,
00611                       _OutputIter __result, _Compare __comp);
00612 
00613 template <class _InputIter1, class _InputIter2, class _OutputIter>
00614 _OutputIter set_intersection(_InputIter1 __first1, _InputIter1 __last1,
00615                              _InputIter2 __first2, _InputIter2 __last2,
00616                              _OutputIter __result);
00617 
00618 template <class _InputIter1, class _InputIter2, class _OutputIter,
00619           class _Compare>
00620 _OutputIter set_intersection(_InputIter1 __first1, _InputIter1 __last1,
00621                              _InputIter2 __first2, _InputIter2 __last2,
00622                              _OutputIter __result, _Compare __comp);
00623 
00624 
00625 
00626 template <class _InputIter1, class _InputIter2, class _OutputIter>
00627 _OutputIter set_difference(_InputIter1 __first1, _InputIter1 __last1,
00628                            _InputIter2 __first2, _InputIter2 __last2,
00629                            _OutputIter __result);
00630 
00631 template <class _InputIter1, class _InputIter2, class _OutputIter, 
00632           class _Compare>
00633 _OutputIter set_difference(_InputIter1 __first1, _InputIter1 __last1,
00634                            _InputIter2 __first2, _InputIter2 __last2, 
00635                            _OutputIter __result, _Compare __comp);
00636 
00637 template <class _InputIter1, class _InputIter2, class _OutputIter>
00638 _OutputIter 
00639 set_symmetric_difference(_InputIter1 __first1, _InputIter1 __last1,
00640                          _InputIter2 __first2, _InputIter2 __last2,
00641                          _OutputIter __result);
00642 
00643 
00644 template <class _InputIter1, class _InputIter2, class _OutputIter,
00645           class _Compare>
00646 _OutputIter 
00647 set_symmetric_difference(_InputIter1 __first1, _InputIter1 __last1,
00648                          _InputIter2 __first2, _InputIter2 __last2,
00649                          _OutputIter __result,
00650                          _Compare __comp);
00651 
00652 
00653 // min_element and max_element, with and without an explicitly supplied
00654 // comparison function.
00655 
00656 template <class _ForwardIter>
00657 _ForwardIter max_element(_ForwardIter __first, _ForwardIter __last);
00658 template <class _ForwardIter, class _Compare>
00659 _ForwardIter max_element(_ForwardIter __first, _ForwardIter __last,
00660                             _Compare __comp);
00661 
00662 template <class _ForwardIter>
00663 _ForwardIter min_element(_ForwardIter __first, _ForwardIter __last);
00664 
00665 template <class _ForwardIter, class _Compare>
00666 _ForwardIter min_element(_ForwardIter __first, _ForwardIter __last,
00667                             _Compare __comp);
00668 
00669 // next_permutation and prev_permutation, with and without an explicitly 
00670 // supplied comparison function.
00671 
00672 template <class _BidirectionalIter>
00673 bool next_permutation(_BidirectionalIter __first, _BidirectionalIter __last);
00674 
00675 template <class _BidirectionalIter, class _Compare>
00676 bool next_permutation(_BidirectionalIter __first, _BidirectionalIter __last,
00677                       _Compare __comp);
00678 
00679 
00680 template <class _BidirectionalIter>
00681 bool prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last);
00682 
00683 
00684 template <class _BidirectionalIter, class _Compare>
00685 bool prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last,
00686                       _Compare __comp);
00687 
00688 # ifndef _STLP_NO_EXTENSIONS
00689 
00690 // is_heap, a predicate testing whether or not a range is
00691 // a heap.  This function is an extension, not part of the C++
00692 // standard.
00693 
00694 template <class _RandomAccessIter>
00695 bool is_heap(_RandomAccessIter __first, _RandomAccessIter __last);
00696 
00697 template <class _RandomAccessIter, class _StrictWeakOrdering>
00698 bool is_heap(_RandomAccessIter __first, _RandomAccessIter __last,
00699              _StrictWeakOrdering __comp);
00700 
00701 
00702 // is_sorted, a predicated testing whether a range is sorted in
00703 // nondescending order.  This is an extension, not part of the C++
00704 // standard.
00705 template <class _ForwardIter, class _StrictWeakOrdering>
00706 bool __is_sorted(_ForwardIter __first, _ForwardIter __last,
00707                  _StrictWeakOrdering __comp);
00708 
00709 template <class _ForwardIter>
00710 inline bool is_sorted(_ForwardIter __first, _ForwardIter __last) {
00711   return __is_sorted(__first, __last, __less(_STLP_VALUE_TYPE(__first, _ForwardIter)));
00712 }
00713 
00714 template <class _ForwardIter, class _StrictWeakOrdering>
00715 inline bool is_sorted(_ForwardIter __first, _ForwardIter __last,
00716                       _StrictWeakOrdering __comp) {
00717   return __is_sorted(__first, __last, __comp);
00718 }
00719 # endif
00720 
00721 _STLP_END_NAMESPACE
00722 
00723 # if !defined (_STLP_LINK_TIME_INSTANTIATION)
00724 #  include <stl/_algo.c>
00725 # endif
00726 
00727 #endif /* _STLP_INTERNAL_ALGO_H */
00728 
00729 // Local Variables:
00730 // mode:C++
00731 // End:
00732 

Generated on Mon Jun 5 10:20:45 2006 for Intelligence.kdevelop by  doxygen 1.4.6