algorithm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // Algorithm extensions -*- C++ -*-
  2. // Copyright (C) 2001-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file ext/algorithm
  46. * This file is a GNU extension to the Standard C++ Library (possibly
  47. * containing extensions from the HP/SGI STL subset).
  48. */
  49. #ifndef _EXT_ALGORITHM
  50. #define _EXT_ALGORITHM 1
  51. #pragma GCC system_header
  52. #include <bits/requires_hosted.h> // GNU extensions are currently omitted
  53. #include <algorithm>
  54. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  55. {
  56. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  57. //--------------------------------------------------
  58. // copy_n (not part of the C++ standard)
  59. template<typename _InputIterator, typename _Size, typename _OutputIterator>
  60. std::pair<_InputIterator, _OutputIterator>
  61. __copy_n(_InputIterator __first, _Size __count,
  62. _OutputIterator __result,
  63. std::input_iterator_tag)
  64. {
  65. for ( ; __count > 0; --__count)
  66. {
  67. *__result = *__first;
  68. ++__first;
  69. ++__result;
  70. }
  71. return std::pair<_InputIterator, _OutputIterator>(__first, __result);
  72. }
  73. template<typename _RAIterator, typename _Size, typename _OutputIterator>
  74. inline std::pair<_RAIterator, _OutputIterator>
  75. __copy_n(_RAIterator __first, _Size __count,
  76. _OutputIterator __result,
  77. std::random_access_iterator_tag)
  78. {
  79. _RAIterator __last = __first + __count;
  80. return std::pair<_RAIterator, _OutputIterator>(__last, std::copy(__first,
  81. __last,
  82. __result));
  83. }
  84. /**
  85. * @brief Copies the range [first,first+count) into [result,result+count).
  86. * @param __first An input iterator.
  87. * @param __count The number of elements to copy.
  88. * @param __result An output iterator.
  89. * @return A std::pair composed of first+count and result+count.
  90. *
  91. * This is an SGI extension.
  92. * This inline function will boil down to a call to @c memmove whenever
  93. * possible. Failing that, if random access iterators are passed, then the
  94. * loop count will be known (and therefore a candidate for compiler
  95. * optimizations such as unrolling).
  96. * @ingroup SGIextensions
  97. */
  98. template<typename _InputIterator, typename _Size, typename _OutputIterator>
  99. inline std::pair<_InputIterator, _OutputIterator>
  100. copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
  101. {
  102. // concept requirements
  103. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  104. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  105. typename std::iterator_traits<_InputIterator>::value_type>)
  106. return __gnu_cxx::__copy_n(__first, __count, __result,
  107. std::__iterator_category(__first));
  108. }
  109. template<typename _InputIterator1, typename _InputIterator2>
  110. int
  111. __lexicographical_compare_3way(_InputIterator1 __first1,
  112. _InputIterator1 __last1,
  113. _InputIterator2 __first2,
  114. _InputIterator2 __last2)
  115. {
  116. while (__first1 != __last1 && __first2 != __last2)
  117. {
  118. if (*__first1 < *__first2)
  119. return -1;
  120. if (*__first2 < *__first1)
  121. return 1;
  122. ++__first1;
  123. ++__first2;
  124. }
  125. if (__first2 == __last2)
  126. return !(__first1 == __last1);
  127. else
  128. return -1;
  129. }
  130. inline int
  131. __lexicographical_compare_3way(const unsigned char* __first1,
  132. const unsigned char* __last1,
  133. const unsigned char* __first2,
  134. const unsigned char* __last2)
  135. {
  136. const std::ptrdiff_t __len1 = __last1 - __first1;
  137. const std::ptrdiff_t __len2 = __last2 - __first2;
  138. const int __result = __builtin_memcmp(__first1, __first2,
  139. (std::min)(__len1, __len2));
  140. return __result != 0 ? __result
  141. : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1));
  142. }
  143. inline int
  144. __lexicographical_compare_3way(const char* __first1, const char* __last1,
  145. const char* __first2, const char* __last2)
  146. {
  147. #if CHAR_MAX == SCHAR_MAX
  148. return __lexicographical_compare_3way((const signed char*) __first1,
  149. (const signed char*) __last1,
  150. (const signed char*) __first2,
  151. (const signed char*) __last2);
  152. #else
  153. return __lexicographical_compare_3way((const unsigned char*) __first1,
  154. (const unsigned char*) __last1,
  155. (const unsigned char*) __first2,
  156. (const unsigned char*) __last2);
  157. #endif
  158. }
  159. /**
  160. * @brief @c memcmp on steroids.
  161. * @param __first1 An input iterator.
  162. * @param __last1 An input iterator.
  163. * @param __first2 An input iterator.
  164. * @param __last2 An input iterator.
  165. * @return An int, as with @c memcmp.
  166. *
  167. * The return value will be less than zero if the first range is
  168. * <em>lexigraphically less than</em> the second, greater than zero
  169. * if the second range is <em>lexigraphically less than</em> the
  170. * first, and zero otherwise.
  171. * This is an SGI extension.
  172. * @ingroup SGIextensions
  173. */
  174. template<typename _InputIterator1, typename _InputIterator2>
  175. int
  176. lexicographical_compare_3way(_InputIterator1 __first1,
  177. _InputIterator1 __last1,
  178. _InputIterator2 __first2,
  179. _InputIterator2 __last2)
  180. {
  181. // concept requirements
  182. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  183. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  184. __glibcxx_function_requires(_LessThanComparableConcept<
  185. typename std::iterator_traits<_InputIterator1>::value_type>)
  186. __glibcxx_function_requires(_LessThanComparableConcept<
  187. typename std::iterator_traits<_InputIterator2>::value_type>)
  188. __glibcxx_requires_valid_range(__first1, __last1);
  189. __glibcxx_requires_valid_range(__first2, __last2);
  190. return __lexicographical_compare_3way(__first1, __last1, __first2,
  191. __last2);
  192. }
  193. // count and count_if: this version, whose return type is void, was present
  194. // in the HP STL, and is retained as an extension for backward compatibility.
  195. template<typename _InputIterator, typename _Tp, typename _Size>
  196. void
  197. count(_InputIterator __first, _InputIterator __last,
  198. const _Tp& __value,
  199. _Size& __n)
  200. {
  201. // concept requirements
  202. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  203. __glibcxx_function_requires(_EqualityComparableConcept<
  204. typename std::iterator_traits<_InputIterator>::value_type >)
  205. __glibcxx_function_requires(_EqualityComparableConcept<_Tp>)
  206. __glibcxx_requires_valid_range(__first, __last);
  207. for ( ; __first != __last; ++__first)
  208. if (*__first == __value)
  209. ++__n;
  210. }
  211. template<typename _InputIterator, typename _Predicate, typename _Size>
  212. void
  213. count_if(_InputIterator __first, _InputIterator __last,
  214. _Predicate __pred,
  215. _Size& __n)
  216. {
  217. // concept requirements
  218. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  219. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  220. typename std::iterator_traits<_InputIterator>::value_type>)
  221. __glibcxx_requires_valid_range(__first, __last);
  222. for ( ; __first != __last; ++__first)
  223. if (__pred(*__first))
  224. ++__n;
  225. }
  226. // random_sample and random_sample_n (extensions, not part of the standard).
  227. /**
  228. * This is an SGI extension.
  229. * @ingroup SGIextensions
  230. * @doctodo
  231. */
  232. template<typename _ForwardIterator, typename _OutputIterator,
  233. typename _Distance>
  234. _OutputIterator
  235. random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
  236. _OutputIterator __out, const _Distance __n)
  237. {
  238. // concept requirements
  239. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  240. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  241. typename std::iterator_traits<_ForwardIterator>::value_type>)
  242. __glibcxx_requires_valid_range(__first, __last);
  243. _Distance __remaining = std::distance(__first, __last);
  244. _Distance __m = (std::min)(__n, __remaining);
  245. while (__m > 0)
  246. {
  247. if ((std::rand() % __remaining) < __m)
  248. {
  249. *__out = *__first;
  250. ++__out;
  251. --__m;
  252. }
  253. --__remaining;
  254. ++__first;
  255. }
  256. return __out;
  257. }
  258. /**
  259. * This is an SGI extension.
  260. * @ingroup SGIextensions
  261. * @doctodo
  262. */
  263. template<typename _ForwardIterator, typename _OutputIterator,
  264. typename _Distance, typename _RandomNumberGenerator>
  265. _OutputIterator
  266. random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
  267. _OutputIterator __out, const _Distance __n,
  268. _RandomNumberGenerator& __rand)
  269. {
  270. // concept requirements
  271. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  272. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  273. typename std::iterator_traits<_ForwardIterator>::value_type>)
  274. __glibcxx_function_requires(_UnaryFunctionConcept<
  275. _RandomNumberGenerator, _Distance, _Distance>)
  276. __glibcxx_requires_valid_range(__first, __last);
  277. _Distance __remaining = std::distance(__first, __last);
  278. _Distance __m = (std::min)(__n, __remaining);
  279. while (__m > 0)
  280. {
  281. if (__rand(__remaining) < __m)
  282. {
  283. *__out = *__first;
  284. ++__out;
  285. --__m;
  286. }
  287. --__remaining;
  288. ++__first;
  289. }
  290. return __out;
  291. }
  292. template<typename _InputIterator, typename _RandomAccessIterator,
  293. typename _Distance>
  294. _RandomAccessIterator
  295. __random_sample(_InputIterator __first, _InputIterator __last,
  296. _RandomAccessIterator __out,
  297. const _Distance __n)
  298. {
  299. _Distance __m = 0;
  300. _Distance __t = __n;
  301. for ( ; __first != __last && __m < __n; ++__m, ++__first)
  302. __out[__m] = *__first;
  303. while (__first != __last)
  304. {
  305. ++__t;
  306. _Distance __M = std::rand() % (__t);
  307. if (__M < __n)
  308. __out[__M] = *__first;
  309. ++__first;
  310. }
  311. return __out + __m;
  312. }
  313. template<typename _InputIterator, typename _RandomAccessIterator,
  314. typename _RandomNumberGenerator, typename _Distance>
  315. _RandomAccessIterator
  316. __random_sample(_InputIterator __first, _InputIterator __last,
  317. _RandomAccessIterator __out,
  318. _RandomNumberGenerator& __rand,
  319. const _Distance __n)
  320. {
  321. // concept requirements
  322. __glibcxx_function_requires(_UnaryFunctionConcept<
  323. _RandomNumberGenerator, _Distance, _Distance>)
  324. _Distance __m = 0;
  325. _Distance __t = __n;
  326. for ( ; __first != __last && __m < __n; ++__m, ++__first)
  327. __out[__m] = *__first;
  328. while (__first != __last)
  329. {
  330. ++__t;
  331. _Distance __M = __rand(__t);
  332. if (__M < __n)
  333. __out[__M] = *__first;
  334. ++__first;
  335. }
  336. return __out + __m;
  337. }
  338. /**
  339. * This is an SGI extension.
  340. * @ingroup SGIextensions
  341. * @doctodo
  342. */
  343. template<typename _InputIterator, typename _RandomAccessIterator>
  344. inline _RandomAccessIterator
  345. random_sample(_InputIterator __first, _InputIterator __last,
  346. _RandomAccessIterator __out_first,
  347. _RandomAccessIterator __out_last)
  348. {
  349. // concept requirements
  350. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  351. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  352. _RandomAccessIterator>)
  353. __glibcxx_requires_valid_range(__first, __last);
  354. __glibcxx_requires_valid_range(__out_first, __out_last);
  355. return __random_sample(__first, __last,
  356. __out_first, __out_last - __out_first);
  357. }
  358. /**
  359. * This is an SGI extension.
  360. * @ingroup SGIextensions
  361. * @doctodo
  362. */
  363. template<typename _InputIterator, typename _RandomAccessIterator,
  364. typename _RandomNumberGenerator>
  365. inline _RandomAccessIterator
  366. random_sample(_InputIterator __first, _InputIterator __last,
  367. _RandomAccessIterator __out_first,
  368. _RandomAccessIterator __out_last,
  369. _RandomNumberGenerator& __rand)
  370. {
  371. // concept requirements
  372. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  373. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  374. _RandomAccessIterator>)
  375. __glibcxx_requires_valid_range(__first, __last);
  376. __glibcxx_requires_valid_range(__out_first, __out_last);
  377. return __random_sample(__first, __last,
  378. __out_first, __rand,
  379. __out_last - __out_first);
  380. }
  381. #if __cplusplus >= 201103L
  382. using std::is_heap;
  383. #else
  384. /**
  385. * This is an SGI extension.
  386. * @ingroup SGIextensions
  387. * @doctodo
  388. */
  389. template<typename _RandomAccessIterator>
  390. inline bool
  391. is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
  392. {
  393. // concept requirements
  394. __glibcxx_function_requires(_RandomAccessIteratorConcept<
  395. _RandomAccessIterator>)
  396. __glibcxx_function_requires(_LessThanComparableConcept<
  397. typename std::iterator_traits<_RandomAccessIterator>::value_type>)
  398. __glibcxx_requires_valid_range(__first, __last);
  399. return std::__is_heap(__first, __last - __first);
  400. }
  401. /**
  402. * This is an SGI extension.
  403. * @ingroup SGIextensions
  404. * @doctodo
  405. */
  406. template<typename _RandomAccessIterator, typename _StrictWeakOrdering>
  407. inline bool
  408. is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
  409. _StrictWeakOrdering __comp)
  410. {
  411. // concept requirements
  412. __glibcxx_function_requires(_RandomAccessIteratorConcept<
  413. _RandomAccessIterator>)
  414. __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
  415. typename std::iterator_traits<_RandomAccessIterator>::value_type,
  416. typename std::iterator_traits<_RandomAccessIterator>::value_type>)
  417. __glibcxx_requires_valid_range(__first, __last);
  418. return std::__is_heap(__first, __comp, __last - __first);
  419. }
  420. #endif
  421. #if __cplusplus >= 201103L
  422. using std::is_sorted;
  423. #else
  424. // is_sorted, a predicated testing whether a range is sorted in
  425. // nondescending order. This is an extension, not part of the C++
  426. // standard.
  427. /**
  428. * This is an SGI extension.
  429. * @ingroup SGIextensions
  430. * @doctodo
  431. */
  432. template<typename _ForwardIterator>
  433. bool
  434. is_sorted(_ForwardIterator __first, _ForwardIterator __last)
  435. {
  436. // concept requirements
  437. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  438. __glibcxx_function_requires(_LessThanComparableConcept<
  439. typename std::iterator_traits<_ForwardIterator>::value_type>)
  440. __glibcxx_requires_valid_range(__first, __last);
  441. if (__first == __last)
  442. return true;
  443. _ForwardIterator __next = __first;
  444. for (++__next; __next != __last; __first = __next, ++__next)
  445. if (*__next < *__first)
  446. return false;
  447. return true;
  448. }
  449. /**
  450. * This is an SGI extension.
  451. * @ingroup SGIextensions
  452. * @doctodo
  453. */
  454. template<typename _ForwardIterator, typename _StrictWeakOrdering>
  455. bool
  456. is_sorted(_ForwardIterator __first, _ForwardIterator __last,
  457. _StrictWeakOrdering __comp)
  458. {
  459. // concept requirements
  460. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  461. __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
  462. typename std::iterator_traits<_ForwardIterator>::value_type,
  463. typename std::iterator_traits<_ForwardIterator>::value_type>)
  464. __glibcxx_requires_valid_range(__first, __last);
  465. if (__first == __last)
  466. return true;
  467. _ForwardIterator __next = __first;
  468. for (++__next; __next != __last; __first = __next, ++__next)
  469. if (__comp(*__next, *__first))
  470. return false;
  471. return true;
  472. }
  473. #endif // C++11
  474. /**
  475. * @brief Find the median of three values.
  476. * @param __a A value.
  477. * @param __b A value.
  478. * @param __c A value.
  479. * @return One of @p a, @p b or @p c.
  480. *
  481. * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
  482. * then the value returned will be @c m.
  483. * This is an SGI extension.
  484. * @ingroup SGIextensions
  485. */
  486. template<typename _Tp>
  487. const _Tp&
  488. __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
  489. {
  490. // concept requirements
  491. __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
  492. if (__a < __b)
  493. if (__b < __c)
  494. return __b;
  495. else if (__a < __c)
  496. return __c;
  497. else
  498. return __a;
  499. else if (__a < __c)
  500. return __a;
  501. else if (__b < __c)
  502. return __c;
  503. else
  504. return __b;
  505. }
  506. /**
  507. * @brief Find the median of three values using a predicate for comparison.
  508. * @param __a A value.
  509. * @param __b A value.
  510. * @param __c A value.
  511. * @param __comp A binary predicate.
  512. * @return One of @p a, @p b or @p c.
  513. *
  514. * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
  515. * and @p comp(m,n) are both true then the value returned will be @c m.
  516. * This is an SGI extension.
  517. * @ingroup SGIextensions
  518. */
  519. template<typename _Tp, typename _Compare>
  520. const _Tp&
  521. __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
  522. {
  523. // concept requirements
  524. __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
  525. _Tp, _Tp>)
  526. if (__comp(__a, __b))
  527. if (__comp(__b, __c))
  528. return __b;
  529. else if (__comp(__a, __c))
  530. return __c;
  531. else
  532. return __a;
  533. else if (__comp(__a, __c))
  534. return __a;
  535. else if (__comp(__b, __c))
  536. return __c;
  537. else
  538. return __b;
  539. }
  540. _GLIBCXX_END_NAMESPACE_VERSION
  541. } // namespace
  542. #endif /* _EXT_ALGORITHM */