ranges_algobase.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // Core algorithmic facilities -*- C++ -*-
  2. // Copyright (C) 2020-2021 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/ranges_base.h> // ranges::begin, ranges::range etc.
  30. #include <bits/invoke.h> // __invoke
  31. #include <bits/cpp_type_traits.h> // __is_byte
  32. #if __cpp_lib_concepts
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. namespace ranges
  37. {
  38. namespace __detail
  39. {
  40. template<typename _Tp>
  41. constexpr inline bool __is_normal_iterator = false;
  42. template<typename _Iterator, typename _Container>
  43. constexpr inline bool
  44. __is_normal_iterator<__gnu_cxx::__normal_iterator<_Iterator,
  45. _Container>> = true;
  46. template<typename _Tp>
  47. constexpr inline bool __is_reverse_iterator = false;
  48. template<typename _Iterator>
  49. constexpr inline bool
  50. __is_reverse_iterator<reverse_iterator<_Iterator>> = true;
  51. template<typename _Tp>
  52. constexpr inline bool __is_move_iterator = false;
  53. template<typename _Iterator>
  54. constexpr inline bool
  55. __is_move_iterator<move_iterator<_Iterator>> = true;
  56. } // namespace __detail
  57. struct __equal_fn
  58. {
  59. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  60. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  61. typename _Pred = ranges::equal_to,
  62. typename _Proj1 = identity, typename _Proj2 = identity>
  63. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  64. constexpr bool
  65. operator()(_Iter1 __first1, _Sent1 __last1,
  66. _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
  67. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  68. {
  69. // TODO: implement more specializations to at least have parity with
  70. // std::equal.
  71. if constexpr (__detail::__is_normal_iterator<_Iter1>
  72. && same_as<_Iter1, _Sent1>)
  73. return (*this)(__first1.base(), __last1.base(),
  74. std::move(__first2), std::move(__last2),
  75. std::move(__pred),
  76. std::move(__proj1), std::move(__proj2));
  77. else if constexpr (__detail::__is_normal_iterator<_Iter2>
  78. && same_as<_Iter2, _Sent2>)
  79. return (*this)(std::move(__first1), std::move(__last1),
  80. __first2.base(), __last2.base(),
  81. std::move(__pred),
  82. std::move(__proj1), std::move(__proj2));
  83. else if constexpr (sized_sentinel_for<_Sent1, _Iter1>
  84. && sized_sentinel_for<_Sent2, _Iter2>)
  85. {
  86. auto __d1 = ranges::distance(__first1, __last1);
  87. auto __d2 = ranges::distance(__first2, __last2);
  88. if (__d1 != __d2)
  89. return false;
  90. using _ValueType1 = iter_value_t<_Iter1>;
  91. constexpr bool __use_memcmp
  92. = ((is_integral_v<_ValueType1> || is_pointer_v<_ValueType1>)
  93. && __memcmpable<_Iter1, _Iter2>::__value
  94. && is_same_v<_Pred, ranges::equal_to>
  95. && is_same_v<_Proj1, identity>
  96. && is_same_v<_Proj2, identity>);
  97. if constexpr (__use_memcmp)
  98. {
  99. if (const size_t __len = (__last1 - __first1))
  100. return !std::__memcmp(__first1, __first2, __len);
  101. return true;
  102. }
  103. else
  104. {
  105. for (; __first1 != __last1; ++__first1, (void)++__first2)
  106. if (!(bool)std::__invoke(__pred,
  107. std::__invoke(__proj1, *__first1),
  108. std::__invoke(__proj2, *__first2)))
  109. return false;
  110. return true;
  111. }
  112. }
  113. else
  114. {
  115. for (; __first1 != __last1 && __first2 != __last2;
  116. ++__first1, (void)++__first2)
  117. if (!(bool)std::__invoke(__pred,
  118. std::__invoke(__proj1, *__first1),
  119. std::__invoke(__proj2, *__first2)))
  120. return false;
  121. return __first1 == __last1 && __first2 == __last2;
  122. }
  123. }
  124. template<input_range _Range1, input_range _Range2,
  125. typename _Pred = ranges::equal_to,
  126. typename _Proj1 = identity, typename _Proj2 = identity>
  127. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
  128. _Pred, _Proj1, _Proj2>
  129. constexpr bool
  130. operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
  131. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  132. {
  133. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  134. ranges::begin(__r2), ranges::end(__r2),
  135. std::move(__pred),
  136. std::move(__proj1), std::move(__proj2));
  137. }
  138. };
  139. inline constexpr __equal_fn equal{};
  140. template<typename _Iter, typename _Out>
  141. struct in_out_result
  142. {
  143. [[no_unique_address]] _Iter in;
  144. [[no_unique_address]] _Out out;
  145. template<typename _Iter2, typename _Out2>
  146. requires convertible_to<const _Iter&, _Iter2>
  147. && convertible_to<const _Out&, _Out2>
  148. constexpr
  149. operator in_out_result<_Iter2, _Out2>() const &
  150. { return {in, out}; }
  151. template<typename _Iter2, typename _Out2>
  152. requires convertible_to<_Iter, _Iter2>
  153. && convertible_to<_Out, _Out2>
  154. constexpr
  155. operator in_out_result<_Iter2, _Out2>() &&
  156. { return {std::move(in), std::move(out)}; }
  157. };
  158. template<typename _Iter, typename _Out>
  159. using copy_result = in_out_result<_Iter, _Out>;
  160. template<typename _Iter, typename _Out>
  161. using move_result = in_out_result<_Iter, _Out>;
  162. template<typename _Iter1, typename _Iter2>
  163. using move_backward_result = in_out_result<_Iter1, _Iter2>;
  164. template<typename _Iter1, typename _Iter2>
  165. using copy_backward_result = in_out_result<_Iter1, _Iter2>;
  166. template<bool _IsMove,
  167. bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  168. bidirectional_iterator _Out>
  169. requires (_IsMove
  170. ? indirectly_movable<_Iter, _Out>
  171. : indirectly_copyable<_Iter, _Out>)
  172. constexpr conditional_t<_IsMove,
  173. move_backward_result<_Iter, _Out>,
  174. copy_backward_result<_Iter, _Out>>
  175. __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result);
  176. template<bool _IsMove,
  177. input_iterator _Iter, sentinel_for<_Iter> _Sent,
  178. weakly_incrementable _Out>
  179. requires (_IsMove
  180. ? indirectly_movable<_Iter, _Out>
  181. : indirectly_copyable<_Iter, _Out>)
  182. constexpr conditional_t<_IsMove,
  183. move_result<_Iter, _Out>,
  184. copy_result<_Iter, _Out>>
  185. __copy_or_move(_Iter __first, _Sent __last, _Out __result)
  186. {
  187. // TODO: implement more specializations to be at least on par with
  188. // std::copy/std::move.
  189. using __detail::__is_move_iterator;
  190. using __detail::__is_reverse_iterator;
  191. using __detail::__is_normal_iterator;
  192. if constexpr (__is_move_iterator<_Iter> && same_as<_Iter, _Sent>)
  193. {
  194. auto [__in, __out]
  195. = ranges::__copy_or_move<true>(std::move(__first).base(),
  196. std::move(__last).base(),
  197. std::move(__result));
  198. return {move_iterator{std::move(__in)}, std::move(__out)};
  199. }
  200. else if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
  201. && __is_reverse_iterator<_Out>)
  202. {
  203. auto [__in,__out]
  204. = ranges::__copy_or_move_backward<_IsMove>(std::move(__last).base(),
  205. std::move(__first).base(),
  206. std::move(__result).base());
  207. return {reverse_iterator{std::move(__in)},
  208. reverse_iterator{std::move(__out)}};
  209. }
  210. else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
  211. {
  212. auto [__in,__out]
  213. = ranges::__copy_or_move<_IsMove>(__first.base(), __last.base(),
  214. __result);
  215. return {decltype(__first){__in}, std::move(__out)};
  216. }
  217. else if constexpr (__is_normal_iterator<_Out>)
  218. {
  219. auto [__in,__out]
  220. = ranges::__copy_or_move<_IsMove>(__first, __last, __result.base());
  221. return {std::move(__in), decltype(__result){__out}};
  222. }
  223. else if constexpr (sized_sentinel_for<_Sent, _Iter>)
  224. {
  225. #ifdef __cpp_lib_is_constant_evaluated
  226. if (!std::is_constant_evaluated())
  227. #endif
  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. #ifdef __cpp_lib_is_constant_evaluated
  355. if (!std::is_constant_evaluated())
  356. #endif
  357. {
  358. if constexpr (__memcpyable<_Out, _Iter>::__value)
  359. {
  360. using _ValueTypeI = iter_value_t<_Iter>;
  361. static_assert(_IsMove
  362. ? is_move_assignable_v<_ValueTypeI>
  363. : is_copy_assignable_v<_ValueTypeI>);
  364. auto __num = __last - __first;
  365. if (__num)
  366. __builtin_memmove(__result - __num, __first,
  367. sizeof(_ValueTypeI) * __num);
  368. return {__first + __num, __result - __num};
  369. }
  370. }
  371. auto __lasti = ranges::next(__first, __last);
  372. auto __tail = __lasti;
  373. for (auto __n = __last - __first; __n > 0; --__n)
  374. {
  375. --__tail;
  376. --__result;
  377. if constexpr (_IsMove)
  378. *__result = std::move(*__tail);
  379. else
  380. *__result = *__tail;
  381. }
  382. return {std::move(__lasti), std::move(__result)};
  383. }
  384. else
  385. {
  386. auto __lasti = ranges::next(__first, __last);
  387. auto __tail = __lasti;
  388. while (__first != __tail)
  389. {
  390. --__tail;
  391. --__result;
  392. if constexpr (_IsMove)
  393. *__result = std::move(*__tail);
  394. else
  395. *__result = *__tail;
  396. }
  397. return {std::move(__lasti), std::move(__result)};
  398. }
  399. }
  400. struct __copy_backward_fn
  401. {
  402. template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  403. bidirectional_iterator _Iter2>
  404. requires indirectly_copyable<_Iter1, _Iter2>
  405. constexpr copy_backward_result<_Iter1, _Iter2>
  406. operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
  407. {
  408. return ranges::__copy_or_move_backward<false>(std::move(__first),
  409. std::move(__last),
  410. std::move(__result));
  411. }
  412. template<bidirectional_range _Range, bidirectional_iterator _Iter>
  413. requires indirectly_copyable<iterator_t<_Range>, _Iter>
  414. constexpr copy_backward_result<borrowed_iterator_t<_Range>, _Iter>
  415. operator()(_Range&& __r, _Iter __result) const
  416. {
  417. return (*this)(ranges::begin(__r), ranges::end(__r),
  418. std::move(__result));
  419. }
  420. };
  421. inline constexpr __copy_backward_fn copy_backward{};
  422. struct __move_backward_fn
  423. {
  424. template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  425. bidirectional_iterator _Iter2>
  426. requires indirectly_movable<_Iter1, _Iter2>
  427. constexpr move_backward_result<_Iter1, _Iter2>
  428. operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
  429. {
  430. return ranges::__copy_or_move_backward<true>(std::move(__first),
  431. std::move(__last),
  432. std::move(__result));
  433. }
  434. template<bidirectional_range _Range, bidirectional_iterator _Iter>
  435. requires indirectly_movable<iterator_t<_Range>, _Iter>
  436. constexpr move_backward_result<borrowed_iterator_t<_Range>, _Iter>
  437. operator()(_Range&& __r, _Iter __result) const
  438. {
  439. return (*this)(ranges::begin(__r), ranges::end(__r),
  440. std::move(__result));
  441. }
  442. };
  443. inline constexpr __move_backward_fn move_backward{};
  444. template<typename _Iter, typename _Out>
  445. using copy_n_result = in_out_result<_Iter, _Out>;
  446. struct __copy_n_fn
  447. {
  448. template<input_iterator _Iter, weakly_incrementable _Out>
  449. requires indirectly_copyable<_Iter, _Out>
  450. constexpr copy_n_result<_Iter, _Out>
  451. operator()(_Iter __first, iter_difference_t<_Iter> __n,
  452. _Out __result) const
  453. {
  454. if constexpr (random_access_iterator<_Iter>)
  455. {
  456. if (__n > 0)
  457. return ranges::copy(__first, __first + __n, std::move(__result));
  458. }
  459. else
  460. {
  461. for (; __n > 0; --__n, (void)++__result, (void)++__first)
  462. *__result = *__first;
  463. }
  464. return {std::move(__first), std::move(__result)};
  465. }
  466. };
  467. inline constexpr __copy_n_fn copy_n{};
  468. struct __fill_n_fn
  469. {
  470. template<typename _Tp, output_iterator<const _Tp&> _Out>
  471. constexpr _Out
  472. operator()(_Out __first, iter_difference_t<_Out> __n,
  473. const _Tp& __value) const
  474. {
  475. // TODO: implement more specializations to be at least on par with
  476. // std::fill_n
  477. if (__n <= 0)
  478. return __first;
  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. __builtin_memset(__first, static_cast<unsigned char>(__value), __n);
  486. return __first + __n;
  487. }
  488. else if constexpr (is_scalar_v<_Tp>)
  489. {
  490. const auto __tmp = __value;
  491. for (; __n > 0; --__n, (void)++__first)
  492. *__first = __tmp;
  493. return __first;
  494. }
  495. else
  496. {
  497. for (; __n > 0; --__n, (void)++__first)
  498. *__first = __value;
  499. return __first;
  500. }
  501. }
  502. };
  503. inline constexpr __fill_n_fn fill_n{};
  504. struct __fill_fn
  505. {
  506. template<typename _Tp,
  507. output_iterator<const _Tp&> _Out, sentinel_for<_Out> _Sent>
  508. constexpr _Out
  509. operator()(_Out __first, _Sent __last, const _Tp& __value) const
  510. {
  511. // TODO: implement more specializations to be at least on par with
  512. // std::fill
  513. if constexpr (sized_sentinel_for<_Sent, _Out>)
  514. {
  515. const auto __len = __last - __first;
  516. return ranges::fill_n(__first, __len, __value);
  517. }
  518. else if constexpr (is_scalar_v<_Tp>)
  519. {
  520. const auto __tmp = __value;
  521. for (; __first != __last; ++__first)
  522. *__first = __tmp;
  523. return __first;
  524. }
  525. else
  526. {
  527. for (; __first != __last; ++__first)
  528. *__first = __value;
  529. return __first;
  530. }
  531. }
  532. template<typename _Tp, output_range<const _Tp&> _Range>
  533. constexpr borrowed_iterator_t<_Range>
  534. operator()(_Range&& __r, const _Tp& __value) const
  535. {
  536. return (*this)(ranges::begin(__r), ranges::end(__r), __value);
  537. }
  538. };
  539. inline constexpr __fill_fn fill{};
  540. }
  541. _GLIBCXX_END_NAMESPACE_VERSION
  542. } // namespace std
  543. #endif // concepts
  544. #endif // C++20
  545. #endif // _RANGES_ALGOBASE_H