ranges_algobase.h 18 KB

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