_algo.h 28 KB

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