You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5890 lines
210KB

  1. // Algorithm implementation -*- 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 bits/stl_algo.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{algorithm}
  48. */
  49. #ifndef _STL_ALGO_H
  50. #define _STL_ALGO_H 1
  51. #include <cstdlib> // for rand
  52. #include <bits/algorithmfwd.h>
  53. #include <bits/stl_heap.h>
  54. #include <bits/stl_tempbuf.h> // for _Temporary_buffer
  55. #include <bits/predefined_ops.h>
  56. #if __cplusplus >= 201103L
  57. #include <bits/uniform_int_dist.h>
  58. #endif
  59. // See concept_check.h for the __glibcxx_*_requires macros.
  60. namespace std _GLIBCXX_VISIBILITY(default)
  61. {
  62. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  63. /// Swaps the median value of *__a, *__b and *__c under __comp to *__result
  64. template<typename _Iterator, typename _Compare>
  65. _GLIBCXX20_CONSTEXPR
  66. void
  67. __move_median_to_first(_Iterator __result,_Iterator __a, _Iterator __b,
  68. _Iterator __c, _Compare __comp)
  69. {
  70. if (__comp(__a, __b))
  71. {
  72. if (__comp(__b, __c))
  73. std::iter_swap(__result, __b);
  74. else if (__comp(__a, __c))
  75. std::iter_swap(__result, __c);
  76. else
  77. std::iter_swap(__result, __a);
  78. }
  79. else if (__comp(__a, __c))
  80. std::iter_swap(__result, __a);
  81. else if (__comp(__b, __c))
  82. std::iter_swap(__result, __c);
  83. else
  84. std::iter_swap(__result, __b);
  85. }
  86. /// Provided for stable_partition to use.
  87. template<typename _InputIterator, typename _Predicate>
  88. _GLIBCXX20_CONSTEXPR
  89. inline _InputIterator
  90. __find_if_not(_InputIterator __first, _InputIterator __last,
  91. _Predicate __pred)
  92. {
  93. return std::__find_if(__first, __last,
  94. __gnu_cxx::__ops::__negate(__pred),
  95. std::__iterator_category(__first));
  96. }
  97. /// Like find_if_not(), but uses and updates a count of the
  98. /// remaining range length instead of comparing against an end
  99. /// iterator.
  100. template<typename _InputIterator, typename _Predicate, typename _Distance>
  101. _GLIBCXX20_CONSTEXPR
  102. _InputIterator
  103. __find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred)
  104. {
  105. for (; __len; --__len, (void) ++__first)
  106. if (!__pred(__first))
  107. break;
  108. return __first;
  109. }
  110. // set_difference
  111. // set_intersection
  112. // set_symmetric_difference
  113. // set_union
  114. // for_each
  115. // find
  116. // find_if
  117. // find_first_of
  118. // adjacent_find
  119. // count
  120. // count_if
  121. // search
  122. template<typename _ForwardIterator1, typename _ForwardIterator2,
  123. typename _BinaryPredicate>
  124. _GLIBCXX20_CONSTEXPR
  125. _ForwardIterator1
  126. __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  127. _ForwardIterator2 __first2, _ForwardIterator2 __last2,
  128. _BinaryPredicate __predicate)
  129. {
  130. // Test for empty ranges
  131. if (__first1 == __last1 || __first2 == __last2)
  132. return __first1;
  133. // Test for a pattern of length 1.
  134. _ForwardIterator2 __p1(__first2);
  135. if (++__p1 == __last2)
  136. return std::__find_if(__first1, __last1,
  137. __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
  138. // General case.
  139. _ForwardIterator1 __current = __first1;
  140. for (;;)
  141. {
  142. __first1 =
  143. std::__find_if(__first1, __last1,
  144. __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
  145. if (__first1 == __last1)
  146. return __last1;
  147. _ForwardIterator2 __p = __p1;
  148. __current = __first1;
  149. if (++__current == __last1)
  150. return __last1;
  151. while (__predicate(__current, __p))
  152. {
  153. if (++__p == __last2)
  154. return __first1;
  155. if (++__current == __last1)
  156. return __last1;
  157. }
  158. ++__first1;
  159. }
  160. return __first1;
  161. }
  162. // search_n
  163. /**
  164. * This is an helper function for search_n overloaded for forward iterators.
  165. */
  166. template<typename _ForwardIterator, typename _Integer,
  167. typename _UnaryPredicate>
  168. _GLIBCXX20_CONSTEXPR
  169. _ForwardIterator
  170. __search_n_aux(_ForwardIterator __first, _ForwardIterator __last,
  171. _Integer __count, _UnaryPredicate __unary_pred,
  172. std::forward_iterator_tag)
  173. {
  174. __first = std::__find_if(__first, __last, __unary_pred);
  175. while (__first != __last)
  176. {
  177. typename iterator_traits<_ForwardIterator>::difference_type
  178. __n = __count;
  179. _ForwardIterator __i = __first;
  180. ++__i;
  181. while (__i != __last && __n != 1 && __unary_pred(__i))
  182. {
  183. ++__i;
  184. --__n;
  185. }
  186. if (__n == 1)
  187. return __first;
  188. if (__i == __last)
  189. return __last;
  190. __first = std::__find_if(++__i, __last, __unary_pred);
  191. }
  192. return __last;
  193. }
  194. /**
  195. * This is an helper function for search_n overloaded for random access
  196. * iterators.
  197. */
  198. template<typename _RandomAccessIter, typename _Integer,
  199. typename _UnaryPredicate>
  200. _GLIBCXX20_CONSTEXPR
  201. _RandomAccessIter
  202. __search_n_aux(_RandomAccessIter __first, _RandomAccessIter __last,
  203. _Integer __count, _UnaryPredicate __unary_pred,
  204. std::random_access_iterator_tag)
  205. {
  206. typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
  207. _DistanceType;
  208. _DistanceType __tailSize = __last - __first;
  209. _DistanceType __remainder = __count;
  210. while (__remainder <= __tailSize) // the main loop...
  211. {
  212. __first += __remainder;
  213. __tailSize -= __remainder;
  214. // __first here is always pointing to one past the last element of
  215. // next possible match.
  216. _RandomAccessIter __backTrack = __first;
  217. while (__unary_pred(--__backTrack))
  218. {
  219. if (--__remainder == 0)
  220. return (__first - __count); // Success
  221. }
  222. __remainder = __count + 1 - (__first - __backTrack);
  223. }
  224. return __last; // Failure
  225. }
  226. template<typename _ForwardIterator, typename _Integer,
  227. typename _UnaryPredicate>
  228. _GLIBCXX20_CONSTEXPR
  229. _ForwardIterator
  230. __search_n(_ForwardIterator __first, _ForwardIterator __last,
  231. _Integer __count,
  232. _UnaryPredicate __unary_pred)
  233. {
  234. if (__count <= 0)
  235. return __first;
  236. if (__count == 1)
  237. return std::__find_if(__first, __last, __unary_pred);
  238. return std::__search_n_aux(__first, __last, __count, __unary_pred,
  239. std::__iterator_category(__first));
  240. }
  241. // find_end for forward iterators.
  242. template<typename _ForwardIterator1, typename _ForwardIterator2,
  243. typename _BinaryPredicate>
  244. _GLIBCXX20_CONSTEXPR
  245. _ForwardIterator1
  246. __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  247. _ForwardIterator2 __first2, _ForwardIterator2 __last2,
  248. forward_iterator_tag, forward_iterator_tag,
  249. _BinaryPredicate __comp)
  250. {
  251. if (__first2 == __last2)
  252. return __last1;
  253. _ForwardIterator1 __result = __last1;
  254. while (1)
  255. {
  256. _ForwardIterator1 __new_result
  257. = std::__search(__first1, __last1, __first2, __last2, __comp);
  258. if (__new_result == __last1)
  259. return __result;
  260. else
  261. {
  262. __result = __new_result;
  263. __first1 = __new_result;
  264. ++__first1;
  265. }
  266. }
  267. }
  268. // find_end for bidirectional iterators (much faster).
  269. template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
  270. typename _BinaryPredicate>
  271. _GLIBCXX20_CONSTEXPR
  272. _BidirectionalIterator1
  273. __find_end(_BidirectionalIterator1 __first1,
  274. _BidirectionalIterator1 __last1,
  275. _BidirectionalIterator2 __first2,
  276. _BidirectionalIterator2 __last2,
  277. bidirectional_iterator_tag, bidirectional_iterator_tag,
  278. _BinaryPredicate __comp)
  279. {
  280. // concept requirements
  281. __glibcxx_function_requires(_BidirectionalIteratorConcept<
  282. _BidirectionalIterator1>)
  283. __glibcxx_function_requires(_BidirectionalIteratorConcept<
  284. _BidirectionalIterator2>)
  285. typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
  286. typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
  287. _RevIterator1 __rlast1(__first1);
  288. _RevIterator2 __rlast2(__first2);
  289. _RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1,
  290. _RevIterator2(__last2), __rlast2,
  291. __comp);
  292. if (__rresult == __rlast1)
  293. return __last1;
  294. else
  295. {
  296. _BidirectionalIterator1 __result = __rresult.base();
  297. std::advance(__result, -std::distance(__first2, __last2));
  298. return __result;
  299. }
  300. }
  301. /**
  302. * @brief Find last matching subsequence in a sequence.
  303. * @ingroup non_mutating_algorithms
  304. * @param __first1 Start of range to search.
  305. * @param __last1 End of range to search.
  306. * @param __first2 Start of sequence to match.
  307. * @param __last2 End of sequence to match.
  308. * @return The last iterator @c i in the range
  309. * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
  310. * @p *(__first2+N) for each @c N in the range @p
  311. * [0,__last2-__first2), or @p __last1 if no such iterator exists.
  312. *
  313. * Searches the range @p [__first1,__last1) for a sub-sequence that
  314. * compares equal value-by-value with the sequence given by @p
  315. * [__first2,__last2) and returns an iterator to the __first
  316. * element of the sub-sequence, or @p __last1 if the sub-sequence
  317. * is not found. The sub-sequence will be the last such
  318. * subsequence contained in [__first1,__last1).
  319. *
  320. * Because the sub-sequence must lie completely within the range @p
  321. * [__first1,__last1) it must start at a position less than @p
  322. * __last1-(__last2-__first2) where @p __last2-__first2 is the
  323. * length of the sub-sequence. This means that the returned
  324. * iterator @c i will be in the range @p
  325. * [__first1,__last1-(__last2-__first2))
  326. */
  327. template<typename _ForwardIterator1, typename _ForwardIterator2>
  328. _GLIBCXX20_CONSTEXPR
  329. inline _ForwardIterator1
  330. find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  331. _ForwardIterator2 __first2, _ForwardIterator2 __last2)
  332. {
  333. // concept requirements
  334. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
  335. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
  336. __glibcxx_function_requires(_EqualOpConcept<
  337. typename iterator_traits<_ForwardIterator1>::value_type,
  338. typename iterator_traits<_ForwardIterator2>::value_type>)
  339. __glibcxx_requires_valid_range(__first1, __last1);
  340. __glibcxx_requires_valid_range(__first2, __last2);
  341. return std::__find_end(__first1, __last1, __first2, __last2,
  342. std::__iterator_category(__first1),
  343. std::__iterator_category(__first2),
  344. __gnu_cxx::__ops::__iter_equal_to_iter());
  345. }
  346. /**
  347. * @brief Find last matching subsequence in a sequence using a predicate.
  348. * @ingroup non_mutating_algorithms
  349. * @param __first1 Start of range to search.
  350. * @param __last1 End of range to search.
  351. * @param __first2 Start of sequence to match.
  352. * @param __last2 End of sequence to match.
  353. * @param __comp The predicate to use.
  354. * @return The last iterator @c i in the range @p
  355. * [__first1,__last1-(__last2-__first2)) such that @c
  356. * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
  357. * range @p [0,__last2-__first2), or @p __last1 if no such iterator
  358. * exists.
  359. *
  360. * Searches the range @p [__first1,__last1) for a sub-sequence that
  361. * compares equal value-by-value with the sequence given by @p
  362. * [__first2,__last2) using comp as a predicate and returns an
  363. * iterator to the first element of the sub-sequence, or @p __last1
  364. * if the sub-sequence is not found. The sub-sequence will be the
  365. * last such subsequence contained in [__first,__last1).
  366. *
  367. * Because the sub-sequence must lie completely within the range @p
  368. * [__first1,__last1) it must start at a position less than @p
  369. * __last1-(__last2-__first2) where @p __last2-__first2 is the
  370. * length of the sub-sequence. This means that the returned
  371. * iterator @c i will be in the range @p
  372. * [__first1,__last1-(__last2-__first2))
  373. */
  374. template<typename _ForwardIterator1, typename _ForwardIterator2,
  375. typename _BinaryPredicate>
  376. _GLIBCXX20_CONSTEXPR
  377. inline _ForwardIterator1
  378. find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  379. _ForwardIterator2 __first2, _ForwardIterator2 __last2,
  380. _BinaryPredicate __comp)
  381. {
  382. // concept requirements
  383. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
  384. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
  385. __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
  386. typename iterator_traits<_ForwardIterator1>::value_type,
  387. typename iterator_traits<_ForwardIterator2>::value_type>)
  388. __glibcxx_requires_valid_range(__first1, __last1);
  389. __glibcxx_requires_valid_range(__first2, __last2);
  390. return std::__find_end(__first1, __last1, __first2, __last2,
  391. std::__iterator_category(__first1),
  392. std::__iterator_category(__first2),
  393. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  394. }
  395. #if __cplusplus >= 201103L
  396. /**
  397. * @brief Checks that a predicate is true for all the elements
  398. * of a sequence.
  399. * @ingroup non_mutating_algorithms
  400. * @param __first An input iterator.
  401. * @param __last An input iterator.
  402. * @param __pred A predicate.
  403. * @return True if the check is true, false otherwise.
  404. *
  405. * Returns true if @p __pred is true for each element in the range
  406. * @p [__first,__last), and false otherwise.
  407. */
  408. template<typename _InputIterator, typename _Predicate>
  409. _GLIBCXX20_CONSTEXPR
  410. inline bool
  411. all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  412. { return __last == std::find_if_not(__first, __last, __pred); }
  413. /**
  414. * @brief Checks that a predicate is false for all the elements
  415. * of a sequence.
  416. * @ingroup non_mutating_algorithms
  417. * @param __first An input iterator.
  418. * @param __last An input iterator.
  419. * @param __pred A predicate.
  420. * @return True if the check is true, false otherwise.
  421. *
  422. * Returns true if @p __pred is false for each element in the range
  423. * @p [__first,__last), and false otherwise.
  424. */
  425. template<typename _InputIterator, typename _Predicate>
  426. _GLIBCXX20_CONSTEXPR
  427. inline bool
  428. none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  429. { return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); }
  430. /**
  431. * @brief Checks that a predicate is true for at least one element
  432. * of a sequence.
  433. * @ingroup non_mutating_algorithms
  434. * @param __first An input iterator.
  435. * @param __last An input iterator.
  436. * @param __pred A predicate.
  437. * @return True if the check is true, false otherwise.
  438. *
  439. * Returns true if an element exists in the range @p
  440. * [__first,__last) such that @p __pred is true, and false
  441. * otherwise.
  442. */
  443. template<typename _InputIterator, typename _Predicate>
  444. _GLIBCXX20_CONSTEXPR
  445. inline bool
  446. any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  447. { return !std::none_of(__first, __last, __pred); }
  448. /**
  449. * @brief Find the first element in a sequence for which a
  450. * predicate is false.
  451. * @ingroup non_mutating_algorithms
  452. * @param __first An input iterator.
  453. * @param __last An input iterator.
  454. * @param __pred A predicate.
  455. * @return The first iterator @c i in the range @p [__first,__last)
  456. * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
  457. */
  458. template<typename _InputIterator, typename _Predicate>
  459. _GLIBCXX20_CONSTEXPR
  460. inline _InputIterator
  461. find_if_not(_InputIterator __first, _InputIterator __last,
  462. _Predicate __pred)
  463. {
  464. // concept requirements
  465. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  466. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  467. typename iterator_traits<_InputIterator>::value_type>)
  468. __glibcxx_requires_valid_range(__first, __last);
  469. return std::__find_if_not(__first, __last,
  470. __gnu_cxx::__ops::__pred_iter(__pred));
  471. }
  472. /**
  473. * @brief Checks whether the sequence is partitioned.
  474. * @ingroup mutating_algorithms
  475. * @param __first An input iterator.
  476. * @param __last An input iterator.
  477. * @param __pred A predicate.
  478. * @return True if the range @p [__first,__last) is partioned by @p __pred,
  479. * i.e. if all elements that satisfy @p __pred appear before those that
  480. * do not.
  481. */
  482. template<typename _InputIterator, typename _Predicate>
  483. _GLIBCXX20_CONSTEXPR
  484. inline bool
  485. is_partitioned(_InputIterator __first, _InputIterator __last,
  486. _Predicate __pred)
  487. {
  488. __first = std::find_if_not(__first, __last, __pred);
  489. if (__first == __last)
  490. return true;
  491. ++__first;
  492. return std::none_of(__first, __last, __pred);
  493. }
  494. /**
  495. * @brief Find the partition point of a partitioned range.
  496. * @ingroup mutating_algorithms
  497. * @param __first An iterator.
  498. * @param __last Another iterator.
  499. * @param __pred A predicate.
  500. * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
  501. * and @p none_of(mid, __last, __pred) are both true.
  502. */
  503. template<typename _ForwardIterator, typename _Predicate>
  504. _GLIBCXX20_CONSTEXPR
  505. _ForwardIterator
  506. partition_point(_ForwardIterator __first, _ForwardIterator __last,
  507. _Predicate __pred)
  508. {
  509. // concept requirements
  510. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  511. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  512. typename iterator_traits<_ForwardIterator>::value_type>)
  513. // A specific debug-mode test will be necessary...
  514. __glibcxx_requires_valid_range(__first, __last);
  515. typedef typename iterator_traits<_ForwardIterator>::difference_type
  516. _DistanceType;
  517. _DistanceType __len = std::distance(__first, __last);
  518. while (__len > 0)
  519. {
  520. _DistanceType __half = __len >> 1;
  521. _ForwardIterator __middle = __first;
  522. std::advance(__middle, __half);
  523. if (__pred(*__middle))
  524. {
  525. __first = __middle;
  526. ++__first;
  527. __len = __len - __half - 1;
  528. }
  529. else
  530. __len = __half;
  531. }
  532. return __first;
  533. }
  534. #endif
  535. template<typename _InputIterator, typename _OutputIterator,
  536. typename _Predicate>
  537. _GLIBCXX20_CONSTEXPR
  538. _OutputIterator
  539. __remove_copy_if(_InputIterator __first, _InputIterator __last,
  540. _OutputIterator __result, _Predicate __pred)
  541. {
  542. for (; __first != __last; ++__first)
  543. if (!__pred(__first))
  544. {
  545. *__result = *__first;
  546. ++__result;
  547. }
  548. return __result;
  549. }
  550. /**
  551. * @brief Copy a sequence, removing elements of a given value.
  552. * @ingroup mutating_algorithms
  553. * @param __first An input iterator.
  554. * @param __last An input iterator.
  555. * @param __result An output iterator.
  556. * @param __value The value to be removed.
  557. * @return An iterator designating the end of the resulting sequence.
  558. *
  559. * Copies each element in the range @p [__first,__last) not equal
  560. * to @p __value to the range beginning at @p __result.
  561. * remove_copy() is stable, so the relative order of elements that
  562. * are copied is unchanged.
  563. */
  564. template<typename _InputIterator, typename _OutputIterator, typename _Tp>
  565. _GLIBCXX20_CONSTEXPR
  566. inline _OutputIterator
  567. remove_copy(_InputIterator __first, _InputIterator __last,
  568. _OutputIterator __result, const _Tp& __value)
  569. {
  570. // concept requirements
  571. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  572. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  573. typename iterator_traits<_InputIterator>::value_type>)
  574. __glibcxx_function_requires(_EqualOpConcept<
  575. typename iterator_traits<_InputIterator>::value_type, _Tp>)
  576. __glibcxx_requires_valid_range(__first, __last);
  577. return std::__remove_copy_if(__first, __last, __result,
  578. __gnu_cxx::__ops::__iter_equals_val(__value));
  579. }
  580. /**
  581. * @brief Copy a sequence, removing elements for which a predicate is true.
  582. * @ingroup mutating_algorithms
  583. * @param __first An input iterator.
  584. * @param __last An input iterator.
  585. * @param __result An output iterator.
  586. * @param __pred A predicate.
  587. * @return An iterator designating the end of the resulting sequence.
  588. *
  589. * Copies each element in the range @p [__first,__last) for which
  590. * @p __pred returns false to the range beginning at @p __result.
  591. *
  592. * remove_copy_if() is stable, so the relative order of elements that are
  593. * copied is unchanged.
  594. */
  595. template<typename _InputIterator, typename _OutputIterator,
  596. typename _Predicate>
  597. _GLIBCXX20_CONSTEXPR
  598. inline _OutputIterator
  599. remove_copy_if(_InputIterator __first, _InputIterator __last,
  600. _OutputIterator __result, _Predicate __pred)
  601. {
  602. // concept requirements
  603. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  604. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  605. typename iterator_traits<_InputIterator>::value_type>)
  606. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  607. typename iterator_traits<_InputIterator>::value_type>)
  608. __glibcxx_requires_valid_range(__first, __last);
  609. return std::__remove_copy_if(__first, __last, __result,
  610. __gnu_cxx::__ops::__pred_iter(__pred));
  611. }
  612. #if __cplusplus >= 201103L
  613. /**
  614. * @brief Copy the elements of a sequence for which a predicate is true.
  615. * @ingroup mutating_algorithms
  616. * @param __first An input iterator.
  617. * @param __last An input iterator.
  618. * @param __result An output iterator.
  619. * @param __pred A predicate.
  620. * @return An iterator designating the end of the resulting sequence.
  621. *
  622. * Copies each element in the range @p [__first,__last) for which
  623. * @p __pred returns true to the range beginning at @p __result.
  624. *
  625. * copy_if() is stable, so the relative order of elements that are
  626. * copied is unchanged.
  627. */
  628. template<typename _InputIterator, typename _OutputIterator,
  629. typename _Predicate>
  630. _GLIBCXX20_CONSTEXPR
  631. _OutputIterator
  632. copy_if(_InputIterator __first, _InputIterator __last,
  633. _OutputIterator __result, _Predicate __pred)
  634. {
  635. // concept requirements
  636. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  637. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  638. typename iterator_traits<_InputIterator>::value_type>)
  639. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  640. typename iterator_traits<_InputIterator>::value_type>)
  641. __glibcxx_requires_valid_range(__first, __last);
  642. for (; __first != __last; ++__first)
  643. if (__pred(*__first))
  644. {
  645. *__result = *__first;
  646. ++__result;
  647. }
  648. return __result;
  649. }
  650. template<typename _InputIterator, typename _Size, typename _OutputIterator>
  651. _GLIBCXX20_CONSTEXPR
  652. _OutputIterator
  653. __copy_n_a(_InputIterator __first, _Size __n, _OutputIterator __result)
  654. {
  655. if (__n > 0)
  656. {
  657. while (true)
  658. {
  659. *__result = *__first;
  660. ++__result;
  661. if (--__n > 0)
  662. ++__first;
  663. else
  664. break;
  665. }
  666. }
  667. return __result;
  668. }
  669. template<typename _CharT, typename _Size>
  670. __enable_if_t<__is_char<_CharT>::__value, _CharT*>
  671. __copy_n_a(istreambuf_iterator<_CharT, char_traits<_CharT>>,
  672. _Size, _CharT*);
  673. template<typename _InputIterator, typename _Size, typename _OutputIterator>
  674. _GLIBCXX20_CONSTEXPR
  675. _OutputIterator
  676. __copy_n(_InputIterator __first, _Size __n,
  677. _OutputIterator __result, input_iterator_tag)
  678. {
  679. return std::__niter_wrap(__result,
  680. __copy_n_a(__first, __n,
  681. std::__niter_base(__result)));
  682. }
  683. template<typename _RandomAccessIterator, typename _Size,
  684. typename _OutputIterator>
  685. _GLIBCXX20_CONSTEXPR
  686. inline _OutputIterator
  687. __copy_n(_RandomAccessIterator __first, _Size __n,
  688. _OutputIterator __result, random_access_iterator_tag)
  689. { return std::copy(__first, __first + __n, __result); }
  690. /**
  691. * @brief Copies the range [first,first+n) into [result,result+n).
  692. * @ingroup mutating_algorithms
  693. * @param __first An input iterator.
  694. * @param __n The number of elements to copy.
  695. * @param __result An output iterator.
  696. * @return result+n.
  697. *
  698. * This inline function will boil down to a call to @c memmove whenever
  699. * possible. Failing that, if random access iterators are passed, then the
  700. * loop count will be known (and therefore a candidate for compiler
  701. * optimizations such as unrolling).
  702. */
  703. template<typename _InputIterator, typename _Size, typename _OutputIterator>
  704. _GLIBCXX20_CONSTEXPR
  705. inline _OutputIterator
  706. copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
  707. {
  708. // concept requirements
  709. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  710. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  711. typename iterator_traits<_InputIterator>::value_type>)
  712. __glibcxx_requires_can_increment(__first, __n);
  713. __glibcxx_requires_can_increment(__result, __n);
  714. return std::__copy_n(__first, __n, __result,
  715. std::__iterator_category(__first));
  716. }
  717. /**
  718. * @brief Copy the elements of a sequence to separate output sequences
  719. * depending on the truth value of a predicate.
  720. * @ingroup mutating_algorithms
  721. * @param __first An input iterator.
  722. * @param __last An input iterator.
  723. * @param __out_true An output iterator.
  724. * @param __out_false An output iterator.
  725. * @param __pred A predicate.
  726. * @return A pair designating the ends of the resulting sequences.
  727. *
  728. * Copies each element in the range @p [__first,__last) for which
  729. * @p __pred returns true to the range beginning at @p out_true
  730. * and each element for which @p __pred returns false to @p __out_false.
  731. */
  732. template<typename _InputIterator, typename _OutputIterator1,
  733. typename _OutputIterator2, typename _Predicate>
  734. _GLIBCXX20_CONSTEXPR
  735. pair<_OutputIterator1, _OutputIterator2>
  736. partition_copy(_InputIterator __first, _InputIterator __last,
  737. _OutputIterator1 __out_true, _OutputIterator2 __out_false,
  738. _Predicate __pred)
  739. {
  740. // concept requirements
  741. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  742. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
  743. typename iterator_traits<_InputIterator>::value_type>)
  744. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
  745. typename iterator_traits<_InputIterator>::value_type>)
  746. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  747. typename iterator_traits<_InputIterator>::value_type>)
  748. __glibcxx_requires_valid_range(__first, __last);
  749. for (; __first != __last; ++__first)
  750. if (__pred(*__first))
  751. {
  752. *__out_true = *__first;
  753. ++__out_true;
  754. }
  755. else
  756. {
  757. *__out_false = *__first;
  758. ++__out_false;
  759. }
  760. return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
  761. }
  762. #endif // C++11
  763. template<typename _ForwardIterator, typename _Predicate>
  764. _GLIBCXX20_CONSTEXPR
  765. _ForwardIterator
  766. __remove_if(_ForwardIterator __first, _ForwardIterator __last,
  767. _Predicate __pred)
  768. {
  769. __first = std::__find_if(__first, __last, __pred);
  770. if (__first == __last)
  771. return __first;
  772. _ForwardIterator __result = __first;
  773. ++__first;
  774. for (; __first != __last; ++__first)
  775. if (!__pred(__first))
  776. {
  777. *__result = _GLIBCXX_MOVE(*__first);
  778. ++__result;
  779. }
  780. return __result;
  781. }
  782. /**
  783. * @brief Remove elements from a sequence.
  784. * @ingroup mutating_algorithms
  785. * @param __first An input iterator.
  786. * @param __last An input iterator.
  787. * @param __value The value to be removed.
  788. * @return An iterator designating the end of the resulting sequence.
  789. *
  790. * All elements equal to @p __value are removed from the range
  791. * @p [__first,__last).
  792. *
  793. * remove() is stable, so the relative order of elements that are
  794. * not removed is unchanged.
  795. *
  796. * Elements between the end of the resulting sequence and @p __last
  797. * are still present, but their value is unspecified.
  798. */
  799. template<typename _ForwardIterator, typename _Tp>
  800. _GLIBCXX20_CONSTEXPR
  801. inline _ForwardIterator
  802. remove(_ForwardIterator __first, _ForwardIterator __last,
  803. const _Tp& __value)
  804. {
  805. // concept requirements
  806. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  807. _ForwardIterator>)
  808. __glibcxx_function_requires(_EqualOpConcept<
  809. typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
  810. __glibcxx_requires_valid_range(__first, __last);
  811. return std::__remove_if(__first, __last,
  812. __gnu_cxx::__ops::__iter_equals_val(__value));
  813. }
  814. /**
  815. * @brief Remove elements from a sequence using a predicate.
  816. * @ingroup mutating_algorithms
  817. * @param __first A forward iterator.
  818. * @param __last A forward iterator.
  819. * @param __pred A predicate.
  820. * @return An iterator designating the end of the resulting sequence.
  821. *
  822. * All elements for which @p __pred returns true are removed from the range
  823. * @p [__first,__last).
  824. *
  825. * remove_if() is stable, so the relative order of elements that are
  826. * not removed is unchanged.
  827. *
  828. * Elements between the end of the resulting sequence and @p __last
  829. * are still present, but their value is unspecified.
  830. */
  831. template<typename _ForwardIterator, typename _Predicate>
  832. _GLIBCXX20_CONSTEXPR
  833. inline _ForwardIterator
  834. remove_if(_ForwardIterator __first, _ForwardIterator __last,
  835. _Predicate __pred)
  836. {
  837. // concept requirements
  838. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  839. _ForwardIterator>)
  840. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  841. typename iterator_traits<_ForwardIterator>::value_type>)
  842. __glibcxx_requires_valid_range(__first, __last);
  843. return std::__remove_if(__first, __last,
  844. __gnu_cxx::__ops::__pred_iter(__pred));
  845. }
  846. template<typename _ForwardIterator, typename _BinaryPredicate>
  847. _GLIBCXX20_CONSTEXPR
  848. _ForwardIterator
  849. __adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
  850. _BinaryPredicate __binary_pred)
  851. {
  852. if (__first == __last)
  853. return __last;
  854. _ForwardIterator __next = __first;
  855. while (++__next != __last)
  856. {
  857. if (__binary_pred(__first, __next))
  858. return __first;
  859. __first = __next;
  860. }
  861. return __last;
  862. }
  863. template<typename _ForwardIterator, typename _BinaryPredicate>
  864. _GLIBCXX20_CONSTEXPR
  865. _ForwardIterator
  866. __unique(_ForwardIterator __first, _ForwardIterator __last,
  867. _BinaryPredicate __binary_pred)
  868. {
  869. // Skip the beginning, if already unique.
  870. __first = std::__adjacent_find(__first, __last, __binary_pred);
  871. if (__first == __last)
  872. return __last;
  873. // Do the real copy work.
  874. _ForwardIterator __dest = __first;
  875. ++__first;
  876. while (++__first != __last)
  877. if (!__binary_pred(__dest, __first))
  878. *++__dest = _GLIBCXX_MOVE(*__first);
  879. return ++__dest;
  880. }
  881. /**
  882. * @brief Remove consecutive duplicate values from a sequence.
  883. * @ingroup mutating_algorithms
  884. * @param __first A forward iterator.
  885. * @param __last A forward iterator.
  886. * @return An iterator designating the end of the resulting sequence.
  887. *
  888. * Removes all but the first element from each group of consecutive
  889. * values that compare equal.
  890. * unique() is stable, so the relative order of elements that are
  891. * not removed is unchanged.
  892. * Elements between the end of the resulting sequence and @p __last
  893. * are still present, but their value is unspecified.
  894. */
  895. template<typename _ForwardIterator>
  896. _GLIBCXX20_CONSTEXPR
  897. inline _ForwardIterator
  898. unique(_ForwardIterator __first, _ForwardIterator __last)
  899. {
  900. // concept requirements
  901. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  902. _ForwardIterator>)
  903. __glibcxx_function_requires(_EqualityComparableConcept<
  904. typename iterator_traits<_ForwardIterator>::value_type>)
  905. __glibcxx_requires_valid_range(__first, __last);
  906. return std::__unique(__first, __last,
  907. __gnu_cxx::__ops::__iter_equal_to_iter());
  908. }
  909. /**
  910. * @brief Remove consecutive values from a sequence using a predicate.
  911. * @ingroup mutating_algorithms
  912. * @param __first A forward iterator.
  913. * @param __last A forward iterator.
  914. * @param __binary_pred A binary predicate.
  915. * @return An iterator designating the end of the resulting sequence.
  916. *
  917. * Removes all but the first element from each group of consecutive
  918. * values for which @p __binary_pred returns true.
  919. * unique() is stable, so the relative order of elements that are
  920. * not removed is unchanged.
  921. * Elements between the end of the resulting sequence and @p __last
  922. * are still present, but their value is unspecified.
  923. */
  924. template<typename _ForwardIterator, typename _BinaryPredicate>
  925. _GLIBCXX20_CONSTEXPR
  926. inline _ForwardIterator
  927. unique(_ForwardIterator __first, _ForwardIterator __last,
  928. _BinaryPredicate __binary_pred)
  929. {
  930. // concept requirements
  931. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  932. _ForwardIterator>)
  933. __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
  934. typename iterator_traits<_ForwardIterator>::value_type,
  935. typename iterator_traits<_ForwardIterator>::value_type>)
  936. __glibcxx_requires_valid_range(__first, __last);
  937. return std::__unique(__first, __last,
  938. __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
  939. }
  940. /**
  941. * This is an uglified
  942. * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
  943. * _BinaryPredicate)
  944. * overloaded for forward iterators and output iterator as result.
  945. */
  946. template<typename _ForwardIterator, typename _OutputIterator,
  947. typename _BinaryPredicate>
  948. _GLIBCXX20_CONSTEXPR
  949. _OutputIterator
  950. __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
  951. _OutputIterator __result, _BinaryPredicate __binary_pred,
  952. forward_iterator_tag, output_iterator_tag)
  953. {
  954. // concept requirements -- iterators already checked
  955. __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
  956. typename iterator_traits<_ForwardIterator>::value_type,
  957. typename iterator_traits<_ForwardIterator>::value_type>)
  958. _ForwardIterator __next = __first;
  959. *__result = *__first;
  960. while (++__next != __last)
  961. if (!__binary_pred(__first, __next))
  962. {
  963. __first = __next;
  964. *++__result = *__first;
  965. }
  966. return ++__result;
  967. }
  968. /**
  969. * This is an uglified
  970. * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
  971. * _BinaryPredicate)
  972. * overloaded for input iterators and output iterator as result.
  973. */
  974. template<typename _InputIterator, typename _OutputIterator,
  975. typename _BinaryPredicate>
  976. _GLIBCXX20_CONSTEXPR
  977. _OutputIterator
  978. __unique_copy(_InputIterator __first, _InputIterator __last,
  979. _OutputIterator __result, _BinaryPredicate __binary_pred,
  980. input_iterator_tag, output_iterator_tag)
  981. {
  982. // concept requirements -- iterators already checked
  983. __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
  984. typename iterator_traits<_InputIterator>::value_type,
  985. typename iterator_traits<_InputIterator>::value_type>)
  986. typename iterator_traits<_InputIterator>::value_type __value = *__first;
  987. __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred))
  988. __rebound_pred
  989. = __gnu_cxx::__ops::__iter_comp_val(__binary_pred);
  990. *__result = __value;
  991. while (++__first != __last)
  992. if (!__rebound_pred(__first, __value))
  993. {
  994. __value = *__first;
  995. *++__result = __value;
  996. }
  997. return ++__result;
  998. }
  999. /**
  1000. * This is an uglified
  1001. * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
  1002. * _BinaryPredicate)
  1003. * overloaded for input iterators and forward iterator as result.
  1004. */
  1005. template<typename _InputIterator, typename _ForwardIterator,
  1006. typename _BinaryPredicate>
  1007. _GLIBCXX20_CONSTEXPR
  1008. _ForwardIterator
  1009. __unique_copy(_InputIterator __first, _InputIterator __last,
  1010. _ForwardIterator __result, _BinaryPredicate __binary_pred,
  1011. input_iterator_tag, forward_iterator_tag)
  1012. {
  1013. // concept requirements -- iterators already checked
  1014. __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
  1015. typename iterator_traits<_ForwardIterator>::value_type,
  1016. typename iterator_traits<_InputIterator>::value_type>)
  1017. *__result = *__first;
  1018. while (++__first != __last)
  1019. if (!__binary_pred(__result, __first))
  1020. *++__result = *__first;
  1021. return ++__result;
  1022. }
  1023. /**
  1024. * This is an uglified reverse(_BidirectionalIterator,
  1025. * _BidirectionalIterator)
  1026. * overloaded for bidirectional iterators.
  1027. */
  1028. template<typename _BidirectionalIterator>
  1029. _GLIBCXX20_CONSTEXPR
  1030. void
  1031. __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
  1032. bidirectional_iterator_tag)
  1033. {
  1034. while (true)
  1035. if (__first == __last || __first == --__last)
  1036. return;
  1037. else
  1038. {
  1039. std::iter_swap(__first, __last);
  1040. ++__first;
  1041. }
  1042. }
  1043. /**
  1044. * This is an uglified reverse(_BidirectionalIterator,
  1045. * _BidirectionalIterator)
  1046. * overloaded for random access iterators.
  1047. */
  1048. template<typename _RandomAccessIterator>
  1049. _GLIBCXX20_CONSTEXPR
  1050. void
  1051. __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
  1052. random_access_iterator_tag)
  1053. {
  1054. if (__first == __last)
  1055. return;
  1056. --__last;
  1057. while (__first < __last)
  1058. {
  1059. std::iter_swap(__first, __last);
  1060. ++__first;
  1061. --__last;
  1062. }
  1063. }
  1064. /**
  1065. * @brief Reverse a sequence.
  1066. * @ingroup mutating_algorithms
  1067. * @param __first A bidirectional iterator.
  1068. * @param __last A bidirectional iterator.
  1069. * @return reverse() returns no value.
  1070. *
  1071. * Reverses the order of the elements in the range @p [__first,__last),
  1072. * so that the first element becomes the last etc.
  1073. * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
  1074. * swaps @p *(__first+i) and @p *(__last-(i+1))
  1075. */
  1076. template<typename _BidirectionalIterator>
  1077. _GLIBCXX20_CONSTEXPR
  1078. inline void
  1079. reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
  1080. {
  1081. // concept requirements
  1082. __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
  1083. _BidirectionalIterator>)
  1084. __glibcxx_requires_valid_range(__first, __last);
  1085. std::__reverse(__first, __last, std::__iterator_category(__first));
  1086. }
  1087. /**
  1088. * @brief Copy a sequence, reversing its elements.
  1089. * @ingroup mutating_algorithms
  1090. * @param __first A bidirectional iterator.
  1091. * @param __last A bidirectional iterator.
  1092. * @param __result An output iterator.
  1093. * @return An iterator designating the end of the resulting sequence.
  1094. *
  1095. * Copies the elements in the range @p [__first,__last) to the
  1096. * range @p [__result,__result+(__last-__first)) such that the
  1097. * order of the elements is reversed. For every @c i such that @p
  1098. * 0<=i<=(__last-__first), @p reverse_copy() performs the
  1099. * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
  1100. * The ranges @p [__first,__last) and @p
  1101. * [__result,__result+(__last-__first)) must not overlap.
  1102. */
  1103. template<typename _BidirectionalIterator, typename _OutputIterator>
  1104. _GLIBCXX20_CONSTEXPR
  1105. _OutputIterator
  1106. reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
  1107. _OutputIterator __result)
  1108. {
  1109. // concept requirements
  1110. __glibcxx_function_requires(_BidirectionalIteratorConcept<
  1111. _BidirectionalIterator>)
  1112. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  1113. typename iterator_traits<_BidirectionalIterator>::value_type>)
  1114. __glibcxx_requires_valid_range(__first, __last);
  1115. while (__first != __last)
  1116. {
  1117. --__last;
  1118. *__result = *__last;
  1119. ++__result;
  1120. }
  1121. return __result;
  1122. }
  1123. /**
  1124. * This is a helper function for the rotate algorithm specialized on RAIs.
  1125. * It returns the greatest common divisor of two integer values.
  1126. */
  1127. template<typename _EuclideanRingElement>
  1128. _GLIBCXX20_CONSTEXPR
  1129. _EuclideanRingElement
  1130. __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
  1131. {
  1132. while (__n != 0)
  1133. {
  1134. _EuclideanRingElement __t = __m % __n;
  1135. __m = __n;
  1136. __n = __t;
  1137. }
  1138. return __m;
  1139. }
  1140. inline namespace _V2
  1141. {
  1142. /// This is a helper function for the rotate algorithm.
  1143. template<typename _ForwardIterator>
  1144. _GLIBCXX20_CONSTEXPR
  1145. _ForwardIterator
  1146. __rotate(_ForwardIterator __first,
  1147. _ForwardIterator __middle,
  1148. _ForwardIterator __last,
  1149. forward_iterator_tag)
  1150. {
  1151. if (__first == __middle)
  1152. return __last;
  1153. else if (__last == __middle)
  1154. return __first;
  1155. _ForwardIterator __first2 = __middle;
  1156. do
  1157. {
  1158. std::iter_swap(__first, __first2);
  1159. ++__first;
  1160. ++__first2;
  1161. if (__first == __middle)
  1162. __middle = __first2;
  1163. }
  1164. while (__first2 != __last);
  1165. _ForwardIterator __ret = __first;
  1166. __first2 = __middle;
  1167. while (__first2 != __last)
  1168. {
  1169. std::iter_swap(__first, __first2);
  1170. ++__first;
  1171. ++__first2;
  1172. if (__first == __middle)
  1173. __middle = __first2;
  1174. else if (__first2 == __last)
  1175. __first2 = __middle;
  1176. }
  1177. return __ret;
  1178. }
  1179. /// This is a helper function for the rotate algorithm.
  1180. template<typename _BidirectionalIterator>
  1181. _GLIBCXX20_CONSTEXPR
  1182. _BidirectionalIterator
  1183. __rotate(_BidirectionalIterator __first,
  1184. _BidirectionalIterator __middle,
  1185. _BidirectionalIterator __last,
  1186. bidirectional_iterator_tag)
  1187. {
  1188. // concept requirements
  1189. __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
  1190. _BidirectionalIterator>)
  1191. if (__first == __middle)
  1192. return __last;
  1193. else if (__last == __middle)
  1194. return __first;
  1195. std::__reverse(__first, __middle, bidirectional_iterator_tag());
  1196. std::__reverse(__middle, __last, bidirectional_iterator_tag());
  1197. while (__first != __middle && __middle != __last)
  1198. {
  1199. std::iter_swap(__first, --__last);
  1200. ++__first;
  1201. }
  1202. if (__first == __middle)
  1203. {
  1204. std::__reverse(__middle, __last, bidirectional_iterator_tag());
  1205. return __last;
  1206. }
  1207. else
  1208. {
  1209. std::__reverse(__first, __middle, bidirectional_iterator_tag());
  1210. return __first;
  1211. }
  1212. }
  1213. /// This is a helper function for the rotate algorithm.
  1214. template<typename _RandomAccessIterator>
  1215. _GLIBCXX20_CONSTEXPR
  1216. _RandomAccessIterator
  1217. __rotate(_RandomAccessIterator __first,
  1218. _RandomAccessIterator __middle,
  1219. _RandomAccessIterator __last,
  1220. random_access_iterator_tag)
  1221. {
  1222. // concept requirements
  1223. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  1224. _RandomAccessIterator>)
  1225. if (__first == __middle)
  1226. return __last;
  1227. else if (__last == __middle)
  1228. return __first;
  1229. typedef typename iterator_traits<_RandomAccessIterator>::difference_type
  1230. _Distance;
  1231. typedef typename iterator_traits<_RandomAccessIterator>::value_type
  1232. _ValueType;
  1233. _Distance __n = __last - __first;
  1234. _Distance __k = __middle - __first;
  1235. if (__k == __n - __k)
  1236. {
  1237. std::swap_ranges(__first, __middle, __middle);
  1238. return __middle;
  1239. }
  1240. _RandomAccessIterator __p = __first;
  1241. _RandomAccessIterator __ret = __first + (__last - __middle);
  1242. for (;;)
  1243. {
  1244. if (__k < __n - __k)
  1245. {
  1246. if (__is_pod(_ValueType) && __k == 1)
  1247. {
  1248. _ValueType __t = _GLIBCXX_MOVE(*__p);
  1249. _GLIBCXX_MOVE3(__p + 1, __p + __n, __p);
  1250. *(__p + __n - 1) = _GLIBCXX_MOVE(__t);
  1251. return __ret;
  1252. }
  1253. _RandomAccessIterator __q = __p + __k;
  1254. for (_Distance __i = 0; __i < __n - __k; ++ __i)
  1255. {
  1256. std::iter_swap(__p, __q);
  1257. ++__p;
  1258. ++__q;
  1259. }
  1260. __n %= __k;
  1261. if (__n == 0)
  1262. return __ret;
  1263. std::swap(__n, __k);
  1264. __k = __n - __k;
  1265. }
  1266. else
  1267. {
  1268. __k = __n - __k;
  1269. if (__is_pod(_ValueType) && __k == 1)
  1270. {
  1271. _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1));
  1272. _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n);
  1273. *__p = _GLIBCXX_MOVE(__t);
  1274. return __ret;
  1275. }
  1276. _RandomAccessIterator __q = __p + __n;
  1277. __p = __q - __k;
  1278. for (_Distance __i = 0; __i < __n - __k; ++ __i)
  1279. {
  1280. --__p;
  1281. --__q;
  1282. std::iter_swap(__p, __q);
  1283. }
  1284. __n %= __k;
  1285. if (__n == 0)
  1286. return __ret;
  1287. std::swap(__n, __k);
  1288. }
  1289. }
  1290. }
  1291. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1292. // DR 488. rotate throws away useful information
  1293. /**
  1294. * @brief Rotate the elements of a sequence.
  1295. * @ingroup mutating_algorithms
  1296. * @param __first A forward iterator.
  1297. * @param __middle A forward iterator.
  1298. * @param __last A forward iterator.
  1299. * @return first + (last - middle).
  1300. *
  1301. * Rotates the elements of the range @p [__first,__last) by
  1302. * @p (__middle - __first) positions so that the element at @p __middle
  1303. * is moved to @p __first, the element at @p __middle+1 is moved to
  1304. * @p __first+1 and so on for each element in the range
  1305. * @p [__first,__last).
  1306. *
  1307. * This effectively swaps the ranges @p [__first,__middle) and
  1308. * @p [__middle,__last).
  1309. *
  1310. * Performs
  1311. * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
  1312. * for each @p n in the range @p [0,__last-__first).
  1313. */
  1314. template<typename _ForwardIterator>
  1315. _GLIBCXX20_CONSTEXPR
  1316. inline _ForwardIterator
  1317. rotate(_ForwardIterator __first, _ForwardIterator __middle,
  1318. _ForwardIterator __last)
  1319. {
  1320. // concept requirements
  1321. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  1322. _ForwardIterator>)
  1323. __glibcxx_requires_valid_range(__first, __middle);
  1324. __glibcxx_requires_valid_range(__middle, __last);
  1325. return std::__rotate(__first, __middle, __last,
  1326. std::__iterator_category(__first));
  1327. }
  1328. } // namespace _V2
  1329. /**
  1330. * @brief Copy a sequence, rotating its elements.
  1331. * @ingroup mutating_algorithms
  1332. * @param __first A forward iterator.
  1333. * @param __middle A forward iterator.
  1334. * @param __last A forward iterator.
  1335. * @param __result An output iterator.
  1336. * @return An iterator designating the end of the resulting sequence.
  1337. *
  1338. * Copies the elements of the range @p [__first,__last) to the
  1339. * range beginning at @result, rotating the copied elements by
  1340. * @p (__middle-__first) positions so that the element at @p __middle
  1341. * is moved to @p __result, the element at @p __middle+1 is moved
  1342. * to @p __result+1 and so on for each element in the range @p
  1343. * [__first,__last).
  1344. *
  1345. * Performs
  1346. * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
  1347. * for each @p n in the range @p [0,__last-__first).
  1348. */
  1349. template<typename _ForwardIterator, typename _OutputIterator>
  1350. _GLIBCXX20_CONSTEXPR
  1351. inline _OutputIterator
  1352. rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
  1353. _ForwardIterator __last, _OutputIterator __result)
  1354. {
  1355. // concept requirements
  1356. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1357. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  1358. typename iterator_traits<_ForwardIterator>::value_type>)
  1359. __glibcxx_requires_valid_range(__first, __middle);
  1360. __glibcxx_requires_valid_range(__middle, __last);
  1361. return std::copy(__first, __middle,
  1362. std::copy(__middle, __last, __result));
  1363. }
  1364. /// This is a helper function...
  1365. template<typename _ForwardIterator, typename _Predicate>
  1366. _GLIBCXX20_CONSTEXPR
  1367. _ForwardIterator
  1368. __partition(_ForwardIterator __first, _ForwardIterator __last,
  1369. _Predicate __pred, forward_iterator_tag)
  1370. {
  1371. if (__first == __last)
  1372. return __first;
  1373. while (__pred(*__first))
  1374. if (++__first == __last)
  1375. return __first;
  1376. _ForwardIterator __next = __first;
  1377. while (++__next != __last)
  1378. if (__pred(*__next))
  1379. {
  1380. std::iter_swap(__first, __next);
  1381. ++__first;
  1382. }
  1383. return __first;
  1384. }
  1385. /// This is a helper function...
  1386. template<typename _BidirectionalIterator, typename _Predicate>
  1387. _GLIBCXX20_CONSTEXPR
  1388. _BidirectionalIterator
  1389. __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
  1390. _Predicate __pred, bidirectional_iterator_tag)
  1391. {
  1392. while (true)
  1393. {
  1394. while (true)
  1395. if (__first == __last)
  1396. return __first;
  1397. else if (__pred(*__first))
  1398. ++__first;
  1399. else
  1400. break;
  1401. --__last;
  1402. while (true)
  1403. if (__first == __last)
  1404. return __first;
  1405. else if (!bool(__pred(*__last)))
  1406. --__last;
  1407. else
  1408. break;
  1409. std::iter_swap(__first, __last);
  1410. ++__first;
  1411. }
  1412. }
  1413. // partition
  1414. /// This is a helper function...
  1415. /// Requires __first != __last and !__pred(__first)
  1416. /// and __len == distance(__first, __last).
  1417. ///
  1418. /// !__pred(__first) allows us to guarantee that we don't
  1419. /// move-assign an element onto itself.
  1420. template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
  1421. typename _Distance>
  1422. _ForwardIterator
  1423. __stable_partition_adaptive(_ForwardIterator __first,
  1424. _ForwardIterator __last,
  1425. _Predicate __pred, _Distance __len,
  1426. _Pointer __buffer,
  1427. _Distance __buffer_size)
  1428. {
  1429. if (__len == 1)
  1430. return __first;
  1431. if (__len <= __buffer_size)
  1432. {
  1433. _ForwardIterator __result1 = __first;
  1434. _Pointer __result2 = __buffer;
  1435. // The precondition guarantees that !__pred(__first), so
  1436. // move that element to the buffer before starting the loop.
  1437. // This ensures that we only call __pred once per element.
  1438. *__result2 = _GLIBCXX_MOVE(*__first);
  1439. ++__result2;
  1440. ++__first;
  1441. for (; __first != __last; ++__first)
  1442. if (__pred(__first))
  1443. {
  1444. *__result1 = _GLIBCXX_MOVE(*__first);
  1445. ++__result1;
  1446. }
  1447. else
  1448. {
  1449. *__result2 = _GLIBCXX_MOVE(*__first);
  1450. ++__result2;
  1451. }
  1452. _GLIBCXX_MOVE3(__buffer, __result2, __result1);
  1453. return __result1;
  1454. }
  1455. _ForwardIterator __middle = __first;
  1456. std::advance(__middle, __len / 2);
  1457. _ForwardIterator __left_split =
  1458. std::__stable_partition_adaptive(__first, __middle, __pred,
  1459. __len / 2, __buffer,
  1460. __buffer_size);
  1461. // Advance past true-predicate values to satisfy this
  1462. // function's preconditions.
  1463. _Distance __right_len = __len - __len / 2;
  1464. _ForwardIterator __right_split =
  1465. std::__find_if_not_n(__middle, __right_len, __pred);
  1466. if (__right_len)
  1467. __right_split =
  1468. std::__stable_partition_adaptive(__right_split, __last, __pred,
  1469. __right_len,
  1470. __buffer, __buffer_size);
  1471. return std::rotate(__left_split, __middle, __right_split);
  1472. }
  1473. template<typename _ForwardIterator, typename _Predicate>
  1474. _ForwardIterator
  1475. __stable_partition(_ForwardIterator __first, _ForwardIterator __last,
  1476. _Predicate __pred)
  1477. {
  1478. __first = std::__find_if_not(__first, __last, __pred);
  1479. if (__first == __last)
  1480. return __first;
  1481. typedef typename iterator_traits<_ForwardIterator>::value_type
  1482. _ValueType;
  1483. typedef typename iterator_traits<_ForwardIterator>::difference_type
  1484. _DistanceType;
  1485. _Temporary_buffer<_ForwardIterator, _ValueType>
  1486. __buf(__first, std::distance(__first, __last));
  1487. return
  1488. std::__stable_partition_adaptive(__first, __last, __pred,
  1489. _DistanceType(__buf.requested_size()),
  1490. __buf.begin(),
  1491. _DistanceType(__buf.size()));
  1492. }
  1493. /**
  1494. * @brief Move elements for which a predicate is true to the beginning
  1495. * of a sequence, preserving relative ordering.
  1496. * @ingroup mutating_algorithms
  1497. * @param __first A forward iterator.
  1498. * @param __last A forward iterator.
  1499. * @param __pred A predicate functor.
  1500. * @return An iterator @p middle such that @p __pred(i) is true for each
  1501. * iterator @p i in the range @p [first,middle) and false for each @p i
  1502. * in the range @p [middle,last).
  1503. *
  1504. * Performs the same function as @p partition() with the additional
  1505. * guarantee that the relative ordering of elements in each group is
  1506. * preserved, so any two elements @p x and @p y in the range
  1507. * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
  1508. * relative ordering after calling @p stable_partition().
  1509. */
  1510. template<typename _ForwardIterator, typename _Predicate>
  1511. inline _ForwardIterator
  1512. stable_partition(_ForwardIterator __first, _ForwardIterator __last,
  1513. _Predicate __pred)
  1514. {
  1515. // concept requirements
  1516. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  1517. _ForwardIterator>)
  1518. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  1519. typename iterator_traits<_ForwardIterator>::value_type>)
  1520. __glibcxx_requires_valid_range(__first, __last);
  1521. return std::__stable_partition(__first, __last,
  1522. __gnu_cxx::__ops::__pred_iter(__pred));
  1523. }
  1524. /// This is a helper function for the sort routines.
  1525. template<typename _RandomAccessIterator, typename _Compare>
  1526. _GLIBCXX20_CONSTEXPR
  1527. void
  1528. __heap_select(_RandomAccessIterator __first,
  1529. _RandomAccessIterator __middle,
  1530. _RandomAccessIterator __last, _Compare __comp)
  1531. {
  1532. std::__make_heap(__first, __middle, __comp);
  1533. for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
  1534. if (__comp(__i, __first))
  1535. std::__pop_heap(__first, __middle, __i, __comp);
  1536. }
  1537. // partial_sort
  1538. template<typename _InputIterator, typename _RandomAccessIterator,
  1539. typename _Compare>
  1540. _GLIBCXX20_CONSTEXPR
  1541. _RandomAccessIterator
  1542. __partial_sort_copy(_InputIterator __first, _InputIterator __last,
  1543. _RandomAccessIterator __result_first,
  1544. _RandomAccessIterator __result_last,
  1545. _Compare __comp)
  1546. {
  1547. typedef typename iterator_traits<_InputIterator>::value_type
  1548. _InputValueType;
  1549. typedef iterator_traits<_RandomAccessIterator> _RItTraits;
  1550. typedef typename _RItTraits::difference_type _DistanceType;
  1551. if (__result_first == __result_last)
  1552. return __result_last;
  1553. _RandomAccessIterator __result_real_last = __result_first;
  1554. while (__first != __last && __result_real_last != __result_last)
  1555. {
  1556. *__result_real_last = *__first;
  1557. ++__result_real_last;
  1558. ++__first;
  1559. }
  1560. std::__make_heap(__result_first, __result_real_last, __comp);
  1561. while (__first != __last)
  1562. {
  1563. if (__comp(__first, __result_first))
  1564. std::__adjust_heap(__result_first, _DistanceType(0),
  1565. _DistanceType(__result_real_last
  1566. - __result_first),
  1567. _InputValueType(*__first), __comp);
  1568. ++__first;
  1569. }
  1570. std::__sort_heap(__result_first, __result_real_last, __comp);
  1571. return __result_real_last;
  1572. }
  1573. /**
  1574. * @brief Copy the smallest elements of a sequence.
  1575. * @ingroup sorting_algorithms
  1576. * @param __first An iterator.
  1577. * @param __last Another iterator.
  1578. * @param __result_first A random-access iterator.
  1579. * @param __result_last Another random-access iterator.
  1580. * @return An iterator indicating the end of the resulting sequence.
  1581. *
  1582. * Copies and sorts the smallest N values from the range @p [__first,__last)
  1583. * to the range beginning at @p __result_first, where the number of
  1584. * elements to be copied, @p N, is the smaller of @p (__last-__first) and
  1585. * @p (__result_last-__result_first).
  1586. * After the sort if @e i and @e j are iterators in the range
  1587. * @p [__result_first,__result_first+N) such that i precedes j then
  1588. * *j<*i is false.
  1589. * The value returned is @p __result_first+N.
  1590. */
  1591. template<typename _InputIterator, typename _RandomAccessIterator>
  1592. _GLIBCXX20_CONSTEXPR
  1593. inline _RandomAccessIterator
  1594. partial_sort_copy(_InputIterator __first, _InputIterator __last,
  1595. _RandomAccessIterator __result_first,
  1596. _RandomAccessIterator __result_last)
  1597. {
  1598. #ifdef _GLIBCXX_CONCEPT_CHECKS
  1599. typedef typename iterator_traits<_InputIterator>::value_type
  1600. _InputValueType;
  1601. typedef typename iterator_traits<_RandomAccessIterator>::value_type
  1602. _OutputValueType;
  1603. #endif
  1604. // concept requirements
  1605. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  1606. __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
  1607. _OutputValueType>)
  1608. __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
  1609. _OutputValueType>)
  1610. __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
  1611. __glibcxx_requires_valid_range(__first, __last);
  1612. __glibcxx_requires_irreflexive(__first, __last);
  1613. __glibcxx_requires_valid_range(__result_first, __result_last);
  1614. return std::__partial_sort_copy(__first, __last,
  1615. __result_first, __result_last,
  1616. __gnu_cxx::__ops::__iter_less_iter());
  1617. }
  1618. /**
  1619. * @brief Copy the smallest elements of a sequence using a predicate for
  1620. * comparison.
  1621. * @ingroup sorting_algorithms
  1622. * @param __first An input iterator.
  1623. * @param __last Another input iterator.
  1624. * @param __result_first A random-access iterator.
  1625. * @param __result_last Another random-access iterator.
  1626. * @param __comp A comparison functor.
  1627. * @return An iterator indicating the end of the resulting sequence.
  1628. *
  1629. * Copies and sorts the smallest N values from the range @p [__first,__last)
  1630. * to the range beginning at @p result_first, where the number of
  1631. * elements to be copied, @p N, is the smaller of @p (__last-__first) and
  1632. * @p (__result_last-__result_first).
  1633. * After the sort if @e i and @e j are iterators in the range
  1634. * @p [__result_first,__result_first+N) such that i precedes j then
  1635. * @p __comp(*j,*i) is false.
  1636. * The value returned is @p __result_first+N.
  1637. */
  1638. template<typename _InputIterator, typename _RandomAccessIterator,
  1639. typename _Compare>
  1640. _GLIBCXX20_CONSTEXPR
  1641. inline _RandomAccessIterator
  1642. partial_sort_copy(_InputIterator __first, _InputIterator __last,
  1643. _RandomAccessIterator __result_first,
  1644. _RandomAccessIterator __result_last,
  1645. _Compare __comp)
  1646. {
  1647. #ifdef _GLIBCXX_CONCEPT_CHECKS
  1648. typedef typename iterator_traits<_InputIterator>::value_type
  1649. _InputValueType;
  1650. typedef typename iterator_traits<_RandomAccessIterator>::value_type
  1651. _OutputValueType;
  1652. #endif
  1653. // concept requirements
  1654. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  1655. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  1656. _RandomAccessIterator>)
  1657. __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
  1658. _OutputValueType>)
  1659. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  1660. _InputValueType, _OutputValueType>)
  1661. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  1662. _OutputValueType, _OutputValueType>)
  1663. __glibcxx_requires_valid_range(__first, __last);
  1664. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  1665. __glibcxx_requires_valid_range(__result_first, __result_last);
  1666. return std::__partial_sort_copy(__first, __last,
  1667. __result_first, __result_last,
  1668. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  1669. }
  1670. /// This is a helper function for the sort routine.
  1671. template<typename _RandomAccessIterator, typename _Compare>
  1672. _GLIBCXX20_CONSTEXPR
  1673. void
  1674. __unguarded_linear_insert(_RandomAccessIterator __last,
  1675. _Compare __comp)
  1676. {
  1677. typename iterator_traits<_RandomAccessIterator>::value_type
  1678. __val = _GLIBCXX_MOVE(*__last);
  1679. _RandomAccessIterator __next = __last;
  1680. --__next;
  1681. while (__comp(__val, __next))
  1682. {
  1683. *__last = _GLIBCXX_MOVE(*__next);
  1684. __last = __next;
  1685. --__next;
  1686. }
  1687. *__last = _GLIBCXX_MOVE(__val);
  1688. }
  1689. /// This is a helper function for the sort routine.
  1690. template<typename _RandomAccessIterator, typename _Compare>
  1691. _GLIBCXX20_CONSTEXPR
  1692. void
  1693. __insertion_sort(_RandomAccessIterator __first,
  1694. _RandomAccessIterator __last, _Compare __comp)
  1695. {
  1696. if (__first == __last) return;
  1697. for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
  1698. {
  1699. if (__comp(__i, __first))
  1700. {
  1701. typename iterator_traits<_RandomAccessIterator>::value_type
  1702. __val = _GLIBCXX_MOVE(*__i);
  1703. _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
  1704. *__first = _GLIBCXX_MOVE(__val);
  1705. }
  1706. else
  1707. std::__unguarded_linear_insert(__i,
  1708. __gnu_cxx::__ops::__val_comp_iter(__comp));
  1709. }
  1710. }
  1711. /// This is a helper function for the sort routine.
  1712. template<typename _RandomAccessIterator, typename _Compare>
  1713. _GLIBCXX20_CONSTEXPR
  1714. inline void
  1715. __unguarded_insertion_sort(_RandomAccessIterator __first,
  1716. _RandomAccessIterator __last, _Compare __comp)
  1717. {
  1718. for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
  1719. std::__unguarded_linear_insert(__i,
  1720. __gnu_cxx::__ops::__val_comp_iter(__comp));
  1721. }
  1722. /**
  1723. * @doctodo
  1724. * This controls some aspect of the sort routines.
  1725. */
  1726. enum { _S_threshold = 16 };
  1727. /// This is a helper function for the sort routine.
  1728. template<typename _RandomAccessIterator, typename _Compare>
  1729. _GLIBCXX20_CONSTEXPR
  1730. void
  1731. __final_insertion_sort(_RandomAccessIterator __first,
  1732. _RandomAccessIterator __last, _Compare __comp)
  1733. {
  1734. if (__last - __first > int(_S_threshold))
  1735. {
  1736. std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
  1737. std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
  1738. __comp);
  1739. }
  1740. else
  1741. std::__insertion_sort(__first, __last, __comp);
  1742. }
  1743. /// This is a helper function...
  1744. template<typename _RandomAccessIterator, typename _Compare>
  1745. _GLIBCXX20_CONSTEXPR
  1746. _RandomAccessIterator
  1747. __unguarded_partition(_RandomAccessIterator __first,
  1748. _RandomAccessIterator __last,
  1749. _RandomAccessIterator __pivot, _Compare __comp)
  1750. {
  1751. while (true)
  1752. {
  1753. while (__comp(__first, __pivot))
  1754. ++__first;
  1755. --__last;
  1756. while (__comp(__pivot, __last))
  1757. --__last;
  1758. if (!(__first < __last))
  1759. return __first;
  1760. std::iter_swap(__first, __last);
  1761. ++__first;
  1762. }
  1763. }
  1764. /// This is a helper function...
  1765. template<typename _RandomAccessIterator, typename _Compare>
  1766. _GLIBCXX20_CONSTEXPR
  1767. inline _RandomAccessIterator
  1768. __unguarded_partition_pivot(_RandomAccessIterator __first,
  1769. _RandomAccessIterator __last, _Compare __comp)
  1770. {
  1771. _RandomAccessIterator __mid = __first + (__last - __first) / 2;
  1772. std::__move_median_to_first(__first, __first + 1, __mid, __last - 1,
  1773. __comp);
  1774. return std::__unguarded_partition(__first + 1, __last, __first, __comp);
  1775. }
  1776. template<typename _RandomAccessIterator, typename _Compare>
  1777. _GLIBCXX20_CONSTEXPR
  1778. inline void
  1779. __partial_sort(_RandomAccessIterator __first,
  1780. _RandomAccessIterator __middle,
  1781. _RandomAccessIterator __last,
  1782. _Compare __comp)
  1783. {
  1784. std::__heap_select(__first, __middle, __last, __comp);
  1785. std::__sort_heap(__first, __middle, __comp);
  1786. }
  1787. /// This is a helper function for the sort routine.
  1788. template<typename _RandomAccessIterator, typename _Size, typename _Compare>
  1789. _GLIBCXX20_CONSTEXPR
  1790. void
  1791. __introsort_loop(_RandomAccessIterator __first,
  1792. _RandomAccessIterator __last,
  1793. _Size __depth_limit, _Compare __comp)
  1794. {
  1795. while (__last - __first > int(_S_threshold))
  1796. {
  1797. if (__depth_limit == 0)
  1798. {
  1799. std::__partial_sort(__first, __last, __last, __comp);
  1800. return;
  1801. }
  1802. --__depth_limit;
  1803. _RandomAccessIterator __cut =
  1804. std::__unguarded_partition_pivot(__first, __last, __comp);
  1805. std::__introsort_loop(__cut, __last, __depth_limit, __comp);
  1806. __last = __cut;
  1807. }
  1808. }
  1809. // sort
  1810. template<typename _RandomAccessIterator, typename _Compare>
  1811. _GLIBCXX20_CONSTEXPR
  1812. inline void
  1813. __sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
  1814. _Compare __comp)
  1815. {
  1816. if (__first != __last)
  1817. {
  1818. std::__introsort_loop(__first, __last,
  1819. std::__lg(__last - __first) * 2,
  1820. __comp);
  1821. std::__final_insertion_sort(__first, __last, __comp);
  1822. }
  1823. }
  1824. template<typename _RandomAccessIterator, typename _Size, typename _Compare>
  1825. _GLIBCXX20_CONSTEXPR
  1826. void
  1827. __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
  1828. _RandomAccessIterator __last, _Size __depth_limit,
  1829. _Compare __comp)
  1830. {
  1831. while (__last - __first > 3)
  1832. {
  1833. if (__depth_limit == 0)
  1834. {
  1835. std::__heap_select(__first, __nth + 1, __last, __comp);
  1836. // Place the nth largest element in its final position.
  1837. std::iter_swap(__first, __nth);
  1838. return;
  1839. }
  1840. --__depth_limit;
  1841. _RandomAccessIterator __cut =
  1842. std::__unguarded_partition_pivot(__first, __last, __comp);
  1843. if (__cut <= __nth)
  1844. __first = __cut;
  1845. else
  1846. __last = __cut;
  1847. }
  1848. std::__insertion_sort(__first, __last, __comp);
  1849. }
  1850. // nth_element
  1851. // lower_bound moved to stl_algobase.h
  1852. /**
  1853. * @brief Finds the first position in which @p __val could be inserted
  1854. * without changing the ordering.
  1855. * @ingroup binary_search_algorithms
  1856. * @param __first An iterator.
  1857. * @param __last Another iterator.
  1858. * @param __val The search term.
  1859. * @param __comp A functor to use for comparisons.
  1860. * @return An iterator pointing to the first element <em>not less
  1861. * than</em> @p __val, or end() if every element is less
  1862. * than @p __val.
  1863. * @ingroup binary_search_algorithms
  1864. *
  1865. * The comparison function should have the same effects on ordering as
  1866. * the function used for the initial sort.
  1867. */
  1868. template<typename _ForwardIterator, typename _Tp, typename _Compare>
  1869. _GLIBCXX20_CONSTEXPR
  1870. inline _ForwardIterator
  1871. lower_bound(_ForwardIterator __first, _ForwardIterator __last,
  1872. const _Tp& __val, _Compare __comp)
  1873. {
  1874. // concept requirements
  1875. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1876. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  1877. typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
  1878. __glibcxx_requires_partitioned_lower_pred(__first, __last,
  1879. __val, __comp);
  1880. return std::__lower_bound(__first, __last, __val,
  1881. __gnu_cxx::__ops::__iter_comp_val(__comp));
  1882. }
  1883. template<typename _ForwardIterator, typename _Tp, typename _Compare>
  1884. _GLIBCXX20_CONSTEXPR
  1885. _ForwardIterator
  1886. __upper_bound(_ForwardIterator __first, _ForwardIterator __last,
  1887. const _Tp& __val, _Compare __comp)
  1888. {
  1889. typedef typename iterator_traits<_ForwardIterator>::difference_type
  1890. _DistanceType;
  1891. _DistanceType __len = std::distance(__first, __last);
  1892. while (__len > 0)
  1893. {
  1894. _DistanceType __half = __len >> 1;
  1895. _ForwardIterator __middle = __first;
  1896. std::advance(__middle, __half);
  1897. if (__comp(__val, __middle))
  1898. __len = __half;
  1899. else
  1900. {
  1901. __first = __middle;
  1902. ++__first;
  1903. __len = __len - __half - 1;
  1904. }
  1905. }
  1906. return __first;
  1907. }
  1908. /**
  1909. * @brief Finds the last position in which @p __val could be inserted
  1910. * without changing the ordering.
  1911. * @ingroup binary_search_algorithms
  1912. * @param __first An iterator.
  1913. * @param __last Another iterator.
  1914. * @param __val The search term.
  1915. * @return An iterator pointing to the first element greater than @p __val,
  1916. * or end() if no elements are greater than @p __val.
  1917. * @ingroup binary_search_algorithms
  1918. */
  1919. template<typename _ForwardIterator, typename _Tp>
  1920. _GLIBCXX20_CONSTEXPR
  1921. inline _ForwardIterator
  1922. upper_bound(_ForwardIterator __first, _ForwardIterator __last,
  1923. const _Tp& __val)
  1924. {
  1925. // concept requirements
  1926. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1927. __glibcxx_function_requires(_LessThanOpConcept<
  1928. _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
  1929. __glibcxx_requires_partitioned_upper(__first, __last, __val);
  1930. return std::__upper_bound(__first, __last, __val,
  1931. __gnu_cxx::__ops::__val_less_iter());
  1932. }
  1933. /**
  1934. * @brief Finds the last position in which @p __val could be inserted
  1935. * without changing the ordering.
  1936. * @ingroup binary_search_algorithms
  1937. * @param __first An iterator.
  1938. * @param __last Another iterator.
  1939. * @param __val The search term.
  1940. * @param __comp A functor to use for comparisons.
  1941. * @return An iterator pointing to the first element greater than @p __val,
  1942. * or end() if no elements are greater than @p __val.
  1943. * @ingroup binary_search_algorithms
  1944. *
  1945. * The comparison function should have the same effects on ordering as
  1946. * the function used for the initial sort.
  1947. */
  1948. template<typename _ForwardIterator, typename _Tp, typename _Compare>
  1949. _GLIBCXX20_CONSTEXPR
  1950. inline _ForwardIterator
  1951. upper_bound(_ForwardIterator __first, _ForwardIterator __last,
  1952. const _Tp& __val, _Compare __comp)
  1953. {
  1954. // concept requirements
  1955. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1956. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  1957. _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
  1958. __glibcxx_requires_partitioned_upper_pred(__first, __last,
  1959. __val, __comp);
  1960. return std::__upper_bound(__first, __last, __val,
  1961. __gnu_cxx::__ops::__val_comp_iter(__comp));
  1962. }
  1963. template<typename _ForwardIterator, typename _Tp,
  1964. typename _CompareItTp, typename _CompareTpIt>
  1965. _GLIBCXX20_CONSTEXPR
  1966. pair<_ForwardIterator, _ForwardIterator>
  1967. __equal_range(_ForwardIterator __first, _ForwardIterator __last,
  1968. const _Tp& __val,
  1969. _CompareItTp __comp_it_val, _CompareTpIt __comp_val_it)
  1970. {
  1971. typedef typename iterator_traits<_ForwardIterator>::difference_type
  1972. _DistanceType;
  1973. _DistanceType __len = std::distance(__first, __last);
  1974. while (__len > 0)
  1975. {
  1976. _DistanceType __half = __len >> 1;
  1977. _ForwardIterator __middle = __first;
  1978. std::advance(__middle, __half);
  1979. if (__comp_it_val(__middle, __val))
  1980. {
  1981. __first = __middle;
  1982. ++__first;
  1983. __len = __len - __half - 1;
  1984. }
  1985. else if (__comp_val_it(__val, __middle))
  1986. __len = __half;
  1987. else
  1988. {
  1989. _ForwardIterator __left
  1990. = std::__lower_bound(__first, __middle, __val, __comp_it_val);
  1991. std::advance(__first, __len);
  1992. _ForwardIterator __right
  1993. = std::__upper_bound(++__middle, __first, __val, __comp_val_it);
  1994. return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
  1995. }
  1996. }
  1997. return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
  1998. }
  1999. /**
  2000. * @brief Finds the largest subrange in which @p __val could be inserted
  2001. * at any place in it without changing the ordering.
  2002. * @ingroup binary_search_algorithms
  2003. * @param __first An iterator.
  2004. * @param __last Another iterator.
  2005. * @param __val The search term.
  2006. * @return An pair of iterators defining the subrange.
  2007. * @ingroup binary_search_algorithms
  2008. *
  2009. * This is equivalent to
  2010. * @code
  2011. * std::make_pair(lower_bound(__first, __last, __val),
  2012. * upper_bound(__first, __last, __val))
  2013. * @endcode
  2014. * but does not actually call those functions.
  2015. */
  2016. template<typename _ForwardIterator, typename _Tp>
  2017. _GLIBCXX20_CONSTEXPR
  2018. inline pair<_ForwardIterator, _ForwardIterator>
  2019. equal_range(_ForwardIterator __first, _ForwardIterator __last,
  2020. const _Tp& __val)
  2021. {
  2022. // concept requirements
  2023. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  2024. __glibcxx_function_requires(_LessThanOpConcept<
  2025. typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
  2026. __glibcxx_function_requires(_LessThanOpConcept<
  2027. _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
  2028. __glibcxx_requires_partitioned_lower(__first, __last, __val);
  2029. __glibcxx_requires_partitioned_upper(__first, __last, __val);
  2030. return std::__equal_range(__first, __last, __val,
  2031. __gnu_cxx::__ops::__iter_less_val(),
  2032. __gnu_cxx::__ops::__val_less_iter());
  2033. }
  2034. /**
  2035. * @brief Finds the largest subrange in which @p __val could be inserted
  2036. * at any place in it without changing the ordering.
  2037. * @param __first An iterator.
  2038. * @param __last Another iterator.
  2039. * @param __val The search term.
  2040. * @param __comp A functor to use for comparisons.
  2041. * @return An pair of iterators defining the subrange.
  2042. * @ingroup binary_search_algorithms
  2043. *
  2044. * This is equivalent to
  2045. * @code
  2046. * std::make_pair(lower_bound(__first, __last, __val, __comp),
  2047. * upper_bound(__first, __last, __val, __comp))
  2048. * @endcode
  2049. * but does not actually call those functions.
  2050. */
  2051. template<typename _ForwardIterator, typename _Tp, typename _Compare>
  2052. _GLIBCXX20_CONSTEXPR
  2053. inline pair<_ForwardIterator, _ForwardIterator>
  2054. equal_range(_ForwardIterator __first, _ForwardIterator __last,
  2055. const _Tp& __val, _Compare __comp)
  2056. {
  2057. // concept requirements
  2058. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  2059. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  2060. typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
  2061. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  2062. _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
  2063. __glibcxx_requires_partitioned_lower_pred(__first, __last,
  2064. __val, __comp);
  2065. __glibcxx_requires_partitioned_upper_pred(__first, __last,
  2066. __val, __comp);
  2067. return std::__equal_range(__first, __last, __val,
  2068. __gnu_cxx::__ops::__iter_comp_val(__comp),
  2069. __gnu_cxx::__ops::__val_comp_iter(__comp));
  2070. }
  2071. /**
  2072. * @brief Determines whether an element exists in a range.
  2073. * @ingroup binary_search_algorithms
  2074. * @param __first An iterator.
  2075. * @param __last Another iterator.
  2076. * @param __val The search term.
  2077. * @return True if @p __val (or its equivalent) is in [@p
  2078. * __first,@p __last ].
  2079. *
  2080. * Note that this does not actually return an iterator to @p __val. For
  2081. * that, use std::find or a container's specialized find member functions.
  2082. */
  2083. template<typename _ForwardIterator, typename _Tp>
  2084. _GLIBCXX20_CONSTEXPR
  2085. bool
  2086. binary_search(_ForwardIterator __first, _ForwardIterator __last,
  2087. const _Tp& __val)
  2088. {
  2089. // concept requirements
  2090. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  2091. __glibcxx_function_requires(_LessThanOpConcept<
  2092. _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
  2093. __glibcxx_requires_partitioned_lower(__first, __last, __val);
  2094. __glibcxx_requires_partitioned_upper(__first, __last, __val);
  2095. _ForwardIterator __i
  2096. = std::__lower_bound(__first, __last, __val,
  2097. __gnu_cxx::__ops::__iter_less_val());
  2098. return __i != __last && !(__val < *__i);
  2099. }
  2100. /**
  2101. * @brief Determines whether an element exists in a range.
  2102. * @ingroup binary_search_algorithms
  2103. * @param __first An iterator.
  2104. * @param __last Another iterator.
  2105. * @param __val The search term.
  2106. * @param __comp A functor to use for comparisons.
  2107. * @return True if @p __val (or its equivalent) is in @p [__first,__last].
  2108. *
  2109. * Note that this does not actually return an iterator to @p __val. For
  2110. * that, use std::find or a container's specialized find member functions.
  2111. *
  2112. * The comparison function should have the same effects on ordering as
  2113. * the function used for the initial sort.
  2114. */
  2115. template<typename _ForwardIterator, typename _Tp, typename _Compare>
  2116. _GLIBCXX20_CONSTEXPR
  2117. bool
  2118. binary_search(_ForwardIterator __first, _ForwardIterator __last,
  2119. const _Tp& __val, _Compare __comp)
  2120. {
  2121. // concept requirements
  2122. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  2123. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  2124. _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
  2125. __glibcxx_requires_partitioned_lower_pred(__first, __last,
  2126. __val, __comp);
  2127. __glibcxx_requires_partitioned_upper_pred(__first, __last,
  2128. __val, __comp);
  2129. _ForwardIterator __i
  2130. = std::__lower_bound(__first, __last, __val,
  2131. __gnu_cxx::__ops::__iter_comp_val(__comp));
  2132. return __i != __last && !bool(__comp(__val, *__i));
  2133. }
  2134. // merge
  2135. /// This is a helper function for the __merge_adaptive routines.
  2136. template<typename _InputIterator1, typename _InputIterator2,
  2137. typename _OutputIterator, typename _Compare>
  2138. void
  2139. __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1,
  2140. _InputIterator2 __first2, _InputIterator2 __last2,
  2141. _OutputIterator __result, _Compare __comp)
  2142. {
  2143. while (__first1 != __last1 && __first2 != __last2)
  2144. {
  2145. if (__comp(__first2, __first1))
  2146. {
  2147. *__result = _GLIBCXX_MOVE(*__first2);
  2148. ++__first2;
  2149. }
  2150. else
  2151. {
  2152. *__result = _GLIBCXX_MOVE(*__first1);
  2153. ++__first1;
  2154. }
  2155. ++__result;
  2156. }
  2157. if (__first1 != __last1)
  2158. _GLIBCXX_MOVE3(__first1, __last1, __result);
  2159. }
  2160. /// This is a helper function for the __merge_adaptive routines.
  2161. template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
  2162. typename _BidirectionalIterator3, typename _Compare>
  2163. void
  2164. __move_merge_adaptive_backward(_BidirectionalIterator1 __first1,
  2165. _BidirectionalIterator1 __last1,
  2166. _BidirectionalIterator2 __first2,
  2167. _BidirectionalIterator2 __last2,
  2168. _BidirectionalIterator3 __result,
  2169. _Compare __comp)
  2170. {
  2171. if (__first1 == __last1)
  2172. {
  2173. _GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result);
  2174. return;
  2175. }
  2176. else if (__first2 == __last2)
  2177. return;
  2178. --__last1;
  2179. --__last2;
  2180. while (true)
  2181. {
  2182. if (__comp(__last2, __last1))
  2183. {
  2184. *--__result = _GLIBCXX_MOVE(*__last1);
  2185. if (__first1 == __last1)
  2186. {
  2187. _GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result);
  2188. return;
  2189. }
  2190. --__last1;
  2191. }
  2192. else
  2193. {
  2194. *--__result = _GLIBCXX_MOVE(*__last2);
  2195. if (__first2 == __last2)
  2196. return;
  2197. --__last2;
  2198. }
  2199. }
  2200. }
  2201. /// This is a helper function for the merge routines.
  2202. template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
  2203. typename _Distance>
  2204. _BidirectionalIterator1
  2205. __rotate_adaptive(_BidirectionalIterator1 __first,
  2206. _BidirectionalIterator1 __middle,
  2207. _BidirectionalIterator1 __last,
  2208. _Distance __len1, _Distance __len2,
  2209. _BidirectionalIterator2 __buffer,
  2210. _Distance __buffer_size)
  2211. {
  2212. _BidirectionalIterator2 __buffer_end;
  2213. if (__len1 > __len2 && __len2 <= __buffer_size)
  2214. {
  2215. if (__len2)
  2216. {
  2217. __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
  2218. _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last);
  2219. return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first);
  2220. }
  2221. else
  2222. return __first;
  2223. }
  2224. else if (__len1 <= __buffer_size)
  2225. {
  2226. if (__len1)
  2227. {
  2228. __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
  2229. _GLIBCXX_MOVE3(__middle, __last, __first);
  2230. return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
  2231. }
  2232. else
  2233. return __last;
  2234. }
  2235. else
  2236. return std::rotate(__first, __middle, __last);
  2237. }
  2238. /// This is a helper function for the merge routines.
  2239. template<typename _BidirectionalIterator, typename _Distance,
  2240. typename _Pointer, typename _Compare>
  2241. void
  2242. __merge_adaptive(_BidirectionalIterator __first,
  2243. _BidirectionalIterator __middle,
  2244. _BidirectionalIterator __last,
  2245. _Distance __len1, _Distance __len2,
  2246. _Pointer __buffer, _Distance __buffer_size,
  2247. _Compare __comp)
  2248. {
  2249. if (__len1 <= __len2 && __len1 <= __buffer_size)
  2250. {
  2251. _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
  2252. std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last,
  2253. __first, __comp);
  2254. }
  2255. else if (__len2 <= __buffer_size)
  2256. {
  2257. _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
  2258. std::__move_merge_adaptive_backward(__first, __middle, __buffer,
  2259. __buffer_end, __last, __comp);
  2260. }
  2261. else
  2262. {
  2263. _BidirectionalIterator __first_cut = __first;
  2264. _BidirectionalIterator __second_cut = __middle;
  2265. _Distance __len11 = 0;
  2266. _Distance __len22 = 0;
  2267. if (__len1 > __len2)
  2268. {
  2269. __len11 = __len1 / 2;
  2270. std::advance(__first_cut, __len11);
  2271. __second_cut
  2272. = std::__lower_bound(__middle, __last, *__first_cut,
  2273. __gnu_cxx::__ops::__iter_comp_val(__comp));
  2274. __len22 = std::distance(__middle, __second_cut);
  2275. }
  2276. else
  2277. {
  2278. __len22 = __len2 / 2;
  2279. std::advance(__second_cut, __len22);
  2280. __first_cut
  2281. = std::__upper_bound(__first, __middle, *__second_cut,
  2282. __gnu_cxx::__ops::__val_comp_iter(__comp));
  2283. __len11 = std::distance(__first, __first_cut);
  2284. }
  2285. _BidirectionalIterator __new_middle
  2286. = std::__rotate_adaptive(__first_cut, __middle, __second_cut,
  2287. __len1 - __len11, __len22, __buffer,
  2288. __buffer_size);
  2289. std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
  2290. __len22, __buffer, __buffer_size, __comp);
  2291. std::__merge_adaptive(__new_middle, __second_cut, __last,
  2292. __len1 - __len11,
  2293. __len2 - __len22, __buffer,
  2294. __buffer_size, __comp);
  2295. }
  2296. }
  2297. /// This is a helper function for the merge routines.
  2298. template<typename _BidirectionalIterator, typename _Distance,
  2299. typename _Compare>
  2300. void
  2301. __merge_without_buffer(_BidirectionalIterator __first,
  2302. _BidirectionalIterator __middle,
  2303. _BidirectionalIterator __last,
  2304. _Distance __len1, _Distance __len2,
  2305. _Compare __comp)
  2306. {
  2307. if (__len1 == 0 || __len2 == 0)
  2308. return;
  2309. if (__len1 + __len2 == 2)
  2310. {
  2311. if (__comp(__middle, __first))
  2312. std::iter_swap(__first, __middle);
  2313. return;
  2314. }
  2315. _BidirectionalIterator __first_cut = __first;
  2316. _BidirectionalIterator __second_cut = __middle;
  2317. _Distance __len11 = 0;
  2318. _Distance __len22 = 0;
  2319. if (__len1 > __len2)
  2320. {
  2321. __len11 = __len1 / 2;
  2322. std::advance(__first_cut, __len11);
  2323. __second_cut
  2324. = std::__lower_bound(__middle, __last, *__first_cut,
  2325. __gnu_cxx::__ops::__iter_comp_val(__comp));
  2326. __len22 = std::distance(__middle, __second_cut);
  2327. }
  2328. else
  2329. {
  2330. __len22 = __len2 / 2;
  2331. std::advance(__second_cut, __len22);
  2332. __first_cut
  2333. = std::__upper_bound(__first, __middle, *__second_cut,
  2334. __gnu_cxx::__ops::__val_comp_iter(__comp));
  2335. __len11 = std::distance(__first, __first_cut);
  2336. }
  2337. _BidirectionalIterator __new_middle
  2338. = std::rotate(__first_cut, __middle, __second_cut);
  2339. std::__merge_without_buffer(__first, __first_cut, __new_middle,
  2340. __len11, __len22, __comp);
  2341. std::__merge_without_buffer(__new_middle, __second_cut, __last,
  2342. __len1 - __len11, __len2 - __len22, __comp);
  2343. }
  2344. template<typename _BidirectionalIterator, typename _Compare>
  2345. void
  2346. __inplace_merge(_BidirectionalIterator __first,
  2347. _BidirectionalIterator __middle,
  2348. _BidirectionalIterator __last,
  2349. _Compare __comp)
  2350. {
  2351. typedef typename iterator_traits<_BidirectionalIterator>::value_type
  2352. _ValueType;
  2353. typedef typename iterator_traits<_BidirectionalIterator>::difference_type
  2354. _DistanceType;
  2355. if (__first == __middle || __middle == __last)
  2356. return;
  2357. const _DistanceType __len1 = std::distance(__first, __middle);
  2358. const _DistanceType __len2 = std::distance(__middle, __last);
  2359. typedef _Temporary_buffer<_BidirectionalIterator, _ValueType> _TmpBuf;
  2360. _TmpBuf __buf(__first, __len1 + __len2);
  2361. if (__buf.begin() == 0)
  2362. std::__merge_without_buffer
  2363. (__first, __middle, __last, __len1, __len2, __comp);
  2364. else
  2365. std::__merge_adaptive
  2366. (__first, __middle, __last, __len1, __len2, __buf.begin(),
  2367. _DistanceType(__buf.size()), __comp);
  2368. }
  2369. /**
  2370. * @brief Merges two sorted ranges in place.
  2371. * @ingroup sorting_algorithms
  2372. * @param __first An iterator.
  2373. * @param __middle Another iterator.
  2374. * @param __last Another iterator.
  2375. * @return Nothing.
  2376. *
  2377. * Merges two sorted and consecutive ranges, [__first,__middle) and
  2378. * [__middle,__last), and puts the result in [__first,__last). The
  2379. * output will be sorted. The sort is @e stable, that is, for
  2380. * equivalent elements in the two ranges, elements from the first
  2381. * range will always come before elements from the second.
  2382. *
  2383. * If enough additional memory is available, this takes (__last-__first)-1
  2384. * comparisons. Otherwise an NlogN algorithm is used, where N is
  2385. * distance(__first,__last).
  2386. */
  2387. template<typename _BidirectionalIterator>
  2388. inline void
  2389. inplace_merge(_BidirectionalIterator __first,
  2390. _BidirectionalIterator __middle,
  2391. _BidirectionalIterator __last)
  2392. {
  2393. // concept requirements
  2394. __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
  2395. _BidirectionalIterator>)
  2396. __glibcxx_function_requires(_LessThanComparableConcept<
  2397. typename iterator_traits<_BidirectionalIterator>::value_type>)
  2398. __glibcxx_requires_sorted(__first, __middle);
  2399. __glibcxx_requires_sorted(__middle, __last);
  2400. __glibcxx_requires_irreflexive(__first, __last);
  2401. std::__inplace_merge(__first, __middle, __last,
  2402. __gnu_cxx::__ops::__iter_less_iter());
  2403. }
  2404. /**
  2405. * @brief Merges two sorted ranges in place.
  2406. * @ingroup sorting_algorithms
  2407. * @param __first An iterator.
  2408. * @param __middle Another iterator.
  2409. * @param __last Another iterator.
  2410. * @param __comp A functor to use for comparisons.
  2411. * @return Nothing.
  2412. *
  2413. * Merges two sorted and consecutive ranges, [__first,__middle) and
  2414. * [middle,last), and puts the result in [__first,__last). The output will
  2415. * be sorted. The sort is @e stable, that is, for equivalent
  2416. * elements in the two ranges, elements from the first range will always
  2417. * come before elements from the second.
  2418. *
  2419. * If enough additional memory is available, this takes (__last-__first)-1
  2420. * comparisons. Otherwise an NlogN algorithm is used, where N is
  2421. * distance(__first,__last).
  2422. *
  2423. * The comparison function should have the same effects on ordering as
  2424. * the function used for the initial sort.
  2425. */
  2426. template<typename _BidirectionalIterator, typename _Compare>
  2427. inline void
  2428. inplace_merge(_BidirectionalIterator __first,
  2429. _BidirectionalIterator __middle,
  2430. _BidirectionalIterator __last,
  2431. _Compare __comp)
  2432. {
  2433. // concept requirements
  2434. __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
  2435. _BidirectionalIterator>)
  2436. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  2437. typename iterator_traits<_BidirectionalIterator>::value_type,
  2438. typename iterator_traits<_BidirectionalIterator>::value_type>)
  2439. __glibcxx_requires_sorted_pred(__first, __middle, __comp);
  2440. __glibcxx_requires_sorted_pred(__middle, __last, __comp);
  2441. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  2442. std::__inplace_merge(__first, __middle, __last,
  2443. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  2444. }
  2445. /// This is a helper function for the __merge_sort_loop routines.
  2446. template<typename _InputIterator, typename _OutputIterator,
  2447. typename _Compare>
  2448. _OutputIterator
  2449. __move_merge(_InputIterator __first1, _InputIterator __last1,
  2450. _InputIterator __first2, _InputIterator __last2,
  2451. _OutputIterator __result, _Compare __comp)
  2452. {
  2453. while (__first1 != __last1 && __first2 != __last2)
  2454. {
  2455. if (__comp(__first2, __first1))
  2456. {
  2457. *__result = _GLIBCXX_MOVE(*__first2);
  2458. ++__first2;
  2459. }
  2460. else
  2461. {
  2462. *__result = _GLIBCXX_MOVE(*__first1);
  2463. ++__first1;
  2464. }
  2465. ++__result;
  2466. }
  2467. return _GLIBCXX_MOVE3(__first2, __last2,
  2468. _GLIBCXX_MOVE3(__first1, __last1,
  2469. __result));
  2470. }
  2471. template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
  2472. typename _Distance, typename _Compare>
  2473. void
  2474. __merge_sort_loop(_RandomAccessIterator1 __first,
  2475. _RandomAccessIterator1 __last,
  2476. _RandomAccessIterator2 __result, _Distance __step_size,
  2477. _Compare __comp)
  2478. {
  2479. const _Distance __two_step = 2 * __step_size;
  2480. while (__last - __first >= __two_step)
  2481. {
  2482. __result = std::__move_merge(__first, __first + __step_size,
  2483. __first + __step_size,
  2484. __first + __two_step,
  2485. __result, __comp);
  2486. __first += __two_step;
  2487. }
  2488. __step_size = std::min(_Distance(__last - __first), __step_size);
  2489. std::__move_merge(__first, __first + __step_size,
  2490. __first + __step_size, __last, __result, __comp);
  2491. }
  2492. template<typename _RandomAccessIterator, typename _Distance,
  2493. typename _Compare>
  2494. _GLIBCXX20_CONSTEXPR
  2495. void
  2496. __chunk_insertion_sort(_RandomAccessIterator __first,
  2497. _RandomAccessIterator __last,
  2498. _Distance __chunk_size, _Compare __comp)
  2499. {
  2500. while (__last - __first >= __chunk_size)
  2501. {
  2502. std::__insertion_sort(__first, __first + __chunk_size, __comp);
  2503. __first += __chunk_size;
  2504. }
  2505. std::__insertion_sort(__first, __last, __comp);
  2506. }
  2507. enum { _S_chunk_size = 7 };
  2508. template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
  2509. void
  2510. __merge_sort_with_buffer(_RandomAccessIterator __first,
  2511. _RandomAccessIterator __last,
  2512. _Pointer __buffer, _Compare __comp)
  2513. {
  2514. typedef typename iterator_traits<_RandomAccessIterator>::difference_type
  2515. _Distance;
  2516. const _Distance __len = __last - __first;
  2517. const _Pointer __buffer_last = __buffer + __len;
  2518. _Distance __step_size = _S_chunk_size;
  2519. std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
  2520. while (__step_size < __len)
  2521. {
  2522. std::__merge_sort_loop(__first, __last, __buffer,
  2523. __step_size, __comp);
  2524. __step_size *= 2;
  2525. std::__merge_sort_loop(__buffer, __buffer_last, __first,
  2526. __step_size, __comp);
  2527. __step_size *= 2;
  2528. }
  2529. }
  2530. template<typename _RandomAccessIterator, typename _Pointer,
  2531. typename _Distance, typename _Compare>
  2532. void
  2533. __stable_sort_adaptive(_RandomAccessIterator __first,
  2534. _RandomAccessIterator __last,
  2535. _Pointer __buffer, _Distance __buffer_size,
  2536. _Compare __comp)
  2537. {
  2538. const _Distance __len = (__last - __first + 1) / 2;
  2539. const _RandomAccessIterator __middle = __first + __len;
  2540. if (__len > __buffer_size)
  2541. {
  2542. std::__stable_sort_adaptive(__first, __middle, __buffer,
  2543. __buffer_size, __comp);
  2544. std::__stable_sort_adaptive(__middle, __last, __buffer,
  2545. __buffer_size, __comp);
  2546. }
  2547. else
  2548. {
  2549. std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
  2550. std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
  2551. }
  2552. std::__merge_adaptive(__first, __middle, __last,
  2553. _Distance(__middle - __first),
  2554. _Distance(__last - __middle),
  2555. __buffer, __buffer_size,
  2556. __comp);
  2557. }
  2558. /// This is a helper function for the stable sorting routines.
  2559. template<typename _RandomAccessIterator, typename _Compare>
  2560. void
  2561. __inplace_stable_sort(_RandomAccessIterator __first,
  2562. _RandomAccessIterator __last, _Compare __comp)
  2563. {
  2564. if (__last - __first < 15)
  2565. {
  2566. std::__insertion_sort(__first, __last, __comp);
  2567. return;
  2568. }
  2569. _RandomAccessIterator __middle = __first + (__last - __first) / 2;
  2570. std::__inplace_stable_sort(__first, __middle, __comp);
  2571. std::__inplace_stable_sort(__middle, __last, __comp);
  2572. std::__merge_without_buffer(__first, __middle, __last,
  2573. __middle - __first,
  2574. __last - __middle,
  2575. __comp);
  2576. }
  2577. // stable_sort
  2578. // Set algorithms: includes, set_union, set_intersection, set_difference,
  2579. // set_symmetric_difference. All of these algorithms have the precondition
  2580. // that their input ranges are sorted and the postcondition that their output
  2581. // ranges are sorted.
  2582. template<typename _InputIterator1, typename _InputIterator2,
  2583. typename _Compare>
  2584. _GLIBCXX20_CONSTEXPR
  2585. bool
  2586. __includes(_InputIterator1 __first1, _InputIterator1 __last1,
  2587. _InputIterator2 __first2, _InputIterator2 __last2,
  2588. _Compare __comp)
  2589. {
  2590. while (__first1 != __last1 && __first2 != __last2)
  2591. if (__comp(__first2, __first1))
  2592. return false;
  2593. else if (__comp(__first1, __first2))
  2594. ++__first1;
  2595. else
  2596. {
  2597. ++__first1;
  2598. ++__first2;
  2599. }
  2600. return __first2 == __last2;
  2601. }
  2602. /**
  2603. * @brief Determines whether all elements of a sequence exists in a range.
  2604. * @param __first1 Start of search range.
  2605. * @param __last1 End of search range.
  2606. * @param __first2 Start of sequence
  2607. * @param __last2 End of sequence.
  2608. * @return True if each element in [__first2,__last2) is contained in order
  2609. * within [__first1,__last1). False otherwise.
  2610. * @ingroup set_algorithms
  2611. *
  2612. * This operation expects both [__first1,__last1) and
  2613. * [__first2,__last2) to be sorted. Searches for the presence of
  2614. * each element in [__first2,__last2) within [__first1,__last1).
  2615. * The iterators over each range only move forward, so this is a
  2616. * linear algorithm. If an element in [__first2,__last2) is not
  2617. * found before the search iterator reaches @p __last2, false is
  2618. * returned.
  2619. */
  2620. template<typename _InputIterator1, typename _InputIterator2>
  2621. _GLIBCXX20_CONSTEXPR
  2622. inline bool
  2623. includes(_InputIterator1 __first1, _InputIterator1 __last1,
  2624. _InputIterator2 __first2, _InputIterator2 __last2)
  2625. {
  2626. // concept requirements
  2627. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  2628. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  2629. __glibcxx_function_requires(_LessThanOpConcept<
  2630. typename iterator_traits<_InputIterator1>::value_type,
  2631. typename iterator_traits<_InputIterator2>::value_type>)
  2632. __glibcxx_function_requires(_LessThanOpConcept<
  2633. typename iterator_traits<_InputIterator2>::value_type,
  2634. typename iterator_traits<_InputIterator1>::value_type>)
  2635. __glibcxx_requires_sorted_set(__first1, __last1, __first2);
  2636. __glibcxx_requires_sorted_set(__first2, __last2, __first1);
  2637. __glibcxx_requires_irreflexive2(__first1, __last1);
  2638. __glibcxx_requires_irreflexive2(__first2, __last2);
  2639. return std::__includes(__first1, __last1, __first2, __last2,
  2640. __gnu_cxx::__ops::__iter_less_iter());
  2641. }
  2642. /**
  2643. * @brief Determines whether all elements of a sequence exists in a range
  2644. * using comparison.
  2645. * @ingroup set_algorithms
  2646. * @param __first1 Start of search range.
  2647. * @param __last1 End of search range.
  2648. * @param __first2 Start of sequence
  2649. * @param __last2 End of sequence.
  2650. * @param __comp Comparison function to use.
  2651. * @return True if each element in [__first2,__last2) is contained
  2652. * in order within [__first1,__last1) according to comp. False
  2653. * otherwise. @ingroup set_algorithms
  2654. *
  2655. * This operation expects both [__first1,__last1) and
  2656. * [__first2,__last2) to be sorted. Searches for the presence of
  2657. * each element in [__first2,__last2) within [__first1,__last1),
  2658. * using comp to decide. The iterators over each range only move
  2659. * forward, so this is a linear algorithm. If an element in
  2660. * [__first2,__last2) is not found before the search iterator
  2661. * reaches @p __last2, false is returned.
  2662. */
  2663. template<typename _InputIterator1, typename _InputIterator2,
  2664. typename _Compare>
  2665. _GLIBCXX20_CONSTEXPR
  2666. inline bool
  2667. includes(_InputIterator1 __first1, _InputIterator1 __last1,
  2668. _InputIterator2 __first2, _InputIterator2 __last2,
  2669. _Compare __comp)
  2670. {
  2671. // concept requirements
  2672. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  2673. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  2674. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  2675. typename iterator_traits<_InputIterator1>::value_type,
  2676. typename iterator_traits<_InputIterator2>::value_type>)
  2677. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  2678. typename iterator_traits<_InputIterator2>::value_type,
  2679. typename iterator_traits<_InputIterator1>::value_type>)
  2680. __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
  2681. __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
  2682. __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
  2683. __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
  2684. return std::__includes(__first1, __last1, __first2, __last2,
  2685. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  2686. }
  2687. // nth_element
  2688. // merge
  2689. // set_difference
  2690. // set_intersection
  2691. // set_union
  2692. // stable_sort
  2693. // set_symmetric_difference
  2694. // min_element
  2695. // max_element
  2696. template<typename _BidirectionalIterator, typename _Compare>
  2697. _GLIBCXX20_CONSTEXPR
  2698. bool
  2699. __next_permutation(_BidirectionalIterator __first,
  2700. _BidirectionalIterator __last, _Compare __comp)
  2701. {
  2702. if (__first == __last)
  2703. return false;
  2704. _BidirectionalIterator __i = __first;
  2705. ++__i;
  2706. if (__i == __last)
  2707. return false;
  2708. __i = __last;
  2709. --__i;
  2710. for(;;)
  2711. {
  2712. _BidirectionalIterator __ii = __i;
  2713. --__i;
  2714. if (__comp(__i, __ii))
  2715. {
  2716. _BidirectionalIterator __j = __last;
  2717. while (!__comp(__i, --__j))
  2718. {}
  2719. std::iter_swap(__i, __j);
  2720. std::__reverse(__ii, __last,
  2721. std::__iterator_category(__first));
  2722. return true;
  2723. }
  2724. if (__i == __first)
  2725. {
  2726. std::__reverse(__first, __last,
  2727. std::__iterator_category(__first));
  2728. return false;
  2729. }
  2730. }
  2731. }
  2732. /**
  2733. * @brief Permute range into the next @e dictionary ordering.
  2734. * @ingroup sorting_algorithms
  2735. * @param __first Start of range.
  2736. * @param __last End of range.
  2737. * @return False if wrapped to first permutation, true otherwise.
  2738. *
  2739. * Treats all permutations of the range as a set of @e dictionary sorted
  2740. * sequences. Permutes the current sequence into the next one of this set.
  2741. * Returns true if there are more sequences to generate. If the sequence
  2742. * is the largest of the set, the smallest is generated and false returned.
  2743. */
  2744. template<typename _BidirectionalIterator>
  2745. _GLIBCXX20_CONSTEXPR
  2746. inline bool
  2747. next_permutation(_BidirectionalIterator __first,
  2748. _BidirectionalIterator __last)
  2749. {
  2750. // concept requirements
  2751. __glibcxx_function_requires(_BidirectionalIteratorConcept<
  2752. _BidirectionalIterator>)
  2753. __glibcxx_function_requires(_LessThanComparableConcept<
  2754. typename iterator_traits<_BidirectionalIterator>::value_type>)
  2755. __glibcxx_requires_valid_range(__first, __last);
  2756. __glibcxx_requires_irreflexive(__first, __last);
  2757. return std::__next_permutation
  2758. (__first, __last, __gnu_cxx::__ops::__iter_less_iter());
  2759. }
  2760. /**
  2761. * @brief Permute range into the next @e dictionary ordering using
  2762. * comparison functor.
  2763. * @ingroup sorting_algorithms
  2764. * @param __first Start of range.
  2765. * @param __last End of range.
  2766. * @param __comp A comparison functor.
  2767. * @return False if wrapped to first permutation, true otherwise.
  2768. *
  2769. * Treats all permutations of the range [__first,__last) as a set of
  2770. * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
  2771. * sequence into the next one of this set. Returns true if there are more
  2772. * sequences to generate. If the sequence is the largest of the set, the
  2773. * smallest is generated and false returned.
  2774. */
  2775. template<typename _BidirectionalIterator, typename _Compare>
  2776. _GLIBCXX20_CONSTEXPR
  2777. inline bool
  2778. next_permutation(_BidirectionalIterator __first,
  2779. _BidirectionalIterator __last, _Compare __comp)
  2780. {
  2781. // concept requirements
  2782. __glibcxx_function_requires(_BidirectionalIteratorConcept<
  2783. _BidirectionalIterator>)
  2784. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  2785. typename iterator_traits<_BidirectionalIterator>::value_type,
  2786. typename iterator_traits<_BidirectionalIterator>::value_type>)
  2787. __glibcxx_requires_valid_range(__first, __last);
  2788. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  2789. return std::__next_permutation
  2790. (__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
  2791. }
  2792. template<typename _BidirectionalIterator, typename _Compare>
  2793. _GLIBCXX20_CONSTEXPR
  2794. bool
  2795. __prev_permutation(_BidirectionalIterator __first,
  2796. _BidirectionalIterator __last, _Compare __comp)
  2797. {
  2798. if (__first == __last)
  2799. return false;
  2800. _BidirectionalIterator __i = __first;
  2801. ++__i;
  2802. if (__i == __last)
  2803. return false;
  2804. __i = __last;
  2805. --__i;
  2806. for(;;)
  2807. {
  2808. _BidirectionalIterator __ii = __i;
  2809. --__i;
  2810. if (__comp(__ii, __i))
  2811. {
  2812. _BidirectionalIterator __j = __last;
  2813. while (!__comp(--__j, __i))
  2814. {}
  2815. std::iter_swap(__i, __j);
  2816. std::__reverse(__ii, __last,
  2817. std::__iterator_category(__first));
  2818. return true;
  2819. }
  2820. if (__i == __first)
  2821. {
  2822. std::__reverse(__first, __last,
  2823. std::__iterator_category(__first));
  2824. return false;
  2825. }
  2826. }
  2827. }
  2828. /**
  2829. * @brief Permute range into the previous @e dictionary ordering.
  2830. * @ingroup sorting_algorithms
  2831. * @param __first Start of range.
  2832. * @param __last End of range.
  2833. * @return False if wrapped to last permutation, true otherwise.
  2834. *
  2835. * Treats all permutations of the range as a set of @e dictionary sorted
  2836. * sequences. Permutes the current sequence into the previous one of this
  2837. * set. Returns true if there are more sequences to generate. If the
  2838. * sequence is the smallest of the set, the largest is generated and false
  2839. * returned.
  2840. */
  2841. template<typename _BidirectionalIterator>
  2842. _GLIBCXX20_CONSTEXPR
  2843. inline bool
  2844. prev_permutation(_BidirectionalIterator __first,
  2845. _BidirectionalIterator __last)
  2846. {
  2847. // concept requirements
  2848. __glibcxx_function_requires(_BidirectionalIteratorConcept<
  2849. _BidirectionalIterator>)
  2850. __glibcxx_function_requires(_LessThanComparableConcept<
  2851. typename iterator_traits<_BidirectionalIterator>::value_type>)
  2852. __glibcxx_requires_valid_range(__first, __last);
  2853. __glibcxx_requires_irreflexive(__first, __last);
  2854. return std::__prev_permutation(__first, __last,
  2855. __gnu_cxx::__ops::__iter_less_iter());
  2856. }
  2857. /**
  2858. * @brief Permute range into the previous @e dictionary ordering using
  2859. * comparison functor.
  2860. * @ingroup sorting_algorithms
  2861. * @param __first Start of range.
  2862. * @param __last End of range.
  2863. * @param __comp A comparison functor.
  2864. * @return False if wrapped to last permutation, true otherwise.
  2865. *
  2866. * Treats all permutations of the range [__first,__last) as a set of
  2867. * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
  2868. * sequence into the previous one of this set. Returns true if there are
  2869. * more sequences to generate. If the sequence is the smallest of the set,
  2870. * the largest is generated and false returned.
  2871. */
  2872. template<typename _BidirectionalIterator, typename _Compare>
  2873. _GLIBCXX20_CONSTEXPR
  2874. inline bool
  2875. prev_permutation(_BidirectionalIterator __first,
  2876. _BidirectionalIterator __last, _Compare __comp)
  2877. {
  2878. // concept requirements
  2879. __glibcxx_function_requires(_BidirectionalIteratorConcept<
  2880. _BidirectionalIterator>)
  2881. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  2882. typename iterator_traits<_BidirectionalIterator>::value_type,
  2883. typename iterator_traits<_BidirectionalIterator>::value_type>)
  2884. __glibcxx_requires_valid_range(__first, __last);
  2885. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  2886. return std::__prev_permutation(__first, __last,
  2887. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  2888. }
  2889. // replace
  2890. // replace_if
  2891. template<typename _InputIterator, typename _OutputIterator,
  2892. typename _Predicate, typename _Tp>
  2893. _GLIBCXX20_CONSTEXPR
  2894. _OutputIterator
  2895. __replace_copy_if(_InputIterator __first, _InputIterator __last,
  2896. _OutputIterator __result,
  2897. _Predicate __pred, const _Tp& __new_value)
  2898. {
  2899. for (; __first != __last; ++__first, (void)++__result)
  2900. if (__pred(__first))
  2901. *__result = __new_value;
  2902. else
  2903. *__result = *__first;
  2904. return __result;
  2905. }
  2906. /**
  2907. * @brief Copy a sequence, replacing each element of one value with another
  2908. * value.
  2909. * @param __first An input iterator.
  2910. * @param __last An input iterator.
  2911. * @param __result An output iterator.
  2912. * @param __old_value The value to be replaced.
  2913. * @param __new_value The replacement value.
  2914. * @return The end of the output sequence, @p result+(last-first).
  2915. *
  2916. * Copies each element in the input range @p [__first,__last) to the
  2917. * output range @p [__result,__result+(__last-__first)) replacing elements
  2918. * equal to @p __old_value with @p __new_value.
  2919. */
  2920. template<typename _InputIterator, typename _OutputIterator, typename _Tp>
  2921. _GLIBCXX20_CONSTEXPR
  2922. inline _OutputIterator
  2923. replace_copy(_InputIterator __first, _InputIterator __last,
  2924. _OutputIterator __result,
  2925. const _Tp& __old_value, const _Tp& __new_value)
  2926. {
  2927. // concept requirements
  2928. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  2929. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  2930. typename iterator_traits<_InputIterator>::value_type>)
  2931. __glibcxx_function_requires(_EqualOpConcept<
  2932. typename iterator_traits<_InputIterator>::value_type, _Tp>)
  2933. __glibcxx_requires_valid_range(__first, __last);
  2934. return std::__replace_copy_if(__first, __last, __result,
  2935. __gnu_cxx::__ops::__iter_equals_val(__old_value),
  2936. __new_value);
  2937. }
  2938. /**
  2939. * @brief Copy a sequence, replacing each value for which a predicate
  2940. * returns true with another value.
  2941. * @ingroup mutating_algorithms
  2942. * @param __first An input iterator.
  2943. * @param __last An input iterator.
  2944. * @param __result An output iterator.
  2945. * @param __pred A predicate.
  2946. * @param __new_value The replacement value.
  2947. * @return The end of the output sequence, @p __result+(__last-__first).
  2948. *
  2949. * Copies each element in the range @p [__first,__last) to the range
  2950. * @p [__result,__result+(__last-__first)) replacing elements for which
  2951. * @p __pred returns true with @p __new_value.
  2952. */
  2953. template<typename _InputIterator, typename _OutputIterator,
  2954. typename _Predicate, typename _Tp>
  2955. _GLIBCXX20_CONSTEXPR
  2956. inline _OutputIterator
  2957. replace_copy_if(_InputIterator __first, _InputIterator __last,
  2958. _OutputIterator __result,
  2959. _Predicate __pred, const _Tp& __new_value)
  2960. {
  2961. // concept requirements
  2962. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  2963. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  2964. typename iterator_traits<_InputIterator>::value_type>)
  2965. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  2966. typename iterator_traits<_InputIterator>::value_type>)
  2967. __glibcxx_requires_valid_range(__first, __last);
  2968. return std::__replace_copy_if(__first, __last, __result,
  2969. __gnu_cxx::__ops::__pred_iter(__pred),
  2970. __new_value);
  2971. }
  2972. #if __cplusplus >= 201103L
  2973. /**
  2974. * @brief Determines whether the elements of a sequence are sorted.
  2975. * @ingroup sorting_algorithms
  2976. * @param __first An iterator.
  2977. * @param __last Another iterator.
  2978. * @return True if the elements are sorted, false otherwise.
  2979. */
  2980. template<typename _ForwardIterator>
  2981. _GLIBCXX20_CONSTEXPR
  2982. inline bool
  2983. is_sorted(_ForwardIterator __first, _ForwardIterator __last)
  2984. { return std::is_sorted_until(__first, __last) == __last; }
  2985. /**
  2986. * @brief Determines whether the elements of a sequence are sorted
  2987. * according to a comparison functor.
  2988. * @ingroup sorting_algorithms
  2989. * @param __first An iterator.
  2990. * @param __last Another iterator.
  2991. * @param __comp A comparison functor.
  2992. * @return True if the elements are sorted, false otherwise.
  2993. */
  2994. template<typename _ForwardIterator, typename _Compare>
  2995. _GLIBCXX20_CONSTEXPR
  2996. inline bool
  2997. is_sorted(_ForwardIterator __first, _ForwardIterator __last,
  2998. _Compare __comp)
  2999. { return std::is_sorted_until(__first, __last, __comp) == __last; }
  3000. template<typename _ForwardIterator, typename _Compare>
  3001. _GLIBCXX20_CONSTEXPR
  3002. _ForwardIterator
  3003. __is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
  3004. _Compare __comp)
  3005. {
  3006. if (__first == __last)
  3007. return __last;
  3008. _ForwardIterator __next = __first;
  3009. for (++__next; __next != __last; __first = __next, (void)++__next)
  3010. if (__comp(__next, __first))
  3011. return __next;
  3012. return __next;
  3013. }
  3014. /**
  3015. * @brief Determines the end of a sorted sequence.
  3016. * @ingroup sorting_algorithms
  3017. * @param __first An iterator.
  3018. * @param __last Another iterator.
  3019. * @return An iterator pointing to the last iterator i in [__first, __last)
  3020. * for which the range [__first, i) is sorted.
  3021. */
  3022. template<typename _ForwardIterator>
  3023. _GLIBCXX20_CONSTEXPR
  3024. inline _ForwardIterator
  3025. is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
  3026. {
  3027. // concept requirements
  3028. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  3029. __glibcxx_function_requires(_LessThanComparableConcept<
  3030. typename iterator_traits<_ForwardIterator>::value_type>)
  3031. __glibcxx_requires_valid_range(__first, __last);
  3032. __glibcxx_requires_irreflexive(__first, __last);
  3033. return std::__is_sorted_until(__first, __last,
  3034. __gnu_cxx::__ops::__iter_less_iter());
  3035. }
  3036. /**
  3037. * @brief Determines the end of a sorted sequence using comparison functor.
  3038. * @ingroup sorting_algorithms
  3039. * @param __first An iterator.
  3040. * @param __last Another iterator.
  3041. * @param __comp A comparison functor.
  3042. * @return An iterator pointing to the last iterator i in [__first, __last)
  3043. * for which the range [__first, i) is sorted.
  3044. */
  3045. template<typename _ForwardIterator, typename _Compare>
  3046. _GLIBCXX20_CONSTEXPR
  3047. inline _ForwardIterator
  3048. is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
  3049. _Compare __comp)
  3050. {
  3051. // concept requirements
  3052. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  3053. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  3054. typename iterator_traits<_ForwardIterator>::value_type,
  3055. typename iterator_traits<_ForwardIterator>::value_type>)
  3056. __glibcxx_requires_valid_range(__first, __last);
  3057. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  3058. return std::__is_sorted_until(__first, __last,
  3059. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  3060. }
  3061. /**
  3062. * @brief Determines min and max at once as an ordered pair.
  3063. * @ingroup sorting_algorithms
  3064. * @param __a A thing of arbitrary type.
  3065. * @param __b Another thing of arbitrary type.
  3066. * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
  3067. * __b) otherwise.
  3068. */
  3069. template<typename _Tp>
  3070. _GLIBCXX14_CONSTEXPR
  3071. inline pair<const _Tp&, const _Tp&>
  3072. minmax(const _Tp& __a, const _Tp& __b)
  3073. {
  3074. // concept requirements
  3075. __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
  3076. return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
  3077. : pair<const _Tp&, const _Tp&>(__a, __b);
  3078. }
  3079. /**
  3080. * @brief Determines min and max at once as an ordered pair.
  3081. * @ingroup sorting_algorithms
  3082. * @param __a A thing of arbitrary type.
  3083. * @param __b Another thing of arbitrary type.
  3084. * @param __comp A @link comparison_functors comparison functor @endlink.
  3085. * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
  3086. * __b) otherwise.
  3087. */
  3088. template<typename _Tp, typename _Compare>
  3089. _GLIBCXX14_CONSTEXPR
  3090. inline pair<const _Tp&, const _Tp&>
  3091. minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
  3092. {
  3093. return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
  3094. : pair<const _Tp&, const _Tp&>(__a, __b);
  3095. }
  3096. template<typename _ForwardIterator, typename _Compare>
  3097. _GLIBCXX14_CONSTEXPR
  3098. pair<_ForwardIterator, _ForwardIterator>
  3099. __minmax_element(_ForwardIterator __first, _ForwardIterator __last,
  3100. _Compare __comp)
  3101. {
  3102. _ForwardIterator __next = __first;
  3103. if (__first == __last
  3104. || ++__next == __last)
  3105. return std::make_pair(__first, __first);
  3106. _ForwardIterator __min{}, __max{};
  3107. if (__comp(__next, __first))
  3108. {
  3109. __min = __next;
  3110. __max = __first;
  3111. }
  3112. else
  3113. {
  3114. __min = __first;
  3115. __max = __next;
  3116. }
  3117. __first = __next;
  3118. ++__first;
  3119. while (__first != __last)
  3120. {
  3121. __next = __first;
  3122. if (++__next == __last)
  3123. {
  3124. if (__comp(__first, __min))
  3125. __min = __first;
  3126. else if (!__comp(__first, __max))
  3127. __max = __first;
  3128. break;
  3129. }
  3130. if (__comp(__next, __first))
  3131. {
  3132. if (__comp(__next, __min))
  3133. __min = __next;
  3134. if (!__comp(__first, __max))
  3135. __max = __first;
  3136. }
  3137. else
  3138. {
  3139. if (__comp(__first, __min))
  3140. __min = __first;
  3141. if (!__comp(__next, __max))
  3142. __max = __next;
  3143. }
  3144. __first = __next;
  3145. ++__first;
  3146. }
  3147. return std::make_pair(__min, __max);
  3148. }
  3149. /**
  3150. * @brief Return a pair of iterators pointing to the minimum and maximum
  3151. * elements in a range.
  3152. * @ingroup sorting_algorithms
  3153. * @param __first Start of range.
  3154. * @param __last End of range.
  3155. * @return make_pair(m, M), where m is the first iterator i in
  3156. * [__first, __last) such that no other element in the range is
  3157. * smaller, and where M is the last iterator i in [__first, __last)
  3158. * such that no other element in the range is larger.
  3159. */
  3160. template<typename _ForwardIterator>
  3161. _GLIBCXX14_CONSTEXPR
  3162. inline pair<_ForwardIterator, _ForwardIterator>
  3163. minmax_element(_ForwardIterator __first, _ForwardIterator __last)
  3164. {
  3165. // concept requirements
  3166. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  3167. __glibcxx_function_requires(_LessThanComparableConcept<
  3168. typename iterator_traits<_ForwardIterator>::value_type>)
  3169. __glibcxx_requires_valid_range(__first, __last);
  3170. __glibcxx_requires_irreflexive(__first, __last);
  3171. return std::__minmax_element(__first, __last,
  3172. __gnu_cxx::__ops::__iter_less_iter());
  3173. }
  3174. /**
  3175. * @brief Return a pair of iterators pointing to the minimum and maximum
  3176. * elements in a range.
  3177. * @ingroup sorting_algorithms
  3178. * @param __first Start of range.
  3179. * @param __last End of range.
  3180. * @param __comp Comparison functor.
  3181. * @return make_pair(m, M), where m is the first iterator i in
  3182. * [__first, __last) such that no other element in the range is
  3183. * smaller, and where M is the last iterator i in [__first, __last)
  3184. * such that no other element in the range is larger.
  3185. */
  3186. template<typename _ForwardIterator, typename _Compare>
  3187. _GLIBCXX14_CONSTEXPR
  3188. inline pair<_ForwardIterator, _ForwardIterator>
  3189. minmax_element(_ForwardIterator __first, _ForwardIterator __last,
  3190. _Compare __comp)
  3191. {
  3192. // concept requirements
  3193. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  3194. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  3195. typename iterator_traits<_ForwardIterator>::value_type,
  3196. typename iterator_traits<_ForwardIterator>::value_type>)
  3197. __glibcxx_requires_valid_range(__first, __last);
  3198. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  3199. return std::__minmax_element(__first, __last,
  3200. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  3201. }
  3202. // N2722 + DR 915.
  3203. template<typename _Tp>
  3204. _GLIBCXX14_CONSTEXPR
  3205. inline _Tp
  3206. min(initializer_list<_Tp> __l)
  3207. { return *std::min_element(__l.begin(), __l.end()); }
  3208. template<typename _Tp, typename _Compare>
  3209. _GLIBCXX14_CONSTEXPR
  3210. inline _Tp
  3211. min(initializer_list<_Tp> __l, _Compare __comp)
  3212. { return *std::min_element(__l.begin(), __l.end(), __comp); }
  3213. template<typename _Tp>
  3214. _GLIBCXX14_CONSTEXPR
  3215. inline _Tp
  3216. max(initializer_list<_Tp> __l)
  3217. { return *std::max_element(__l.begin(), __l.end()); }
  3218. template<typename _Tp, typename _Compare>
  3219. _GLIBCXX14_CONSTEXPR
  3220. inline _Tp
  3221. max(initializer_list<_Tp> __l, _Compare __comp)
  3222. { return *std::max_element(__l.begin(), __l.end(), __comp); }
  3223. template<typename _Tp>
  3224. _GLIBCXX14_CONSTEXPR
  3225. inline pair<_Tp, _Tp>
  3226. minmax(initializer_list<_Tp> __l)
  3227. {
  3228. pair<const _Tp*, const _Tp*> __p =
  3229. std::minmax_element(__l.begin(), __l.end());
  3230. return std::make_pair(*__p.first, *__p.second);
  3231. }
  3232. template<typename _Tp, typename _Compare>
  3233. _GLIBCXX14_CONSTEXPR
  3234. inline pair<_Tp, _Tp>
  3235. minmax(initializer_list<_Tp> __l, _Compare __comp)
  3236. {
  3237. pair<const _Tp*, const _Tp*> __p =
  3238. std::minmax_element(__l.begin(), __l.end(), __comp);
  3239. return std::make_pair(*__p.first, *__p.second);
  3240. }
  3241. /**
  3242. * @brief Checks whether a permutation of the second sequence is equal
  3243. * to the first sequence.
  3244. * @ingroup non_mutating_algorithms
  3245. * @param __first1 Start of first range.
  3246. * @param __last1 End of first range.
  3247. * @param __first2 Start of second range.
  3248. * @param __pred A binary predicate.
  3249. * @return true if there exists a permutation of the elements in
  3250. * the range [__first2, __first2 + (__last1 - __first1)),
  3251. * beginning with ForwardIterator2 begin, such that
  3252. * equal(__first1, __last1, __begin, __pred) returns true;
  3253. * otherwise, returns false.
  3254. */
  3255. template<typename _ForwardIterator1, typename _ForwardIterator2,
  3256. typename _BinaryPredicate>
  3257. _GLIBCXX20_CONSTEXPR
  3258. inline bool
  3259. is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  3260. _ForwardIterator2 __first2, _BinaryPredicate __pred)
  3261. {
  3262. // concept requirements
  3263. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
  3264. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
  3265. __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
  3266. typename iterator_traits<_ForwardIterator1>::value_type,
  3267. typename iterator_traits<_ForwardIterator2>::value_type>)
  3268. __glibcxx_requires_valid_range(__first1, __last1);
  3269. return std::__is_permutation(__first1, __last1, __first2,
  3270. __gnu_cxx::__ops::__iter_comp_iter(__pred));
  3271. }
  3272. #if __cplusplus > 201103L
  3273. template<typename _ForwardIterator1, typename _ForwardIterator2,
  3274. typename _BinaryPredicate>
  3275. _GLIBCXX20_CONSTEXPR
  3276. bool
  3277. __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  3278. _ForwardIterator2 __first2, _ForwardIterator2 __last2,
  3279. _BinaryPredicate __pred)
  3280. {
  3281. using _Cat1
  3282. = typename iterator_traits<_ForwardIterator1>::iterator_category;
  3283. using _Cat2
  3284. = typename iterator_traits<_ForwardIterator2>::iterator_category;
  3285. using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>;
  3286. using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>;
  3287. constexpr bool __ra_iters = _It1_is_RA() && _It2_is_RA();
  3288. if (__ra_iters)
  3289. {
  3290. auto __d1 = std::distance(__first1, __last1);
  3291. auto __d2 = std::distance(__first2, __last2);
  3292. if (__d1 != __d2)
  3293. return false;
  3294. }
  3295. // Efficiently compare identical prefixes: O(N) if sequences
  3296. // have the same elements in the same order.
  3297. for (; __first1 != __last1 && __first2 != __last2;
  3298. ++__first1, (void)++__first2)
  3299. if (!__pred(__first1, __first2))
  3300. break;
  3301. if (__ra_iters)
  3302. {
  3303. if (__first1 == __last1)
  3304. return true;
  3305. }
  3306. else
  3307. {
  3308. auto __d1 = std::distance(__first1, __last1);
  3309. auto __d2 = std::distance(__first2, __last2);
  3310. if (__d1 == 0 && __d2 == 0)
  3311. return true;
  3312. if (__d1 != __d2)
  3313. return false;
  3314. }
  3315. for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
  3316. {
  3317. if (__scan != std::__find_if(__first1, __scan,
  3318. __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
  3319. continue; // We've seen this one before.
  3320. auto __matches = std::__count_if(__first2, __last2,
  3321. __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
  3322. if (0 == __matches
  3323. || std::__count_if(__scan, __last1,
  3324. __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
  3325. != __matches)
  3326. return false;
  3327. }
  3328. return true;
  3329. }
  3330. /**
  3331. * @brief Checks whether a permutaion of the second sequence is equal
  3332. * to the first sequence.
  3333. * @ingroup non_mutating_algorithms
  3334. * @param __first1 Start of first range.
  3335. * @param __last1 End of first range.
  3336. * @param __first2 Start of second range.
  3337. * @param __last2 End of first range.
  3338. * @return true if there exists a permutation of the elements in the range
  3339. * [__first2, __last2), beginning with ForwardIterator2 begin,
  3340. * such that equal(__first1, __last1, begin) returns true;
  3341. * otherwise, returns false.
  3342. */
  3343. template<typename _ForwardIterator1, typename _ForwardIterator2>
  3344. _GLIBCXX20_CONSTEXPR
  3345. inline bool
  3346. is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  3347. _ForwardIterator2 __first2, _ForwardIterator2 __last2)
  3348. {
  3349. __glibcxx_requires_valid_range(__first1, __last1);
  3350. __glibcxx_requires_valid_range(__first2, __last2);
  3351. return
  3352. std::__is_permutation(__first1, __last1, __first2, __last2,
  3353. __gnu_cxx::__ops::__iter_equal_to_iter());
  3354. }
  3355. /**
  3356. * @brief Checks whether a permutation of the second sequence is equal
  3357. * to the first sequence.
  3358. * @ingroup non_mutating_algorithms
  3359. * @param __first1 Start of first range.
  3360. * @param __last1 End of first range.
  3361. * @param __first2 Start of second range.
  3362. * @param __last2 End of first range.
  3363. * @param __pred A binary predicate.
  3364. * @return true if there exists a permutation of the elements in the range
  3365. * [__first2, __last2), beginning with ForwardIterator2 begin,
  3366. * such that equal(__first1, __last1, __begin, __pred) returns true;
  3367. * otherwise, returns false.
  3368. */
  3369. template<typename _ForwardIterator1, typename _ForwardIterator2,
  3370. typename _BinaryPredicate>
  3371. _GLIBCXX20_CONSTEXPR
  3372. inline bool
  3373. is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  3374. _ForwardIterator2 __first2, _ForwardIterator2 __last2,
  3375. _BinaryPredicate __pred)
  3376. {
  3377. __glibcxx_requires_valid_range(__first1, __last1);
  3378. __glibcxx_requires_valid_range(__first2, __last2);
  3379. return std::__is_permutation(__first1, __last1, __first2, __last2,
  3380. __gnu_cxx::__ops::__iter_comp_iter(__pred));
  3381. }
  3382. #if __cplusplus > 201402L
  3383. #define __cpp_lib_clamp 201603
  3384. /**
  3385. * @brief Returns the value clamped between lo and hi.
  3386. * @ingroup sorting_algorithms
  3387. * @param __val A value of arbitrary type.
  3388. * @param __lo A lower limit of arbitrary type.
  3389. * @param __hi An upper limit of arbitrary type.
  3390. * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise.
  3391. */
  3392. template<typename _Tp>
  3393. constexpr const _Tp&
  3394. clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi)
  3395. {
  3396. __glibcxx_assert(!(__hi < __lo));
  3397. return (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val;
  3398. }
  3399. /**
  3400. * @brief Returns the value clamped between lo and hi.
  3401. * @ingroup sorting_algorithms
  3402. * @param __val A value of arbitrary type.
  3403. * @param __lo A lower limit of arbitrary type.
  3404. * @param __hi An upper limit of arbitrary type.
  3405. * @param __comp A comparison functor.
  3406. * @return max(__val, __lo, __comp) if __comp(__val, __hi)
  3407. * or min(__val, __hi, __comp) otherwise.
  3408. */
  3409. template<typename _Tp, typename _Compare>
  3410. constexpr const _Tp&
  3411. clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
  3412. {
  3413. __glibcxx_assert(!__comp(__hi, __lo));
  3414. return __comp(__val, __lo) ? __lo : __comp(__hi, __val) ? __hi : __val;
  3415. }
  3416. #endif // C++17
  3417. #endif // C++14
  3418. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  3419. /**
  3420. * @brief Generate two uniformly distributed integers using a
  3421. * single distribution invocation.
  3422. * @param __b0 The upper bound for the first integer.
  3423. * @param __b1 The upper bound for the second integer.
  3424. * @param __g A UniformRandomBitGenerator.
  3425. * @return A pair (i, j) with i and j uniformly distributed
  3426. * over [0, __b0) and [0, __b1), respectively.
  3427. *
  3428. * Requires: __b0 * __b1 <= __g.max() - __g.min().
  3429. *
  3430. * Using uniform_int_distribution with a range that is very
  3431. * small relative to the range of the generator ends up wasting
  3432. * potentially expensively generated randomness, since
  3433. * uniform_int_distribution does not store leftover randomness
  3434. * between invocations.
  3435. *
  3436. * If we know we want two integers in ranges that are sufficiently
  3437. * small, we can compose the ranges, use a single distribution
  3438. * invocation, and significantly reduce the waste.
  3439. */
  3440. template<typename _IntType, typename _UniformRandomBitGenerator>
  3441. pair<_IntType, _IntType>
  3442. __gen_two_uniform_ints(_IntType __b0, _IntType __b1,
  3443. _UniformRandomBitGenerator&& __g)
  3444. {
  3445. _IntType __x
  3446. = uniform_int_distribution<_IntType>{0, (__b0 * __b1) - 1}(__g);
  3447. return std::make_pair(__x / __b1, __x % __b1);
  3448. }
  3449. /**
  3450. * @brief Shuffle the elements of a sequence using a uniform random
  3451. * number generator.
  3452. * @ingroup mutating_algorithms
  3453. * @param __first A forward iterator.
  3454. * @param __last A forward iterator.
  3455. * @param __g A UniformRandomNumberGenerator (26.5.1.3).
  3456. * @return Nothing.
  3457. *
  3458. * Reorders the elements in the range @p [__first,__last) using @p __g to
  3459. * provide random numbers.
  3460. */
  3461. template<typename _RandomAccessIterator,
  3462. typename _UniformRandomNumberGenerator>
  3463. void
  3464. shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
  3465. _UniformRandomNumberGenerator&& __g)
  3466. {
  3467. // concept requirements
  3468. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  3469. _RandomAccessIterator>)
  3470. __glibcxx_requires_valid_range(__first, __last);
  3471. if (__first == __last)
  3472. return;
  3473. typedef typename iterator_traits<_RandomAccessIterator>::difference_type
  3474. _DistanceType;
  3475. typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
  3476. typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
  3477. typedef typename __distr_type::param_type __p_type;
  3478. typedef typename remove_reference<_UniformRandomNumberGenerator>::type
  3479. _Gen;
  3480. typedef typename common_type<typename _Gen::result_type, __ud_type>::type
  3481. __uc_type;
  3482. const __uc_type __urngrange = __g.max() - __g.min();
  3483. const __uc_type __urange = __uc_type(__last - __first);
  3484. if (__urngrange / __urange >= __urange)
  3485. // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
  3486. {
  3487. _RandomAccessIterator __i = __first + 1;
  3488. // Since we know the range isn't empty, an even number of elements
  3489. // means an uneven number of elements /to swap/, in which case we
  3490. // do the first one up front:
  3491. if ((__urange % 2) == 0)
  3492. {
  3493. __distr_type __d{0, 1};
  3494. std::iter_swap(__i++, __first + __d(__g));
  3495. }
  3496. // Now we know that __last - __i is even, so we do the rest in pairs,
  3497. // using a single distribution invocation to produce swap positions
  3498. // for two successive elements at a time:
  3499. while (__i != __last)
  3500. {
  3501. const __uc_type __swap_range = __uc_type(__i - __first) + 1;
  3502. const pair<__uc_type, __uc_type> __pospos =
  3503. __gen_two_uniform_ints(__swap_range, __swap_range + 1, __g);
  3504. std::iter_swap(__i++, __first + __pospos.first);
  3505. std::iter_swap(__i++, __first + __pospos.second);
  3506. }
  3507. return;
  3508. }
  3509. __distr_type __d;
  3510. for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
  3511. std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
  3512. }
  3513. #endif
  3514. #endif // C++11
  3515. _GLIBCXX_BEGIN_NAMESPACE_ALGO
  3516. /**
  3517. * @brief Apply a function to every element of a sequence.
  3518. * @ingroup non_mutating_algorithms
  3519. * @param __first An input iterator.
  3520. * @param __last An input iterator.
  3521. * @param __f A unary function object.
  3522. * @return @p __f
  3523. *
  3524. * Applies the function object @p __f to each element in the range
  3525. * @p [first,last). @p __f must not modify the order of the sequence.
  3526. * If @p __f has a return value it is ignored.
  3527. */
  3528. template<typename _InputIterator, typename _Function>
  3529. _GLIBCXX20_CONSTEXPR
  3530. _Function
  3531. for_each(_InputIterator __first, _InputIterator __last, _Function __f)
  3532. {
  3533. // concept requirements
  3534. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  3535. __glibcxx_requires_valid_range(__first, __last);
  3536. for (; __first != __last; ++__first)
  3537. __f(*__first);
  3538. return __f; // N.B. [alg.foreach] says std::move(f) but it's redundant.
  3539. }
  3540. #if __cplusplus >= 201703L
  3541. /**
  3542. * @brief Apply a function to every element of a sequence.
  3543. * @ingroup non_mutating_algorithms
  3544. * @param __first An input iterator.
  3545. * @param __n A value convertible to an integer.
  3546. * @param __f A unary function object.
  3547. * @return `__first+__n`
  3548. *
  3549. * Applies the function object `__f` to each element in the range
  3550. * `[first, first+n)`. `__f` must not modify the order of the sequence.
  3551. * If `__f` has a return value it is ignored.
  3552. */
  3553. template<typename _InputIterator, typename _Size, typename _Function>
  3554. _GLIBCXX20_CONSTEXPR
  3555. _InputIterator
  3556. for_each_n(_InputIterator __first, _Size __n, _Function __f)
  3557. {
  3558. auto __n2 = std::__size_to_integer(__n);
  3559. using _Cat = typename iterator_traits<_InputIterator>::iterator_category;
  3560. if constexpr (is_base_of_v<random_access_iterator_tag, _Cat>)
  3561. {
  3562. if (__n2 <= 0)
  3563. return __first;
  3564. auto __last = __first + __n2;
  3565. std::for_each(__first, __last, std::move(__f));
  3566. return __last;
  3567. }
  3568. else
  3569. {
  3570. while (__n2-->0)
  3571. {
  3572. __f(*__first);
  3573. ++__first;
  3574. }
  3575. return __first;
  3576. }
  3577. }
  3578. #endif // C++17
  3579. /**
  3580. * @brief Find the first occurrence of a value in a sequence.
  3581. * @ingroup non_mutating_algorithms
  3582. * @param __first An input iterator.
  3583. * @param __last An input iterator.
  3584. * @param __val The value to find.
  3585. * @return The first iterator @c i in the range @p [__first,__last)
  3586. * such that @c *i == @p __val, or @p __last if no such iterator exists.
  3587. */
  3588. template<typename _InputIterator, typename _Tp>
  3589. _GLIBCXX20_CONSTEXPR
  3590. inline _InputIterator
  3591. find(_InputIterator __first, _InputIterator __last,
  3592. const _Tp& __val)
  3593. {
  3594. // concept requirements
  3595. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  3596. __glibcxx_function_requires(_EqualOpConcept<
  3597. typename iterator_traits<_InputIterator>::value_type, _Tp>)
  3598. __glibcxx_requires_valid_range(__first, __last);
  3599. return std::__find_if(__first, __last,
  3600. __gnu_cxx::__ops::__iter_equals_val(__val));
  3601. }
  3602. /**
  3603. * @brief Find the first element in a sequence for which a
  3604. * predicate is true.
  3605. * @ingroup non_mutating_algorithms
  3606. * @param __first An input iterator.
  3607. * @param __last An input iterator.
  3608. * @param __pred A predicate.
  3609. * @return The first iterator @c i in the range @p [__first,__last)
  3610. * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
  3611. */
  3612. template<typename _InputIterator, typename _Predicate>
  3613. _GLIBCXX20_CONSTEXPR
  3614. inline _InputIterator
  3615. find_if(_InputIterator __first, _InputIterator __last,
  3616. _Predicate __pred)
  3617. {
  3618. // concept requirements
  3619. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  3620. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  3621. typename iterator_traits<_InputIterator>::value_type>)
  3622. __glibcxx_requires_valid_range(__first, __last);
  3623. return std::__find_if(__first, __last,
  3624. __gnu_cxx::__ops::__pred_iter(__pred));
  3625. }
  3626. /**
  3627. * @brief Find element from a set in a sequence.
  3628. * @ingroup non_mutating_algorithms
  3629. * @param __first1 Start of range to search.
  3630. * @param __last1 End of range to search.
  3631. * @param __first2 Start of match candidates.
  3632. * @param __last2 End of match candidates.
  3633. * @return The first iterator @c i in the range
  3634. * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
  3635. * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
  3636. *
  3637. * Searches the range @p [__first1,__last1) for an element that is
  3638. * equal to some element in the range [__first2,__last2). If
  3639. * found, returns an iterator in the range [__first1,__last1),
  3640. * otherwise returns @p __last1.
  3641. */
  3642. template<typename _InputIterator, typename _ForwardIterator>
  3643. _GLIBCXX20_CONSTEXPR
  3644. _InputIterator
  3645. find_first_of(_InputIterator __first1, _InputIterator __last1,
  3646. _ForwardIterator __first2, _ForwardIterator __last2)
  3647. {
  3648. // concept requirements
  3649. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  3650. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  3651. __glibcxx_function_requires(_EqualOpConcept<
  3652. typename iterator_traits<_InputIterator>::value_type,
  3653. typename iterator_traits<_ForwardIterator>::value_type>)
  3654. __glibcxx_requires_valid_range(__first1, __last1);
  3655. __glibcxx_requires_valid_range(__first2, __last2);
  3656. for (; __first1 != __last1; ++__first1)
  3657. for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
  3658. if (*__first1 == *__iter)
  3659. return __first1;
  3660. return __last1;
  3661. }
  3662. /**
  3663. * @brief Find element from a set in a sequence using a predicate.
  3664. * @ingroup non_mutating_algorithms
  3665. * @param __first1 Start of range to search.
  3666. * @param __last1 End of range to search.
  3667. * @param __first2 Start of match candidates.
  3668. * @param __last2 End of match candidates.
  3669. * @param __comp Predicate to use.
  3670. * @return The first iterator @c i in the range
  3671. * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
  3672. * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
  3673. * such iterator exists.
  3674. *
  3675. * Searches the range @p [__first1,__last1) for an element that is
  3676. * equal to some element in the range [__first2,__last2). If
  3677. * found, returns an iterator in the range [__first1,__last1),
  3678. * otherwise returns @p __last1.
  3679. */
  3680. template<typename _InputIterator, typename _ForwardIterator,
  3681. typename _BinaryPredicate>
  3682. _GLIBCXX20_CONSTEXPR
  3683. _InputIterator
  3684. find_first_of(_InputIterator __first1, _InputIterator __last1,
  3685. _ForwardIterator __first2, _ForwardIterator __last2,
  3686. _BinaryPredicate __comp)
  3687. {
  3688. // concept requirements
  3689. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  3690. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  3691. __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
  3692. typename iterator_traits<_InputIterator>::value_type,
  3693. typename iterator_traits<_ForwardIterator>::value_type>)
  3694. __glibcxx_requires_valid_range(__first1, __last1);
  3695. __glibcxx_requires_valid_range(__first2, __last2);
  3696. for (; __first1 != __last1; ++__first1)
  3697. for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
  3698. if (__comp(*__first1, *__iter))
  3699. return __first1;
  3700. return __last1;
  3701. }
  3702. /**
  3703. * @brief Find two adjacent values in a sequence that are equal.
  3704. * @ingroup non_mutating_algorithms
  3705. * @param __first A forward iterator.
  3706. * @param __last A forward iterator.
  3707. * @return The first iterator @c i such that @c i and @c i+1 are both
  3708. * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
  3709. * or @p __last if no such iterator exists.
  3710. */
  3711. template<typename _ForwardIterator>
  3712. _GLIBCXX20_CONSTEXPR
  3713. inline _ForwardIterator
  3714. adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
  3715. {
  3716. // concept requirements
  3717. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  3718. __glibcxx_function_requires(_EqualityComparableConcept<
  3719. typename iterator_traits<_ForwardIterator>::value_type>)
  3720. __glibcxx_requires_valid_range(__first, __last);
  3721. return std::__adjacent_find(__first, __last,
  3722. __gnu_cxx::__ops::__iter_equal_to_iter());
  3723. }
  3724. /**
  3725. * @brief Find two adjacent values in a sequence using a predicate.
  3726. * @ingroup non_mutating_algorithms
  3727. * @param __first A forward iterator.
  3728. * @param __last A forward iterator.
  3729. * @param __binary_pred A binary predicate.
  3730. * @return The first iterator @c i such that @c i and @c i+1 are both
  3731. * valid iterators in @p [__first,__last) and such that
  3732. * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
  3733. * exists.
  3734. */
  3735. template<typename _ForwardIterator, typename _BinaryPredicate>
  3736. _GLIBCXX20_CONSTEXPR
  3737. inline _ForwardIterator
  3738. adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
  3739. _BinaryPredicate __binary_pred)
  3740. {
  3741. // concept requirements
  3742. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  3743. __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
  3744. typename iterator_traits<_ForwardIterator>::value_type,
  3745. typename iterator_traits<_ForwardIterator>::value_type>)
  3746. __glibcxx_requires_valid_range(__first, __last);
  3747. return std::__adjacent_find(__first, __last,
  3748. __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
  3749. }
  3750. /**
  3751. * @brief Count the number of copies of a value in a sequence.
  3752. * @ingroup non_mutating_algorithms
  3753. * @param __first An input iterator.
  3754. * @param __last An input iterator.
  3755. * @param __value The value to be counted.
  3756. * @return The number of iterators @c i in the range @p [__first,__last)
  3757. * for which @c *i == @p __value
  3758. */
  3759. template<typename _InputIterator, typename _Tp>
  3760. _GLIBCXX20_CONSTEXPR
  3761. inline typename iterator_traits<_InputIterator>::difference_type
  3762. count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
  3763. {
  3764. // concept requirements
  3765. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  3766. __glibcxx_function_requires(_EqualOpConcept<
  3767. typename iterator_traits<_InputIterator>::value_type, _Tp>)
  3768. __glibcxx_requires_valid_range(__first, __last);
  3769. return std::__count_if(__first, __last,
  3770. __gnu_cxx::__ops::__iter_equals_val(__value));
  3771. }
  3772. /**
  3773. * @brief Count the elements of a sequence for which a predicate is true.
  3774. * @ingroup non_mutating_algorithms
  3775. * @param __first An input iterator.
  3776. * @param __last An input iterator.
  3777. * @param __pred A predicate.
  3778. * @return The number of iterators @c i in the range @p [__first,__last)
  3779. * for which @p __pred(*i) is true.
  3780. */
  3781. template<typename _InputIterator, typename _Predicate>
  3782. _GLIBCXX20_CONSTEXPR
  3783. inline typename iterator_traits<_InputIterator>::difference_type
  3784. count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  3785. {
  3786. // concept requirements
  3787. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  3788. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  3789. typename iterator_traits<_InputIterator>::value_type>)
  3790. __glibcxx_requires_valid_range(__first, __last);
  3791. return std::__count_if(__first, __last,
  3792. __gnu_cxx::__ops::__pred_iter(__pred));
  3793. }
  3794. /**
  3795. * @brief Search a sequence for a matching sub-sequence.
  3796. * @ingroup non_mutating_algorithms
  3797. * @param __first1 A forward iterator.
  3798. * @param __last1 A forward iterator.
  3799. * @param __first2 A forward iterator.
  3800. * @param __last2 A forward iterator.
  3801. * @return The first iterator @c i in the range @p
  3802. * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
  3803. * *(__first2+N) for each @c N in the range @p
  3804. * [0,__last2-__first2), or @p __last1 if no such iterator exists.
  3805. *
  3806. * Searches the range @p [__first1,__last1) for a sub-sequence that
  3807. * compares equal value-by-value with the sequence given by @p
  3808. * [__first2,__last2) and returns an iterator to the first element
  3809. * of the sub-sequence, or @p __last1 if the sub-sequence is not
  3810. * found.
  3811. *
  3812. * Because the sub-sequence must lie completely within the range @p
  3813. * [__first1,__last1) it must start at a position less than @p
  3814. * __last1-(__last2-__first2) where @p __last2-__first2 is the
  3815. * length of the sub-sequence.
  3816. *
  3817. * This means that the returned iterator @c i will be in the range
  3818. * @p [__first1,__last1-(__last2-__first2))
  3819. */
  3820. template<typename _ForwardIterator1, typename _ForwardIterator2>
  3821. _GLIBCXX20_CONSTEXPR
  3822. inline _ForwardIterator1
  3823. search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  3824. _ForwardIterator2 __first2, _ForwardIterator2 __last2)
  3825. {
  3826. // concept requirements
  3827. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
  3828. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
  3829. __glibcxx_function_requires(_EqualOpConcept<
  3830. typename iterator_traits<_ForwardIterator1>::value_type,
  3831. typename iterator_traits<_ForwardIterator2>::value_type>)
  3832. __glibcxx_requires_valid_range(__first1, __last1);
  3833. __glibcxx_requires_valid_range(__first2, __last2);
  3834. return std::__search(__first1, __last1, __first2, __last2,
  3835. __gnu_cxx::__ops::__iter_equal_to_iter());
  3836. }
  3837. /**
  3838. * @brief Search a sequence for a matching sub-sequence using a predicate.
  3839. * @ingroup non_mutating_algorithms
  3840. * @param __first1 A forward iterator.
  3841. * @param __last1 A forward iterator.
  3842. * @param __first2 A forward iterator.
  3843. * @param __last2 A forward iterator.
  3844. * @param __predicate A binary predicate.
  3845. * @return The first iterator @c i in the range
  3846. * @p [__first1,__last1-(__last2-__first2)) such that
  3847. * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
  3848. * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
  3849. *
  3850. * Searches the range @p [__first1,__last1) for a sub-sequence that
  3851. * compares equal value-by-value with the sequence given by @p
  3852. * [__first2,__last2), using @p __predicate to determine equality,
  3853. * and returns an iterator to the first element of the
  3854. * sub-sequence, or @p __last1 if no such iterator exists.
  3855. *
  3856. * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
  3857. */
  3858. template<typename _ForwardIterator1, typename _ForwardIterator2,
  3859. typename _BinaryPredicate>
  3860. _GLIBCXX20_CONSTEXPR
  3861. inline _ForwardIterator1
  3862. search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  3863. _ForwardIterator2 __first2, _ForwardIterator2 __last2,
  3864. _BinaryPredicate __predicate)
  3865. {
  3866. // concept requirements
  3867. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
  3868. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
  3869. __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
  3870. typename iterator_traits<_ForwardIterator1>::value_type,
  3871. typename iterator_traits<_ForwardIterator2>::value_type>)
  3872. __glibcxx_requires_valid_range(__first1, __last1);
  3873. __glibcxx_requires_valid_range(__first2, __last2);
  3874. return std::__search(__first1, __last1, __first2, __last2,
  3875. __gnu_cxx::__ops::__iter_comp_iter(__predicate));
  3876. }
  3877. /**
  3878. * @brief Search a sequence for a number of consecutive values.
  3879. * @ingroup non_mutating_algorithms
  3880. * @param __first A forward iterator.
  3881. * @param __last A forward iterator.
  3882. * @param __count The number of consecutive values.
  3883. * @param __val The value to find.
  3884. * @return The first iterator @c i in the range @p
  3885. * [__first,__last-__count) such that @c *(i+N) == @p __val for
  3886. * each @c N in the range @p [0,__count), or @p __last if no such
  3887. * iterator exists.
  3888. *
  3889. * Searches the range @p [__first,__last) for @p count consecutive elements
  3890. * equal to @p __val.
  3891. */
  3892. template<typename _ForwardIterator, typename _Integer, typename _Tp>
  3893. _GLIBCXX20_CONSTEXPR
  3894. inline _ForwardIterator
  3895. search_n(_ForwardIterator __first, _ForwardIterator __last,
  3896. _Integer __count, const _Tp& __val)
  3897. {
  3898. // concept requirements
  3899. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  3900. __glibcxx_function_requires(_EqualOpConcept<
  3901. typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
  3902. __glibcxx_requires_valid_range(__first, __last);
  3903. return std::__search_n(__first, __last, __count,
  3904. __gnu_cxx::__ops::__iter_equals_val(__val));
  3905. }
  3906. /**
  3907. * @brief Search a sequence for a number of consecutive values using a
  3908. * predicate.
  3909. * @ingroup non_mutating_algorithms
  3910. * @param __first A forward iterator.
  3911. * @param __last A forward iterator.
  3912. * @param __count The number of consecutive values.
  3913. * @param __val The value to find.
  3914. * @param __binary_pred A binary predicate.
  3915. * @return The first iterator @c i in the range @p
  3916. * [__first,__last-__count) such that @p
  3917. * __binary_pred(*(i+N),__val) is true for each @c N in the range
  3918. * @p [0,__count), or @p __last if no such iterator exists.
  3919. *
  3920. * Searches the range @p [__first,__last) for @p __count
  3921. * consecutive elements for which the predicate returns true.
  3922. */
  3923. template<typename _ForwardIterator, typename _Integer, typename _Tp,
  3924. typename _BinaryPredicate>
  3925. _GLIBCXX20_CONSTEXPR
  3926. inline _ForwardIterator
  3927. search_n(_ForwardIterator __first, _ForwardIterator __last,
  3928. _Integer __count, const _Tp& __val,
  3929. _BinaryPredicate __binary_pred)
  3930. {
  3931. // concept requirements
  3932. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  3933. __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
  3934. typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
  3935. __glibcxx_requires_valid_range(__first, __last);
  3936. return std::__search_n(__first, __last, __count,
  3937. __gnu_cxx::__ops::__iter_comp_val(__binary_pred, __val));
  3938. }
  3939. #if __cplusplus > 201402L
  3940. /** @brief Search a sequence using a Searcher object.
  3941. *
  3942. * @param __first A forward iterator.
  3943. * @param __last A forward iterator.
  3944. * @param __searcher A callable object.
  3945. * @return @p __searcher(__first,__last).first
  3946. */
  3947. template<typename _ForwardIterator, typename _Searcher>
  3948. _GLIBCXX20_CONSTEXPR
  3949. inline _ForwardIterator
  3950. search(_ForwardIterator __first, _ForwardIterator __last,
  3951. const _Searcher& __searcher)
  3952. { return __searcher(__first, __last).first; }
  3953. #endif
  3954. /**
  3955. * @brief Perform an operation on a sequence.
  3956. * @ingroup mutating_algorithms
  3957. * @param __first An input iterator.
  3958. * @param __last An input iterator.
  3959. * @param __result An output iterator.
  3960. * @param __unary_op A unary operator.
  3961. * @return An output iterator equal to @p __result+(__last-__first).
  3962. *
  3963. * Applies the operator to each element in the input range and assigns
  3964. * the results to successive elements of the output sequence.
  3965. * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
  3966. * range @p [0,__last-__first).
  3967. *
  3968. * @p unary_op must not alter its argument.
  3969. */
  3970. template<typename _InputIterator, typename _OutputIterator,
  3971. typename _UnaryOperation>
  3972. _GLIBCXX20_CONSTEXPR
  3973. _OutputIterator
  3974. transform(_InputIterator __first, _InputIterator __last,
  3975. _OutputIterator __result, _UnaryOperation __unary_op)
  3976. {
  3977. // concept requirements
  3978. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  3979. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  3980. // "the type returned by a _UnaryOperation"
  3981. __typeof__(__unary_op(*__first))>)
  3982. __glibcxx_requires_valid_range(__first, __last);
  3983. for (; __first != __last; ++__first, (void)++__result)
  3984. *__result = __unary_op(*__first);
  3985. return __result;
  3986. }
  3987. /**
  3988. * @brief Perform an operation on corresponding elements of two sequences.
  3989. * @ingroup mutating_algorithms
  3990. * @param __first1 An input iterator.
  3991. * @param __last1 An input iterator.
  3992. * @param __first2 An input iterator.
  3993. * @param __result An output iterator.
  3994. * @param __binary_op A binary operator.
  3995. * @return An output iterator equal to @p result+(last-first).
  3996. *
  3997. * Applies the operator to the corresponding elements in the two
  3998. * input ranges and assigns the results to successive elements of the
  3999. * output sequence.
  4000. * Evaluates @p
  4001. * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
  4002. * @c N in the range @p [0,__last1-__first1).
  4003. *
  4004. * @p binary_op must not alter either of its arguments.
  4005. */
  4006. template<typename _InputIterator1, typename _InputIterator2,
  4007. typename _OutputIterator, typename _BinaryOperation>
  4008. _GLIBCXX20_CONSTEXPR
  4009. _OutputIterator
  4010. transform(_InputIterator1 __first1, _InputIterator1 __last1,
  4011. _InputIterator2 __first2, _OutputIterator __result,
  4012. _BinaryOperation __binary_op)
  4013. {
  4014. // concept requirements
  4015. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  4016. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  4017. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4018. // "the type returned by a _BinaryOperation"
  4019. __typeof__(__binary_op(*__first1,*__first2))>)
  4020. __glibcxx_requires_valid_range(__first1, __last1);
  4021. for (; __first1 != __last1; ++__first1, (void)++__first2, ++__result)
  4022. *__result = __binary_op(*__first1, *__first2);
  4023. return __result;
  4024. }
  4025. /**
  4026. * @brief Replace each occurrence of one value in a sequence with another
  4027. * value.
  4028. * @ingroup mutating_algorithms
  4029. * @param __first A forward iterator.
  4030. * @param __last A forward iterator.
  4031. * @param __old_value The value to be replaced.
  4032. * @param __new_value The replacement value.
  4033. * @return replace() returns no value.
  4034. *
  4035. * For each iterator @c i in the range @p [__first,__last) if @c *i ==
  4036. * @p __old_value then the assignment @c *i = @p __new_value is performed.
  4037. */
  4038. template<typename _ForwardIterator, typename _Tp>
  4039. _GLIBCXX20_CONSTEXPR
  4040. void
  4041. replace(_ForwardIterator __first, _ForwardIterator __last,
  4042. const _Tp& __old_value, const _Tp& __new_value)
  4043. {
  4044. // concept requirements
  4045. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  4046. _ForwardIterator>)
  4047. __glibcxx_function_requires(_EqualOpConcept<
  4048. typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
  4049. __glibcxx_function_requires(_ConvertibleConcept<_Tp,
  4050. typename iterator_traits<_ForwardIterator>::value_type>)
  4051. __glibcxx_requires_valid_range(__first, __last);
  4052. for (; __first != __last; ++__first)
  4053. if (*__first == __old_value)
  4054. *__first = __new_value;
  4055. }
  4056. /**
  4057. * @brief Replace each value in a sequence for which a predicate returns
  4058. * true with another value.
  4059. * @ingroup mutating_algorithms
  4060. * @param __first A forward iterator.
  4061. * @param __last A forward iterator.
  4062. * @param __pred A predicate.
  4063. * @param __new_value The replacement value.
  4064. * @return replace_if() returns no value.
  4065. *
  4066. * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
  4067. * is true then the assignment @c *i = @p __new_value is performed.
  4068. */
  4069. template<typename _ForwardIterator, typename _Predicate, typename _Tp>
  4070. _GLIBCXX20_CONSTEXPR
  4071. void
  4072. replace_if(_ForwardIterator __first, _ForwardIterator __last,
  4073. _Predicate __pred, const _Tp& __new_value)
  4074. {
  4075. // concept requirements
  4076. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  4077. _ForwardIterator>)
  4078. __glibcxx_function_requires(_ConvertibleConcept<_Tp,
  4079. typename iterator_traits<_ForwardIterator>::value_type>)
  4080. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  4081. typename iterator_traits<_ForwardIterator>::value_type>)
  4082. __glibcxx_requires_valid_range(__first, __last);
  4083. for (; __first != __last; ++__first)
  4084. if (__pred(*__first))
  4085. *__first = __new_value;
  4086. }
  4087. /**
  4088. * @brief Assign the result of a function object to each value in a
  4089. * sequence.
  4090. * @ingroup mutating_algorithms
  4091. * @param __first A forward iterator.
  4092. * @param __last A forward iterator.
  4093. * @param __gen A function object taking no arguments and returning
  4094. * std::iterator_traits<_ForwardIterator>::value_type
  4095. * @return generate() returns no value.
  4096. *
  4097. * Performs the assignment @c *i = @p __gen() for each @c i in the range
  4098. * @p [__first,__last).
  4099. */
  4100. template<typename _ForwardIterator, typename _Generator>
  4101. _GLIBCXX20_CONSTEXPR
  4102. void
  4103. generate(_ForwardIterator __first, _ForwardIterator __last,
  4104. _Generator __gen)
  4105. {
  4106. // concept requirements
  4107. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  4108. __glibcxx_function_requires(_GeneratorConcept<_Generator,
  4109. typename iterator_traits<_ForwardIterator>::value_type>)
  4110. __glibcxx_requires_valid_range(__first, __last);
  4111. for (; __first != __last; ++__first)
  4112. *__first = __gen();
  4113. }
  4114. /**
  4115. * @brief Assign the result of a function object to each value in a
  4116. * sequence.
  4117. * @ingroup mutating_algorithms
  4118. * @param __first A forward iterator.
  4119. * @param __n The length of the sequence.
  4120. * @param __gen A function object taking no arguments and returning
  4121. * std::iterator_traits<_ForwardIterator>::value_type
  4122. * @return The end of the sequence, @p __first+__n
  4123. *
  4124. * Performs the assignment @c *i = @p __gen() for each @c i in the range
  4125. * @p [__first,__first+__n).
  4126. *
  4127. * If @p __n is negative, the function does nothing and returns @p __first.
  4128. */
  4129. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  4130. // DR 865. More algorithms that throw away information
  4131. // DR 426. search_n(), fill_n(), and generate_n() with negative n
  4132. template<typename _OutputIterator, typename _Size, typename _Generator>
  4133. _GLIBCXX20_CONSTEXPR
  4134. _OutputIterator
  4135. generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
  4136. {
  4137. // concept requirements
  4138. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4139. // "the type returned by a _Generator"
  4140. __typeof__(__gen())>)
  4141. typedef __decltype(std::__size_to_integer(__n)) _IntSize;
  4142. for (_IntSize __niter = std::__size_to_integer(__n);
  4143. __niter > 0; --__niter, (void) ++__first)
  4144. *__first = __gen();
  4145. return __first;
  4146. }
  4147. /**
  4148. * @brief Copy a sequence, removing consecutive duplicate values.
  4149. * @ingroup mutating_algorithms
  4150. * @param __first An input iterator.
  4151. * @param __last An input iterator.
  4152. * @param __result An output iterator.
  4153. * @return An iterator designating the end of the resulting sequence.
  4154. *
  4155. * Copies each element in the range @p [__first,__last) to the range
  4156. * beginning at @p __result, except that only the first element is copied
  4157. * from groups of consecutive elements that compare equal.
  4158. * unique_copy() is stable, so the relative order of elements that are
  4159. * copied is unchanged.
  4160. *
  4161. * _GLIBCXX_RESOLVE_LIB_DEFECTS
  4162. * DR 241. Does unique_copy() require CopyConstructible and Assignable?
  4163. *
  4164. * _GLIBCXX_RESOLVE_LIB_DEFECTS
  4165. * DR 538. 241 again: Does unique_copy() require CopyConstructible and
  4166. * Assignable?
  4167. */
  4168. template<typename _InputIterator, typename _OutputIterator>
  4169. _GLIBCXX20_CONSTEXPR
  4170. inline _OutputIterator
  4171. unique_copy(_InputIterator __first, _InputIterator __last,
  4172. _OutputIterator __result)
  4173. {
  4174. // concept requirements
  4175. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  4176. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4177. typename iterator_traits<_InputIterator>::value_type>)
  4178. __glibcxx_function_requires(_EqualityComparableConcept<
  4179. typename iterator_traits<_InputIterator>::value_type>)
  4180. __glibcxx_requires_valid_range(__first, __last);
  4181. if (__first == __last)
  4182. return __result;
  4183. return std::__unique_copy(__first, __last, __result,
  4184. __gnu_cxx::__ops::__iter_equal_to_iter(),
  4185. std::__iterator_category(__first),
  4186. std::__iterator_category(__result));
  4187. }
  4188. /**
  4189. * @brief Copy a sequence, removing consecutive values using a predicate.
  4190. * @ingroup mutating_algorithms
  4191. * @param __first An input iterator.
  4192. * @param __last An input iterator.
  4193. * @param __result An output iterator.
  4194. * @param __binary_pred A binary predicate.
  4195. * @return An iterator designating the end of the resulting sequence.
  4196. *
  4197. * Copies each element in the range @p [__first,__last) to the range
  4198. * beginning at @p __result, except that only the first element is copied
  4199. * from groups of consecutive elements for which @p __binary_pred returns
  4200. * true.
  4201. * unique_copy() is stable, so the relative order of elements that are
  4202. * copied is unchanged.
  4203. *
  4204. * _GLIBCXX_RESOLVE_LIB_DEFECTS
  4205. * DR 241. Does unique_copy() require CopyConstructible and Assignable?
  4206. */
  4207. template<typename _InputIterator, typename _OutputIterator,
  4208. typename _BinaryPredicate>
  4209. _GLIBCXX20_CONSTEXPR
  4210. inline _OutputIterator
  4211. unique_copy(_InputIterator __first, _InputIterator __last,
  4212. _OutputIterator __result,
  4213. _BinaryPredicate __binary_pred)
  4214. {
  4215. // concept requirements -- predicates checked later
  4216. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  4217. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4218. typename iterator_traits<_InputIterator>::value_type>)
  4219. __glibcxx_requires_valid_range(__first, __last);
  4220. if (__first == __last)
  4221. return __result;
  4222. return std::__unique_copy(__first, __last, __result,
  4223. __gnu_cxx::__ops::__iter_comp_iter(__binary_pred),
  4224. std::__iterator_category(__first),
  4225. std::__iterator_category(__result));
  4226. }
  4227. #if _GLIBCXX_HOSTED
  4228. /**
  4229. * @brief Randomly shuffle the elements of a sequence.
  4230. * @ingroup mutating_algorithms
  4231. * @param __first A forward iterator.
  4232. * @param __last A forward iterator.
  4233. * @return Nothing.
  4234. *
  4235. * Reorder the elements in the range @p [__first,__last) using a random
  4236. * distribution, so that every possible ordering of the sequence is
  4237. * equally likely.
  4238. */
  4239. template<typename _RandomAccessIterator>
  4240. inline void
  4241. random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
  4242. {
  4243. // concept requirements
  4244. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  4245. _RandomAccessIterator>)
  4246. __glibcxx_requires_valid_range(__first, __last);
  4247. if (__first != __last)
  4248. for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
  4249. {
  4250. // XXX rand() % N is not uniformly distributed
  4251. _RandomAccessIterator __j = __first
  4252. + std::rand() % ((__i - __first) + 1);
  4253. if (__i != __j)
  4254. std::iter_swap(__i, __j);
  4255. }
  4256. }
  4257. #endif
  4258. /**
  4259. * @brief Shuffle the elements of a sequence using a random number
  4260. * generator.
  4261. * @ingroup mutating_algorithms
  4262. * @param __first A forward iterator.
  4263. * @param __last A forward iterator.
  4264. * @param __rand The RNG functor or function.
  4265. * @return Nothing.
  4266. *
  4267. * Reorders the elements in the range @p [__first,__last) using @p __rand to
  4268. * provide a random distribution. Calling @p __rand(N) for a positive
  4269. * integer @p N should return a randomly chosen integer from the
  4270. * range [0,N).
  4271. */
  4272. template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
  4273. void
  4274. random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
  4275. #if __cplusplus >= 201103L
  4276. _RandomNumberGenerator&& __rand)
  4277. #else
  4278. _RandomNumberGenerator& __rand)
  4279. #endif
  4280. {
  4281. // concept requirements
  4282. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  4283. _RandomAccessIterator>)
  4284. __glibcxx_requires_valid_range(__first, __last);
  4285. if (__first == __last)
  4286. return;
  4287. for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
  4288. {
  4289. _RandomAccessIterator __j = __first + __rand((__i - __first) + 1);
  4290. if (__i != __j)
  4291. std::iter_swap(__i, __j);
  4292. }
  4293. }
  4294. /**
  4295. * @brief Move elements for which a predicate is true to the beginning
  4296. * of a sequence.
  4297. * @ingroup mutating_algorithms
  4298. * @param __first A forward iterator.
  4299. * @param __last A forward iterator.
  4300. * @param __pred A predicate functor.
  4301. * @return An iterator @p middle such that @p __pred(i) is true for each
  4302. * iterator @p i in the range @p [__first,middle) and false for each @p i
  4303. * in the range @p [middle,__last).
  4304. *
  4305. * @p __pred must not modify its operand. @p partition() does not preserve
  4306. * the relative ordering of elements in each group, use
  4307. * @p stable_partition() if this is needed.
  4308. */
  4309. template<typename _ForwardIterator, typename _Predicate>
  4310. _GLIBCXX20_CONSTEXPR
  4311. inline _ForwardIterator
  4312. partition(_ForwardIterator __first, _ForwardIterator __last,
  4313. _Predicate __pred)
  4314. {
  4315. // concept requirements
  4316. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  4317. _ForwardIterator>)
  4318. __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
  4319. typename iterator_traits<_ForwardIterator>::value_type>)
  4320. __glibcxx_requires_valid_range(__first, __last);
  4321. return std::__partition(__first, __last, __pred,
  4322. std::__iterator_category(__first));
  4323. }
  4324. /**
  4325. * @brief Sort the smallest elements of a sequence.
  4326. * @ingroup sorting_algorithms
  4327. * @param __first An iterator.
  4328. * @param __middle Another iterator.
  4329. * @param __last Another iterator.
  4330. * @return Nothing.
  4331. *
  4332. * Sorts the smallest @p (__middle-__first) elements in the range
  4333. * @p [first,last) and moves them to the range @p [__first,__middle). The
  4334. * order of the remaining elements in the range @p [__middle,__last) is
  4335. * undefined.
  4336. * After the sort if @e i and @e j are iterators in the range
  4337. * @p [__first,__middle) such that i precedes j and @e k is an iterator in
  4338. * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
  4339. */
  4340. template<typename _RandomAccessIterator>
  4341. _GLIBCXX20_CONSTEXPR
  4342. inline void
  4343. partial_sort(_RandomAccessIterator __first,
  4344. _RandomAccessIterator __middle,
  4345. _RandomAccessIterator __last)
  4346. {
  4347. // concept requirements
  4348. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  4349. _RandomAccessIterator>)
  4350. __glibcxx_function_requires(_LessThanComparableConcept<
  4351. typename iterator_traits<_RandomAccessIterator>::value_type>)
  4352. __glibcxx_requires_valid_range(__first, __middle);
  4353. __glibcxx_requires_valid_range(__middle, __last);
  4354. __glibcxx_requires_irreflexive(__first, __last);
  4355. std::__partial_sort(__first, __middle, __last,
  4356. __gnu_cxx::__ops::__iter_less_iter());
  4357. }
  4358. /**
  4359. * @brief Sort the smallest elements of a sequence using a predicate
  4360. * for comparison.
  4361. * @ingroup sorting_algorithms
  4362. * @param __first An iterator.
  4363. * @param __middle Another iterator.
  4364. * @param __last Another iterator.
  4365. * @param __comp A comparison functor.
  4366. * @return Nothing.
  4367. *
  4368. * Sorts the smallest @p (__middle-__first) elements in the range
  4369. * @p [__first,__last) and moves them to the range @p [__first,__middle). The
  4370. * order of the remaining elements in the range @p [__middle,__last) is
  4371. * undefined.
  4372. * After the sort if @e i and @e j are iterators in the range
  4373. * @p [__first,__middle) such that i precedes j and @e k is an iterator in
  4374. * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
  4375. * are both false.
  4376. */
  4377. template<typename _RandomAccessIterator, typename _Compare>
  4378. _GLIBCXX20_CONSTEXPR
  4379. inline void
  4380. partial_sort(_RandomAccessIterator __first,
  4381. _RandomAccessIterator __middle,
  4382. _RandomAccessIterator __last,
  4383. _Compare __comp)
  4384. {
  4385. // concept requirements
  4386. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  4387. _RandomAccessIterator>)
  4388. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  4389. typename iterator_traits<_RandomAccessIterator>::value_type,
  4390. typename iterator_traits<_RandomAccessIterator>::value_type>)
  4391. __glibcxx_requires_valid_range(__first, __middle);
  4392. __glibcxx_requires_valid_range(__middle, __last);
  4393. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  4394. std::__partial_sort(__first, __middle, __last,
  4395. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  4396. }
  4397. /**
  4398. * @brief Sort a sequence just enough to find a particular position.
  4399. * @ingroup sorting_algorithms
  4400. * @param __first An iterator.
  4401. * @param __nth Another iterator.
  4402. * @param __last Another iterator.
  4403. * @return Nothing.
  4404. *
  4405. * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
  4406. * is the same element that would have been in that position had the
  4407. * whole sequence been sorted. The elements either side of @p *__nth are
  4408. * not completely sorted, but for any iterator @e i in the range
  4409. * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
  4410. * holds that *j < *i is false.
  4411. */
  4412. template<typename _RandomAccessIterator>
  4413. _GLIBCXX20_CONSTEXPR
  4414. inline void
  4415. nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
  4416. _RandomAccessIterator __last)
  4417. {
  4418. // concept requirements
  4419. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  4420. _RandomAccessIterator>)
  4421. __glibcxx_function_requires(_LessThanComparableConcept<
  4422. typename iterator_traits<_RandomAccessIterator>::value_type>)
  4423. __glibcxx_requires_valid_range(__first, __nth);
  4424. __glibcxx_requires_valid_range(__nth, __last);
  4425. __glibcxx_requires_irreflexive(__first, __last);
  4426. if (__first == __last || __nth == __last)
  4427. return;
  4428. std::__introselect(__first, __nth, __last,
  4429. std::__lg(__last - __first) * 2,
  4430. __gnu_cxx::__ops::__iter_less_iter());
  4431. }
  4432. /**
  4433. * @brief Sort a sequence just enough to find a particular position
  4434. * using a predicate for comparison.
  4435. * @ingroup sorting_algorithms
  4436. * @param __first An iterator.
  4437. * @param __nth Another iterator.
  4438. * @param __last Another iterator.
  4439. * @param __comp A comparison functor.
  4440. * @return Nothing.
  4441. *
  4442. * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
  4443. * is the same element that would have been in that position had the
  4444. * whole sequence been sorted. The elements either side of @p *__nth are
  4445. * not completely sorted, but for any iterator @e i in the range
  4446. * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
  4447. * holds that @p __comp(*j,*i) is false.
  4448. */
  4449. template<typename _RandomAccessIterator, typename _Compare>
  4450. _GLIBCXX20_CONSTEXPR
  4451. inline void
  4452. nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
  4453. _RandomAccessIterator __last, _Compare __comp)
  4454. {
  4455. // concept requirements
  4456. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  4457. _RandomAccessIterator>)
  4458. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  4459. typename iterator_traits<_RandomAccessIterator>::value_type,
  4460. typename iterator_traits<_RandomAccessIterator>::value_type>)
  4461. __glibcxx_requires_valid_range(__first, __nth);
  4462. __glibcxx_requires_valid_range(__nth, __last);
  4463. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  4464. if (__first == __last || __nth == __last)
  4465. return;
  4466. std::__introselect(__first, __nth, __last,
  4467. std::__lg(__last - __first) * 2,
  4468. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  4469. }
  4470. /**
  4471. * @brief Sort the elements of a sequence.
  4472. * @ingroup sorting_algorithms
  4473. * @param __first An iterator.
  4474. * @param __last Another iterator.
  4475. * @return Nothing.
  4476. *
  4477. * Sorts the elements in the range @p [__first,__last) in ascending order,
  4478. * such that for each iterator @e i in the range @p [__first,__last-1),
  4479. * *(i+1)<*i is false.
  4480. *
  4481. * The relative ordering of equivalent elements is not preserved, use
  4482. * @p stable_sort() if this is needed.
  4483. */
  4484. template<typename _RandomAccessIterator>
  4485. _GLIBCXX20_CONSTEXPR
  4486. inline void
  4487. sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
  4488. {
  4489. // concept requirements
  4490. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  4491. _RandomAccessIterator>)
  4492. __glibcxx_function_requires(_LessThanComparableConcept<
  4493. typename iterator_traits<_RandomAccessIterator>::value_type>)
  4494. __glibcxx_requires_valid_range(__first, __last);
  4495. __glibcxx_requires_irreflexive(__first, __last);
  4496. std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter());
  4497. }
  4498. /**
  4499. * @brief Sort the elements of a sequence using a predicate for comparison.
  4500. * @ingroup sorting_algorithms
  4501. * @param __first An iterator.
  4502. * @param __last Another iterator.
  4503. * @param __comp A comparison functor.
  4504. * @return Nothing.
  4505. *
  4506. * Sorts the elements in the range @p [__first,__last) in ascending order,
  4507. * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
  4508. * range @p [__first,__last-1).
  4509. *
  4510. * The relative ordering of equivalent elements is not preserved, use
  4511. * @p stable_sort() if this is needed.
  4512. */
  4513. template<typename _RandomAccessIterator, typename _Compare>
  4514. _GLIBCXX20_CONSTEXPR
  4515. inline void
  4516. sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
  4517. _Compare __comp)
  4518. {
  4519. // concept requirements
  4520. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  4521. _RandomAccessIterator>)
  4522. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  4523. typename iterator_traits<_RandomAccessIterator>::value_type,
  4524. typename iterator_traits<_RandomAccessIterator>::value_type>)
  4525. __glibcxx_requires_valid_range(__first, __last);
  4526. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  4527. std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
  4528. }
  4529. template<typename _InputIterator1, typename _InputIterator2,
  4530. typename _OutputIterator, typename _Compare>
  4531. _GLIBCXX20_CONSTEXPR
  4532. _OutputIterator
  4533. __merge(_InputIterator1 __first1, _InputIterator1 __last1,
  4534. _InputIterator2 __first2, _InputIterator2 __last2,
  4535. _OutputIterator __result, _Compare __comp)
  4536. {
  4537. while (__first1 != __last1 && __first2 != __last2)
  4538. {
  4539. if (__comp(__first2, __first1))
  4540. {
  4541. *__result = *__first2;
  4542. ++__first2;
  4543. }
  4544. else
  4545. {
  4546. *__result = *__first1;
  4547. ++__first1;
  4548. }
  4549. ++__result;
  4550. }
  4551. return std::copy(__first2, __last2,
  4552. std::copy(__first1, __last1, __result));
  4553. }
  4554. /**
  4555. * @brief Merges two sorted ranges.
  4556. * @ingroup sorting_algorithms
  4557. * @param __first1 An iterator.
  4558. * @param __first2 Another iterator.
  4559. * @param __last1 Another iterator.
  4560. * @param __last2 Another iterator.
  4561. * @param __result An iterator pointing to the end of the merged range.
  4562. * @return An output iterator equal to @p __result + (__last1 - __first1)
  4563. * + (__last2 - __first2).
  4564. *
  4565. * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
  4566. * the sorted range @p [__result, __result + (__last1-__first1) +
  4567. * (__last2-__first2)). Both input ranges must be sorted, and the
  4568. * output range must not overlap with either of the input ranges.
  4569. * The sort is @e stable, that is, for equivalent elements in the
  4570. * two ranges, elements from the first range will always come
  4571. * before elements from the second.
  4572. */
  4573. template<typename _InputIterator1, typename _InputIterator2,
  4574. typename _OutputIterator>
  4575. _GLIBCXX20_CONSTEXPR
  4576. inline _OutputIterator
  4577. merge(_InputIterator1 __first1, _InputIterator1 __last1,
  4578. _InputIterator2 __first2, _InputIterator2 __last2,
  4579. _OutputIterator __result)
  4580. {
  4581. // concept requirements
  4582. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  4583. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  4584. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4585. typename iterator_traits<_InputIterator1>::value_type>)
  4586. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4587. typename iterator_traits<_InputIterator2>::value_type>)
  4588. __glibcxx_function_requires(_LessThanOpConcept<
  4589. typename iterator_traits<_InputIterator2>::value_type,
  4590. typename iterator_traits<_InputIterator1>::value_type>)
  4591. __glibcxx_requires_sorted_set(__first1, __last1, __first2);
  4592. __glibcxx_requires_sorted_set(__first2, __last2, __first1);
  4593. __glibcxx_requires_irreflexive2(__first1, __last1);
  4594. __glibcxx_requires_irreflexive2(__first2, __last2);
  4595. return _GLIBCXX_STD_A::__merge(__first1, __last1,
  4596. __first2, __last2, __result,
  4597. __gnu_cxx::__ops::__iter_less_iter());
  4598. }
  4599. /**
  4600. * @brief Merges two sorted ranges.
  4601. * @ingroup sorting_algorithms
  4602. * @param __first1 An iterator.
  4603. * @param __first2 Another iterator.
  4604. * @param __last1 Another iterator.
  4605. * @param __last2 Another iterator.
  4606. * @param __result An iterator pointing to the end of the merged range.
  4607. * @param __comp A functor to use for comparisons.
  4608. * @return An output iterator equal to @p __result + (__last1 - __first1)
  4609. * + (__last2 - __first2).
  4610. *
  4611. * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
  4612. * the sorted range @p [__result, __result + (__last1-__first1) +
  4613. * (__last2-__first2)). Both input ranges must be sorted, and the
  4614. * output range must not overlap with either of the input ranges.
  4615. * The sort is @e stable, that is, for equivalent elements in the
  4616. * two ranges, elements from the first range will always come
  4617. * before elements from the second.
  4618. *
  4619. * The comparison function should have the same effects on ordering as
  4620. * the function used for the initial sort.
  4621. */
  4622. template<typename _InputIterator1, typename _InputIterator2,
  4623. typename _OutputIterator, typename _Compare>
  4624. _GLIBCXX20_CONSTEXPR
  4625. inline _OutputIterator
  4626. merge(_InputIterator1 __first1, _InputIterator1 __last1,
  4627. _InputIterator2 __first2, _InputIterator2 __last2,
  4628. _OutputIterator __result, _Compare __comp)
  4629. {
  4630. // concept requirements
  4631. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  4632. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  4633. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4634. typename iterator_traits<_InputIterator1>::value_type>)
  4635. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4636. typename iterator_traits<_InputIterator2>::value_type>)
  4637. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  4638. typename iterator_traits<_InputIterator2>::value_type,
  4639. typename iterator_traits<_InputIterator1>::value_type>)
  4640. __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
  4641. __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
  4642. __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
  4643. __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
  4644. return _GLIBCXX_STD_A::__merge(__first1, __last1,
  4645. __first2, __last2, __result,
  4646. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  4647. }
  4648. template<typename _RandomAccessIterator, typename _Compare>
  4649. inline void
  4650. __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
  4651. _Compare __comp)
  4652. {
  4653. typedef typename iterator_traits<_RandomAccessIterator>::value_type
  4654. _ValueType;
  4655. typedef typename iterator_traits<_RandomAccessIterator>::difference_type
  4656. _DistanceType;
  4657. typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf;
  4658. _TmpBuf __buf(__first, std::distance(__first, __last));
  4659. if (__buf.begin() == 0)
  4660. std::__inplace_stable_sort(__first, __last, __comp);
  4661. else
  4662. std::__stable_sort_adaptive(__first, __last, __buf.begin(),
  4663. _DistanceType(__buf.size()), __comp);
  4664. }
  4665. /**
  4666. * @brief Sort the elements of a sequence, preserving the relative order
  4667. * of equivalent elements.
  4668. * @ingroup sorting_algorithms
  4669. * @param __first An iterator.
  4670. * @param __last Another iterator.
  4671. * @return Nothing.
  4672. *
  4673. * Sorts the elements in the range @p [__first,__last) in ascending order,
  4674. * such that for each iterator @p i in the range @p [__first,__last-1),
  4675. * @p *(i+1)<*i is false.
  4676. *
  4677. * The relative ordering of equivalent elements is preserved, so any two
  4678. * elements @p x and @p y in the range @p [__first,__last) such that
  4679. * @p x<y is false and @p y<x is false will have the same relative
  4680. * ordering after calling @p stable_sort().
  4681. */
  4682. template<typename _RandomAccessIterator>
  4683. inline void
  4684. stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
  4685. {
  4686. // concept requirements
  4687. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  4688. _RandomAccessIterator>)
  4689. __glibcxx_function_requires(_LessThanComparableConcept<
  4690. typename iterator_traits<_RandomAccessIterator>::value_type>)
  4691. __glibcxx_requires_valid_range(__first, __last);
  4692. __glibcxx_requires_irreflexive(__first, __last);
  4693. _GLIBCXX_STD_A::__stable_sort(__first, __last,
  4694. __gnu_cxx::__ops::__iter_less_iter());
  4695. }
  4696. /**
  4697. * @brief Sort the elements of a sequence using a predicate for comparison,
  4698. * preserving the relative order of equivalent elements.
  4699. * @ingroup sorting_algorithms
  4700. * @param __first An iterator.
  4701. * @param __last Another iterator.
  4702. * @param __comp A comparison functor.
  4703. * @return Nothing.
  4704. *
  4705. * Sorts the elements in the range @p [__first,__last) in ascending order,
  4706. * such that for each iterator @p i in the range @p [__first,__last-1),
  4707. * @p __comp(*(i+1),*i) is false.
  4708. *
  4709. * The relative ordering of equivalent elements is preserved, so any two
  4710. * elements @p x and @p y in the range @p [__first,__last) such that
  4711. * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
  4712. * relative ordering after calling @p stable_sort().
  4713. */
  4714. template<typename _RandomAccessIterator, typename _Compare>
  4715. inline void
  4716. stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
  4717. _Compare __comp)
  4718. {
  4719. // concept requirements
  4720. __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  4721. _RandomAccessIterator>)
  4722. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  4723. typename iterator_traits<_RandomAccessIterator>::value_type,
  4724. typename iterator_traits<_RandomAccessIterator>::value_type>)
  4725. __glibcxx_requires_valid_range(__first, __last);
  4726. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  4727. _GLIBCXX_STD_A::__stable_sort(__first, __last,
  4728. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  4729. }
  4730. template<typename _InputIterator1, typename _InputIterator2,
  4731. typename _OutputIterator,
  4732. typename _Compare>
  4733. _GLIBCXX20_CONSTEXPR
  4734. _OutputIterator
  4735. __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
  4736. _InputIterator2 __first2, _InputIterator2 __last2,
  4737. _OutputIterator __result, _Compare __comp)
  4738. {
  4739. while (__first1 != __last1 && __first2 != __last2)
  4740. {
  4741. if (__comp(__first1, __first2))
  4742. {
  4743. *__result = *__first1;
  4744. ++__first1;
  4745. }
  4746. else if (__comp(__first2, __first1))
  4747. {
  4748. *__result = *__first2;
  4749. ++__first2;
  4750. }
  4751. else
  4752. {
  4753. *__result = *__first1;
  4754. ++__first1;
  4755. ++__first2;
  4756. }
  4757. ++__result;
  4758. }
  4759. return std::copy(__first2, __last2,
  4760. std::copy(__first1, __last1, __result));
  4761. }
  4762. /**
  4763. * @brief Return the union of two sorted ranges.
  4764. * @ingroup set_algorithms
  4765. * @param __first1 Start of first range.
  4766. * @param __last1 End of first range.
  4767. * @param __first2 Start of second range.
  4768. * @param __last2 End of second range.
  4769. * @param __result Start of output range.
  4770. * @return End of the output range.
  4771. * @ingroup set_algorithms
  4772. *
  4773. * This operation iterates over both ranges, copying elements present in
  4774. * each range in order to the output range. Iterators increment for each
  4775. * range. When the current element of one range is less than the other,
  4776. * that element is copied and the iterator advanced. If an element is
  4777. * contained in both ranges, the element from the first range is copied and
  4778. * both ranges advance. The output range may not overlap either input
  4779. * range.
  4780. */
  4781. template<typename _InputIterator1, typename _InputIterator2,
  4782. typename _OutputIterator>
  4783. _GLIBCXX20_CONSTEXPR
  4784. inline _OutputIterator
  4785. set_union(_InputIterator1 __first1, _InputIterator1 __last1,
  4786. _InputIterator2 __first2, _InputIterator2 __last2,
  4787. _OutputIterator __result)
  4788. {
  4789. // concept requirements
  4790. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  4791. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  4792. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4793. typename iterator_traits<_InputIterator1>::value_type>)
  4794. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4795. typename iterator_traits<_InputIterator2>::value_type>)
  4796. __glibcxx_function_requires(_LessThanOpConcept<
  4797. typename iterator_traits<_InputIterator1>::value_type,
  4798. typename iterator_traits<_InputIterator2>::value_type>)
  4799. __glibcxx_function_requires(_LessThanOpConcept<
  4800. typename iterator_traits<_InputIterator2>::value_type,
  4801. typename iterator_traits<_InputIterator1>::value_type>)
  4802. __glibcxx_requires_sorted_set(__first1, __last1, __first2);
  4803. __glibcxx_requires_sorted_set(__first2, __last2, __first1);
  4804. __glibcxx_requires_irreflexive2(__first1, __last1);
  4805. __glibcxx_requires_irreflexive2(__first2, __last2);
  4806. return _GLIBCXX_STD_A::__set_union(__first1, __last1,
  4807. __first2, __last2, __result,
  4808. __gnu_cxx::__ops::__iter_less_iter());
  4809. }
  4810. /**
  4811. * @brief Return the union of two sorted ranges using a comparison functor.
  4812. * @ingroup set_algorithms
  4813. * @param __first1 Start of first range.
  4814. * @param __last1 End of first range.
  4815. * @param __first2 Start of second range.
  4816. * @param __last2 End of second range.
  4817. * @param __result Start of output range.
  4818. * @param __comp The comparison functor.
  4819. * @return End of the output range.
  4820. * @ingroup set_algorithms
  4821. *
  4822. * This operation iterates over both ranges, copying elements present in
  4823. * each range in order to the output range. Iterators increment for each
  4824. * range. When the current element of one range is less than the other
  4825. * according to @p __comp, that element is copied and the iterator advanced.
  4826. * If an equivalent element according to @p __comp is contained in both
  4827. * ranges, the element from the first range is copied and both ranges
  4828. * advance. The output range may not overlap either input range.
  4829. */
  4830. template<typename _InputIterator1, typename _InputIterator2,
  4831. typename _OutputIterator, typename _Compare>
  4832. _GLIBCXX20_CONSTEXPR
  4833. inline _OutputIterator
  4834. set_union(_InputIterator1 __first1, _InputIterator1 __last1,
  4835. _InputIterator2 __first2, _InputIterator2 __last2,
  4836. _OutputIterator __result, _Compare __comp)
  4837. {
  4838. // concept requirements
  4839. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  4840. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  4841. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4842. typename iterator_traits<_InputIterator1>::value_type>)
  4843. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4844. typename iterator_traits<_InputIterator2>::value_type>)
  4845. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  4846. typename iterator_traits<_InputIterator1>::value_type,
  4847. typename iterator_traits<_InputIterator2>::value_type>)
  4848. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  4849. typename iterator_traits<_InputIterator2>::value_type,
  4850. typename iterator_traits<_InputIterator1>::value_type>)
  4851. __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
  4852. __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
  4853. __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
  4854. __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
  4855. return _GLIBCXX_STD_A::__set_union(__first1, __last1,
  4856. __first2, __last2, __result,
  4857. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  4858. }
  4859. template<typename _InputIterator1, typename _InputIterator2,
  4860. typename _OutputIterator,
  4861. typename _Compare>
  4862. _GLIBCXX20_CONSTEXPR
  4863. _OutputIterator
  4864. __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
  4865. _InputIterator2 __first2, _InputIterator2 __last2,
  4866. _OutputIterator __result, _Compare __comp)
  4867. {
  4868. while (__first1 != __last1 && __first2 != __last2)
  4869. if (__comp(__first1, __first2))
  4870. ++__first1;
  4871. else if (__comp(__first2, __first1))
  4872. ++__first2;
  4873. else
  4874. {
  4875. *__result = *__first1;
  4876. ++__first1;
  4877. ++__first2;
  4878. ++__result;
  4879. }
  4880. return __result;
  4881. }
  4882. /**
  4883. * @brief Return the intersection of two sorted ranges.
  4884. * @ingroup set_algorithms
  4885. * @param __first1 Start of first range.
  4886. * @param __last1 End of first range.
  4887. * @param __first2 Start of second range.
  4888. * @param __last2 End of second range.
  4889. * @param __result Start of output range.
  4890. * @return End of the output range.
  4891. * @ingroup set_algorithms
  4892. *
  4893. * This operation iterates over both ranges, copying elements present in
  4894. * both ranges in order to the output range. Iterators increment for each
  4895. * range. When the current element of one range is less than the other,
  4896. * that iterator advances. If an element is contained in both ranges, the
  4897. * element from the first range is copied and both ranges advance. The
  4898. * output range may not overlap either input range.
  4899. */
  4900. template<typename _InputIterator1, typename _InputIterator2,
  4901. typename _OutputIterator>
  4902. _GLIBCXX20_CONSTEXPR
  4903. inline _OutputIterator
  4904. set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
  4905. _InputIterator2 __first2, _InputIterator2 __last2,
  4906. _OutputIterator __result)
  4907. {
  4908. // concept requirements
  4909. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  4910. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  4911. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4912. typename iterator_traits<_InputIterator1>::value_type>)
  4913. __glibcxx_function_requires(_LessThanOpConcept<
  4914. typename iterator_traits<_InputIterator1>::value_type,
  4915. typename iterator_traits<_InputIterator2>::value_type>)
  4916. __glibcxx_function_requires(_LessThanOpConcept<
  4917. typename iterator_traits<_InputIterator2>::value_type,
  4918. typename iterator_traits<_InputIterator1>::value_type>)
  4919. __glibcxx_requires_sorted_set(__first1, __last1, __first2);
  4920. __glibcxx_requires_sorted_set(__first2, __last2, __first1);
  4921. __glibcxx_requires_irreflexive2(__first1, __last1);
  4922. __glibcxx_requires_irreflexive2(__first2, __last2);
  4923. return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
  4924. __first2, __last2, __result,
  4925. __gnu_cxx::__ops::__iter_less_iter());
  4926. }
  4927. /**
  4928. * @brief Return the intersection of two sorted ranges using comparison
  4929. * functor.
  4930. * @ingroup set_algorithms
  4931. * @param __first1 Start of first range.
  4932. * @param __last1 End of first range.
  4933. * @param __first2 Start of second range.
  4934. * @param __last2 End of second range.
  4935. * @param __result Start of output range.
  4936. * @param __comp The comparison functor.
  4937. * @return End of the output range.
  4938. * @ingroup set_algorithms
  4939. *
  4940. * This operation iterates over both ranges, copying elements present in
  4941. * both ranges in order to the output range. Iterators increment for each
  4942. * range. When the current element of one range is less than the other
  4943. * according to @p __comp, that iterator advances. If an element is
  4944. * contained in both ranges according to @p __comp, the element from the
  4945. * first range is copied and both ranges advance. The output range may not
  4946. * overlap either input range.
  4947. */
  4948. template<typename _InputIterator1, typename _InputIterator2,
  4949. typename _OutputIterator, typename _Compare>
  4950. _GLIBCXX20_CONSTEXPR
  4951. inline _OutputIterator
  4952. set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
  4953. _InputIterator2 __first2, _InputIterator2 __last2,
  4954. _OutputIterator __result, _Compare __comp)
  4955. {
  4956. // concept requirements
  4957. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  4958. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  4959. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  4960. typename iterator_traits<_InputIterator1>::value_type>)
  4961. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  4962. typename iterator_traits<_InputIterator1>::value_type,
  4963. typename iterator_traits<_InputIterator2>::value_type>)
  4964. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  4965. typename iterator_traits<_InputIterator2>::value_type,
  4966. typename iterator_traits<_InputIterator1>::value_type>)
  4967. __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
  4968. __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
  4969. __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
  4970. __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
  4971. return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
  4972. __first2, __last2, __result,
  4973. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  4974. }
  4975. template<typename _InputIterator1, typename _InputIterator2,
  4976. typename _OutputIterator,
  4977. typename _Compare>
  4978. _GLIBCXX20_CONSTEXPR
  4979. _OutputIterator
  4980. __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
  4981. _InputIterator2 __first2, _InputIterator2 __last2,
  4982. _OutputIterator __result, _Compare __comp)
  4983. {
  4984. while (__first1 != __last1 && __first2 != __last2)
  4985. if (__comp(__first1, __first2))
  4986. {
  4987. *__result = *__first1;
  4988. ++__first1;
  4989. ++__result;
  4990. }
  4991. else if (__comp(__first2, __first1))
  4992. ++__first2;
  4993. else
  4994. {
  4995. ++__first1;
  4996. ++__first2;
  4997. }
  4998. return std::copy(__first1, __last1, __result);
  4999. }
  5000. /**
  5001. * @brief Return the difference of two sorted ranges.
  5002. * @ingroup set_algorithms
  5003. * @param __first1 Start of first range.
  5004. * @param __last1 End of first range.
  5005. * @param __first2 Start of second range.
  5006. * @param __last2 End of second range.
  5007. * @param __result Start of output range.
  5008. * @return End of the output range.
  5009. * @ingroup set_algorithms
  5010. *
  5011. * This operation iterates over both ranges, copying elements present in
  5012. * the first range but not the second in order to the output range.
  5013. * Iterators increment for each range. When the current element of the
  5014. * first range is less than the second, that element is copied and the
  5015. * iterator advances. If the current element of the second range is less,
  5016. * the iterator advances, but no element is copied. If an element is
  5017. * contained in both ranges, no elements are copied and both ranges
  5018. * advance. The output range may not overlap either input range.
  5019. */
  5020. template<typename _InputIterator1, typename _InputIterator2,
  5021. typename _OutputIterator>
  5022. _GLIBCXX20_CONSTEXPR
  5023. inline _OutputIterator
  5024. set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
  5025. _InputIterator2 __first2, _InputIterator2 __last2,
  5026. _OutputIterator __result)
  5027. {
  5028. // concept requirements
  5029. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  5030. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  5031. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  5032. typename iterator_traits<_InputIterator1>::value_type>)
  5033. __glibcxx_function_requires(_LessThanOpConcept<
  5034. typename iterator_traits<_InputIterator1>::value_type,
  5035. typename iterator_traits<_InputIterator2>::value_type>)
  5036. __glibcxx_function_requires(_LessThanOpConcept<
  5037. typename iterator_traits<_InputIterator2>::value_type,
  5038. typename iterator_traits<_InputIterator1>::value_type>)
  5039. __glibcxx_requires_sorted_set(__first1, __last1, __first2);
  5040. __glibcxx_requires_sorted_set(__first2, __last2, __first1);
  5041. __glibcxx_requires_irreflexive2(__first1, __last1);
  5042. __glibcxx_requires_irreflexive2(__first2, __last2);
  5043. return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
  5044. __first2, __last2, __result,
  5045. __gnu_cxx::__ops::__iter_less_iter());
  5046. }
  5047. /**
  5048. * @brief Return the difference of two sorted ranges using comparison
  5049. * functor.
  5050. * @ingroup set_algorithms
  5051. * @param __first1 Start of first range.
  5052. * @param __last1 End of first range.
  5053. * @param __first2 Start of second range.
  5054. * @param __last2 End of second range.
  5055. * @param __result Start of output range.
  5056. * @param __comp The comparison functor.
  5057. * @return End of the output range.
  5058. * @ingroup set_algorithms
  5059. *
  5060. * This operation iterates over both ranges, copying elements present in
  5061. * the first range but not the second in order to the output range.
  5062. * Iterators increment for each range. When the current element of the
  5063. * first range is less than the second according to @p __comp, that element
  5064. * is copied and the iterator advances. If the current element of the
  5065. * second range is less, no element is copied and the iterator advances.
  5066. * If an element is contained in both ranges according to @p __comp, no
  5067. * elements are copied and both ranges advance. The output range may not
  5068. * overlap either input range.
  5069. */
  5070. template<typename _InputIterator1, typename _InputIterator2,
  5071. typename _OutputIterator, typename _Compare>
  5072. _GLIBCXX20_CONSTEXPR
  5073. inline _OutputIterator
  5074. set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
  5075. _InputIterator2 __first2, _InputIterator2 __last2,
  5076. _OutputIterator __result, _Compare __comp)
  5077. {
  5078. // concept requirements
  5079. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  5080. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  5081. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  5082. typename iterator_traits<_InputIterator1>::value_type>)
  5083. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  5084. typename iterator_traits<_InputIterator1>::value_type,
  5085. typename iterator_traits<_InputIterator2>::value_type>)
  5086. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  5087. typename iterator_traits<_InputIterator2>::value_type,
  5088. typename iterator_traits<_InputIterator1>::value_type>)
  5089. __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
  5090. __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
  5091. __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
  5092. __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
  5093. return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
  5094. __first2, __last2, __result,
  5095. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  5096. }
  5097. template<typename _InputIterator1, typename _InputIterator2,
  5098. typename _OutputIterator,
  5099. typename _Compare>
  5100. _GLIBCXX20_CONSTEXPR
  5101. _OutputIterator
  5102. __set_symmetric_difference(_InputIterator1 __first1,
  5103. _InputIterator1 __last1,
  5104. _InputIterator2 __first2,
  5105. _InputIterator2 __last2,
  5106. _OutputIterator __result,
  5107. _Compare __comp)
  5108. {
  5109. while (__first1 != __last1 && __first2 != __last2)
  5110. if (__comp(__first1, __first2))
  5111. {
  5112. *__result = *__first1;
  5113. ++__first1;
  5114. ++__result;
  5115. }
  5116. else if (__comp(__first2, __first1))
  5117. {
  5118. *__result = *__first2;
  5119. ++__first2;
  5120. ++__result;
  5121. }
  5122. else
  5123. {
  5124. ++__first1;
  5125. ++__first2;
  5126. }
  5127. return std::copy(__first2, __last2,
  5128. std::copy(__first1, __last1, __result));
  5129. }
  5130. /**
  5131. * @brief Return the symmetric difference of two sorted ranges.
  5132. * @ingroup set_algorithms
  5133. * @param __first1 Start of first range.
  5134. * @param __last1 End of first range.
  5135. * @param __first2 Start of second range.
  5136. * @param __last2 End of second range.
  5137. * @param __result Start of output range.
  5138. * @return End of the output range.
  5139. * @ingroup set_algorithms
  5140. *
  5141. * This operation iterates over both ranges, copying elements present in
  5142. * one range but not the other in order to the output range. Iterators
  5143. * increment for each range. When the current element of one range is less
  5144. * than the other, that element is copied and the iterator advances. If an
  5145. * element is contained in both ranges, no elements are copied and both
  5146. * ranges advance. The output range may not overlap either input range.
  5147. */
  5148. template<typename _InputIterator1, typename _InputIterator2,
  5149. typename _OutputIterator>
  5150. _GLIBCXX20_CONSTEXPR
  5151. inline _OutputIterator
  5152. set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
  5153. _InputIterator2 __first2, _InputIterator2 __last2,
  5154. _OutputIterator __result)
  5155. {
  5156. // concept requirements
  5157. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  5158. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  5159. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  5160. typename iterator_traits<_InputIterator1>::value_type>)
  5161. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  5162. typename iterator_traits<_InputIterator2>::value_type>)
  5163. __glibcxx_function_requires(_LessThanOpConcept<
  5164. typename iterator_traits<_InputIterator1>::value_type,
  5165. typename iterator_traits<_InputIterator2>::value_type>)
  5166. __glibcxx_function_requires(_LessThanOpConcept<
  5167. typename iterator_traits<_InputIterator2>::value_type,
  5168. typename iterator_traits<_InputIterator1>::value_type>)
  5169. __glibcxx_requires_sorted_set(__first1, __last1, __first2);
  5170. __glibcxx_requires_sorted_set(__first2, __last2, __first1);
  5171. __glibcxx_requires_irreflexive2(__first1, __last1);
  5172. __glibcxx_requires_irreflexive2(__first2, __last2);
  5173. return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
  5174. __first2, __last2, __result,
  5175. __gnu_cxx::__ops::__iter_less_iter());
  5176. }
  5177. /**
  5178. * @brief Return the symmetric difference of two sorted ranges using
  5179. * comparison functor.
  5180. * @ingroup set_algorithms
  5181. * @param __first1 Start of first range.
  5182. * @param __last1 End of first range.
  5183. * @param __first2 Start of second range.
  5184. * @param __last2 End of second range.
  5185. * @param __result Start of output range.
  5186. * @param __comp The comparison functor.
  5187. * @return End of the output range.
  5188. * @ingroup set_algorithms
  5189. *
  5190. * This operation iterates over both ranges, copying elements present in
  5191. * one range but not the other in order to the output range. Iterators
  5192. * increment for each range. When the current element of one range is less
  5193. * than the other according to @p comp, that element is copied and the
  5194. * iterator advances. If an element is contained in both ranges according
  5195. * to @p __comp, no elements are copied and both ranges advance. The output
  5196. * range may not overlap either input range.
  5197. */
  5198. template<typename _InputIterator1, typename _InputIterator2,
  5199. typename _OutputIterator, typename _Compare>
  5200. _GLIBCXX20_CONSTEXPR
  5201. inline _OutputIterator
  5202. set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
  5203. _InputIterator2 __first2, _InputIterator2 __last2,
  5204. _OutputIterator __result,
  5205. _Compare __comp)
  5206. {
  5207. // concept requirements
  5208. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  5209. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  5210. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  5211. typename iterator_traits<_InputIterator1>::value_type>)
  5212. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  5213. typename iterator_traits<_InputIterator2>::value_type>)
  5214. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  5215. typename iterator_traits<_InputIterator1>::value_type,
  5216. typename iterator_traits<_InputIterator2>::value_type>)
  5217. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  5218. typename iterator_traits<_InputIterator2>::value_type,
  5219. typename iterator_traits<_InputIterator1>::value_type>)
  5220. __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
  5221. __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
  5222. __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
  5223. __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
  5224. return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
  5225. __first2, __last2, __result,
  5226. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  5227. }
  5228. template<typename _ForwardIterator, typename _Compare>
  5229. _GLIBCXX14_CONSTEXPR
  5230. _ForwardIterator
  5231. __min_element(_ForwardIterator __first, _ForwardIterator __last,
  5232. _Compare __comp)
  5233. {
  5234. if (__first == __last)
  5235. return __first;
  5236. _ForwardIterator __result = __first;
  5237. while (++__first != __last)
  5238. if (__comp(__first, __result))
  5239. __result = __first;
  5240. return __result;
  5241. }
  5242. /**
  5243. * @brief Return the minimum element in a range.
  5244. * @ingroup sorting_algorithms
  5245. * @param __first Start of range.
  5246. * @param __last End of range.
  5247. * @return Iterator referencing the first instance of the smallest value.
  5248. */
  5249. template<typename _ForwardIterator>
  5250. _GLIBCXX14_CONSTEXPR
  5251. _ForwardIterator
  5252. inline min_element(_ForwardIterator __first, _ForwardIterator __last)
  5253. {
  5254. // concept requirements
  5255. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  5256. __glibcxx_function_requires(_LessThanComparableConcept<
  5257. typename iterator_traits<_ForwardIterator>::value_type>)
  5258. __glibcxx_requires_valid_range(__first, __last);
  5259. __glibcxx_requires_irreflexive(__first, __last);
  5260. return _GLIBCXX_STD_A::__min_element(__first, __last,
  5261. __gnu_cxx::__ops::__iter_less_iter());
  5262. }
  5263. /**
  5264. * @brief Return the minimum element in a range using comparison functor.
  5265. * @ingroup sorting_algorithms
  5266. * @param __first Start of range.
  5267. * @param __last End of range.
  5268. * @param __comp Comparison functor.
  5269. * @return Iterator referencing the first instance of the smallest value
  5270. * according to __comp.
  5271. */
  5272. template<typename _ForwardIterator, typename _Compare>
  5273. _GLIBCXX14_CONSTEXPR
  5274. inline _ForwardIterator
  5275. min_element(_ForwardIterator __first, _ForwardIterator __last,
  5276. _Compare __comp)
  5277. {
  5278. // concept requirements
  5279. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  5280. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  5281. typename iterator_traits<_ForwardIterator>::value_type,
  5282. typename iterator_traits<_ForwardIterator>::value_type>)
  5283. __glibcxx_requires_valid_range(__first, __last);
  5284. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  5285. return _GLIBCXX_STD_A::__min_element(__first, __last,
  5286. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  5287. }
  5288. template<typename _ForwardIterator, typename _Compare>
  5289. _GLIBCXX14_CONSTEXPR
  5290. _ForwardIterator
  5291. __max_element(_ForwardIterator __first, _ForwardIterator __last,
  5292. _Compare __comp)
  5293. {
  5294. if (__first == __last) return __first;
  5295. _ForwardIterator __result = __first;
  5296. while (++__first != __last)
  5297. if (__comp(__result, __first))
  5298. __result = __first;
  5299. return __result;
  5300. }
  5301. /**
  5302. * @brief Return the maximum element in a range.
  5303. * @ingroup sorting_algorithms
  5304. * @param __first Start of range.
  5305. * @param __last End of range.
  5306. * @return Iterator referencing the first instance of the largest value.
  5307. */
  5308. template<typename _ForwardIterator>
  5309. _GLIBCXX14_CONSTEXPR
  5310. inline _ForwardIterator
  5311. max_element(_ForwardIterator __first, _ForwardIterator __last)
  5312. {
  5313. // concept requirements
  5314. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  5315. __glibcxx_function_requires(_LessThanComparableConcept<
  5316. typename iterator_traits<_ForwardIterator>::value_type>)
  5317. __glibcxx_requires_valid_range(__first, __last);
  5318. __glibcxx_requires_irreflexive(__first, __last);
  5319. return _GLIBCXX_STD_A::__max_element(__first, __last,
  5320. __gnu_cxx::__ops::__iter_less_iter());
  5321. }
  5322. /**
  5323. * @brief Return the maximum element in a range using comparison functor.
  5324. * @ingroup sorting_algorithms
  5325. * @param __first Start of range.
  5326. * @param __last End of range.
  5327. * @param __comp Comparison functor.
  5328. * @return Iterator referencing the first instance of the largest value
  5329. * according to __comp.
  5330. */
  5331. template<typename _ForwardIterator, typename _Compare>
  5332. _GLIBCXX14_CONSTEXPR
  5333. inline _ForwardIterator
  5334. max_element(_ForwardIterator __first, _ForwardIterator __last,
  5335. _Compare __comp)
  5336. {
  5337. // concept requirements
  5338. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  5339. __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
  5340. typename iterator_traits<_ForwardIterator>::value_type,
  5341. typename iterator_traits<_ForwardIterator>::value_type>)
  5342. __glibcxx_requires_valid_range(__first, __last);
  5343. __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
  5344. return _GLIBCXX_STD_A::__max_element(__first, __last,
  5345. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  5346. }
  5347. #if __cplusplus >= 201402L
  5348. /// Reservoir sampling algorithm.
  5349. template<typename _InputIterator, typename _RandomAccessIterator,
  5350. typename _Size, typename _UniformRandomBitGenerator>
  5351. _RandomAccessIterator
  5352. __sample(_InputIterator __first, _InputIterator __last, input_iterator_tag,
  5353. _RandomAccessIterator __out, random_access_iterator_tag,
  5354. _Size __n, _UniformRandomBitGenerator&& __g)
  5355. {
  5356. using __distrib_type = uniform_int_distribution<_Size>;
  5357. using __param_type = typename __distrib_type::param_type;
  5358. __distrib_type __d{};
  5359. _Size __sample_sz = 0;
  5360. while (__first != __last && __sample_sz != __n)
  5361. {
  5362. __out[__sample_sz++] = *__first;
  5363. ++__first;
  5364. }
  5365. for (auto __pop_sz = __sample_sz; __first != __last;
  5366. ++__first, (void) ++__pop_sz)
  5367. {
  5368. const auto __k = __d(__g, __param_type{0, __pop_sz});
  5369. if (__k < __n)
  5370. __out[__k] = *__first;
  5371. }
  5372. return __out + __sample_sz;
  5373. }
  5374. /// Selection sampling algorithm.
  5375. template<typename _ForwardIterator, typename _OutputIterator, typename _Cat,
  5376. typename _Size, typename _UniformRandomBitGenerator>
  5377. _OutputIterator
  5378. __sample(_ForwardIterator __first, _ForwardIterator __last,
  5379. forward_iterator_tag,
  5380. _OutputIterator __out, _Cat,
  5381. _Size __n, _UniformRandomBitGenerator&& __g)
  5382. {
  5383. using __distrib_type = uniform_int_distribution<_Size>;
  5384. using __param_type = typename __distrib_type::param_type;
  5385. using _USize = make_unsigned_t<_Size>;
  5386. using _Gen = remove_reference_t<_UniformRandomBitGenerator>;
  5387. using __uc_type = common_type_t<typename _Gen::result_type, _USize>;
  5388. if (__first == __last)
  5389. return __out;
  5390. __distrib_type __d{};
  5391. _Size __unsampled_sz = std::distance(__first, __last);
  5392. __n = std::min(__n, __unsampled_sz);
  5393. // If possible, we use __gen_two_uniform_ints to efficiently produce
  5394. // two random numbers using a single distribution invocation:
  5395. const __uc_type __urngrange = __g.max() - __g.min();
  5396. if (__urngrange / __uc_type(__unsampled_sz) >= __uc_type(__unsampled_sz))
  5397. // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
  5398. // wrapping issues.
  5399. {
  5400. while (__n != 0 && __unsampled_sz >= 2)
  5401. {
  5402. const pair<_Size, _Size> __p =
  5403. __gen_two_uniform_ints(__unsampled_sz, __unsampled_sz - 1, __g);
  5404. --__unsampled_sz;
  5405. if (__p.first < __n)
  5406. {
  5407. *__out++ = *__first;
  5408. --__n;
  5409. }
  5410. ++__first;
  5411. if (__n == 0) break;
  5412. --__unsampled_sz;
  5413. if (__p.second < __n)
  5414. {
  5415. *__out++ = *__first;
  5416. --__n;
  5417. }
  5418. ++__first;
  5419. }
  5420. }
  5421. // The loop above is otherwise equivalent to this one-at-a-time version:
  5422. for (; __n != 0; ++__first)
  5423. if (__d(__g, __param_type{0, --__unsampled_sz}) < __n)
  5424. {
  5425. *__out++ = *__first;
  5426. --__n;
  5427. }
  5428. return __out;
  5429. }
  5430. #if __cplusplus > 201402L
  5431. #define __cpp_lib_sample 201603
  5432. /// Take a random sample from a population.
  5433. template<typename _PopulationIterator, typename _SampleIterator,
  5434. typename _Distance, typename _UniformRandomBitGenerator>
  5435. _SampleIterator
  5436. sample(_PopulationIterator __first, _PopulationIterator __last,
  5437. _SampleIterator __out, _Distance __n,
  5438. _UniformRandomBitGenerator&& __g)
  5439. {
  5440. using __pop_cat = typename
  5441. std::iterator_traits<_PopulationIterator>::iterator_category;
  5442. using __samp_cat = typename
  5443. std::iterator_traits<_SampleIterator>::iterator_category;
  5444. static_assert(
  5445. __or_<is_convertible<__pop_cat, forward_iterator_tag>,
  5446. is_convertible<__samp_cat, random_access_iterator_tag>>::value,
  5447. "output range must use a RandomAccessIterator when input range"
  5448. " does not meet the ForwardIterator requirements");
  5449. static_assert(is_integral<_Distance>::value,
  5450. "sample size must be an integer type");
  5451. typename iterator_traits<_PopulationIterator>::difference_type __d = __n;
  5452. return _GLIBCXX_STD_A::
  5453. __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d,
  5454. std::forward<_UniformRandomBitGenerator>(__g));
  5455. }
  5456. #endif // C++17
  5457. #endif // C++14
  5458. _GLIBCXX_END_NAMESPACE_ALGO
  5459. _GLIBCXX_END_NAMESPACE_VERSION
  5460. } // namespace std
  5461. #endif /* _STL_ALGO_H */