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.

3793 lines
118KB

  1. // Core algorithmic facilities -*- C++ -*-
  2. // Copyright (C) 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. /** @file bits/ranges_algo.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{algorithm}
  23. */
  24. #ifndef _RANGES_ALGO_H
  25. #define _RANGES_ALGO_H 1
  26. #if __cplusplus > 201703L
  27. #include <bits/ranges_algobase.h>
  28. #include <bits/uniform_int_dist.h> // concept uniform_random_bit_generator
  29. #if __cpp_lib_concepts
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. namespace ranges
  34. {
  35. namespace __detail
  36. {
  37. template<typename _Comp, typename _Proj>
  38. constexpr auto
  39. __make_comp_proj(_Comp& __comp, _Proj& __proj)
  40. {
  41. return [&] (auto&& __lhs, auto&& __rhs) -> bool {
  42. using _TL = decltype(__lhs);
  43. using _TR = decltype(__rhs);
  44. return std::__invoke(__comp,
  45. std::__invoke(__proj, std::forward<_TL>(__lhs)),
  46. std::__invoke(__proj, std::forward<_TR>(__rhs)));
  47. };
  48. }
  49. template<typename _Pred, typename _Proj>
  50. constexpr auto
  51. __make_pred_proj(_Pred& __pred, _Proj& __proj)
  52. {
  53. return [&] <typename _Tp> (_Tp&& __arg) -> bool {
  54. return std::__invoke(__pred,
  55. std::__invoke(__proj, std::forward<_Tp>(__arg)));
  56. };
  57. }
  58. } // namespace __detail
  59. struct __all_of_fn
  60. {
  61. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  62. typename _Proj = identity,
  63. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  64. constexpr bool
  65. operator()(_Iter __first, _Sent __last,
  66. _Pred __pred, _Proj __proj = {}) const
  67. {
  68. for (; __first != __last; ++__first)
  69. if (!(bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
  70. return false;
  71. return true;
  72. }
  73. template<input_range _Range, typename _Proj = identity,
  74. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  75. _Pred>
  76. constexpr bool
  77. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  78. {
  79. return (*this)(ranges::begin(__r), ranges::end(__r),
  80. std::move(__pred), std::move(__proj));
  81. }
  82. };
  83. inline constexpr __all_of_fn all_of{};
  84. struct __any_of_fn
  85. {
  86. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  87. typename _Proj = identity,
  88. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  89. constexpr bool
  90. operator()(_Iter __first, _Sent __last,
  91. _Pred __pred, _Proj __proj = {}) const
  92. {
  93. for (; __first != __last; ++__first)
  94. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  95. return true;
  96. return false;
  97. }
  98. template<input_range _Range, typename _Proj = identity,
  99. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  100. _Pred>
  101. constexpr bool
  102. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  103. {
  104. return (*this)(ranges::begin(__r), ranges::end(__r),
  105. std::move(__pred), std::move(__proj));
  106. }
  107. };
  108. inline constexpr __any_of_fn any_of{};
  109. struct __none_of_fn
  110. {
  111. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  112. typename _Proj = identity,
  113. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  114. constexpr bool
  115. operator()(_Iter __first, _Sent __last,
  116. _Pred __pred, _Proj __proj = {}) const
  117. {
  118. for (; __first != __last; ++__first)
  119. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  120. return false;
  121. return true;
  122. }
  123. template<input_range _Range, typename _Proj = identity,
  124. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  125. _Pred>
  126. constexpr bool
  127. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  128. {
  129. return (*this)(ranges::begin(__r), ranges::end(__r),
  130. std::move(__pred), std::move(__proj));
  131. }
  132. };
  133. inline constexpr __none_of_fn none_of{};
  134. template<typename _Iter, typename _Fp>
  135. struct in_fun_result
  136. {
  137. [[no_unique_address]] _Iter in;
  138. [[no_unique_address]] _Fp fun;
  139. template<typename _Iter2, typename _F2p>
  140. requires convertible_to<const _Iter&, _Iter2>
  141. && convertible_to<const _Fp&, _F2p>
  142. constexpr
  143. operator in_fun_result<_Iter2, _F2p>() const &
  144. { return {in, fun}; }
  145. template<typename _Iter2, typename _F2p>
  146. requires convertible_to<_Iter, _Iter2> && convertible_to<_Fp, _F2p>
  147. constexpr
  148. operator in_fun_result<_Iter2, _F2p>() &&
  149. { return {std::move(in), std::move(fun)}; }
  150. };
  151. template<typename _Iter, typename _Fp>
  152. using for_each_result = in_fun_result<_Iter, _Fp>;
  153. struct __for_each_fn
  154. {
  155. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  156. typename _Proj = identity,
  157. indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
  158. constexpr for_each_result<_Iter, _Fun>
  159. operator()(_Iter __first, _Sent __last, _Fun __f, _Proj __proj = {}) const
  160. {
  161. for (; __first != __last; ++__first)
  162. std::__invoke(__f, std::__invoke(__proj, *__first));
  163. return { std::move(__first), std::move(__f) };
  164. }
  165. template<input_range _Range, typename _Proj = identity,
  166. indirectly_unary_invocable<projected<iterator_t<_Range>, _Proj>>
  167. _Fun>
  168. constexpr for_each_result<borrowed_iterator_t<_Range>, _Fun>
  169. operator()(_Range&& __r, _Fun __f, _Proj __proj = {}) const
  170. {
  171. return (*this)(ranges::begin(__r), ranges::end(__r),
  172. std::move(__f), std::move(__proj));
  173. }
  174. };
  175. inline constexpr __for_each_fn for_each{};
  176. template<typename _Iter, typename _Fp>
  177. using for_each_n_result = in_fun_result<_Iter, _Fp>;
  178. struct __for_each_n_fn
  179. {
  180. template<input_iterator _Iter, typename _Proj = identity,
  181. indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
  182. constexpr for_each_n_result<_Iter, _Fun>
  183. operator()(_Iter __first, iter_difference_t<_Iter> __n,
  184. _Fun __f, _Proj __proj = {}) const
  185. {
  186. if constexpr (random_access_iterator<_Iter>)
  187. {
  188. if (__n <= 0)
  189. return {std::move(__first), std::move(__f)};
  190. auto __last = __first + __n;
  191. return ranges::for_each(std::move(__first), std::move(__last),
  192. std::move(__f), std::move(__proj));
  193. }
  194. else
  195. {
  196. while (__n-- > 0)
  197. {
  198. std::__invoke(__f, std::__invoke(__proj, *__first));
  199. ++__first;
  200. }
  201. return {std::move(__first), std::move(__f)};
  202. }
  203. }
  204. };
  205. inline constexpr __for_each_n_fn for_each_n{};
  206. struct __find_fn
  207. {
  208. template<input_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Tp,
  209. typename _Proj = identity>
  210. requires indirect_binary_predicate<ranges::equal_to,
  211. projected<_Iter, _Proj>, const _Tp*>
  212. constexpr _Iter
  213. operator()(_Iter __first, _Sent __last,
  214. const _Tp& __value, _Proj __proj = {}) const
  215. {
  216. while (__first != __last
  217. && !(std::__invoke(__proj, *__first) == __value))
  218. ++__first;
  219. return __first;
  220. }
  221. template<input_range _Range, typename _Tp, typename _Proj = identity>
  222. requires indirect_binary_predicate<ranges::equal_to,
  223. projected<iterator_t<_Range>, _Proj>,
  224. const _Tp*>
  225. constexpr borrowed_iterator_t<_Range>
  226. operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
  227. {
  228. return (*this)(ranges::begin(__r), ranges::end(__r),
  229. __value, std::move(__proj));
  230. }
  231. };
  232. inline constexpr __find_fn find{};
  233. struct __find_if_fn
  234. {
  235. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  236. typename _Proj = identity,
  237. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  238. constexpr _Iter
  239. operator()(_Iter __first, _Sent __last,
  240. _Pred __pred, _Proj __proj = {}) const
  241. {
  242. while (__first != __last
  243. && !(bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
  244. ++__first;
  245. return __first;
  246. }
  247. template<input_range _Range, typename _Proj = identity,
  248. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  249. _Pred>
  250. constexpr borrowed_iterator_t<_Range>
  251. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  252. {
  253. return (*this)(ranges::begin(__r), ranges::end(__r),
  254. std::move(__pred), std::move(__proj));
  255. }
  256. };
  257. inline constexpr __find_if_fn find_if{};
  258. struct __find_if_not_fn
  259. {
  260. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  261. typename _Proj = identity,
  262. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  263. constexpr _Iter
  264. operator()(_Iter __first, _Sent __last,
  265. _Pred __pred, _Proj __proj = {}) const
  266. {
  267. while (__first != __last
  268. && (bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
  269. ++__first;
  270. return __first;
  271. }
  272. template<input_range _Range, typename _Proj = identity,
  273. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  274. _Pred>
  275. constexpr borrowed_iterator_t<_Range>
  276. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  277. {
  278. return (*this)(ranges::begin(__r), ranges::end(__r),
  279. std::move(__pred), std::move(__proj));
  280. }
  281. };
  282. inline constexpr __find_if_not_fn find_if_not{};
  283. struct __find_first_of_fn
  284. {
  285. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  286. forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  287. typename _Pred = ranges::equal_to,
  288. typename _Proj1 = identity, typename _Proj2 = identity>
  289. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  290. constexpr _Iter1
  291. operator()(_Iter1 __first1, _Sent1 __last1,
  292. _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
  293. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  294. {
  295. for (; __first1 != __last1; ++__first1)
  296. for (auto __iter = __first2; __iter != __last2; ++__iter)
  297. if (std::__invoke(__pred,
  298. std::__invoke(__proj1, *__first1),
  299. std::__invoke(__proj2, *__iter)))
  300. return __first1;
  301. return __first1;
  302. }
  303. template<input_range _Range1, forward_range _Range2,
  304. typename _Pred = ranges::equal_to,
  305. typename _Proj1 = identity, typename _Proj2 = identity>
  306. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
  307. _Pred, _Proj1, _Proj2>
  308. constexpr borrowed_iterator_t<_Range1>
  309. operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
  310. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  311. {
  312. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  313. ranges::begin(__r2), ranges::end(__r2),
  314. std::move(__pred),
  315. std::move(__proj1), std::move(__proj2));
  316. }
  317. };
  318. inline constexpr __find_first_of_fn find_first_of{};
  319. struct __count_fn
  320. {
  321. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  322. typename _Tp, typename _Proj = identity>
  323. requires indirect_binary_predicate<ranges::equal_to,
  324. projected<_Iter, _Proj>,
  325. const _Tp*>
  326. constexpr iter_difference_t<_Iter>
  327. operator()(_Iter __first, _Sent __last,
  328. const _Tp& __value, _Proj __proj = {}) const
  329. {
  330. iter_difference_t<_Iter> __n = 0;
  331. for (; __first != __last; ++__first)
  332. if (std::__invoke(__proj, *__first) == __value)
  333. ++__n;
  334. return __n;
  335. }
  336. template<input_range _Range, typename _Tp, typename _Proj = identity>
  337. requires indirect_binary_predicate<ranges::equal_to,
  338. projected<iterator_t<_Range>, _Proj>,
  339. const _Tp*>
  340. constexpr range_difference_t<_Range>
  341. operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
  342. {
  343. return (*this)(ranges::begin(__r), ranges::end(__r),
  344. __value, std::move(__proj));
  345. }
  346. };
  347. inline constexpr __count_fn count{};
  348. struct __count_if_fn
  349. {
  350. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  351. typename _Proj = identity,
  352. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  353. constexpr iter_difference_t<_Iter>
  354. operator()(_Iter __first, _Sent __last,
  355. _Pred __pred, _Proj __proj = {}) const
  356. {
  357. iter_difference_t<_Iter> __n = 0;
  358. for (; __first != __last; ++__first)
  359. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  360. ++__n;
  361. return __n;
  362. }
  363. template<input_range _Range,
  364. typename _Proj = identity,
  365. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  366. _Pred>
  367. constexpr range_difference_t<_Range>
  368. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  369. {
  370. return (*this)(ranges::begin(__r), ranges::end(__r),
  371. std::move(__pred), std::move(__proj));
  372. }
  373. };
  374. inline constexpr __count_if_fn count_if{};
  375. template<typename _Iter1, typename _Iter2>
  376. struct in_in_result
  377. {
  378. [[no_unique_address]] _Iter1 in1;
  379. [[no_unique_address]] _Iter2 in2;
  380. template<typename _IIter1, typename _IIter2>
  381. requires convertible_to<const _Iter1&, _IIter1>
  382. && convertible_to<const _Iter2&, _IIter2>
  383. constexpr
  384. operator in_in_result<_IIter1, _IIter2>() const &
  385. { return {in1, in2}; }
  386. template<typename _IIter1, typename _IIter2>
  387. requires convertible_to<_Iter1, _IIter1>
  388. && convertible_to<_Iter2, _IIter2>
  389. constexpr
  390. operator in_in_result<_IIter1, _IIter2>() &&
  391. { return {std::move(in1), std::move(in2)}; }
  392. };
  393. template<typename _Iter1, typename _Iter2>
  394. using mismatch_result = in_in_result<_Iter1, _Iter2>;
  395. struct __mismatch_fn
  396. {
  397. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  398. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  399. typename _Pred = ranges::equal_to,
  400. typename _Proj1 = identity, typename _Proj2 = identity>
  401. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  402. constexpr mismatch_result<_Iter1, _Iter2>
  403. operator()(_Iter1 __first1, _Sent1 __last1,
  404. _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
  405. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  406. {
  407. while (__first1 != __last1 && __first2 != __last2
  408. && (bool)std::__invoke(__pred,
  409. std::__invoke(__proj1, *__first1),
  410. std::__invoke(__proj2, *__first2)))
  411. {
  412. ++__first1;
  413. ++__first2;
  414. }
  415. return { std::move(__first1), std::move(__first2) };
  416. }
  417. template<input_range _Range1, input_range _Range2,
  418. typename _Pred = ranges::equal_to,
  419. typename _Proj1 = identity, typename _Proj2 = identity>
  420. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
  421. _Pred, _Proj1, _Proj2>
  422. constexpr mismatch_result<iterator_t<_Range1>, iterator_t<_Range2>>
  423. operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
  424. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  425. {
  426. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  427. ranges::begin(__r2), ranges::end(__r2),
  428. std::move(__pred),
  429. std::move(__proj1), std::move(__proj2));
  430. }
  431. };
  432. inline constexpr __mismatch_fn mismatch{};
  433. struct __search_fn
  434. {
  435. template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  436. forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  437. typename _Pred = ranges::equal_to,
  438. typename _Proj1 = identity, typename _Proj2 = identity>
  439. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  440. constexpr subrange<_Iter1>
  441. operator()(_Iter1 __first1, _Sent1 __last1,
  442. _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
  443. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  444. {
  445. if (__first1 == __last1 || __first2 == __last2)
  446. return {__first1, __first1};
  447. for (;;)
  448. {
  449. for (;;)
  450. {
  451. if (__first1 == __last1)
  452. return {__first1, __first1};
  453. if (std::__invoke(__pred,
  454. std::__invoke(__proj1, *__first1),
  455. std::__invoke(__proj2, *__first2)))
  456. break;
  457. ++__first1;
  458. }
  459. auto __cur1 = __first1;
  460. auto __cur2 = __first2;
  461. for (;;)
  462. {
  463. if (++__cur2 == __last2)
  464. return {__first1, ++__cur1};
  465. if (++__cur1 == __last1)
  466. return {__cur1, __cur1};
  467. if (!(bool)std::__invoke(__pred,
  468. std::__invoke(__proj1, *__cur1),
  469. std::__invoke(__proj2, *__cur2)))
  470. {
  471. ++__first1;
  472. break;
  473. }
  474. }
  475. }
  476. }
  477. template<forward_range _Range1, forward_range _Range2,
  478. typename _Pred = ranges::equal_to,
  479. typename _Proj1 = identity, typename _Proj2 = identity>
  480. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
  481. _Pred, _Proj1, _Proj2>
  482. constexpr borrowed_subrange_t<_Range1>
  483. operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
  484. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  485. {
  486. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  487. ranges::begin(__r2), ranges::end(__r2),
  488. std::move(__pred),
  489. std::move(__proj1), std::move(__proj2));
  490. }
  491. };
  492. inline constexpr __search_fn search{};
  493. struct __search_n_fn
  494. {
  495. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Tp,
  496. typename _Pred = ranges::equal_to, typename _Proj = identity>
  497. requires indirectly_comparable<_Iter, const _Tp*, _Pred, _Proj>
  498. constexpr subrange<_Iter>
  499. operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __count,
  500. const _Tp& __value, _Pred __pred = {}, _Proj __proj = {}) const
  501. {
  502. if (__count <= 0)
  503. return {__first, __first};
  504. auto __value_comp = [&] <typename _Rp> (_Rp&& __arg) {
  505. return std::__invoke(__pred, std::forward<_Rp>(__arg), __value);
  506. };
  507. if (__count == 1)
  508. {
  509. __first = ranges::find_if(std::move(__first), __last,
  510. std::move(__value_comp),
  511. std::move(__proj));
  512. if (__first == __last)
  513. return {__first, __first};
  514. else
  515. {
  516. auto __end = __first;
  517. return {__first, ++__end};
  518. }
  519. }
  520. if constexpr (sized_sentinel_for<_Sent, _Iter>)
  521. {
  522. auto __tail_size = __last - __first;
  523. auto __remainder = __count;
  524. while (__remainder <= __tail_size)
  525. {
  526. __first += __remainder;
  527. __tail_size -= __remainder;
  528. auto __backtrack = __first;
  529. while (__value_comp(std::__invoke(__proj, *--__backtrack)))
  530. {
  531. if (--__remainder == 0)
  532. return {__first - __count, __first};
  533. }
  534. }
  535. auto __i = __first + __tail_size;
  536. return {__i, __i};
  537. }
  538. else
  539. {
  540. __first = ranges::find_if(__first, __last, __value_comp, __proj);
  541. while (__first != __last)
  542. {
  543. auto __n = __count;
  544. auto __i = __first;
  545. ++__i;
  546. while (__i != __last && __n != 1
  547. && __value_comp(std::__invoke(__proj, *__i)))
  548. {
  549. ++__i;
  550. --__n;
  551. }
  552. if (__n == 1)
  553. return {__first, __i};
  554. if (__i == __last)
  555. return {__i, __i};
  556. __first = ranges::find_if(++__i, __last, __value_comp, __proj);
  557. }
  558. return {__first, __first};
  559. }
  560. }
  561. template<forward_range _Range, typename _Tp,
  562. typename _Pred = ranges::equal_to, typename _Proj = identity>
  563. requires indirectly_comparable<iterator_t<_Range>, const _Tp*,
  564. _Pred, _Proj>
  565. constexpr borrowed_subrange_t<_Range>
  566. operator()(_Range&& __r, range_difference_t<_Range> __count,
  567. const _Tp& __value, _Pred __pred = {}, _Proj __proj = {}) const
  568. {
  569. return (*this)(ranges::begin(__r), ranges::end(__r),
  570. std::move(__count), __value,
  571. std::move(__pred), std::move(__proj));
  572. }
  573. };
  574. inline constexpr __search_n_fn search_n{};
  575. struct __find_end_fn
  576. {
  577. template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  578. forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  579. typename _Pred = ranges::equal_to,
  580. typename _Proj1 = identity, typename _Proj2 = identity>
  581. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  582. constexpr subrange<_Iter1>
  583. operator()(_Iter1 __first1, _Sent1 __last1,
  584. _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
  585. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  586. {
  587. if constexpr (bidirectional_iterator<_Iter1>
  588. && bidirectional_iterator<_Iter2>)
  589. {
  590. auto __i1 = ranges::next(__first1, __last1);
  591. auto __i2 = ranges::next(__first2, __last2);
  592. auto __rresult
  593. = ranges::search(reverse_iterator<_Iter1>{__i1},
  594. reverse_iterator<_Iter1>{__first1},
  595. reverse_iterator<_Iter2>{__i2},
  596. reverse_iterator<_Iter2>{__first2},
  597. std::move(__pred),
  598. std::move(__proj1), std::move(__proj2));
  599. auto __result_first = ranges::end(__rresult).base();
  600. auto __result_last = ranges::begin(__rresult).base();
  601. if (__result_last == __first1)
  602. return {__i1, __i1};
  603. else
  604. return {__result_first, __result_last};
  605. }
  606. else
  607. {
  608. auto __i = ranges::next(__first1, __last1);
  609. if (__first2 == __last2)
  610. return {__i, __i};
  611. auto __result_begin = __i;
  612. auto __result_end = __i;
  613. for (;;)
  614. {
  615. auto __new_range = ranges::search(__first1, __last1,
  616. __first2, __last2,
  617. __pred, __proj1, __proj2);
  618. auto __new_result_begin = ranges::begin(__new_range);
  619. auto __new_result_end = ranges::end(__new_range);
  620. if (__new_result_begin == __last1)
  621. return {__result_begin, __result_end};
  622. else
  623. {
  624. __result_begin = __new_result_begin;
  625. __result_end = __new_result_end;
  626. __first1 = __result_begin;
  627. ++__first1;
  628. }
  629. }
  630. }
  631. }
  632. template<forward_range _Range1, forward_range _Range2,
  633. typename _Pred = ranges::equal_to,
  634. typename _Proj1 = identity, typename _Proj2 = identity>
  635. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
  636. _Pred, _Proj1, _Proj2>
  637. constexpr borrowed_subrange_t<_Range1>
  638. operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
  639. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  640. {
  641. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  642. ranges::begin(__r2), ranges::end(__r2),
  643. std::move(__pred),
  644. std::move(__proj1), std::move(__proj2));
  645. }
  646. };
  647. inline constexpr __find_end_fn find_end{};
  648. struct __adjacent_find_fn
  649. {
  650. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  651. typename _Proj = identity,
  652. indirect_binary_predicate<projected<_Iter, _Proj>,
  653. projected<_Iter, _Proj>> _Pred
  654. = ranges::equal_to>
  655. constexpr _Iter
  656. operator()(_Iter __first, _Sent __last,
  657. _Pred __pred = {}, _Proj __proj = {}) const
  658. {
  659. if (__first == __last)
  660. return __first;
  661. auto __next = __first;
  662. for (; ++__next != __last; __first = __next)
  663. {
  664. if (std::__invoke(__pred,
  665. std::__invoke(__proj, *__first),
  666. std::__invoke(__proj, *__next)))
  667. return __first;
  668. }
  669. return __next;
  670. }
  671. template<forward_range _Range, typename _Proj = identity,
  672. indirect_binary_predicate<
  673. projected<iterator_t<_Range>, _Proj>,
  674. projected<iterator_t<_Range>, _Proj>> _Pred = ranges::equal_to>
  675. constexpr borrowed_iterator_t<_Range>
  676. operator()(_Range&& __r, _Pred __pred = {}, _Proj __proj = {}) const
  677. {
  678. return (*this)(ranges::begin(__r), ranges::end(__r),
  679. std::move(__pred), std::move(__proj));
  680. }
  681. };
  682. inline constexpr __adjacent_find_fn adjacent_find{};
  683. struct __is_permutation_fn
  684. {
  685. template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  686. forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  687. typename _Proj1 = identity, typename _Proj2 = identity,
  688. indirect_equivalence_relation<projected<_Iter1, _Proj1>,
  689. projected<_Iter2, _Proj2>> _Pred
  690. = ranges::equal_to>
  691. constexpr bool
  692. operator()(_Iter1 __first1, _Sent1 __last1,
  693. _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
  694. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  695. {
  696. constexpr bool __sized_iters
  697. = (sized_sentinel_for<_Sent1, _Iter1>
  698. && sized_sentinel_for<_Sent2, _Iter2>);
  699. if constexpr (__sized_iters)
  700. {
  701. auto __d1 = ranges::distance(__first1, __last1);
  702. auto __d2 = ranges::distance(__first2, __last2);
  703. if (__d1 != __d2)
  704. return false;
  705. }
  706. // Efficiently compare identical prefixes: O(N) if sequences
  707. // have the same elements in the same order.
  708. for (; __first1 != __last1 && __first2 != __last2;
  709. ++__first1, (void)++__first2)
  710. if (!(bool)std::__invoke(__pred,
  711. std::__invoke(__proj1, *__first1),
  712. std::__invoke(__proj2, *__first2)))
  713. break;
  714. if constexpr (__sized_iters)
  715. {
  716. if (__first1 == __last1)
  717. return true;
  718. }
  719. else
  720. {
  721. auto __d1 = ranges::distance(__first1, __last1);
  722. auto __d2 = ranges::distance(__first2, __last2);
  723. if (__d1 == 0 && __d2 == 0)
  724. return true;
  725. if (__d1 != __d2)
  726. return false;
  727. }
  728. for (auto __scan = __first1; __scan != __last1; ++__scan)
  729. {
  730. auto __proj_scan = std::__invoke(__proj1, *__scan);
  731. auto __comp_scan = [&] <typename _Tp> (_Tp&& __arg) {
  732. return std::__invoke(__pred, __proj_scan,
  733. std::forward<_Tp>(__arg));
  734. };
  735. if (__scan != ranges::find_if(__first1, __scan,
  736. __comp_scan, __proj1))
  737. continue; // We've seen this one before.
  738. auto __matches = ranges::count_if(__first2, __last2,
  739. __comp_scan, __proj2);
  740. if (__matches == 0
  741. || ranges::count_if(__scan, __last1,
  742. __comp_scan, __proj1) != __matches)
  743. return false;
  744. }
  745. return true;
  746. }
  747. template<forward_range _Range1, forward_range _Range2,
  748. typename _Proj1 = identity, typename _Proj2 = identity,
  749. indirect_equivalence_relation<
  750. projected<iterator_t<_Range1>, _Proj1>,
  751. projected<iterator_t<_Range2>, _Proj2>> _Pred = ranges::equal_to>
  752. constexpr bool
  753. operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
  754. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  755. {
  756. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  757. ranges::begin(__r2), ranges::end(__r2),
  758. std::move(__pred),
  759. std::move(__proj1), std::move(__proj2));
  760. }
  761. };
  762. inline constexpr __is_permutation_fn is_permutation{};
  763. template<typename _Iter, typename _Out>
  764. using copy_if_result = in_out_result<_Iter, _Out>;
  765. struct __copy_if_fn
  766. {
  767. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  768. weakly_incrementable _Out, typename _Proj = identity,
  769. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  770. requires indirectly_copyable<_Iter, _Out>
  771. constexpr copy_if_result<_Iter, _Out>
  772. operator()(_Iter __first, _Sent __last, _Out __result,
  773. _Pred __pred, _Proj __proj = {}) const
  774. {
  775. for (; __first != __last; ++__first)
  776. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  777. {
  778. *__result = *__first;
  779. ++__result;
  780. }
  781. return {std::move(__first), std::move(__result)};
  782. }
  783. template<input_range _Range, weakly_incrementable _Out,
  784. typename _Proj = identity,
  785. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  786. _Pred>
  787. requires indirectly_copyable<iterator_t<_Range>, _Out>
  788. constexpr copy_if_result<borrowed_iterator_t<_Range>, _Out>
  789. operator()(_Range&& __r, _Out __result,
  790. _Pred __pred, _Proj __proj = {}) const
  791. {
  792. return (*this)(ranges::begin(__r), ranges::end(__r),
  793. std::move(__result),
  794. std::move(__pred), std::move(__proj));
  795. }
  796. };
  797. inline constexpr __copy_if_fn copy_if{};
  798. template<typename _Iter1, typename _Iter2>
  799. using swap_ranges_result = in_in_result<_Iter1, _Iter2>;
  800. struct __swap_ranges_fn
  801. {
  802. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  803. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2>
  804. requires indirectly_swappable<_Iter1, _Iter2>
  805. constexpr swap_ranges_result<_Iter1, _Iter2>
  806. operator()(_Iter1 __first1, _Sent1 __last1,
  807. _Iter2 __first2, _Sent2 __last2) const
  808. {
  809. for (; __first1 != __last1 && __first2 != __last2;
  810. ++__first1, (void)++__first2)
  811. ranges::iter_swap(__first1, __first2);
  812. return {std::move(__first1), std::move(__first2)};
  813. }
  814. template<input_range _Range1, input_range _Range2>
  815. requires indirectly_swappable<iterator_t<_Range1>, iterator_t<_Range2>>
  816. constexpr swap_ranges_result<borrowed_iterator_t<_Range1>,
  817. borrowed_iterator_t<_Range2>>
  818. operator()(_Range1&& __r1, _Range2&& __r2) const
  819. {
  820. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  821. ranges::begin(__r2), ranges::end(__r2));
  822. }
  823. };
  824. inline constexpr __swap_ranges_fn swap_ranges{};
  825. template<typename _Iter, typename _Out>
  826. using unary_transform_result = in_out_result<_Iter, _Out>;
  827. template<typename _Iter1, typename _Iter2, typename _Out>
  828. struct in_in_out_result
  829. {
  830. [[no_unique_address]] _Iter1 in1;
  831. [[no_unique_address]] _Iter2 in2;
  832. [[no_unique_address]] _Out out;
  833. template<typename _IIter1, typename _IIter2, typename _OOut>
  834. requires convertible_to<const _Iter1&, _IIter1>
  835. && convertible_to<const _Iter2&, _IIter2>
  836. && convertible_to<const _Out&, _OOut>
  837. constexpr
  838. operator in_in_out_result<_IIter1, _IIter2, _OOut>() const &
  839. { return {in1, in2, out}; }
  840. template<typename _IIter1, typename _IIter2, typename _OOut>
  841. requires convertible_to<_Iter1, _IIter1>
  842. && convertible_to<_Iter2, _IIter2>
  843. && convertible_to<_Out, _OOut>
  844. constexpr
  845. operator in_in_out_result<_IIter1, _IIter2, _OOut>() &&
  846. { return {std::move(in1), std::move(in2), std::move(out)}; }
  847. };
  848. template<typename _Iter1, typename _Iter2, typename _Out>
  849. using binary_transform_result = in_in_out_result<_Iter1, _Iter2, _Out>;
  850. struct __transform_fn
  851. {
  852. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  853. weakly_incrementable _Out,
  854. copy_constructible _Fp, typename _Proj = identity>
  855. requires indirectly_writable<_Out,
  856. indirect_result_t<_Fp&,
  857. projected<_Iter, _Proj>>>
  858. constexpr unary_transform_result<_Iter, _Out>
  859. operator()(_Iter __first1, _Sent __last1, _Out __result,
  860. _Fp __op, _Proj __proj = {}) const
  861. {
  862. for (; __first1 != __last1; ++__first1, (void)++__result)
  863. *__result = std::__invoke(__op, std::__invoke(__proj, *__first1));
  864. return {std::move(__first1), std::move(__result)};
  865. }
  866. template<input_range _Range, weakly_incrementable _Out,
  867. copy_constructible _Fp, typename _Proj = identity>
  868. requires indirectly_writable<_Out,
  869. indirect_result_t<_Fp&,
  870. projected<iterator_t<_Range>, _Proj>>>
  871. constexpr unary_transform_result<borrowed_iterator_t<_Range>, _Out>
  872. operator()(_Range&& __r, _Out __result, _Fp __op, _Proj __proj = {}) const
  873. {
  874. return (*this)(ranges::begin(__r), ranges::end(__r),
  875. std::move(__result),
  876. std::move(__op), std::move(__proj));
  877. }
  878. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  879. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  880. weakly_incrementable _Out, copy_constructible _Fp,
  881. typename _Proj1 = identity, typename _Proj2 = identity>
  882. requires indirectly_writable<_Out,
  883. indirect_result_t<_Fp&,
  884. projected<_Iter1, _Proj1>,
  885. projected<_Iter2, _Proj2>>>
  886. constexpr binary_transform_result<_Iter1, _Iter2, _Out>
  887. operator()(_Iter1 __first1, _Sent1 __last1,
  888. _Iter2 __first2, _Sent2 __last2,
  889. _Out __result, _Fp __binary_op,
  890. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  891. {
  892. for (; __first1 != __last1 && __first2 != __last2;
  893. ++__first1, (void)++__first2, ++__result)
  894. *__result = std::__invoke(__binary_op,
  895. std::__invoke(__proj1, *__first1),
  896. std::__invoke(__proj2, *__first2));
  897. return {std::move(__first1), std::move(__first2), std::move(__result)};
  898. }
  899. template<input_range _Range1, input_range _Range2,
  900. weakly_incrementable _Out, copy_constructible _Fp,
  901. typename _Proj1 = identity, typename _Proj2 = identity>
  902. requires indirectly_writable<_Out,
  903. indirect_result_t<_Fp&,
  904. projected<iterator_t<_Range1>, _Proj1>,
  905. projected<iterator_t<_Range2>, _Proj2>>>
  906. constexpr binary_transform_result<borrowed_iterator_t<_Range1>,
  907. borrowed_iterator_t<_Range2>, _Out>
  908. operator()(_Range1&& __r1, _Range2&& __r2, _Out __result, _Fp __binary_op,
  909. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  910. {
  911. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  912. ranges::begin(__r2), ranges::end(__r2),
  913. std::move(__result), std::move(__binary_op),
  914. std::move(__proj1), std::move(__proj2));
  915. }
  916. };
  917. inline constexpr __transform_fn transform{};
  918. struct __replace_fn
  919. {
  920. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  921. typename _Tp1, typename _Tp2, typename _Proj = identity>
  922. requires indirectly_writable<_Iter, const _Tp2&>
  923. && indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>,
  924. const _Tp1*>
  925. constexpr _Iter
  926. operator()(_Iter __first, _Sent __last,
  927. const _Tp1& __old_value, const _Tp2& __new_value,
  928. _Proj __proj = {}) const
  929. {
  930. for (; __first != __last; ++__first)
  931. if (std::__invoke(__proj, *__first) == __old_value)
  932. *__first = __new_value;
  933. return __first;
  934. }
  935. template<input_range _Range,
  936. typename _Tp1, typename _Tp2, typename _Proj = identity>
  937. requires indirectly_writable<iterator_t<_Range>, const _Tp2&>
  938. && indirect_binary_predicate<ranges::equal_to,
  939. projected<iterator_t<_Range>, _Proj>,
  940. const _Tp1*>
  941. constexpr borrowed_iterator_t<_Range>
  942. operator()(_Range&& __r,
  943. const _Tp1& __old_value, const _Tp2& __new_value,
  944. _Proj __proj = {}) const
  945. {
  946. return (*this)(ranges::begin(__r), ranges::end(__r),
  947. __old_value, __new_value, std::move(__proj));
  948. }
  949. };
  950. inline constexpr __replace_fn replace{};
  951. struct __replace_if_fn
  952. {
  953. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  954. typename _Tp, typename _Proj = identity,
  955. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  956. requires indirectly_writable<_Iter, const _Tp&>
  957. constexpr _Iter
  958. operator()(_Iter __first, _Sent __last,
  959. _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
  960. {
  961. for (; __first != __last; ++__first)
  962. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  963. *__first = __new_value;
  964. return std::move(__first);
  965. }
  966. template<input_range _Range, typename _Tp, typename _Proj = identity,
  967. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  968. _Pred>
  969. requires indirectly_writable<iterator_t<_Range>, const _Tp&>
  970. constexpr borrowed_iterator_t<_Range>
  971. operator()(_Range&& __r,
  972. _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
  973. {
  974. return (*this)(ranges::begin(__r), ranges::end(__r),
  975. std::move(__pred), __new_value, std::move(__proj));
  976. }
  977. };
  978. inline constexpr __replace_if_fn replace_if{};
  979. template<typename _Iter, typename _Out>
  980. using replace_copy_result = in_out_result<_Iter, _Out>;
  981. struct __replace_copy_fn
  982. {
  983. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  984. typename _Tp1, typename _Tp2, output_iterator<const _Tp2&> _Out,
  985. typename _Proj = identity>
  986. requires indirectly_copyable<_Iter, _Out>
  987. && indirect_binary_predicate<ranges::equal_to,
  988. projected<_Iter, _Proj>, const _Tp1*>
  989. constexpr replace_copy_result<_Iter, _Out>
  990. operator()(_Iter __first, _Sent __last, _Out __result,
  991. const _Tp1& __old_value, const _Tp2& __new_value,
  992. _Proj __proj = {}) const
  993. {
  994. for (; __first != __last; ++__first, (void)++__result)
  995. if (std::__invoke(__proj, *__first) == __old_value)
  996. *__result = __new_value;
  997. else
  998. *__result = *__first;
  999. return {std::move(__first), std::move(__result)};
  1000. }
  1001. template<input_range _Range, typename _Tp1, typename _Tp2,
  1002. output_iterator<const _Tp2&> _Out, typename _Proj = identity>
  1003. requires indirectly_copyable<iterator_t<_Range>, _Out>
  1004. && indirect_binary_predicate<ranges::equal_to,
  1005. projected<iterator_t<_Range>, _Proj>,
  1006. const _Tp1*>
  1007. constexpr replace_copy_result<borrowed_iterator_t<_Range>, _Out>
  1008. operator()(_Range&& __r, _Out __result,
  1009. const _Tp1& __old_value, const _Tp2& __new_value,
  1010. _Proj __proj = {}) const
  1011. {
  1012. return (*this)(ranges::begin(__r), ranges::end(__r),
  1013. std::move(__result), __old_value,
  1014. __new_value, std::move(__proj));
  1015. }
  1016. };
  1017. inline constexpr __replace_copy_fn replace_copy{};
  1018. template<typename _Iter, typename _Out>
  1019. using replace_copy_if_result = in_out_result<_Iter, _Out>;
  1020. struct __replace_copy_if_fn
  1021. {
  1022. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  1023. typename _Tp, output_iterator<const _Tp&> _Out,
  1024. typename _Proj = identity,
  1025. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  1026. requires indirectly_copyable<_Iter, _Out>
  1027. constexpr replace_copy_if_result<_Iter, _Out>
  1028. operator()(_Iter __first, _Sent __last, _Out __result,
  1029. _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
  1030. {
  1031. for (; __first != __last; ++__first, (void)++__result)
  1032. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  1033. *__result = __new_value;
  1034. else
  1035. *__result = *__first;
  1036. return {std::move(__first), std::move(__result)};
  1037. }
  1038. template<input_range _Range,
  1039. typename _Tp, output_iterator<const _Tp&> _Out,
  1040. typename _Proj = identity,
  1041. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  1042. _Pred>
  1043. requires indirectly_copyable<iterator_t<_Range>, _Out>
  1044. constexpr replace_copy_if_result<borrowed_iterator_t<_Range>, _Out>
  1045. operator()(_Range&& __r, _Out __result,
  1046. _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
  1047. {
  1048. return (*this)(ranges::begin(__r), ranges::end(__r),
  1049. std::move(__result), std::move(__pred),
  1050. __new_value, std::move(__proj));
  1051. }
  1052. };
  1053. inline constexpr __replace_copy_if_fn replace_copy_if{};
  1054. struct __generate_n_fn
  1055. {
  1056. template<input_or_output_iterator _Out, copy_constructible _Fp>
  1057. requires invocable<_Fp&>
  1058. && indirectly_writable<_Out, invoke_result_t<_Fp&>>
  1059. constexpr _Out
  1060. operator()(_Out __first, iter_difference_t<_Out> __n, _Fp __gen) const
  1061. {
  1062. for (; __n > 0; --__n, (void)++__first)
  1063. *__first = std::__invoke(__gen);
  1064. return __first;
  1065. }
  1066. };
  1067. inline constexpr __generate_n_fn generate_n{};
  1068. struct __generate_fn
  1069. {
  1070. template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent,
  1071. copy_constructible _Fp>
  1072. requires invocable<_Fp&>
  1073. && indirectly_writable<_Out, invoke_result_t<_Fp&>>
  1074. constexpr _Out
  1075. operator()(_Out __first, _Sent __last, _Fp __gen) const
  1076. {
  1077. for (; __first != __last; ++__first)
  1078. *__first = std::__invoke(__gen);
  1079. return __first;
  1080. }
  1081. template<typename _Range, copy_constructible _Fp>
  1082. requires invocable<_Fp&> && output_range<_Range, invoke_result_t<_Fp&>>
  1083. constexpr borrowed_iterator_t<_Range>
  1084. operator()(_Range&& __r, _Fp __gen) const
  1085. {
  1086. return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__gen));
  1087. }
  1088. };
  1089. inline constexpr __generate_fn generate{};
  1090. struct __remove_if_fn
  1091. {
  1092. template<permutable _Iter, sentinel_for<_Iter> _Sent,
  1093. typename _Proj = identity,
  1094. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  1095. constexpr subrange<_Iter>
  1096. operator()(_Iter __first, _Sent __last,
  1097. _Pred __pred, _Proj __proj = {}) const
  1098. {
  1099. __first = ranges::find_if(__first, __last, __pred, __proj);
  1100. if (__first == __last)
  1101. return {__first, __first};
  1102. auto __result = __first;
  1103. ++__first;
  1104. for (; __first != __last; ++__first)
  1105. if (!std::__invoke(__pred, std::__invoke(__proj, *__first)))
  1106. {
  1107. *__result = std::move(*__first);
  1108. ++__result;
  1109. }
  1110. return {__result, __first};
  1111. }
  1112. template<forward_range _Range, typename _Proj = identity,
  1113. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  1114. _Pred>
  1115. requires permutable<iterator_t<_Range>>
  1116. constexpr borrowed_subrange_t<_Range>
  1117. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  1118. {
  1119. return (*this)(ranges::begin(__r), ranges::end(__r),
  1120. std::move(__pred), std::move(__proj));
  1121. }
  1122. };
  1123. inline constexpr __remove_if_fn remove_if{};
  1124. struct __remove_fn
  1125. {
  1126. template<permutable _Iter, sentinel_for<_Iter> _Sent,
  1127. typename _Tp, typename _Proj = identity>
  1128. requires indirect_binary_predicate<ranges::equal_to,
  1129. projected<_Iter, _Proj>,
  1130. const _Tp*>
  1131. constexpr subrange<_Iter>
  1132. operator()(_Iter __first, _Sent __last,
  1133. const _Tp& __value, _Proj __proj = {}) const
  1134. {
  1135. auto __pred = [&] (auto&& __arg) {
  1136. return std::forward<decltype(__arg)>(__arg) == __value;
  1137. };
  1138. return ranges::remove_if(__first, __last,
  1139. std::move(__pred), std::move(__proj));
  1140. }
  1141. template<forward_range _Range, typename _Tp, typename _Proj = identity>
  1142. requires permutable<iterator_t<_Range>>
  1143. && indirect_binary_predicate<ranges::equal_to,
  1144. projected<iterator_t<_Range>, _Proj>,
  1145. const _Tp*>
  1146. constexpr borrowed_subrange_t<_Range>
  1147. operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
  1148. {
  1149. return (*this)(ranges::begin(__r), ranges::end(__r),
  1150. __value, std::move(__proj));
  1151. }
  1152. };
  1153. inline constexpr __remove_fn remove{};
  1154. template<typename _Iter, typename _Out>
  1155. using remove_copy_if_result = in_out_result<_Iter, _Out>;
  1156. struct __remove_copy_if_fn
  1157. {
  1158. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  1159. weakly_incrementable _Out, typename _Proj = identity,
  1160. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  1161. requires indirectly_copyable<_Iter, _Out>
  1162. constexpr remove_copy_if_result<_Iter, _Out>
  1163. operator()(_Iter __first, _Sent __last, _Out __result,
  1164. _Pred __pred, _Proj __proj = {}) const
  1165. {
  1166. for (; __first != __last; ++__first)
  1167. if (!std::__invoke(__pred, std::__invoke(__proj, *__first)))
  1168. {
  1169. *__result = *__first;
  1170. ++__result;
  1171. }
  1172. return {std::move(__first), std::move(__result)};
  1173. }
  1174. template<input_range _Range, weakly_incrementable _Out,
  1175. typename _Proj = identity,
  1176. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  1177. _Pred>
  1178. requires indirectly_copyable<iterator_t<_Range>, _Out>
  1179. constexpr remove_copy_if_result<borrowed_iterator_t<_Range>, _Out>
  1180. operator()(_Range&& __r, _Out __result,
  1181. _Pred __pred, _Proj __proj = {}) const
  1182. {
  1183. return (*this)(ranges::begin(__r), ranges::end(__r),
  1184. std::move(__result),
  1185. std::move(__pred), std::move(__proj));
  1186. }
  1187. };
  1188. inline constexpr __remove_copy_if_fn remove_copy_if{};
  1189. template<typename _Iter, typename _Out>
  1190. using remove_copy_result = in_out_result<_Iter, _Out>;
  1191. struct __remove_copy_fn
  1192. {
  1193. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  1194. weakly_incrementable _Out, typename _Tp, typename _Proj = identity>
  1195. requires indirectly_copyable<_Iter, _Out>
  1196. && indirect_binary_predicate<ranges::equal_to,
  1197. projected<_Iter, _Proj>,
  1198. const _Tp*>
  1199. constexpr remove_copy_result<_Iter, _Out>
  1200. operator()(_Iter __first, _Sent __last, _Out __result,
  1201. const _Tp& __value, _Proj __proj = {}) const
  1202. {
  1203. for (; __first != __last; ++__first)
  1204. if (!(std::__invoke(__proj, *__first) == __value))
  1205. {
  1206. *__result = *__first;
  1207. ++__result;
  1208. }
  1209. return {std::move(__first), std::move(__result)};
  1210. }
  1211. template<input_range _Range, weakly_incrementable _Out,
  1212. typename _Tp, typename _Proj = identity>
  1213. requires indirectly_copyable<iterator_t<_Range>, _Out>
  1214. && indirect_binary_predicate<ranges::equal_to,
  1215. projected<iterator_t<_Range>, _Proj>,
  1216. const _Tp*>
  1217. constexpr remove_copy_result<borrowed_iterator_t<_Range>, _Out>
  1218. operator()(_Range&& __r, _Out __result,
  1219. const _Tp& __value, _Proj __proj = {}) const
  1220. {
  1221. return (*this)(ranges::begin(__r), ranges::end(__r),
  1222. std::move(__result), __value, std::move(__proj));
  1223. }
  1224. };
  1225. inline constexpr __remove_copy_fn remove_copy{};
  1226. struct __unique_fn
  1227. {
  1228. template<permutable _Iter, sentinel_for<_Iter> _Sent,
  1229. typename _Proj = identity,
  1230. indirect_equivalence_relation<
  1231. projected<_Iter, _Proj>> _Comp = ranges::equal_to>
  1232. constexpr subrange<_Iter>
  1233. operator()(_Iter __first, _Sent __last,
  1234. _Comp __comp = {}, _Proj __proj = {}) const
  1235. {
  1236. __first = ranges::adjacent_find(__first, __last, __comp, __proj);
  1237. if (__first == __last)
  1238. return {__first, __first};
  1239. auto __dest = __first;
  1240. ++__first;
  1241. while (++__first != __last)
  1242. if (!std::__invoke(__comp,
  1243. std::__invoke(__proj, *__dest),
  1244. std::__invoke(__proj, *__first)))
  1245. *++__dest = std::move(*__first);
  1246. return {++__dest, __first};
  1247. }
  1248. template<forward_range _Range, typename _Proj = identity,
  1249. indirect_equivalence_relation<
  1250. projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
  1251. requires permutable<iterator_t<_Range>>
  1252. constexpr borrowed_subrange_t<_Range>
  1253. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1254. {
  1255. return (*this)(ranges::begin(__r), ranges::end(__r),
  1256. std::move(__comp), std::move(__proj));
  1257. }
  1258. };
  1259. inline constexpr __unique_fn unique{};
  1260. template<typename _Iter, typename _Out>
  1261. using unique_copy_result = in_out_result<_Iter, _Out>;
  1262. struct __unique_copy_fn
  1263. {
  1264. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  1265. weakly_incrementable _Out, typename _Proj = identity,
  1266. indirect_equivalence_relation<
  1267. projected<_Iter, _Proj>> _Comp = ranges::equal_to>
  1268. requires indirectly_copyable<_Iter, _Out>
  1269. && (forward_iterator<_Iter>
  1270. || (input_iterator<_Out>
  1271. && same_as<iter_value_t<_Iter>, iter_value_t<_Out>>)
  1272. || indirectly_copyable_storable<_Iter, _Out>)
  1273. constexpr unique_copy_result<_Iter, _Out>
  1274. operator()(_Iter __first, _Sent __last, _Out __result,
  1275. _Comp __comp = {}, _Proj __proj = {}) const
  1276. {
  1277. if (__first == __last)
  1278. return {std::move(__first), std::move(__result)};
  1279. // TODO: perform a closer comparison with reference implementations
  1280. if constexpr (forward_iterator<_Iter>)
  1281. {
  1282. auto __next = __first;
  1283. *__result = *__next;
  1284. while (++__next != __last)
  1285. if (!std::__invoke(__comp,
  1286. std::__invoke(__proj, *__first),
  1287. std::__invoke(__proj, *__next)))
  1288. {
  1289. __first = __next;
  1290. *++__result = *__first;
  1291. }
  1292. return {__next, std::move(++__result)};
  1293. }
  1294. else if constexpr (input_iterator<_Out>
  1295. && same_as<iter_value_t<_Iter>, iter_value_t<_Out>>)
  1296. {
  1297. *__result = *__first;
  1298. while (++__first != __last)
  1299. if (!std::__invoke(__comp,
  1300. std::__invoke(__proj, *__result),
  1301. std::__invoke(__proj, *__first)))
  1302. *++__result = *__first;
  1303. return {std::move(__first), std::move(++__result)};
  1304. }
  1305. else // indirectly_copyable_storable<_Iter, _Out>
  1306. {
  1307. auto __value = *__first;
  1308. *__result = __value;
  1309. while (++__first != __last)
  1310. {
  1311. if (!(bool)std::__invoke(__comp,
  1312. std::__invoke(__proj, *__first),
  1313. std::__invoke(__proj, __value)))
  1314. {
  1315. __value = *__first;
  1316. *++__result = __value;
  1317. }
  1318. }
  1319. return {std::move(__first), std::move(++__result)};
  1320. }
  1321. }
  1322. template<input_range _Range,
  1323. weakly_incrementable _Out, typename _Proj = identity,
  1324. indirect_equivalence_relation<
  1325. projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
  1326. requires indirectly_copyable<iterator_t<_Range>, _Out>
  1327. && (forward_iterator<iterator_t<_Range>>
  1328. || (input_iterator<_Out>
  1329. && same_as<range_value_t<_Range>, iter_value_t<_Out>>)
  1330. || indirectly_copyable_storable<iterator_t<_Range>, _Out>)
  1331. constexpr unique_copy_result<borrowed_iterator_t<_Range>, _Out>
  1332. operator()(_Range&& __r, _Out __result,
  1333. _Comp __comp = {}, _Proj __proj = {}) const
  1334. {
  1335. return (*this)(ranges::begin(__r), ranges::end(__r),
  1336. std::move(__result),
  1337. std::move(__comp), std::move(__proj));
  1338. }
  1339. };
  1340. inline constexpr __unique_copy_fn unique_copy{};
  1341. struct __reverse_fn
  1342. {
  1343. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent>
  1344. requires permutable<_Iter>
  1345. constexpr _Iter
  1346. operator()(_Iter __first, _Sent __last) const
  1347. {
  1348. auto __i = ranges::next(__first, __last);
  1349. auto __tail = __i;
  1350. if constexpr (random_access_iterator<_Iter>)
  1351. {
  1352. if (__first != __last)
  1353. {
  1354. --__tail;
  1355. while (__first < __tail)
  1356. {
  1357. ranges::iter_swap(__first, __tail);
  1358. ++__first;
  1359. --__tail;
  1360. }
  1361. }
  1362. return __i;
  1363. }
  1364. else
  1365. {
  1366. for (;;)
  1367. if (__first == __tail || __first == --__tail)
  1368. break;
  1369. else
  1370. {
  1371. ranges::iter_swap(__first, __tail);
  1372. ++__first;
  1373. }
  1374. return __i;
  1375. }
  1376. }
  1377. template<bidirectional_range _Range>
  1378. requires permutable<iterator_t<_Range>>
  1379. constexpr borrowed_iterator_t<_Range>
  1380. operator()(_Range&& __r) const
  1381. {
  1382. return (*this)(ranges::begin(__r), ranges::end(__r));
  1383. }
  1384. };
  1385. inline constexpr __reverse_fn reverse{};
  1386. template<typename _Iter, typename _Out>
  1387. using reverse_copy_result = in_out_result<_Iter, _Out>;
  1388. struct __reverse_copy_fn
  1389. {
  1390. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  1391. weakly_incrementable _Out>
  1392. requires indirectly_copyable<_Iter, _Out>
  1393. constexpr reverse_copy_result<_Iter, _Out>
  1394. operator()(_Iter __first, _Sent __last, _Out __result) const
  1395. {
  1396. auto __i = ranges::next(__first, __last);
  1397. auto __tail = __i;
  1398. while (__first != __tail)
  1399. {
  1400. --__tail;
  1401. *__result = *__tail;
  1402. ++__result;
  1403. }
  1404. return {__i, __result};
  1405. }
  1406. template<bidirectional_range _Range, weakly_incrementable _Out>
  1407. requires indirectly_copyable<iterator_t<_Range>, _Out>
  1408. constexpr reverse_copy_result<borrowed_iterator_t<_Range>, _Out>
  1409. operator()(_Range&& __r, _Out __result) const
  1410. {
  1411. return (*this)(ranges::begin(__r), ranges::end(__r),
  1412. std::move(__result));
  1413. }
  1414. };
  1415. inline constexpr __reverse_copy_fn reverse_copy{};
  1416. struct __rotate_fn
  1417. {
  1418. template<permutable _Iter, sentinel_for<_Iter> _Sent>
  1419. constexpr subrange<_Iter>
  1420. operator()(_Iter __first, _Iter __middle, _Sent __last) const
  1421. {
  1422. auto __lasti = ranges::next(__first, __last);
  1423. if (__first == __middle)
  1424. return {__lasti, __lasti};
  1425. if (__last == __middle)
  1426. return {std::move(__first), std::move(__lasti)};
  1427. if constexpr (random_access_iterator<_Iter>)
  1428. {
  1429. auto __n = __lasti - __first;
  1430. auto __k = __middle - __first;
  1431. if (__k == __n - __k)
  1432. {
  1433. ranges::swap_ranges(__first, __middle, __middle, __middle + __k);
  1434. return {std::move(__middle), std::move(__lasti)};
  1435. }
  1436. auto __p = __first;
  1437. auto __ret = __first + (__lasti - __middle);
  1438. for (;;)
  1439. {
  1440. if (__k < __n - __k)
  1441. {
  1442. // TODO: is_pod is deprecated, but this condition is
  1443. // consistent with the STL implementation.
  1444. if constexpr (__is_pod(iter_value_t<_Iter>))
  1445. if (__k == 1)
  1446. {
  1447. auto __t = std::move(*__p);
  1448. ranges::move(__p + 1, __p + __n, __p);
  1449. *(__p + __n - 1) = std::move(__t);
  1450. return {std::move(__ret), std::move(__lasti)};
  1451. }
  1452. auto __q = __p + __k;
  1453. for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
  1454. {
  1455. ranges::iter_swap(__p, __q);
  1456. ++__p;
  1457. ++__q;
  1458. }
  1459. __n %= __k;
  1460. if (__n == 0)
  1461. return {std::move(__ret), std::move(__lasti)};
  1462. ranges::swap(__n, __k);
  1463. __k = __n - __k;
  1464. }
  1465. else
  1466. {
  1467. __k = __n - __k;
  1468. // TODO: is_pod is deprecated, but this condition is
  1469. // consistent with the STL implementation.
  1470. if constexpr (__is_pod(iter_value_t<_Iter>))
  1471. if (__k == 1)
  1472. {
  1473. auto __t = std::move(*(__p + __n - 1));
  1474. ranges::move_backward(__p, __p + __n - 1, __p + __n);
  1475. *__p = std::move(__t);
  1476. return {std::move(__ret), std::move(__lasti)};
  1477. }
  1478. auto __q = __p + __n;
  1479. __p = __q - __k;
  1480. for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
  1481. {
  1482. --__p;
  1483. --__q;
  1484. ranges::iter_swap(__p, __q);
  1485. }
  1486. __n %= __k;
  1487. if (__n == 0)
  1488. return {std::move(__ret), std::move(__lasti)};
  1489. std::swap(__n, __k);
  1490. }
  1491. }
  1492. }
  1493. else if constexpr (bidirectional_iterator<_Iter>)
  1494. {
  1495. auto __tail = __lasti;
  1496. ranges::reverse(__first, __middle);
  1497. ranges::reverse(__middle, __tail);
  1498. while (__first != __middle && __middle != __tail)
  1499. {
  1500. ranges::iter_swap(__first, --__tail);
  1501. ++__first;
  1502. }
  1503. if (__first == __middle)
  1504. {
  1505. ranges::reverse(__middle, __tail);
  1506. return {std::move(__tail), std::move(__lasti)};
  1507. }
  1508. else
  1509. {
  1510. ranges::reverse(__first, __middle);
  1511. return {std::move(__first), std::move(__lasti)};
  1512. }
  1513. }
  1514. else
  1515. {
  1516. auto __first2 = __middle;
  1517. do
  1518. {
  1519. ranges::iter_swap(__first, __first2);
  1520. ++__first;
  1521. ++__first2;
  1522. if (__first == __middle)
  1523. __middle = __first2;
  1524. } while (__first2 != __last);
  1525. auto __ret = __first;
  1526. __first2 = __middle;
  1527. while (__first2 != __last)
  1528. {
  1529. ranges::iter_swap(__first, __first2);
  1530. ++__first;
  1531. ++__first2;
  1532. if (__first == __middle)
  1533. __middle = __first2;
  1534. else if (__first2 == __last)
  1535. __first2 = __middle;
  1536. }
  1537. return {std::move(__ret), std::move(__lasti)};
  1538. }
  1539. }
  1540. template<forward_range _Range>
  1541. requires permutable<iterator_t<_Range>>
  1542. constexpr borrowed_subrange_t<_Range>
  1543. operator()(_Range&& __r, iterator_t<_Range> __middle) const
  1544. {
  1545. return (*this)(ranges::begin(__r), std::move(__middle),
  1546. ranges::end(__r));
  1547. }
  1548. };
  1549. inline constexpr __rotate_fn rotate{};
  1550. template<typename _Iter, typename _Out>
  1551. using rotate_copy_result = in_out_result<_Iter, _Out>;
  1552. struct __rotate_copy_fn
  1553. {
  1554. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  1555. weakly_incrementable _Out>
  1556. requires indirectly_copyable<_Iter, _Out>
  1557. constexpr rotate_copy_result<_Iter, _Out>
  1558. operator()(_Iter __first, _Iter __middle, _Sent __last,
  1559. _Out __result) const
  1560. {
  1561. auto __copy1 = ranges::copy(__middle,
  1562. std::move(__last),
  1563. std::move(__result));
  1564. auto __copy2 = ranges::copy(std::move(__first),
  1565. std::move(__middle),
  1566. std::move(__copy1.out));
  1567. return { std::move(__copy1.in), std::move(__copy2.out) };
  1568. }
  1569. template<forward_range _Range, weakly_incrementable _Out>
  1570. requires indirectly_copyable<iterator_t<_Range>, _Out>
  1571. constexpr rotate_copy_result<borrowed_iterator_t<_Range>, _Out>
  1572. operator()(_Range&& __r, iterator_t<_Range> __middle, _Out __result) const
  1573. {
  1574. return (*this)(ranges::begin(__r), std::move(__middle),
  1575. ranges::end(__r), std::move(__result));
  1576. }
  1577. };
  1578. inline constexpr __rotate_copy_fn rotate_copy{};
  1579. struct __sample_fn
  1580. {
  1581. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  1582. weakly_incrementable _Out, typename _Gen>
  1583. requires (forward_iterator<_Iter> || random_access_iterator<_Out>)
  1584. && indirectly_copyable<_Iter, _Out>
  1585. && uniform_random_bit_generator<remove_reference_t<_Gen>>
  1586. _Out
  1587. operator()(_Iter __first, _Sent __last, _Out __out,
  1588. iter_difference_t<_Iter> __n, _Gen&& __g) const
  1589. {
  1590. if constexpr (forward_iterator<_Iter>)
  1591. {
  1592. // FIXME: Forwarding to std::sample here requires computing __lasti
  1593. // which may take linear time.
  1594. auto __lasti = ranges::next(__first, __last);
  1595. return std::sample(std::move(__first), std::move(__lasti),
  1596. std::move(__out), __n, std::forward<_Gen>(__g));
  1597. }
  1598. else
  1599. {
  1600. using __distrib_type
  1601. = uniform_int_distribution<iter_difference_t<_Iter>>;
  1602. using __param_type = typename __distrib_type::param_type;
  1603. __distrib_type __d{};
  1604. iter_difference_t<_Iter> __sample_sz = 0;
  1605. while (__first != __last && __sample_sz != __n)
  1606. {
  1607. __out[__sample_sz++] = *__first;
  1608. ++__first;
  1609. }
  1610. for (auto __pop_sz = __sample_sz; __first != __last;
  1611. ++__first, (void) ++__pop_sz)
  1612. {
  1613. const auto __k = __d(__g, __param_type{0, __pop_sz});
  1614. if (__k < __n)
  1615. __out[__k] = *__first;
  1616. }
  1617. return __out + __sample_sz;
  1618. }
  1619. }
  1620. template<input_range _Range, weakly_incrementable _Out, typename _Gen>
  1621. requires (forward_range<_Range> || random_access_iterator<_Out>)
  1622. && indirectly_copyable<iterator_t<_Range>, _Out>
  1623. && uniform_random_bit_generator<remove_reference_t<_Gen>>
  1624. _Out
  1625. operator()(_Range&& __r, _Out __out,
  1626. range_difference_t<_Range> __n, _Gen&& __g) const
  1627. {
  1628. return (*this)(ranges::begin(__r), ranges::end(__r),
  1629. std::move(__out), __n,
  1630. std::forward<_Gen>(__g));
  1631. }
  1632. };
  1633. inline constexpr __sample_fn sample{};
  1634. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  1635. struct __shuffle_fn
  1636. {
  1637. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1638. typename _Gen>
  1639. requires permutable<_Iter>
  1640. && uniform_random_bit_generator<remove_reference_t<_Gen>>
  1641. _Iter
  1642. operator()(_Iter __first, _Sent __last, _Gen&& __g) const
  1643. {
  1644. auto __lasti = ranges::next(__first, __last);
  1645. std::shuffle(std::move(__first), __lasti, std::forward<_Gen>(__g));
  1646. return __lasti;
  1647. }
  1648. template<random_access_range _Range, typename _Gen>
  1649. requires permutable<iterator_t<_Range>>
  1650. && uniform_random_bit_generator<remove_reference_t<_Gen>>
  1651. borrowed_iterator_t<_Range>
  1652. operator()(_Range&& __r, _Gen&& __g) const
  1653. {
  1654. return (*this)(ranges::begin(__r), ranges::end(__r),
  1655. std::forward<_Gen>(__g));
  1656. }
  1657. };
  1658. inline constexpr __shuffle_fn shuffle{};
  1659. #endif
  1660. struct __push_heap_fn
  1661. {
  1662. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1663. typename _Comp = ranges::less, typename _Proj = identity>
  1664. requires sortable<_Iter, _Comp, _Proj>
  1665. constexpr _Iter
  1666. operator()(_Iter __first, _Sent __last,
  1667. _Comp __comp = {}, _Proj __proj = {}) const
  1668. {
  1669. auto __lasti = ranges::next(__first, __last);
  1670. std::push_heap(__first, __lasti,
  1671. __detail::__make_comp_proj(__comp, __proj));
  1672. return __lasti;
  1673. }
  1674. template<random_access_range _Range,
  1675. typename _Comp = ranges::less, typename _Proj = identity>
  1676. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1677. constexpr borrowed_iterator_t<_Range>
  1678. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1679. {
  1680. return (*this)(ranges::begin(__r), ranges::end(__r),
  1681. std::move(__comp), std::move(__proj));
  1682. }
  1683. };
  1684. inline constexpr __push_heap_fn push_heap{};
  1685. struct __pop_heap_fn
  1686. {
  1687. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1688. typename _Comp = ranges::less, typename _Proj = identity>
  1689. requires sortable<_Iter, _Comp, _Proj>
  1690. constexpr _Iter
  1691. operator()(_Iter __first, _Sent __last,
  1692. _Comp __comp = {}, _Proj __proj = {}) const
  1693. {
  1694. auto __lasti = ranges::next(__first, __last);
  1695. std::pop_heap(__first, __lasti,
  1696. __detail::__make_comp_proj(__comp, __proj));
  1697. return __lasti;
  1698. }
  1699. template<random_access_range _Range,
  1700. typename _Comp = ranges::less, typename _Proj = identity>
  1701. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1702. constexpr borrowed_iterator_t<_Range>
  1703. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1704. {
  1705. return (*this)(ranges::begin(__r), ranges::end(__r),
  1706. std::move(__comp), std::move(__proj));
  1707. }
  1708. };
  1709. inline constexpr __pop_heap_fn pop_heap{};
  1710. struct __make_heap_fn
  1711. {
  1712. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1713. typename _Comp = ranges::less, typename _Proj = identity>
  1714. requires sortable<_Iter, _Comp, _Proj>
  1715. constexpr _Iter
  1716. operator()(_Iter __first, _Sent __last,
  1717. _Comp __comp = {}, _Proj __proj = {}) const
  1718. {
  1719. auto __lasti = ranges::next(__first, __last);
  1720. std::make_heap(__first, __lasti,
  1721. __detail::__make_comp_proj(__comp, __proj));
  1722. return __lasti;
  1723. }
  1724. template<random_access_range _Range,
  1725. typename _Comp = ranges::less, typename _Proj = identity>
  1726. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1727. constexpr borrowed_iterator_t<_Range>
  1728. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1729. {
  1730. return (*this)(ranges::begin(__r), ranges::end(__r),
  1731. std::move(__comp), std::move(__proj));
  1732. }
  1733. };
  1734. inline constexpr __make_heap_fn make_heap{};
  1735. struct __sort_heap_fn
  1736. {
  1737. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1738. typename _Comp = ranges::less, typename _Proj = identity>
  1739. requires sortable<_Iter, _Comp, _Proj>
  1740. constexpr _Iter
  1741. operator()(_Iter __first, _Sent __last,
  1742. _Comp __comp = {}, _Proj __proj = {}) const
  1743. {
  1744. auto __lasti = ranges::next(__first, __last);
  1745. std::sort_heap(__first, __lasti,
  1746. __detail::__make_comp_proj(__comp, __proj));
  1747. return __lasti;
  1748. }
  1749. template<random_access_range _Range,
  1750. typename _Comp = ranges::less, typename _Proj = identity>
  1751. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1752. constexpr borrowed_iterator_t<_Range>
  1753. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1754. {
  1755. return (*this)(ranges::begin(__r), ranges::end(__r),
  1756. std::move(__comp), std::move(__proj));
  1757. }
  1758. };
  1759. inline constexpr __sort_heap_fn sort_heap{};
  1760. struct __is_heap_until_fn
  1761. {
  1762. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1763. typename _Proj = identity,
  1764. indirect_strict_weak_order<projected<_Iter, _Proj>>
  1765. _Comp = ranges::less>
  1766. constexpr _Iter
  1767. operator()(_Iter __first, _Sent __last,
  1768. _Comp __comp = {}, _Proj __proj = {}) const
  1769. {
  1770. iter_difference_t<_Iter> __n = ranges::distance(__first, __last);
  1771. iter_difference_t<_Iter> __parent = 0, __child = 1;
  1772. for (; __child < __n; ++__child)
  1773. if (std::__invoke(__comp,
  1774. std::__invoke(__proj, *(__first + __parent)),
  1775. std::__invoke(__proj, *(__first + __child))))
  1776. return __first + __child;
  1777. else if ((__child & 1) == 0)
  1778. ++__parent;
  1779. return __first + __n;
  1780. }
  1781. template<random_access_range _Range,
  1782. typename _Proj = identity,
  1783. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  1784. _Comp = ranges::less>
  1785. constexpr borrowed_iterator_t<_Range>
  1786. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1787. {
  1788. return (*this)(ranges::begin(__r), ranges::end(__r),
  1789. std::move(__comp), std::move(__proj));
  1790. }
  1791. };
  1792. inline constexpr __is_heap_until_fn is_heap_until{};
  1793. struct __is_heap_fn
  1794. {
  1795. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1796. typename _Proj = identity,
  1797. indirect_strict_weak_order<projected<_Iter, _Proj>>
  1798. _Comp = ranges::less>
  1799. constexpr bool
  1800. operator()(_Iter __first, _Sent __last,
  1801. _Comp __comp = {}, _Proj __proj = {}) const
  1802. {
  1803. return (__last
  1804. == ranges::is_heap_until(__first, __last,
  1805. std::move(__comp),
  1806. std::move(__proj)));
  1807. }
  1808. template<random_access_range _Range,
  1809. typename _Proj = identity,
  1810. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  1811. _Comp = ranges::less>
  1812. constexpr bool
  1813. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1814. {
  1815. return (*this)(ranges::begin(__r), ranges::end(__r),
  1816. std::move(__comp), std::move(__proj));
  1817. }
  1818. };
  1819. inline constexpr __is_heap_fn is_heap{};
  1820. struct __sort_fn
  1821. {
  1822. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1823. typename _Comp = ranges::less, typename _Proj = identity>
  1824. requires sortable<_Iter, _Comp, _Proj>
  1825. constexpr _Iter
  1826. operator()(_Iter __first, _Sent __last,
  1827. _Comp __comp = {}, _Proj __proj = {}) const
  1828. {
  1829. auto __lasti = ranges::next(__first, __last);
  1830. std::sort(std::move(__first), __lasti,
  1831. __detail::__make_comp_proj(__comp, __proj));
  1832. return __lasti;
  1833. }
  1834. template<random_access_range _Range,
  1835. typename _Comp = ranges::less, typename _Proj = identity>
  1836. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1837. constexpr borrowed_iterator_t<_Range>
  1838. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1839. {
  1840. return (*this)(ranges::begin(__r), ranges::end(__r),
  1841. std::move(__comp), std::move(__proj));
  1842. }
  1843. };
  1844. inline constexpr __sort_fn sort{};
  1845. struct __stable_sort_fn
  1846. {
  1847. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1848. typename _Comp = ranges::less, typename _Proj = identity>
  1849. requires sortable<_Iter, _Comp, _Proj>
  1850. _Iter
  1851. operator()(_Iter __first, _Sent __last,
  1852. _Comp __comp = {}, _Proj __proj = {}) const
  1853. {
  1854. auto __lasti = ranges::next(__first, __last);
  1855. std::stable_sort(std::move(__first), __lasti,
  1856. __detail::__make_comp_proj(__comp, __proj));
  1857. return __lasti;
  1858. }
  1859. template<random_access_range _Range,
  1860. typename _Comp = ranges::less, typename _Proj = identity>
  1861. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1862. borrowed_iterator_t<_Range>
  1863. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1864. {
  1865. return (*this)(ranges::begin(__r), ranges::end(__r),
  1866. std::move(__comp), std::move(__proj));
  1867. }
  1868. };
  1869. inline constexpr __stable_sort_fn stable_sort{};
  1870. struct __partial_sort_fn
  1871. {
  1872. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1873. typename _Comp = ranges::less, typename _Proj = identity>
  1874. requires sortable<_Iter, _Comp, _Proj>
  1875. constexpr _Iter
  1876. operator()(_Iter __first, _Iter __middle, _Sent __last,
  1877. _Comp __comp = {}, _Proj __proj = {}) const
  1878. {
  1879. if (__first == __middle)
  1880. return ranges::next(__first, __last);
  1881. ranges::make_heap(__first, __middle, __comp, __proj);
  1882. auto __i = __middle;
  1883. for (; __i != __last; ++__i)
  1884. if (std::__invoke(__comp,
  1885. std::__invoke(__proj, *__i),
  1886. std::__invoke(__proj, *__first)))
  1887. {
  1888. ranges::pop_heap(__first, __middle, __comp, __proj);
  1889. ranges::iter_swap(__middle-1, __i);
  1890. ranges::push_heap(__first, __middle, __comp, __proj);
  1891. }
  1892. ranges::sort_heap(__first, __middle, __comp, __proj);
  1893. return __i;
  1894. }
  1895. template<random_access_range _Range,
  1896. typename _Comp = ranges::less, typename _Proj = identity>
  1897. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1898. constexpr borrowed_iterator_t<_Range>
  1899. operator()(_Range&& __r, iterator_t<_Range> __middle,
  1900. _Comp __comp = {}, _Proj __proj = {}) const
  1901. {
  1902. return (*this)(ranges::begin(__r), std::move(__middle),
  1903. ranges::end(__r),
  1904. std::move(__comp), std::move(__proj));
  1905. }
  1906. };
  1907. inline constexpr __partial_sort_fn partial_sort{};
  1908. template<typename _Iter, typename _Out>
  1909. using partial_sort_copy_result = in_out_result<_Iter, _Out>;
  1910. struct __partial_sort_copy_fn
  1911. {
  1912. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  1913. random_access_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  1914. typename _Comp = ranges::less,
  1915. typename _Proj1 = identity, typename _Proj2 = identity>
  1916. requires indirectly_copyable<_Iter1, _Iter2>
  1917. && sortable<_Iter2, _Comp, _Proj2>
  1918. && indirect_strict_weak_order<_Comp,
  1919. projected<_Iter1, _Proj1>,
  1920. projected<_Iter2, _Proj2>>
  1921. constexpr partial_sort_copy_result<_Iter1, _Iter2>
  1922. operator()(_Iter1 __first, _Sent1 __last,
  1923. _Iter2 __result_first, _Sent2 __result_last,
  1924. _Comp __comp = {},
  1925. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  1926. {
  1927. if (__result_first == __result_last)
  1928. {
  1929. // TODO: Eliminating the variable __lasti triggers an ICE.
  1930. auto __lasti = ranges::next(std::move(__first),
  1931. std::move(__last));
  1932. return {std::move(__lasti), std::move(__result_first)};
  1933. }
  1934. auto __result_real_last = __result_first;
  1935. while (__first != __last && __result_real_last != __result_last)
  1936. {
  1937. *__result_real_last = *__first;
  1938. ++__result_real_last;
  1939. ++__first;
  1940. }
  1941. ranges::make_heap(__result_first, __result_real_last, __comp, __proj2);
  1942. for (; __first != __last; ++__first)
  1943. if (std::__invoke(__comp,
  1944. std::__invoke(__proj1, *__first),
  1945. std::__invoke(__proj2, *__result_first)))
  1946. {
  1947. ranges::pop_heap(__result_first, __result_real_last,
  1948. __comp, __proj2);
  1949. *(__result_real_last-1) = *__first;
  1950. ranges::push_heap(__result_first, __result_real_last,
  1951. __comp, __proj2);
  1952. }
  1953. ranges::sort_heap(__result_first, __result_real_last, __comp, __proj2);
  1954. return {std::move(__first), std::move(__result_real_last)};
  1955. }
  1956. template<input_range _Range1, random_access_range _Range2,
  1957. typename _Comp = ranges::less,
  1958. typename _Proj1 = identity, typename _Proj2 = identity>
  1959. requires indirectly_copyable<iterator_t<_Range1>, iterator_t<_Range2>>
  1960. && sortable<iterator_t<_Range2>, _Comp, _Proj2>
  1961. && indirect_strict_weak_order<_Comp,
  1962. projected<iterator_t<_Range1>, _Proj1>,
  1963. projected<iterator_t<_Range2>, _Proj2>>
  1964. constexpr partial_sort_copy_result<borrowed_iterator_t<_Range1>,
  1965. borrowed_iterator_t<_Range2>>
  1966. operator()(_Range1&& __r, _Range2&& __out, _Comp __comp = {},
  1967. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  1968. {
  1969. return (*this)(ranges::begin(__r), ranges::end(__r),
  1970. ranges::begin(__out), ranges::end(__out),
  1971. std::move(__comp),
  1972. std::move(__proj1), std::move(__proj2));
  1973. }
  1974. };
  1975. inline constexpr __partial_sort_copy_fn partial_sort_copy{};
  1976. struct __is_sorted_until_fn
  1977. {
  1978. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  1979. typename _Proj = identity,
  1980. indirect_strict_weak_order<projected<_Iter, _Proj>>
  1981. _Comp = ranges::less>
  1982. constexpr _Iter
  1983. operator()(_Iter __first, _Sent __last,
  1984. _Comp __comp = {}, _Proj __proj = {}) const
  1985. {
  1986. if (__first == __last)
  1987. return __first;
  1988. auto __next = __first;
  1989. for (++__next; __next != __last; __first = __next, (void)++__next)
  1990. if (std::__invoke(__comp,
  1991. std::__invoke(__proj, *__next),
  1992. std::__invoke(__proj, *__first)))
  1993. return __next;
  1994. return __next;
  1995. }
  1996. template<forward_range _Range, typename _Proj = identity,
  1997. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  1998. _Comp = ranges::less>
  1999. constexpr borrowed_iterator_t<_Range>
  2000. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  2001. {
  2002. return (*this)(ranges::begin(__r), ranges::end(__r),
  2003. std::move(__comp), std::move(__proj));
  2004. }
  2005. };
  2006. inline constexpr __is_sorted_until_fn is_sorted_until{};
  2007. struct __is_sorted_fn
  2008. {
  2009. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  2010. typename _Proj = identity,
  2011. indirect_strict_weak_order<projected<_Iter, _Proj>>
  2012. _Comp = ranges::less>
  2013. constexpr bool
  2014. operator()(_Iter __first, _Sent __last,
  2015. _Comp __comp = {}, _Proj __proj = {}) const
  2016. {
  2017. if (__first == __last)
  2018. return true;
  2019. auto __next = __first;
  2020. for (++__next; __next != __last; __first = __next, (void)++__next)
  2021. if (std::__invoke(__comp,
  2022. std::__invoke(__proj, *__next),
  2023. std::__invoke(__proj, *__first)))
  2024. return false;
  2025. return true;
  2026. }
  2027. template<forward_range _Range, typename _Proj = identity,
  2028. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  2029. _Comp = ranges::less>
  2030. constexpr bool
  2031. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  2032. {
  2033. return (*this)(ranges::begin(__r), ranges::end(__r),
  2034. std::move(__comp), std::move(__proj));
  2035. }
  2036. };
  2037. inline constexpr __is_sorted_fn is_sorted{};
  2038. struct __nth_element_fn
  2039. {
  2040. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  2041. typename _Comp = ranges::less, typename _Proj = identity>
  2042. requires sortable<_Iter, _Comp, _Proj>
  2043. constexpr _Iter
  2044. operator()(_Iter __first, _Iter __nth, _Sent __last,
  2045. _Comp __comp = {}, _Proj __proj = {}) const
  2046. {
  2047. auto __lasti = ranges::next(__first, __last);
  2048. std::nth_element(std::move(__first), std::move(__nth), __lasti,
  2049. __detail::__make_comp_proj(__comp, __proj));
  2050. return __lasti;
  2051. }
  2052. template<random_access_range _Range,
  2053. typename _Comp = ranges::less, typename _Proj = identity>
  2054. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  2055. constexpr borrowed_iterator_t<_Range>
  2056. operator()(_Range&& __r, iterator_t<_Range> __nth,
  2057. _Comp __comp = {}, _Proj __proj = {}) const
  2058. {
  2059. return (*this)(ranges::begin(__r), std::move(__nth),
  2060. ranges::end(__r), std::move(__comp), std::move(__proj));
  2061. }
  2062. };
  2063. inline constexpr __nth_element_fn nth_element{};
  2064. struct __lower_bound_fn
  2065. {
  2066. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  2067. typename _Tp, typename _Proj = identity,
  2068. indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
  2069. _Comp = ranges::less>
  2070. constexpr _Iter
  2071. operator()(_Iter __first, _Sent __last,
  2072. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  2073. {
  2074. auto __len = ranges::distance(__first, __last);
  2075. while (__len > 0)
  2076. {
  2077. auto __half = __len / 2;
  2078. auto __middle = __first;
  2079. ranges::advance(__middle, __half);
  2080. if (std::__invoke(__comp, std::__invoke(__proj, *__middle), __value))
  2081. {
  2082. __first = __middle;
  2083. ++__first;
  2084. __len = __len - __half - 1;
  2085. }
  2086. else
  2087. __len = __half;
  2088. }
  2089. return __first;
  2090. }
  2091. template<forward_range _Range, typename _Tp, typename _Proj = identity,
  2092. indirect_strict_weak_order<const _Tp*,
  2093. projected<iterator_t<_Range>, _Proj>>
  2094. _Comp = ranges::less>
  2095. constexpr borrowed_iterator_t<_Range>
  2096. operator()(_Range&& __r,
  2097. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  2098. {
  2099. return (*this)(ranges::begin(__r), ranges::end(__r),
  2100. __value, std::move(__comp), std::move(__proj));
  2101. }
  2102. };
  2103. inline constexpr __lower_bound_fn lower_bound{};
  2104. struct __upper_bound_fn
  2105. {
  2106. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  2107. typename _Tp, typename _Proj = identity,
  2108. indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
  2109. _Comp = ranges::less>
  2110. constexpr _Iter
  2111. operator()(_Iter __first, _Sent __last,
  2112. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  2113. {
  2114. auto __len = ranges::distance(__first, __last);
  2115. while (__len > 0)
  2116. {
  2117. auto __half = __len / 2;
  2118. auto __middle = __first;
  2119. ranges::advance(__middle, __half);
  2120. if (std::__invoke(__comp, __value, std::__invoke(__proj, *__middle)))
  2121. __len = __half;
  2122. else
  2123. {
  2124. __first = __middle;
  2125. ++__first;
  2126. __len = __len - __half - 1;
  2127. }
  2128. }
  2129. return __first;
  2130. }
  2131. template<forward_range _Range, typename _Tp, typename _Proj = identity,
  2132. indirect_strict_weak_order<const _Tp*,
  2133. projected<iterator_t<_Range>, _Proj>>
  2134. _Comp = ranges::less>
  2135. constexpr borrowed_iterator_t<_Range>
  2136. operator()(_Range&& __r,
  2137. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  2138. {
  2139. return (*this)(ranges::begin(__r), ranges::end(__r),
  2140. __value, std::move(__comp), std::move(__proj));
  2141. }
  2142. };
  2143. inline constexpr __upper_bound_fn upper_bound{};
  2144. struct __equal_range_fn
  2145. {
  2146. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  2147. typename _Tp, typename _Proj = identity,
  2148. indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
  2149. _Comp = ranges::less>
  2150. constexpr subrange<_Iter>
  2151. operator()(_Iter __first, _Sent __last,
  2152. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  2153. {
  2154. auto __len = ranges::distance(__first, __last);
  2155. while (__len > 0)
  2156. {
  2157. auto __half = __len / 2;
  2158. auto __middle = __first;
  2159. ranges::advance(__middle, __half);
  2160. if (std::__invoke(__comp,
  2161. std::__invoke(__proj, *__middle),
  2162. __value))
  2163. {
  2164. __first = __middle;
  2165. ++__first;
  2166. __len = __len - __half - 1;
  2167. }
  2168. else if (std::__invoke(__comp,
  2169. __value,
  2170. std::__invoke(__proj, *__middle)))
  2171. __len = __half;
  2172. else
  2173. {
  2174. auto __left
  2175. = ranges::lower_bound(__first, __middle,
  2176. __value, __comp, __proj);
  2177. ranges::advance(__first, __len);
  2178. auto __right
  2179. = ranges::upper_bound(++__middle, __first,
  2180. __value, __comp, __proj);
  2181. return {__left, __right};
  2182. }
  2183. }
  2184. return {__first, __first};
  2185. }
  2186. template<forward_range _Range,
  2187. typename _Tp, typename _Proj = identity,
  2188. indirect_strict_weak_order<const _Tp*,
  2189. projected<iterator_t<_Range>, _Proj>>
  2190. _Comp = ranges::less>
  2191. constexpr borrowed_subrange_t<_Range>
  2192. operator()(_Range&& __r, const _Tp& __value,
  2193. _Comp __comp = {}, _Proj __proj = {}) const
  2194. {
  2195. return (*this)(ranges::begin(__r), ranges::end(__r),
  2196. __value, std::move(__comp), std::move(__proj));
  2197. }
  2198. };
  2199. inline constexpr __equal_range_fn equal_range{};
  2200. struct __binary_search_fn
  2201. {
  2202. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  2203. typename _Tp, typename _Proj = identity,
  2204. indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
  2205. _Comp = ranges::less>
  2206. constexpr bool
  2207. operator()(_Iter __first, _Sent __last,
  2208. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  2209. {
  2210. auto __i = ranges::lower_bound(__first, __last, __value, __comp, __proj);
  2211. if (__i == __last)
  2212. return false;
  2213. return !(bool)std::__invoke(__comp, __value,
  2214. std::__invoke(__proj, *__i));
  2215. }
  2216. template<forward_range _Range,
  2217. typename _Tp, typename _Proj = identity,
  2218. indirect_strict_weak_order<const _Tp*,
  2219. projected<iterator_t<_Range>, _Proj>>
  2220. _Comp = ranges::less>
  2221. constexpr bool
  2222. operator()(_Range&& __r, const _Tp& __value, _Comp __comp = {},
  2223. _Proj __proj = {}) const
  2224. {
  2225. return (*this)(ranges::begin(__r), ranges::end(__r),
  2226. __value, std::move(__comp), std::move(__proj));
  2227. }
  2228. };
  2229. inline constexpr __binary_search_fn binary_search{};
  2230. struct __is_partitioned_fn
  2231. {
  2232. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  2233. typename _Proj = identity,
  2234. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  2235. constexpr bool
  2236. operator()(_Iter __first, _Sent __last,
  2237. _Pred __pred, _Proj __proj = {}) const
  2238. {
  2239. __first = ranges::find_if_not(std::move(__first), __last,
  2240. __pred, __proj);
  2241. if (__first == __last)
  2242. return true;
  2243. ++__first;
  2244. return ranges::none_of(std::move(__first), std::move(__last),
  2245. std::move(__pred), std::move(__proj));
  2246. }
  2247. template<input_range _Range, typename _Proj = identity,
  2248. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  2249. _Pred>
  2250. constexpr bool
  2251. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  2252. {
  2253. return (*this)(ranges::begin(__r), ranges::end(__r),
  2254. std::move(__pred), std::move(__proj));
  2255. }
  2256. };
  2257. inline constexpr __is_partitioned_fn is_partitioned{};
  2258. struct __partition_fn
  2259. {
  2260. template<permutable _Iter, sentinel_for<_Iter> _Sent,
  2261. typename _Proj = identity,
  2262. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  2263. constexpr subrange<_Iter>
  2264. operator()(_Iter __first, _Sent __last,
  2265. _Pred __pred, _Proj __proj = {}) const
  2266. {
  2267. if constexpr (bidirectional_iterator<_Iter>)
  2268. {
  2269. auto __lasti = ranges::next(__first, __last);
  2270. auto __tail = __lasti;
  2271. for (;;)
  2272. {
  2273. for (;;)
  2274. if (__first == __tail)
  2275. return {std::move(__first), std::move(__lasti)};
  2276. else if (std::__invoke(__pred,
  2277. std::__invoke(__proj, *__first)))
  2278. ++__first;
  2279. else
  2280. break;
  2281. --__tail;
  2282. for (;;)
  2283. if (__first == __tail)
  2284. return {std::move(__first), std::move(__lasti)};
  2285. else if (!(bool)std::__invoke(__pred,
  2286. std::__invoke(__proj, *__tail)))
  2287. --__tail;
  2288. else
  2289. break;
  2290. ranges::iter_swap(__first, __tail);
  2291. ++__first;
  2292. }
  2293. }
  2294. else
  2295. {
  2296. if (__first == __last)
  2297. return {std::move(__first), std::move(__first)};
  2298. while (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  2299. if (++__first == __last)
  2300. return {std::move(__first), std::move(__first)};
  2301. auto __next = __first;
  2302. while (++__next != __last)
  2303. if (std::__invoke(__pred, std::__invoke(__proj, *__next)))
  2304. {
  2305. ranges::iter_swap(__first, __next);
  2306. ++__first;
  2307. }
  2308. return {std::move(__first), std::move(__next)};
  2309. }
  2310. }
  2311. template<forward_range _Range, typename _Proj = identity,
  2312. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  2313. _Pred>
  2314. requires permutable<iterator_t<_Range>>
  2315. constexpr borrowed_subrange_t<_Range>
  2316. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  2317. {
  2318. return (*this)(ranges::begin(__r), ranges::end(__r),
  2319. std::move(__pred), std::move(__proj));
  2320. }
  2321. };
  2322. inline constexpr __partition_fn partition{};
  2323. struct __stable_partition_fn
  2324. {
  2325. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  2326. typename _Proj = identity,
  2327. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  2328. requires permutable<_Iter>
  2329. subrange<_Iter>
  2330. operator()(_Iter __first, _Sent __last,
  2331. _Pred __pred, _Proj __proj = {}) const
  2332. {
  2333. auto __lasti = ranges::next(__first, __last);
  2334. auto __middle
  2335. = std::stable_partition(std::move(__first), __lasti,
  2336. __detail::__make_pred_proj(__pred, __proj));
  2337. return {std::move(__middle), std::move(__lasti)};
  2338. }
  2339. template<bidirectional_range _Range, typename _Proj = identity,
  2340. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  2341. _Pred>
  2342. requires permutable<iterator_t<_Range>>
  2343. borrowed_subrange_t<_Range>
  2344. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  2345. {
  2346. return (*this)(ranges::begin(__r), ranges::end(__r),
  2347. std::move(__pred), std::move(__proj));
  2348. }
  2349. };
  2350. inline constexpr __stable_partition_fn stable_partition{};
  2351. template<typename _Iter, typename _Out1, typename _Out2>
  2352. struct in_out_out_result
  2353. {
  2354. [[no_unique_address]] _Iter in;
  2355. [[no_unique_address]] _Out1 out1;
  2356. [[no_unique_address]] _Out2 out2;
  2357. template<typename _IIter, typename _OOut1, typename _OOut2>
  2358. requires convertible_to<const _Iter&, _IIter>
  2359. && convertible_to<const _Out1&, _OOut1>
  2360. && convertible_to<const _Out2&, _OOut2>
  2361. constexpr
  2362. operator in_out_out_result<_IIter, _OOut1, _OOut2>() const &
  2363. { return {in, out1, out2}; }
  2364. template<typename _IIter, typename _OOut1, typename _OOut2>
  2365. requires convertible_to<_Iter, _IIter>
  2366. && convertible_to<_Out1, _OOut1>
  2367. && convertible_to<_Out2, _OOut2>
  2368. constexpr
  2369. operator in_out_out_result<_IIter, _OOut1, _OOut2>() &&
  2370. { return {std::move(in), std::move(out1), std::move(out2)}; }
  2371. };
  2372. template<typename _Iter, typename _Out1, typename _Out2>
  2373. using partition_copy_result = in_out_out_result<_Iter, _Out1, _Out2>;
  2374. struct __partition_copy_fn
  2375. {
  2376. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  2377. weakly_incrementable _Out1, weakly_incrementable _O2,
  2378. typename _Proj = identity,
  2379. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  2380. requires indirectly_copyable<_Iter, _Out1>
  2381. && indirectly_copyable<_Iter, _O2>
  2382. constexpr partition_copy_result<_Iter, _Out1, _O2>
  2383. operator()(_Iter __first, _Sent __last,
  2384. _Out1 __out_true, _O2 __out_false,
  2385. _Pred __pred, _Proj __proj = {}) const
  2386. {
  2387. for (; __first != __last; ++__first)
  2388. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  2389. {
  2390. *__out_true = *__first;
  2391. ++__out_true;
  2392. }
  2393. else
  2394. {
  2395. *__out_false = *__first;
  2396. ++__out_false;
  2397. }
  2398. return {std::move(__first),
  2399. std::move(__out_true), std::move(__out_false)};
  2400. }
  2401. template<input_range _Range, weakly_incrementable _Out1,
  2402. weakly_incrementable _O2,
  2403. typename _Proj = identity,
  2404. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  2405. _Pred>
  2406. requires indirectly_copyable<iterator_t<_Range>, _Out1>
  2407. && indirectly_copyable<iterator_t<_Range>, _O2>
  2408. constexpr partition_copy_result<borrowed_iterator_t<_Range>, _Out1, _O2>
  2409. operator()(_Range&& __r, _Out1 out_true, _O2 out_false,
  2410. _Pred __pred, _Proj __proj = {}) const
  2411. {
  2412. return (*this)(ranges::begin(__r), ranges::end(__r),
  2413. std::move(out_true), std::move(out_false),
  2414. std::move(__pred), std::move(__proj));
  2415. }
  2416. };
  2417. inline constexpr __partition_copy_fn partition_copy{};
  2418. struct __partition_point_fn
  2419. {
  2420. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  2421. typename _Proj = identity,
  2422. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  2423. constexpr _Iter
  2424. operator()(_Iter __first, _Sent __last,
  2425. _Pred __pred, _Proj __proj = {}) const
  2426. {
  2427. auto __len = ranges::distance(__first, __last);
  2428. while (__len > 0)
  2429. {
  2430. auto __half = __len / 2;
  2431. auto __middle = __first;
  2432. ranges::advance(__middle, __half);
  2433. if (std::__invoke(__pred, std::__invoke(__proj, *__middle)))
  2434. {
  2435. __first = __middle;
  2436. ++__first;
  2437. __len = __len - __half - 1;
  2438. }
  2439. else
  2440. __len = __half;
  2441. }
  2442. return __first;
  2443. }
  2444. template<forward_range _Range, typename _Proj = identity,
  2445. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  2446. _Pred>
  2447. constexpr borrowed_iterator_t<_Range>
  2448. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  2449. {
  2450. return (*this)(ranges::begin(__r), ranges::end(__r),
  2451. std::move(__pred), std::move(__proj));
  2452. }
  2453. };
  2454. inline constexpr __partition_point_fn partition_point{};
  2455. template<typename _Iter1, typename _Iter2, typename _Out>
  2456. using merge_result = in_in_out_result<_Iter1, _Iter2, _Out>;
  2457. struct __merge_fn
  2458. {
  2459. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2460. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2461. weakly_incrementable _Out, typename _Comp = ranges::less,
  2462. typename _Proj1 = identity, typename _Proj2 = identity>
  2463. requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
  2464. constexpr merge_result<_Iter1, _Iter2, _Out>
  2465. operator()(_Iter1 __first1, _Sent1 __last1,
  2466. _Iter2 __first2, _Sent2 __last2, _Out __result,
  2467. _Comp __comp = {},
  2468. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2469. {
  2470. while (__first1 != __last1 && __first2 != __last2)
  2471. {
  2472. if (std::__invoke(__comp,
  2473. std::__invoke(__proj2, *__first2),
  2474. std::__invoke(__proj1, *__first1)))
  2475. {
  2476. *__result = *__first2;
  2477. ++__first2;
  2478. }
  2479. else
  2480. {
  2481. *__result = *__first1;
  2482. ++__first1;
  2483. }
  2484. ++__result;
  2485. }
  2486. auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
  2487. std::move(__result));
  2488. auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
  2489. std::move(__copy1.out));
  2490. return { std::move(__copy1.in), std::move(__copy2.in),
  2491. std::move(__copy2.out) };
  2492. }
  2493. template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
  2494. typename _Comp = ranges::less,
  2495. typename _Proj1 = identity, typename _Proj2 = identity>
  2496. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
  2497. _Comp, _Proj1, _Proj2>
  2498. constexpr merge_result<borrowed_iterator_t<_Range1>,
  2499. borrowed_iterator_t<_Range2>,
  2500. _Out>
  2501. operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
  2502. _Comp __comp = {},
  2503. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2504. {
  2505. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2506. ranges::begin(__r2), ranges::end(__r2),
  2507. std::move(__result), std::move(__comp),
  2508. std::move(__proj1), std::move(__proj2));
  2509. }
  2510. };
  2511. inline constexpr __merge_fn merge{};
  2512. struct __inplace_merge_fn
  2513. {
  2514. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  2515. typename _Comp = ranges::less,
  2516. typename _Proj = identity>
  2517. requires sortable<_Iter, _Comp, _Proj>
  2518. _Iter
  2519. operator()(_Iter __first, _Iter __middle, _Sent __last,
  2520. _Comp __comp = {}, _Proj __proj = {}) const
  2521. {
  2522. auto __lasti = ranges::next(__first, __last);
  2523. std::inplace_merge(std::move(__first), std::move(__middle), __lasti,
  2524. __detail::__make_comp_proj(__comp, __proj));
  2525. return __lasti;
  2526. }
  2527. template<bidirectional_range _Range,
  2528. typename _Comp = ranges::less, typename _Proj = identity>
  2529. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  2530. borrowed_iterator_t<_Range>
  2531. operator()(_Range&& __r, iterator_t<_Range> __middle,
  2532. _Comp __comp = {}, _Proj __proj = {}) const
  2533. {
  2534. return (*this)(ranges::begin(__r), std::move(__middle),
  2535. ranges::end(__r),
  2536. std::move(__comp), std::move(__proj));
  2537. }
  2538. };
  2539. inline constexpr __inplace_merge_fn inplace_merge{};
  2540. struct __includes_fn
  2541. {
  2542. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2543. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2544. typename _Proj1 = identity, typename _Proj2 = identity,
  2545. indirect_strict_weak_order<projected<_Iter1, _Proj1>,
  2546. projected<_Iter2, _Proj2>>
  2547. _Comp = ranges::less>
  2548. constexpr bool
  2549. operator()(_Iter1 __first1, _Sent1 __last1,
  2550. _Iter2 __first2, _Sent2 __last2,
  2551. _Comp __comp = {},
  2552. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2553. {
  2554. while (__first1 != __last1 && __first2 != __last2)
  2555. if (std::__invoke(__comp,
  2556. std::__invoke(__proj2, *__first2),
  2557. std::__invoke(__proj1, *__first1)))
  2558. return false;
  2559. else if (std::__invoke(__comp,
  2560. std::__invoke(__proj1, *__first1),
  2561. std::__invoke(__proj2, *__first2)))
  2562. ++__first1;
  2563. else
  2564. {
  2565. ++__first1;
  2566. ++__first2;
  2567. }
  2568. return __first2 == __last2;
  2569. }
  2570. template<input_range _Range1, input_range _Range2,
  2571. typename _Proj1 = identity, typename _Proj2 = identity,
  2572. indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
  2573. projected<iterator_t<_Range2>, _Proj2>>
  2574. _Comp = ranges::less>
  2575. constexpr bool
  2576. operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
  2577. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2578. {
  2579. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2580. ranges::begin(__r2), ranges::end(__r2),
  2581. std::move(__comp),
  2582. std::move(__proj1), std::move(__proj2));
  2583. }
  2584. };
  2585. inline constexpr __includes_fn includes{};
  2586. template<typename _Iter1, typename _Iter2, typename _Out>
  2587. using set_union_result = in_in_out_result<_Iter1, _Iter2, _Out>;
  2588. struct __set_union_fn
  2589. {
  2590. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2591. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2592. weakly_incrementable _Out, typename _Comp = ranges::less,
  2593. typename _Proj1 = identity, typename _Proj2 = identity>
  2594. requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
  2595. constexpr set_union_result<_Iter1, _Iter2, _Out>
  2596. operator()(_Iter1 __first1, _Sent1 __last1,
  2597. _Iter2 __first2, _Sent2 __last2,
  2598. _Out __result, _Comp __comp = {},
  2599. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2600. {
  2601. while (__first1 != __last1 && __first2 != __last2)
  2602. {
  2603. if (std::__invoke(__comp,
  2604. std::__invoke(__proj1, *__first1),
  2605. std::__invoke(__proj2, *__first2)))
  2606. {
  2607. *__result = *__first1;
  2608. ++__first1;
  2609. }
  2610. else if (std::__invoke(__comp,
  2611. std::__invoke(__proj2, *__first2),
  2612. std::__invoke(__proj1, *__first1)))
  2613. {
  2614. *__result = *__first2;
  2615. ++__first2;
  2616. }
  2617. else
  2618. {
  2619. *__result = *__first1;
  2620. ++__first1;
  2621. ++__first2;
  2622. }
  2623. ++__result;
  2624. }
  2625. auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
  2626. std::move(__result));
  2627. auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
  2628. std::move(__copy1.out));
  2629. return {std::move(__copy1.in), std::move(__copy2.in),
  2630. std::move(__copy2.out)};
  2631. }
  2632. template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
  2633. typename _Comp = ranges::less,
  2634. typename _Proj1 = identity, typename _Proj2 = identity>
  2635. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
  2636. _Comp, _Proj1, _Proj2>
  2637. constexpr set_union_result<borrowed_iterator_t<_Range1>,
  2638. borrowed_iterator_t<_Range2>, _Out>
  2639. operator()(_Range1&& __r1, _Range2&& __r2,
  2640. _Out __result, _Comp __comp = {},
  2641. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2642. {
  2643. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2644. ranges::begin(__r2), ranges::end(__r2),
  2645. std::move(__result), std::move(__comp),
  2646. std::move(__proj1), std::move(__proj2));
  2647. }
  2648. };
  2649. inline constexpr __set_union_fn set_union{};
  2650. template<typename _Iter1, typename _Iter2, typename _Out>
  2651. using set_intersection_result = in_in_out_result<_Iter1, _Iter2, _Out>;
  2652. struct __set_intersection_fn
  2653. {
  2654. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2655. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2656. weakly_incrementable _Out, typename _Comp = ranges::less,
  2657. typename _Proj1 = identity, typename _Proj2 = identity>
  2658. requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
  2659. constexpr set_intersection_result<_Iter1, _Iter2, _Out>
  2660. operator()(_Iter1 __first1, _Sent1 __last1,
  2661. _Iter2 __first2, _Sent2 __last2, _Out __result,
  2662. _Comp __comp = {},
  2663. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2664. {
  2665. while (__first1 != __last1 && __first2 != __last2)
  2666. if (std::__invoke(__comp,
  2667. std::__invoke(__proj1, *__first1),
  2668. std::__invoke(__proj2, *__first2)))
  2669. ++__first1;
  2670. else if (std::__invoke(__comp,
  2671. std::__invoke(__proj2, *__first2),
  2672. std::__invoke(__proj1, *__first1)))
  2673. ++__first2;
  2674. else
  2675. {
  2676. *__result = *__first1;
  2677. ++__first1;
  2678. ++__first2;
  2679. ++__result;
  2680. }
  2681. // TODO: Eliminating these variables triggers an ICE.
  2682. auto __last1i = ranges::next(std::move(__first1), std::move(__last1));
  2683. auto __last2i = ranges::next(std::move(__first2), std::move(__last2));
  2684. return {std::move(__last1i), std::move(__last2i), std::move(__result)};
  2685. }
  2686. template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
  2687. typename _Comp = ranges::less,
  2688. typename _Proj1 = identity, typename _Proj2 = identity>
  2689. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
  2690. _Comp, _Proj1, _Proj2>
  2691. constexpr set_intersection_result<borrowed_iterator_t<_Range1>,
  2692. borrowed_iterator_t<_Range2>, _Out>
  2693. operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
  2694. _Comp __comp = {},
  2695. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2696. {
  2697. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2698. ranges::begin(__r2), ranges::end(__r2),
  2699. std::move(__result), std::move(__comp),
  2700. std::move(__proj1), std::move(__proj2));
  2701. }
  2702. };
  2703. inline constexpr __set_intersection_fn set_intersection{};
  2704. template<typename _Iter, typename _Out>
  2705. using set_difference_result = in_out_result<_Iter, _Out>;
  2706. struct __set_difference_fn
  2707. {
  2708. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2709. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2710. weakly_incrementable _Out, typename _Comp = ranges::less,
  2711. typename _Proj1 = identity, typename _Proj2 = identity>
  2712. requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
  2713. constexpr set_difference_result<_Iter1, _Out>
  2714. operator()(_Iter1 __first1, _Sent1 __last1,
  2715. _Iter2 __first2, _Sent2 __last2, _Out __result,
  2716. _Comp __comp = {},
  2717. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2718. {
  2719. while (__first1 != __last1 && __first2 != __last2)
  2720. if (std::__invoke(__comp,
  2721. std::__invoke(__proj1, *__first1),
  2722. std::__invoke(__proj2, *__first2)))
  2723. {
  2724. *__result = *__first1;
  2725. ++__first1;
  2726. ++__result;
  2727. }
  2728. else if (std::__invoke(__comp,
  2729. std::__invoke(__proj2, *__first2),
  2730. std::__invoke(__proj1, *__first1)))
  2731. ++__first2;
  2732. else
  2733. {
  2734. ++__first1;
  2735. ++__first2;
  2736. }
  2737. return ranges::copy(std::move(__first1), std::move(__last1),
  2738. std::move(__result));
  2739. }
  2740. template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
  2741. typename _Comp = ranges::less,
  2742. typename _Proj1 = identity, typename _Proj2 = identity>
  2743. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
  2744. _Comp, _Proj1, _Proj2>
  2745. constexpr set_difference_result<borrowed_iterator_t<_Range1>, _Out>
  2746. operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
  2747. _Comp __comp = {},
  2748. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2749. {
  2750. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2751. ranges::begin(__r2), ranges::end(__r2),
  2752. std::move(__result), std::move(__comp),
  2753. std::move(__proj1), std::move(__proj2));
  2754. }
  2755. };
  2756. inline constexpr __set_difference_fn set_difference{};
  2757. template<typename _Iter1, typename _Iter2, typename _Out>
  2758. using set_symmetric_difference_result
  2759. = in_in_out_result<_Iter1, _Iter2, _Out>;
  2760. struct __set_symmetric_difference_fn
  2761. {
  2762. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2763. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2764. weakly_incrementable _Out, typename _Comp = ranges::less,
  2765. typename _Proj1 = identity, typename _Proj2 = identity>
  2766. requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
  2767. constexpr set_symmetric_difference_result<_Iter1, _Iter2, _Out>
  2768. operator()(_Iter1 __first1, _Sent1 __last1,
  2769. _Iter2 __first2, _Sent2 __last2,
  2770. _Out __result, _Comp __comp = {},
  2771. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2772. {
  2773. while (__first1 != __last1 && __first2 != __last2)
  2774. if (std::__invoke(__comp,
  2775. std::__invoke(__proj1, *__first1),
  2776. std::__invoke(__proj2, *__first2)))
  2777. {
  2778. *__result = *__first1;
  2779. ++__first1;
  2780. ++__result;
  2781. }
  2782. else if (std::__invoke(__comp,
  2783. std::__invoke(__proj2, *__first2),
  2784. std::__invoke(__proj1, *__first1)))
  2785. {
  2786. *__result = *__first2;
  2787. ++__first2;
  2788. ++__result;
  2789. }
  2790. else
  2791. {
  2792. ++__first1;
  2793. ++__first2;
  2794. }
  2795. auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
  2796. std::move(__result));
  2797. auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
  2798. std::move(__copy1.out));
  2799. return {std::move(__copy1.in), std::move(__copy2.in),
  2800. std::move(__copy2.out)};
  2801. }
  2802. template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
  2803. typename _Comp = ranges::less,
  2804. typename _Proj1 = identity, typename _Proj2 = identity>
  2805. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
  2806. _Comp, _Proj1, _Proj2>
  2807. constexpr set_symmetric_difference_result<borrowed_iterator_t<_Range1>,
  2808. borrowed_iterator_t<_Range2>,
  2809. _Out>
  2810. operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
  2811. _Comp __comp = {},
  2812. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2813. {
  2814. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2815. ranges::begin(__r2), ranges::end(__r2),
  2816. std::move(__result), std::move(__comp),
  2817. std::move(__proj1), std::move(__proj2));
  2818. }
  2819. };
  2820. inline constexpr __set_symmetric_difference_fn set_symmetric_difference{};
  2821. struct __min_fn
  2822. {
  2823. template<typename _Tp, typename _Proj = identity,
  2824. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  2825. _Comp = ranges::less>
  2826. constexpr const _Tp&
  2827. operator()(const _Tp& __a, const _Tp& __b,
  2828. _Comp __comp = {}, _Proj __proj = {}) const
  2829. {
  2830. if (std::__invoke(std::move(__comp),
  2831. std::__invoke(__proj, __b),
  2832. std::__invoke(__proj, __a)))
  2833. return __b;
  2834. else
  2835. return __a;
  2836. }
  2837. template<input_range _Range, typename _Proj = identity,
  2838. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  2839. _Comp = ranges::less>
  2840. requires indirectly_copyable_storable<iterator_t<_Range>,
  2841. range_value_t<_Range>*>
  2842. constexpr range_value_t<_Range>
  2843. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  2844. {
  2845. auto __first = ranges::begin(__r);
  2846. auto __last = ranges::end(__r);
  2847. __glibcxx_assert(__first != __last);
  2848. auto __result = *__first;
  2849. while (++__first != __last)
  2850. {
  2851. auto __tmp = *__first;
  2852. if (std::__invoke(__comp,
  2853. std::__invoke(__proj, __tmp),
  2854. std::__invoke(__proj, __result)))
  2855. __result = std::move(__tmp);
  2856. }
  2857. return __result;
  2858. }
  2859. template<copyable _Tp, typename _Proj = identity,
  2860. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  2861. _Comp = ranges::less>
  2862. constexpr _Tp
  2863. operator()(initializer_list<_Tp> __r,
  2864. _Comp __comp = {}, _Proj __proj = {}) const
  2865. {
  2866. return (*this)(ranges::subrange(__r),
  2867. std::move(__comp), std::move(__proj));
  2868. }
  2869. };
  2870. inline constexpr __min_fn min{};
  2871. struct __max_fn
  2872. {
  2873. template<typename _Tp, typename _Proj = identity,
  2874. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  2875. _Comp = ranges::less>
  2876. constexpr const _Tp&
  2877. operator()(const _Tp& __a, const _Tp& __b,
  2878. _Comp __comp = {}, _Proj __proj = {}) const
  2879. {
  2880. if (std::__invoke(std::move(__comp),
  2881. std::__invoke(__proj, __a),
  2882. std::__invoke(__proj, __b)))
  2883. return __b;
  2884. else
  2885. return __a;
  2886. }
  2887. template<input_range _Range, typename _Proj = identity,
  2888. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  2889. _Comp = ranges::less>
  2890. requires indirectly_copyable_storable<iterator_t<_Range>,
  2891. range_value_t<_Range>*>
  2892. constexpr range_value_t<_Range>
  2893. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  2894. {
  2895. auto __first = ranges::begin(__r);
  2896. auto __last = ranges::end(__r);
  2897. __glibcxx_assert(__first != __last);
  2898. auto __result = *__first;
  2899. while (++__first != __last)
  2900. {
  2901. auto __tmp = *__first;
  2902. if (std::__invoke(__comp,
  2903. std::__invoke(__proj, __result),
  2904. std::__invoke(__proj, __tmp)))
  2905. __result = std::move(__tmp);
  2906. }
  2907. return __result;
  2908. }
  2909. template<copyable _Tp, typename _Proj = identity,
  2910. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  2911. _Comp = ranges::less>
  2912. constexpr _Tp
  2913. operator()(initializer_list<_Tp> __r,
  2914. _Comp __comp = {}, _Proj __proj = {}) const
  2915. {
  2916. return (*this)(ranges::subrange(__r),
  2917. std::move(__comp), std::move(__proj));
  2918. }
  2919. };
  2920. inline constexpr __max_fn max{};
  2921. struct __clamp_fn
  2922. {
  2923. template<typename _Tp, typename _Proj = identity,
  2924. indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp
  2925. = ranges::less>
  2926. constexpr const _Tp&
  2927. operator()(const _Tp& __val, const _Tp& __lo, const _Tp& __hi,
  2928. _Comp __comp = {}, _Proj __proj = {}) const
  2929. {
  2930. __glibcxx_assert(!(std::__invoke(__comp,
  2931. std::__invoke(__proj, __hi),
  2932. std::__invoke(__proj, __lo))));
  2933. auto&& __proj_val = std::__invoke(__proj, __val);
  2934. if (std::__invoke(__comp, __proj_val, std::__invoke(__proj, __lo)))
  2935. return __lo;
  2936. else if (std::__invoke(__comp, std::__invoke(__proj, __hi), __proj_val))
  2937. return __hi;
  2938. else
  2939. return __val;
  2940. }
  2941. };
  2942. inline constexpr __clamp_fn clamp{};
  2943. template<typename _Tp>
  2944. struct min_max_result
  2945. {
  2946. [[no_unique_address]] _Tp min;
  2947. [[no_unique_address]] _Tp max;
  2948. template<typename _Tp2>
  2949. requires convertible_to<const _Tp&, _Tp2>
  2950. constexpr
  2951. operator min_max_result<_Tp2>() const &
  2952. { return {min, max}; }
  2953. template<typename _Tp2>
  2954. requires convertible_to<_Tp, _Tp2>
  2955. constexpr
  2956. operator min_max_result<_Tp2>() &&
  2957. { return {std::move(min), std::move(max)}; }
  2958. };
  2959. template<typename _Tp>
  2960. using minmax_result = min_max_result<_Tp>;
  2961. struct __minmax_fn
  2962. {
  2963. template<typename _Tp, typename _Proj = identity,
  2964. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  2965. _Comp = ranges::less>
  2966. constexpr minmax_result<const _Tp&>
  2967. operator()(const _Tp& __a, const _Tp& __b,
  2968. _Comp __comp = {}, _Proj __proj = {}) const
  2969. {
  2970. if (std::__invoke(std::move(__comp),
  2971. std::__invoke(__proj, __b),
  2972. std::__invoke(__proj, __a)))
  2973. return {__b, __a};
  2974. else
  2975. return {__a, __b};
  2976. }
  2977. template<input_range _Range, typename _Proj = identity,
  2978. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  2979. _Comp = ranges::less>
  2980. requires indirectly_copyable_storable<iterator_t<_Range>,
  2981. range_value_t<_Range>*>
  2982. constexpr minmax_result<range_value_t<_Range>>
  2983. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  2984. {
  2985. auto __first = ranges::begin(__r);
  2986. auto __last = ranges::end(__r);
  2987. __glibcxx_assert(__first != __last);
  2988. minmax_result<range_value_t<_Range>> __result = {*__first, *__first};
  2989. while (++__first != __last)
  2990. {
  2991. auto __tmp = *__first;
  2992. if (std::__invoke(__comp,
  2993. std::__invoke(__proj, __tmp),
  2994. std::__invoke(__proj, __result.min)))
  2995. __result.min = std::move(__tmp);
  2996. if (!(bool)std::__invoke(__comp,
  2997. std::__invoke(__proj, __tmp),
  2998. std::__invoke(__proj, __result.max)))
  2999. __result.max = std::move(__tmp);
  3000. }
  3001. return __result;
  3002. }
  3003. template<copyable _Tp, typename _Proj = identity,
  3004. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  3005. _Comp = ranges::less>
  3006. constexpr minmax_result<_Tp>
  3007. operator()(initializer_list<_Tp> __r,
  3008. _Comp __comp = {}, _Proj __proj = {}) const
  3009. {
  3010. return (*this)(ranges::subrange(__r),
  3011. std::move(__comp), std::move(__proj));
  3012. }
  3013. };
  3014. inline constexpr __minmax_fn minmax{};
  3015. struct __min_element_fn
  3016. {
  3017. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  3018. typename _Proj = identity,
  3019. indirect_strict_weak_order<projected<_Iter, _Proj>>
  3020. _Comp = ranges::less>
  3021. constexpr _Iter
  3022. operator()(_Iter __first, _Sent __last,
  3023. _Comp __comp = {}, _Proj __proj = {}) const
  3024. {
  3025. if (__first == __last)
  3026. return __first;
  3027. auto __i = __first;
  3028. while (++__i != __last)
  3029. {
  3030. if (std::__invoke(__comp,
  3031. std::__invoke(__proj, *__i),
  3032. std::__invoke(__proj, *__first)))
  3033. __first = __i;
  3034. }
  3035. return __first;
  3036. }
  3037. template<forward_range _Range, typename _Proj = identity,
  3038. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  3039. _Comp = ranges::less>
  3040. constexpr borrowed_iterator_t<_Range>
  3041. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  3042. {
  3043. return (*this)(ranges::begin(__r), ranges::end(__r),
  3044. std::move(__comp), std::move(__proj));
  3045. }
  3046. };
  3047. inline constexpr __min_element_fn min_element{};
  3048. struct __max_element_fn
  3049. {
  3050. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  3051. typename _Proj = identity,
  3052. indirect_strict_weak_order<projected<_Iter, _Proj>>
  3053. _Comp = ranges::less>
  3054. constexpr _Iter
  3055. operator()(_Iter __first, _Sent __last,
  3056. _Comp __comp = {}, _Proj __proj = {}) const
  3057. {
  3058. if (__first == __last)
  3059. return __first;
  3060. auto __i = __first;
  3061. while (++__i != __last)
  3062. {
  3063. if (std::__invoke(__comp,
  3064. std::__invoke(__proj, *__first),
  3065. std::__invoke(__proj, *__i)))
  3066. __first = __i;
  3067. }
  3068. return __first;
  3069. }
  3070. template<forward_range _Range, typename _Proj = identity,
  3071. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  3072. _Comp = ranges::less>
  3073. constexpr borrowed_iterator_t<_Range>
  3074. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  3075. {
  3076. return (*this)(ranges::begin(__r), ranges::end(__r),
  3077. std::move(__comp), std::move(__proj));
  3078. }
  3079. };
  3080. inline constexpr __max_element_fn max_element{};
  3081. template<typename _Iter>
  3082. using minmax_element_result = min_max_result<_Iter>;
  3083. struct __minmax_element_fn
  3084. {
  3085. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  3086. typename _Proj = identity,
  3087. indirect_strict_weak_order<projected<_Iter, _Proj>>
  3088. _Comp = ranges::less>
  3089. constexpr minmax_element_result<_Iter>
  3090. operator()(_Iter __first, _Sent __last,
  3091. _Comp __comp = {}, _Proj __proj = {}) const
  3092. {
  3093. if (__first == __last)
  3094. return {__first, __first};
  3095. minmax_element_result<_Iter> __result = {__first, __first};
  3096. auto __i = __first;
  3097. while (++__i != __last)
  3098. {
  3099. if (std::__invoke(__comp,
  3100. std::__invoke(__proj, *__i),
  3101. std::__invoke(__proj, *__result.min)))
  3102. __result.min = __i;
  3103. if (!(bool)std::__invoke(__comp,
  3104. std::__invoke(__proj, *__i),
  3105. std::__invoke(__proj, *__result.max)))
  3106. __result.max = __i;
  3107. }
  3108. return __result;
  3109. }
  3110. template<forward_range _Range, typename _Proj = identity,
  3111. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  3112. _Comp = ranges::less>
  3113. constexpr minmax_element_result<borrowed_iterator_t<_Range>>
  3114. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  3115. {
  3116. return (*this)(ranges::begin(__r), ranges::end(__r),
  3117. std::move(__comp), std::move(__proj));
  3118. }
  3119. };
  3120. inline constexpr __minmax_element_fn minmax_element{};
  3121. struct __lexicographical_compare_fn
  3122. {
  3123. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  3124. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  3125. typename _Proj1 = identity, typename _Proj2 = identity,
  3126. indirect_strict_weak_order<projected<_Iter1, _Proj1>,
  3127. projected<_Iter2, _Proj2>>
  3128. _Comp = ranges::less>
  3129. constexpr bool
  3130. operator()(_Iter1 __first1, _Sent1 __last1,
  3131. _Iter2 __first2, _Sent2 __last2,
  3132. _Comp __comp = {},
  3133. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  3134. {
  3135. if constexpr (__detail::__is_normal_iterator<_Iter1>
  3136. && same_as<_Iter1, _Sent1>)
  3137. return (*this)(__first1.base(), __last1.base(),
  3138. std::move(__first2), std::move(__last2),
  3139. std::move(__comp),
  3140. std::move(__proj1), std::move(__proj2));
  3141. else if constexpr (__detail::__is_normal_iterator<_Iter2>
  3142. && same_as<_Iter2, _Sent2>)
  3143. return (*this)(std::move(__first1), std::move(__last1),
  3144. __first2.base(), __last2.base(),
  3145. std::move(__comp),
  3146. std::move(__proj1), std::move(__proj2));
  3147. else
  3148. {
  3149. constexpr bool __sized_iters
  3150. = (sized_sentinel_for<_Sent1, _Iter1>
  3151. && sized_sentinel_for<_Sent2, _Iter2>);
  3152. if constexpr (__sized_iters)
  3153. {
  3154. using _ValueType1 = iter_value_t<_Iter1>;
  3155. using _ValueType2 = iter_value_t<_Iter2>;
  3156. // This condition is consistent with the one in
  3157. // __lexicographical_compare_aux in <bits/stl_algobase.h>.
  3158. constexpr bool __use_memcmp
  3159. = (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
  3160. && __ptr_to_nonvolatile<_Iter1>
  3161. && __ptr_to_nonvolatile<_Iter2>
  3162. && (is_same_v<_Comp, ranges::less>
  3163. || is_same_v<_Comp, ranges::greater>)
  3164. && is_same_v<_Proj1, identity>
  3165. && is_same_v<_Proj2, identity>);
  3166. if constexpr (__use_memcmp)
  3167. {
  3168. const auto __d1 = __last1 - __first1;
  3169. const auto __d2 = __last2 - __first2;
  3170. if (const auto __len = std::min(__d1, __d2))
  3171. {
  3172. const auto __c
  3173. = std::__memcmp(__first1, __first2, __len);
  3174. if constexpr (is_same_v<_Comp, ranges::less>)
  3175. {
  3176. if (__c < 0)
  3177. return true;
  3178. if (__c > 0)
  3179. return false;
  3180. }
  3181. else if constexpr (is_same_v<_Comp, ranges::greater>)
  3182. {
  3183. if (__c > 0)
  3184. return true;
  3185. if (__c < 0)
  3186. return false;
  3187. }
  3188. }
  3189. return __d1 < __d2;
  3190. }
  3191. }
  3192. for (; __first1 != __last1 && __first2 != __last2;
  3193. ++__first1, (void) ++__first2)
  3194. {
  3195. if (std::__invoke(__comp,
  3196. std::__invoke(__proj1, *__first1),
  3197. std::__invoke(__proj2, *__first2)))
  3198. return true;
  3199. if (std::__invoke(__comp,
  3200. std::__invoke(__proj2, *__first2),
  3201. std::__invoke(__proj1, *__first1)))
  3202. return false;
  3203. }
  3204. return __first1 == __last1 && __first2 != __last2;
  3205. }
  3206. }
  3207. template<input_range _Range1, input_range _Range2,
  3208. typename _Proj1 = identity, typename _Proj2 = identity,
  3209. indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
  3210. projected<iterator_t<_Range2>, _Proj2>>
  3211. _Comp = ranges::less>
  3212. constexpr bool
  3213. operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
  3214. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  3215. {
  3216. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  3217. ranges::begin(__r2), ranges::end(__r2),
  3218. std::move(__comp),
  3219. std::move(__proj1), std::move(__proj2));
  3220. }
  3221. private:
  3222. template<typename _Iter, typename _Ref = iter_reference_t<_Iter>>
  3223. static constexpr bool __ptr_to_nonvolatile
  3224. = is_pointer_v<_Iter> && !is_volatile_v<remove_reference_t<_Ref>>;
  3225. };
  3226. inline constexpr __lexicographical_compare_fn lexicographical_compare;
  3227. template<typename _Iter>
  3228. struct in_found_result
  3229. {
  3230. [[no_unique_address]] _Iter in;
  3231. bool found;
  3232. template<typename _Iter2>
  3233. requires convertible_to<const _Iter&, _Iter2>
  3234. constexpr
  3235. operator in_found_result<_Iter2>() const &
  3236. { return {in, found}; }
  3237. template<typename _Iter2>
  3238. requires convertible_to<_Iter, _Iter2>
  3239. constexpr
  3240. operator in_found_result<_Iter2>() &&
  3241. { return {std::move(in), found}; }
  3242. };
  3243. template<typename _Iter>
  3244. using next_permutation_result = in_found_result<_Iter>;
  3245. struct __next_permutation_fn
  3246. {
  3247. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  3248. typename _Comp = ranges::less, typename _Proj = identity>
  3249. requires sortable<_Iter, _Comp, _Proj>
  3250. constexpr next_permutation_result<_Iter>
  3251. operator()(_Iter __first, _Sent __last,
  3252. _Comp __comp = {}, _Proj __proj = {}) const
  3253. {
  3254. if (__first == __last)
  3255. return {std::move(__first), false};
  3256. auto __i = __first;
  3257. ++__i;
  3258. if (__i == __last)
  3259. return {std::move(__i), false};
  3260. auto __lasti = ranges::next(__first, __last);
  3261. __i = __lasti;
  3262. --__i;
  3263. for (;;)
  3264. {
  3265. auto __ii = __i;
  3266. --__i;
  3267. if (std::__invoke(__comp,
  3268. std::__invoke(__proj, *__i),
  3269. std::__invoke(__proj, *__ii)))
  3270. {
  3271. auto __j = __lasti;
  3272. while (!(bool)std::__invoke(__comp,
  3273. std::__invoke(__proj, *__i),
  3274. std::__invoke(__proj, *--__j)))
  3275. ;
  3276. ranges::iter_swap(__i, __j);
  3277. ranges::reverse(__ii, __last);
  3278. return {std::move(__lasti), true};
  3279. }
  3280. if (__i == __first)
  3281. {
  3282. ranges::reverse(__first, __last);
  3283. return {std::move(__lasti), false};
  3284. }
  3285. }
  3286. }
  3287. template<bidirectional_range _Range, typename _Comp = ranges::less,
  3288. typename _Proj = identity>
  3289. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  3290. constexpr next_permutation_result<borrowed_iterator_t<_Range>>
  3291. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  3292. {
  3293. return (*this)(ranges::begin(__r), ranges::end(__r),
  3294. std::move(__comp), std::move(__proj));
  3295. }
  3296. };
  3297. inline constexpr __next_permutation_fn next_permutation{};
  3298. template<typename _Iter>
  3299. using prev_permutation_result = in_found_result<_Iter>;
  3300. struct __prev_permutation_fn
  3301. {
  3302. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  3303. typename _Comp = ranges::less, typename _Proj = identity>
  3304. requires sortable<_Iter, _Comp, _Proj>
  3305. constexpr prev_permutation_result<_Iter>
  3306. operator()(_Iter __first, _Sent __last,
  3307. _Comp __comp = {}, _Proj __proj = {}) const
  3308. {
  3309. if (__first == __last)
  3310. return {std::move(__first), false};
  3311. auto __i = __first;
  3312. ++__i;
  3313. if (__i == __last)
  3314. return {std::move(__i), false};
  3315. auto __lasti = ranges::next(__first, __last);
  3316. __i = __lasti;
  3317. --__i;
  3318. for (;;)
  3319. {
  3320. auto __ii = __i;
  3321. --__i;
  3322. if (std::__invoke(__comp,
  3323. std::__invoke(__proj, *__ii),
  3324. std::__invoke(__proj, *__i)))
  3325. {
  3326. auto __j = __lasti;
  3327. while (!(bool)std::__invoke(__comp,
  3328. std::__invoke(__proj, *--__j),
  3329. std::__invoke(__proj, *__i)))
  3330. ;
  3331. ranges::iter_swap(__i, __j);
  3332. ranges::reverse(__ii, __last);
  3333. return {std::move(__lasti), true};
  3334. }
  3335. if (__i == __first)
  3336. {
  3337. ranges::reverse(__first, __last);
  3338. return {std::move(__lasti), false};
  3339. }
  3340. }
  3341. }
  3342. template<bidirectional_range _Range, typename _Comp = ranges::less,
  3343. typename _Proj = identity>
  3344. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  3345. constexpr prev_permutation_result<borrowed_iterator_t<_Range>>
  3346. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  3347. {
  3348. return (*this)(ranges::begin(__r), ranges::end(__r),
  3349. std::move(__comp), std::move(__proj));
  3350. }
  3351. };
  3352. inline constexpr __prev_permutation_fn prev_permutation{};
  3353. } // namespace ranges
  3354. #define __cpp_lib_shift 201806L
  3355. template<typename _ForwardIterator>
  3356. constexpr _ForwardIterator
  3357. shift_left(_ForwardIterator __first, _ForwardIterator __last,
  3358. typename iterator_traits<_ForwardIterator>::difference_type __n)
  3359. {
  3360. __glibcxx_assert(__n >= 0);
  3361. if (__n == 0)
  3362. return __last;
  3363. auto __mid = ranges::next(__first, __n, __last);
  3364. if (__mid == __last)
  3365. return __first;
  3366. return std::move(std::move(__mid), std::move(__last), std::move(__first));
  3367. }
  3368. template<typename _ForwardIterator>
  3369. constexpr _ForwardIterator
  3370. shift_right(_ForwardIterator __first, _ForwardIterator __last,
  3371. typename iterator_traits<_ForwardIterator>::difference_type __n)
  3372. {
  3373. __glibcxx_assert(__n >= 0);
  3374. if (__n == 0)
  3375. return __first;
  3376. using _Cat
  3377. = typename iterator_traits<_ForwardIterator>::iterator_category;
  3378. if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
  3379. {
  3380. auto __mid = ranges::next(__last, -__n, __first);
  3381. if (__mid == __first)
  3382. return __last;
  3383. return std::move_backward(std::move(__first), std::move(__mid),
  3384. std::move(__last));
  3385. }
  3386. else
  3387. {
  3388. auto __result = ranges::next(__first, __n, __last);
  3389. if (__result == __last)
  3390. return __last;
  3391. auto __dest_head = __first, __dest_tail = __result;
  3392. while (__dest_head != __result)
  3393. {
  3394. if (__dest_tail == __last)
  3395. {
  3396. // If we get here, then we must have
  3397. // 2*n >= distance(__first, __last)
  3398. // i.e. we are shifting out at least half of the range. In
  3399. // this case we can safely perform the shift with a single
  3400. // move.
  3401. std::move(std::move(__first), std::move(__dest_head),
  3402. std::move(__result));
  3403. return __result;
  3404. }
  3405. ++__dest_head;
  3406. ++__dest_tail;
  3407. }
  3408. for (;;)
  3409. {
  3410. // At the start of each iteration of this outer loop, the range
  3411. // [__first, __result) contains those elements that after shifting
  3412. // the whole range right by __n, should end up in
  3413. // [__dest_head, __dest_tail) in order.
  3414. // The below inner loop swaps the elements of [__first, __result)
  3415. // and [__dest_head, __dest_tail), while simultaneously shifting
  3416. // the latter range by __n.
  3417. auto __cursor = __first;
  3418. while (__cursor != __result)
  3419. {
  3420. if (__dest_tail == __last)
  3421. {
  3422. // At this point the ranges [__first, result) and
  3423. // [__dest_head, dest_tail) are disjoint, so we can safely
  3424. // move the remaining elements.
  3425. __dest_head = std::move(__cursor, __result,
  3426. std::move(__dest_head));
  3427. std::move(std::move(__first), std::move(__cursor),
  3428. std::move(__dest_head));
  3429. return __result;
  3430. }
  3431. std::iter_swap(__cursor, __dest_head);
  3432. ++__dest_head;
  3433. ++__dest_tail;
  3434. ++__cursor;
  3435. }
  3436. }
  3437. }
  3438. }
  3439. _GLIBCXX_END_NAMESPACE_VERSION
  3440. } // namespace std
  3441. #endif // concepts
  3442. #endif // C++20
  3443. #endif // _RANGES_ALGO_H