algorithm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // Algorithm extensions -*- C++ -*-
  2. // Copyright (C) 2001-2018 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 <algorithm>
  53. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  54. {
  55. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  56. using std::ptrdiff_t;
  57. using std::min;
  58. using std::pair;
  59. using std::input_iterator_tag;
  60. using std::random_access_iterator_tag;
  61. using std::iterator_traits;
  62. //--------------------------------------------------
  63. // copy_n (not part of the C++ standard)
  64. template<typename _InputIterator, typename _Size, typename _OutputIterator>
  65. pair<_InputIterator, _OutputIterator>
  66. __copy_n(_InputIterator __first, _Size __count,
  67. _OutputIterator __result,
  68. input_iterator_tag)
  69. {
  70. for ( ; __count > 0; --__count)
  71. {
  72. *__result = *__first;
  73. ++__first;
  74. ++__result;
  75. }
  76. return pair<_InputIterator, _OutputIterator>(__first, __result);
  77. }
  78. template<typename _RAIterator, typename _Size, typename _OutputIterator>
  79. inline pair<_RAIterator, _OutputIterator>
  80. __copy_n(_RAIterator __first, _Size __count,
  81. _OutputIterator __result,
  82. random_access_iterator_tag)
  83. {
  84. _RAIterator __last = __first + __count;
  85. return pair<_RAIterator, _OutputIterator>(__last, std::copy(__first,
  86. __last,
  87. __result));
  88. }
  89. /**
  90. * @brief Copies the range [first,first+count) into [result,result+count).
  91. * @param __first An input iterator.
  92. * @param __count The number of elements to copy.
  93. * @param __result An output iterator.
  94. * @return A std::pair composed of first+count and result+count.
  95. *
  96. * This is an SGI extension.
  97. * This inline function will boil down to a call to @c memmove whenever
  98. * possible. Failing that, if random access iterators are passed, then the
  99. * loop count will be known (and therefore a candidate for compiler
  100. * optimizations such as unrolling).
  101. * @ingroup SGIextensions
  102. */
  103. template<typename _InputIterator, typename _Size, typename _OutputIterator>
  104. inline pair<_InputIterator, _OutputIterator>
  105. copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
  106. {
  107. // concept requirements
  108. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  109. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  110. typename iterator_traits<_InputIterator>::value_type>)
  111. return __gnu_cxx::__copy_n(__first, __count, __result,
  112. std::__iterator_category(__first));
  113. }
  114. template<typename _InputIterator1, typename _InputIterator2>
  115. int
  116. __lexicographical_compare_3way(_InputIterator1 __first1,
  117. _InputIterator1 __last1,
  118. _InputIterator2 __first2,
  119. _InputIterator2 __last2)
  120. {
  121. while (__first1 != __last1 && __first2 != __last2)
  122. {
  123. if (*__first1 < *__first2)
  124. return -1;
  125. if (*__first2 < *__first1)
  126. return 1;
  127. ++__first1;
  128. ++__first2;
  129. }
  130. if (__first2 == __last2)
  131. return !(__first1 == __last1);
  132. else
  133. return -1;
  134. }
  135. inline int
  136. __lexicographical_compare_3way(const unsigned char* __first1,
  137. const unsigned char* __last1,
  138. const unsigned char* __first2,
  139. const unsigned char* __last2)
  140. {
  141. const ptrdiff_t __len1 = __last1 - __first1;
  142. const ptrdiff_t __len2 = __last2 - __first2;
  143. const int __result = __builtin_memcmp(__first1, __first2,
  144. min(__len1, __len2));
  145. return __result != 0 ? __result
  146. : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1));
  147. }
  148. inline int
  149. __lexicographical_compare_3way(const char* __first1, const char* __last1,
  150. const char* __first2, const char* __last2)
  151. {
  152. #if CHAR_MAX == SCHAR_MAX
  153. return __lexicographical_compare_3way((const signed char*) __first1,
  154. (const signed char*) __last1,
  155. (const signed char*) __first2,
  156. (const signed char*) __last2);
  157. #else
  158. return __lexicographical_compare_3way((const unsigned char*) __first1,
  159. (const unsigned char*) __last1,
  160. (const unsigned char*) __first2,
  161. (const unsigned char*) __last2);
  162. #endif
  163. }
  164. /**
  165. * @brief @c memcmp on steroids.
  166. * @param __first1 An input iterator.
  167. * @param __last1 An input iterator.
  168. * @param __first2 An input iterator.
  169. * @param __last2 An input iterator.
  170. * @return An int, as with @c memcmp.
  171. *
  172. * The return value will be less than zero if the first range is
  173. * <em>lexigraphically less than</em> the second, greater than zero
  174. * if the second range is <em>lexigraphically less than</em> the
  175. * first, and zero otherwise.
  176. * This is an SGI extension.
  177. * @ingroup SGIextensions
  178. */
  179. template<typename _InputIterator1, typename _InputIterator2>
  180. int
  181. lexicographical_compare_3way(_InputIterator1 __first1,
  182. _InputIterator1 __last1,
  183. _InputIterator2 __first2,
  184. _InputIterator2 __last2)
  185. {
  186. // concept requirements
  187. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  188. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  189. __glibcxx_function_requires(_LessThanComparableConcept<
  190. typename iterator_traits<_InputIterator1>::value_type>)
  191. __glibcxx_function_requires(_LessThanComparableConcept<
  192. typename iterator_traits<_InputIterator2>::value_type>)
  193. __glibcxx_requires_valid_range(__first1, __last1);
  194. __glibcxx_requires_valid_range(__first2, __last2);
  195. return __lexicographical_compare_3way(__first1, __last1, __first2,
  196. __last2);
  197. }
  198. // count and count_if: this version, whose return type is void, was present
  199. // in the HP STL, and is retained as an extension for backward compatibility.
  200. template<typename _InputIterator, typename _Tp, typename _Size>
  201. void
  202. count(_InputIterator __first, _InputIterator __last,
  203. const _Tp& __value,
  204. _Size& __n)
  205. {
  206. // concept requirements
  207. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  208. __glibcxx_function_requires(_EqualityComparableConcept<
  209. typename iterator_traits<_InputIterator>::value_type >)
  210. __glibcxx_function_requires(_EqualityComparableConcept<_Tp>)
  211. __glibcxx_requires_valid_range(__first, __last);
  212. for ( ; __first != __last; ++__first)
  213. if (*__first == __value)
  214. ++__n;
  215. }
  216. template<typename _InputIterator, typename _Predicate, typename _Size>
  217. void
  218. count_if(_InputIterator __first, _InputIterator __last,
  219. _Predicate __pred,
  220. _Size& __n)
  221. {
  222. // concept requirements
  223. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  224. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  225. typename iterator_traits<_InputIterator>::value_type>)
  226. __glibcxx_requires_valid_range(__first, __last);
  227. for ( ; __first != __last; ++__first)
  228. if (__pred(*__first))
  229. ++__n;
  230. }
  231. // random_sample and random_sample_n (extensions, not part of the standard).
  232. /**
  233. * This is an SGI extension.
  234. * @ingroup SGIextensions
  235. * @doctodo
  236. */
  237. template<typename _ForwardIterator, typename _OutputIterator,
  238. typename _Distance>
  239. _OutputIterator
  240. random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
  241. _OutputIterator __out, const _Distance __n)
  242. {
  243. // concept requirements
  244. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  245. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  246. typename iterator_traits<_ForwardIterator>::value_type>)
  247. __glibcxx_requires_valid_range(__first, __last);
  248. _Distance __remaining = std::distance(__first, __last);
  249. _Distance __m = min(__n, __remaining);
  250. while (__m > 0)
  251. {
  252. if ((std::rand() % __remaining) < __m)
  253. {
  254. *__out = *__first;
  255. ++__out;
  256. --__m;
  257. }
  258. --__remaining;
  259. ++__first;
  260. }
  261. return __out;
  262. }
  263. /**
  264. * This is an SGI extension.
  265. * @ingroup SGIextensions
  266. * @doctodo
  267. */
  268. template<typename _ForwardIterator, typename _OutputIterator,
  269. typename _Distance, typename _RandomNumberGenerator>
  270. _OutputIterator
  271. random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
  272. _OutputIterator __out, const _Distance __n,
  273. _RandomNumberGenerator& __rand)
  274. {
  275. // concept requirements
  276. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  277. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  278. typename iterator_traits<_ForwardIterator>::value_type>)
  279. __glibcxx_function_requires(_UnaryFunctionConcept<
  280. _RandomNumberGenerator, _Distance, _Distance>)
  281. __glibcxx_requires_valid_range(__first, __last);
  282. _Distance __remaining = std::distance(__first, __last);
  283. _Distance __m = min(__n, __remaining);
  284. while (__m > 0)
  285. {
  286. if (__rand(__remaining) < __m)
  287. {
  288. *__out = *__first;
  289. ++__out;
  290. --__m;
  291. }
  292. --__remaining;
  293. ++__first;
  294. }
  295. return __out;
  296. }
  297. template<typename _InputIterator, typename _RandomAccessIterator,
  298. typename _Distance>
  299. _RandomAccessIterator
  300. __random_sample(_InputIterator __first, _InputIterator __last,
  301. _RandomAccessIterator __out,
  302. const _Distance __n)
  303. {
  304. _Distance __m = 0;
  305. _Distance __t = __n;
  306. for ( ; __first != __last && __m < __n; ++__m, ++__first)
  307. __out[__m] = *__first;
  308. while (__first != __last)
  309. {
  310. ++__t;
  311. _Distance __M = std::rand() % (__t);
  312. if (__M < __n)
  313. __out[__M] = *__first;
  314. ++__first;
  315. }
  316. return __out + __m;
  317. }
  318. template<typename _InputIterator, typename _RandomAccessIterator,
  319. typename _RandomNumberGenerator, typename _Distance>
  320. _RandomAccessIterator
  321. __random_sample(_InputIterator __first, _InputIterator __last,
  322. _RandomAccessIterator __out,
  323. _RandomNumberGenerator& __rand,
  324. const _Distance __n)
  325. {
  326. // concept requirements
  327. __glibcxx_function_requires(_UnaryFunctionConcept<
  328. _RandomNumberGenerator, _Distance, _Distance>)
  329. _Distance __m = 0;
  330. _Distance __t = __n;
  331. for ( ; __first != __last && __m < __n; ++__m, ++__first)
  332. __out[__m] = *__first;
  333. while (__first != __last)
  334. {
  335. ++__t;
  336. _Distance __M = __rand(__t);
  337. if (__M < __n)
  338. __out[__M] = *__first;
  339. ++__first;
  340. }
  341. return __out + __m;
  342. }
  343. /**
  344. * This is an SGI extension.
  345. * @ingroup SGIextensions
  346. * @doctodo
  347. */
  348. template<typename _InputIterator, typename _RandomAccessIterator>
  349. inline _RandomAccessIterator
  350. random_sample(_InputIterator __first, _InputIterator __last,
  351. _RandomAccessIterator __out_first,
  352. _RandomAccessIterator __out_last)
  353. {
  354. // concept requirements
  355. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  356. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  357. _RandomAccessIterator>)
  358. __glibcxx_requires_valid_range(__first, __last);
  359. __glibcxx_requires_valid_range(__out_first, __out_last);
  360. return __random_sample(__first, __last,
  361. __out_first, __out_last - __out_first);
  362. }
  363. /**
  364. * This is an SGI extension.
  365. * @ingroup SGIextensions
  366. * @doctodo
  367. */
  368. template<typename _InputIterator, typename _RandomAccessIterator,
  369. typename _RandomNumberGenerator>
  370. inline _RandomAccessIterator
  371. random_sample(_InputIterator __first, _InputIterator __last,
  372. _RandomAccessIterator __out_first,
  373. _RandomAccessIterator __out_last,
  374. _RandomNumberGenerator& __rand)
  375. {
  376. // concept requirements
  377. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  378. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  379. _RandomAccessIterator>)
  380. __glibcxx_requires_valid_range(__first, __last);
  381. __glibcxx_requires_valid_range(__out_first, __out_last);
  382. return __random_sample(__first, __last,
  383. __out_first, __rand,
  384. __out_last - __out_first);
  385. }
  386. #if __cplusplus >= 201103L
  387. using std::is_heap;
  388. #else
  389. /**
  390. * This is an SGI extension.
  391. * @ingroup SGIextensions
  392. * @doctodo
  393. */
  394. template<typename _RandomAccessIterator>
  395. inline bool
  396. is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
  397. {
  398. // concept requirements
  399. __glibcxx_function_requires(_RandomAccessIteratorConcept<
  400. _RandomAccessIterator>)
  401. __glibcxx_function_requires(_LessThanComparableConcept<
  402. typename iterator_traits<_RandomAccessIterator>::value_type>)
  403. __glibcxx_requires_valid_range(__first, __last);
  404. return std::__is_heap(__first, __last - __first);
  405. }
  406. /**
  407. * This is an SGI extension.
  408. * @ingroup SGIextensions
  409. * @doctodo
  410. */
  411. template<typename _RandomAccessIterator, typename _StrictWeakOrdering>
  412. inline bool
  413. is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
  414. _StrictWeakOrdering __comp)
  415. {
  416. // concept requirements
  417. __glibcxx_function_requires(_RandomAccessIteratorConcept<
  418. _RandomAccessIterator>)
  419. __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
  420. typename iterator_traits<_RandomAccessIterator>::value_type,
  421. typename iterator_traits<_RandomAccessIterator>::value_type>)
  422. __glibcxx_requires_valid_range(__first, __last);
  423. return std::__is_heap(__first, __comp, __last - __first);
  424. }
  425. #endif
  426. #if __cplusplus >= 201103L
  427. using std::is_sorted;
  428. #else
  429. // is_sorted, a predicated testing whether a range is sorted in
  430. // nondescending order. This is an extension, not part of the C++
  431. // standard.
  432. /**
  433. * This is an SGI extension.
  434. * @ingroup SGIextensions
  435. * @doctodo
  436. */
  437. template<typename _ForwardIterator>
  438. bool
  439. is_sorted(_ForwardIterator __first, _ForwardIterator __last)
  440. {
  441. // concept requirements
  442. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  443. __glibcxx_function_requires(_LessThanComparableConcept<
  444. typename iterator_traits<_ForwardIterator>::value_type>)
  445. __glibcxx_requires_valid_range(__first, __last);
  446. if (__first == __last)
  447. return true;
  448. _ForwardIterator __next = __first;
  449. for (++__next; __next != __last; __first = __next, ++__next)
  450. if (*__next < *__first)
  451. return false;
  452. return true;
  453. }
  454. /**
  455. * This is an SGI extension.
  456. * @ingroup SGIextensions
  457. * @doctodo
  458. */
  459. template<typename _ForwardIterator, typename _StrictWeakOrdering>
  460. bool
  461. is_sorted(_ForwardIterator __first, _ForwardIterator __last,
  462. _StrictWeakOrdering __comp)
  463. {
  464. // concept requirements
  465. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  466. __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
  467. typename iterator_traits<_ForwardIterator>::value_type,
  468. typename iterator_traits<_ForwardIterator>::value_type>)
  469. __glibcxx_requires_valid_range(__first, __last);
  470. if (__first == __last)
  471. return true;
  472. _ForwardIterator __next = __first;
  473. for (++__next; __next != __last; __first = __next, ++__next)
  474. if (__comp(*__next, *__first))
  475. return false;
  476. return true;
  477. }
  478. #endif // C++11
  479. /**
  480. * @brief Find the median of three values.
  481. * @param __a A value.
  482. * @param __b A value.
  483. * @param __c A value.
  484. * @return One of @p a, @p b or @p c.
  485. *
  486. * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
  487. * then the value returned will be @c m.
  488. * This is an SGI extension.
  489. * @ingroup SGIextensions
  490. */
  491. template<typename _Tp>
  492. const _Tp&
  493. __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
  494. {
  495. // concept requirements
  496. __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
  497. if (__a < __b)
  498. if (__b < __c)
  499. return __b;
  500. else if (__a < __c)
  501. return __c;
  502. else
  503. return __a;
  504. else if (__a < __c)
  505. return __a;
  506. else if (__b < __c)
  507. return __c;
  508. else
  509. return __b;
  510. }
  511. /**
  512. * @brief Find the median of three values using a predicate for comparison.
  513. * @param __a A value.
  514. * @param __b A value.
  515. * @param __c A value.
  516. * @param __comp A binary predicate.
  517. * @return One of @p a, @p b or @p c.
  518. *
  519. * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
  520. * and @p comp(m,n) are both true then the value returned will be @c m.
  521. * This is an SGI extension.
  522. * @ingroup SGIextensions
  523. */
  524. template<typename _Tp, typename _Compare>
  525. const _Tp&
  526. __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
  527. {
  528. // concept requirements
  529. __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
  530. _Tp, _Tp>)
  531. if (__comp(__a, __b))
  532. if (__comp(__b, __c))
  533. return __b;
  534. else if (__comp(__a, __c))
  535. return __c;
  536. else
  537. return __a;
  538. else if (__comp(__a, __c))
  539. return __a;
  540. else if (__comp(__b, __c))
  541. return __c;
  542. else
  543. return __b;
  544. }
  545. _GLIBCXX_END_NAMESPACE_VERSION
  546. } // namespace
  547. #endif /* _EXT_ALGORITHM */