Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

597 lines
19KB

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