ranges_algobase.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // Core algorithmic facilities -*- C++ -*-
  2. // Copyright (C) 2020-2022 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_algobase.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_ALGOBASE_H
  25. #define _RANGES_ALGOBASE_H 1
  26. #if __cplusplus > 201703L
  27. #include <compare>
  28. #include <bits/stl_iterator_base_types.h>
  29. #include <bits/stl_iterator_base_funcs.h>
  30. #include <bits/stl_iterator.h>
  31. #include <bits/ranges_base.h> // ranges::begin, ranges::range etc.
  32. #include <bits/invoke.h> // __invoke
  33. #include <bits/cpp_type_traits.h> // __is_byte
  34. #if __cpp_lib_concepts
  35. namespace std _GLIBCXX_VISIBILITY(default)
  36. {
  37. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  38. namespace ranges
  39. {
  40. namespace __detail
  41. {
  42. template<typename _Tp>
  43. constexpr inline bool __is_normal_iterator = false;
  44. template<typename _Iterator, typename _Container>
  45. constexpr inline bool
  46. __is_normal_iterator<__gnu_cxx::__normal_iterator<_Iterator,
  47. _Container>> = true;
  48. template<typename _Tp>
  49. constexpr inline bool __is_reverse_iterator = false;
  50. template<typename _Iterator>
  51. constexpr inline bool
  52. __is_reverse_iterator<reverse_iterator<_Iterator>> = true;
  53. template<typename _Tp>
  54. constexpr inline bool __is_move_iterator = false;
  55. template<typename _Iterator>
  56. constexpr inline bool
  57. __is_move_iterator<move_iterator<_Iterator>> = true;
  58. } // namespace __detail
  59. struct __equal_fn
  60. {
  61. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  62. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  63. typename _Pred = ranges::equal_to,
  64. typename _Proj1 = identity, typename _Proj2 = identity>
  65. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  66. constexpr bool
  67. operator()(_Iter1 __first1, _Sent1 __last1,
  68. _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
  69. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  70. {
  71. // TODO: implement more specializations to at least have parity with
  72. // std::equal.
  73. if constexpr (__detail::__is_normal_iterator<_Iter1>
  74. && same_as<_Iter1, _Sent1>)
  75. return (*this)(__first1.base(), __last1.base(),
  76. std::move(__first2), std::move(__last2),
  77. std::move(__pred),
  78. std::move(__proj1), std::move(__proj2));
  79. else if constexpr (__detail::__is_normal_iterator<_Iter2>
  80. && same_as<_Iter2, _Sent2>)
  81. return (*this)(std::move(__first1), std::move(__last1),
  82. __first2.base(), __last2.base(),
  83. std::move(__pred),
  84. std::move(__proj1), std::move(__proj2));
  85. else if constexpr (sized_sentinel_for<_Sent1, _Iter1>
  86. && sized_sentinel_for<_Sent2, _Iter2>)
  87. {
  88. auto __d1 = ranges::distance(__first1, __last1);
  89. auto __d2 = ranges::distance(__first2, __last2);
  90. if (__d1 != __d2)
  91. return false;
  92. using _ValueType1 = iter_value_t<_Iter1>;
  93. constexpr bool __use_memcmp
  94. = ((is_integral_v<_ValueType1> || is_pointer_v<_ValueType1>)
  95. && __memcmpable<_Iter1, _Iter2>::__value
  96. && is_same_v<_Pred, ranges::equal_to>
  97. && is_same_v<_Proj1, identity>
  98. && is_same_v<_Proj2, identity>);
  99. if constexpr (__use_memcmp)
  100. {
  101. if (const size_t __len = (__last1 - __first1))
  102. return !std::__memcmp(__first1, __first2, __len);
  103. return true;
  104. }
  105. else
  106. {
  107. for (; __first1 != __last1; ++__first1, (void)++__first2)
  108. if (!(bool)std::__invoke(__pred,
  109. std::__invoke(__proj1, *__first1),
  110. std::__invoke(__proj2, *__first2)))
  111. return false;
  112. return true;
  113. }
  114. }
  115. else
  116. {
  117. for (; __first1 != __last1 && __first2 != __last2;
  118. ++__first1, (void)++__first2)
  119. if (!(bool)std::__invoke(__pred,
  120. std::__invoke(__proj1, *__first1),
  121. std::__invoke(__proj2, *__first2)))
  122. return false;
  123. return __first1 == __last1 && __first2 == __last2;
  124. }
  125. }
  126. template<input_range _Range1, input_range _Range2,
  127. typename _Pred = ranges::equal_to,
  128. typename _Proj1 = identity, typename _Proj2 = identity>
  129. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
  130. _Pred, _Proj1, _Proj2>
  131. constexpr bool
  132. operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
  133. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  134. {
  135. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  136. ranges::begin(__r2), ranges::end(__r2),
  137. std::move(__pred),
  138. std::move(__proj1), std::move(__proj2));
  139. }
  140. };
  141. inline constexpr __equal_fn equal{};
  142. template<typename _Iter, typename _Out>
  143. struct in_out_result
  144. {
  145. [[no_unique_address]] _Iter in;
  146. [[no_unique_address]] _Out out;
  147. template<typename _Iter2, typename _Out2>
  148. requires convertible_to<const _Iter&, _Iter2>
  149. && convertible_to<const _Out&, _Out2>
  150. constexpr
  151. operator in_out_result<_Iter2, _Out2>() const &
  152. { return {in, out}; }
  153. template<typename _Iter2, typename _Out2>
  154. requires convertible_to<_Iter, _Iter2>
  155. && convertible_to<_Out, _Out2>
  156. constexpr
  157. operator in_out_result<_Iter2, _Out2>() &&
  158. { return {std::move(in), std::move(out)}; }
  159. };
  160. template<typename _Iter, typename _Out>
  161. using copy_result = in_out_result<_Iter, _Out>;
  162. template<typename _Iter, typename _Out>
  163. using move_result = in_out_result<_Iter, _Out>;
  164. template<typename _Iter1, typename _Iter2>
  165. using move_backward_result = in_out_result<_Iter1, _Iter2>;
  166. template<typename _Iter1, typename _Iter2>
  167. using copy_backward_result = in_out_result<_Iter1, _Iter2>;
  168. template<bool _IsMove,
  169. bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  170. bidirectional_iterator _Out>
  171. requires (_IsMove
  172. ? indirectly_movable<_Iter, _Out>
  173. : indirectly_copyable<_Iter, _Out>)
  174. constexpr __conditional_t<_IsMove,
  175. move_backward_result<_Iter, _Out>,
  176. copy_backward_result<_Iter, _Out>>
  177. __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result);
  178. template<bool _IsMove,
  179. input_iterator _Iter, sentinel_for<_Iter> _Sent,
  180. weakly_incrementable _Out>
  181. requires (_IsMove
  182. ? indirectly_movable<_Iter, _Out>
  183. : indirectly_copyable<_Iter, _Out>)
  184. constexpr __conditional_t<_IsMove,
  185. move_result<_Iter, _Out>,
  186. copy_result<_Iter, _Out>>
  187. __copy_or_move(_Iter __first, _Sent __last, _Out __result)
  188. {
  189. // TODO: implement more specializations to be at least on par with
  190. // std::copy/std::move.
  191. using __detail::__is_move_iterator;
  192. using __detail::__is_reverse_iterator;
  193. using __detail::__is_normal_iterator;
  194. if constexpr (__is_move_iterator<_Iter> && same_as<_Iter, _Sent>)
  195. {
  196. auto [__in, __out]
  197. = ranges::__copy_or_move<true>(std::move(__first).base(),
  198. std::move(__last).base(),
  199. std::move(__result));
  200. return {move_iterator{std::move(__in)}, std::move(__out)};
  201. }
  202. else if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
  203. && __is_reverse_iterator<_Out>)
  204. {
  205. auto [__in,__out]
  206. = ranges::__copy_or_move_backward<_IsMove>(std::move(__last).base(),
  207. std::move(__first).base(),
  208. std::move(__result).base());
  209. return {reverse_iterator{std::move(__in)},
  210. reverse_iterator{std::move(__out)}};
  211. }
  212. else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
  213. {
  214. auto [__in,__out]
  215. = ranges::__copy_or_move<_IsMove>(__first.base(), __last.base(),
  216. __result);
  217. return {decltype(__first){__in}, std::move(__out)};
  218. }
  219. else if constexpr (__is_normal_iterator<_Out>)
  220. {
  221. auto [__in,__out]
  222. = ranges::__copy_or_move<_IsMove>(std::move(__first), __last, __result.base());
  223. return {std::move(__in), decltype(__result){__out}};
  224. }
  225. else if constexpr (sized_sentinel_for<_Sent, _Iter>)
  226. {
  227. if (!std::__is_constant_evaluated())
  228. {
  229. if constexpr (__memcpyable<_Iter, _Out>::__value)
  230. {
  231. using _ValueTypeI = iter_value_t<_Iter>;
  232. static_assert(_IsMove
  233. ? is_move_assignable_v<_ValueTypeI>
  234. : is_copy_assignable_v<_ValueTypeI>);
  235. auto __num = __last - __first;
  236. if (__num)
  237. __builtin_memmove(__result, __first,
  238. sizeof(_ValueTypeI) * __num);
  239. return {__first + __num, __result + __num};
  240. }
  241. }
  242. for (auto __n = __last - __first; __n > 0; --__n)
  243. {
  244. if constexpr (_IsMove)
  245. *__result = std::move(*__first);
  246. else
  247. *__result = *__first;
  248. ++__first;
  249. ++__result;
  250. }
  251. return {std::move(__first), std::move(__result)};
  252. }
  253. else
  254. {
  255. while (__first != __last)
  256. {
  257. if constexpr (_IsMove)
  258. *__result = std::move(*__first);
  259. else
  260. *__result = *__first;
  261. ++__first;
  262. ++__result;
  263. }
  264. return {std::move(__first), std::move(__result)};
  265. }
  266. }
  267. struct __copy_fn
  268. {
  269. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  270. weakly_incrementable _Out>
  271. requires indirectly_copyable<_Iter, _Out>
  272. constexpr copy_result<_Iter, _Out>
  273. operator()(_Iter __first, _Sent __last, _Out __result) const
  274. {
  275. return ranges::__copy_or_move<false>(std::move(__first),
  276. std::move(__last),
  277. std::move(__result));
  278. }
  279. template<input_range _Range, weakly_incrementable _Out>
  280. requires indirectly_copyable<iterator_t<_Range>, _Out>
  281. constexpr copy_result<borrowed_iterator_t<_Range>, _Out>
  282. operator()(_Range&& __r, _Out __result) const
  283. {
  284. return (*this)(ranges::begin(__r), ranges::end(__r),
  285. std::move(__result));
  286. }
  287. };
  288. inline constexpr __copy_fn copy{};
  289. struct __move_fn
  290. {
  291. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  292. weakly_incrementable _Out>
  293. requires indirectly_movable<_Iter, _Out>
  294. constexpr move_result<_Iter, _Out>
  295. operator()(_Iter __first, _Sent __last, _Out __result) const
  296. {
  297. return ranges::__copy_or_move<true>(std::move(__first),
  298. std::move(__last),
  299. std::move(__result));
  300. }
  301. template<input_range _Range, weakly_incrementable _Out>
  302. requires indirectly_movable<iterator_t<_Range>, _Out>
  303. constexpr move_result<borrowed_iterator_t<_Range>, _Out>
  304. operator()(_Range&& __r, _Out __result) const
  305. {
  306. return (*this)(ranges::begin(__r), ranges::end(__r),
  307. std::move(__result));
  308. }
  309. };
  310. inline constexpr __move_fn move{};
  311. template<bool _IsMove,
  312. bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  313. bidirectional_iterator _Out>
  314. requires (_IsMove
  315. ? indirectly_movable<_Iter, _Out>
  316. : indirectly_copyable<_Iter, _Out>)
  317. constexpr __conditional_t<_IsMove,
  318. move_backward_result<_Iter, _Out>,
  319. copy_backward_result<_Iter, _Out>>
  320. __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result)
  321. {
  322. // TODO: implement more specializations to be at least on par with
  323. // std::copy_backward/std::move_backward.
  324. using __detail::__is_reverse_iterator;
  325. using __detail::__is_normal_iterator;
  326. if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
  327. && __is_reverse_iterator<_Out>)
  328. {
  329. auto [__in,__out]
  330. = ranges::__copy_or_move<_IsMove>(std::move(__last).base(),
  331. std::move(__first).base(),
  332. std::move(__result).base());
  333. return {reverse_iterator{std::move(__in)},
  334. reverse_iterator{std::move(__out)}};
  335. }
  336. else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
  337. {
  338. auto [__in,__out]
  339. = ranges::__copy_or_move_backward<_IsMove>(__first.base(),
  340. __last.base(),
  341. std::move(__result));
  342. return {decltype(__first){__in}, std::move(__out)};
  343. }
  344. else if constexpr (__is_normal_iterator<_Out>)
  345. {
  346. auto [__in,__out]
  347. = ranges::__copy_or_move_backward<_IsMove>(std::move(__first),
  348. std::move(__last),
  349. __result.base());
  350. return {std::move(__in), decltype(__result){__out}};
  351. }
  352. else if constexpr (sized_sentinel_for<_Sent, _Iter>)
  353. {
  354. if (!std::__is_constant_evaluated())
  355. {
  356. if constexpr (__memcpyable<_Out, _Iter>::__value)
  357. {
  358. using _ValueTypeI = iter_value_t<_Iter>;
  359. static_assert(_IsMove
  360. ? is_move_assignable_v<_ValueTypeI>
  361. : is_copy_assignable_v<_ValueTypeI>);
  362. auto __num = __last - __first;
  363. if (__num)
  364. __builtin_memmove(__result - __num, __first,
  365. sizeof(_ValueTypeI) * __num);
  366. return {__first + __num, __result - __num};
  367. }
  368. }
  369. auto __lasti = ranges::next(__first, __last);
  370. auto __tail = __lasti;
  371. for (auto __n = __last - __first; __n > 0; --__n)
  372. {
  373. --__tail;
  374. --__result;
  375. if constexpr (_IsMove)
  376. *__result = std::move(*__tail);
  377. else
  378. *__result = *__tail;
  379. }
  380. return {std::move(__lasti), std::move(__result)};
  381. }
  382. else
  383. {
  384. auto __lasti = ranges::next(__first, __last);
  385. auto __tail = __lasti;
  386. while (__first != __tail)
  387. {
  388. --__tail;
  389. --__result;
  390. if constexpr (_IsMove)
  391. *__result = std::move(*__tail);
  392. else
  393. *__result = *__tail;
  394. }
  395. return {std::move(__lasti), std::move(__result)};
  396. }
  397. }
  398. struct __copy_backward_fn
  399. {
  400. template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  401. bidirectional_iterator _Iter2>
  402. requires indirectly_copyable<_Iter1, _Iter2>
  403. constexpr copy_backward_result<_Iter1, _Iter2>
  404. operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
  405. {
  406. return ranges::__copy_or_move_backward<false>(std::move(__first),
  407. std::move(__last),
  408. std::move(__result));
  409. }
  410. template<bidirectional_range _Range, bidirectional_iterator _Iter>
  411. requires indirectly_copyable<iterator_t<_Range>, _Iter>
  412. constexpr copy_backward_result<borrowed_iterator_t<_Range>, _Iter>
  413. operator()(_Range&& __r, _Iter __result) const
  414. {
  415. return (*this)(ranges::begin(__r), ranges::end(__r),
  416. std::move(__result));
  417. }
  418. };
  419. inline constexpr __copy_backward_fn copy_backward{};
  420. struct __move_backward_fn
  421. {
  422. template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  423. bidirectional_iterator _Iter2>
  424. requires indirectly_movable<_Iter1, _Iter2>
  425. constexpr move_backward_result<_Iter1, _Iter2>
  426. operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
  427. {
  428. return ranges::__copy_or_move_backward<true>(std::move(__first),
  429. std::move(__last),
  430. std::move(__result));
  431. }
  432. template<bidirectional_range _Range, bidirectional_iterator _Iter>
  433. requires indirectly_movable<iterator_t<_Range>, _Iter>
  434. constexpr move_backward_result<borrowed_iterator_t<_Range>, _Iter>
  435. operator()(_Range&& __r, _Iter __result) const
  436. {
  437. return (*this)(ranges::begin(__r), ranges::end(__r),
  438. std::move(__result));
  439. }
  440. };
  441. inline constexpr __move_backward_fn move_backward{};
  442. template<typename _Iter, typename _Out>
  443. using copy_n_result = in_out_result<_Iter, _Out>;
  444. struct __copy_n_fn
  445. {
  446. template<input_iterator _Iter, weakly_incrementable _Out>
  447. requires indirectly_copyable<_Iter, _Out>
  448. constexpr copy_n_result<_Iter, _Out>
  449. operator()(_Iter __first, iter_difference_t<_Iter> __n,
  450. _Out __result) const
  451. {
  452. if constexpr (random_access_iterator<_Iter>)
  453. {
  454. if (__n > 0)
  455. return ranges::copy(__first, __first + __n, std::move(__result));
  456. }
  457. else
  458. {
  459. for (; __n > 0; --__n, (void)++__result, (void)++__first)
  460. *__result = *__first;
  461. }
  462. return {std::move(__first), std::move(__result)};
  463. }
  464. };
  465. inline constexpr __copy_n_fn copy_n{};
  466. struct __fill_n_fn
  467. {
  468. template<typename _Tp, output_iterator<const _Tp&> _Out>
  469. constexpr _Out
  470. operator()(_Out __first, iter_difference_t<_Out> __n,
  471. const _Tp& __value) const
  472. {
  473. // TODO: implement more specializations to be at least on par with
  474. // std::fill_n
  475. if (__n <= 0)
  476. return __first;
  477. if constexpr (is_scalar_v<_Tp>)
  478. {
  479. // TODO: Generalize this optimization to contiguous iterators.
  480. if constexpr (is_pointer_v<_Out>
  481. // Note that __is_byte already implies !is_volatile.
  482. && __is_byte<remove_pointer_t<_Out>>::__value
  483. && integral<_Tp>)
  484. {
  485. if (!std::__is_constant_evaluated())
  486. {
  487. __builtin_memset(__first,
  488. static_cast<unsigned char>(__value),
  489. __n);
  490. return __first + __n;
  491. }
  492. }
  493. const auto __tmp = __value;
  494. for (; __n > 0; --__n, (void)++__first)
  495. *__first = __tmp;
  496. return __first;
  497. }
  498. else
  499. {
  500. for (; __n > 0; --__n, (void)++__first)
  501. *__first = __value;
  502. return __first;
  503. }
  504. }
  505. };
  506. inline constexpr __fill_n_fn fill_n{};
  507. struct __fill_fn
  508. {
  509. template<typename _Tp,
  510. output_iterator<const _Tp&> _Out, sentinel_for<_Out> _Sent>
  511. constexpr _Out
  512. operator()(_Out __first, _Sent __last, const _Tp& __value) const
  513. {
  514. // TODO: implement more specializations to be at least on par with
  515. // std::fill
  516. if constexpr (sized_sentinel_for<_Sent, _Out>)
  517. {
  518. const auto __len = __last - __first;
  519. return ranges::fill_n(__first, __len, __value);
  520. }
  521. else if constexpr (is_scalar_v<_Tp>)
  522. {
  523. const auto __tmp = __value;
  524. for (; __first != __last; ++__first)
  525. *__first = __tmp;
  526. return __first;
  527. }
  528. else
  529. {
  530. for (; __first != __last; ++__first)
  531. *__first = __value;
  532. return __first;
  533. }
  534. }
  535. template<typename _Tp, output_range<const _Tp&> _Range>
  536. constexpr borrowed_iterator_t<_Range>
  537. operator()(_Range&& __r, const _Tp& __value) const
  538. {
  539. return (*this)(ranges::begin(__r), ranges::end(__r), __value);
  540. }
  541. };
  542. inline constexpr __fill_fn fill{};
  543. }
  544. _GLIBCXX_END_NAMESPACE_VERSION
  545. } // namespace std
  546. #endif // concepts
  547. #endif // C++20
  548. #endif // _RANGES_ALGOBASE_H