No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

598 líneas
18KB

  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. && same_as<_Iter1, _Sent1>)
  74. return (*this)(__first1.base(), __last1.base(),
  75. std::move(__first2), std::move(__last2),
  76. std::move(__pred),
  77. std::move(__proj1), std::move(__proj2));
  78. else if constexpr (__detail::__is_normal_iterator<_Iter2>
  79. && same_as<_Iter2, _Sent2>)
  80. return (*this)(std::move(__first1), std::move(__last1),
  81. __first2.base(), __last2.base(),
  82. std::move(__pred),
  83. std::move(__proj1), std::move(__proj2));
  84. else if constexpr (sized_sentinel_for<_Sent1, _Iter1>
  85. && sized_sentinel_for<_Sent2, _Iter2>)
  86. {
  87. auto __d1 = ranges::distance(__first1, __last1);
  88. auto __d2 = ranges::distance(__first2, __last2);
  89. if (__d1 != __d2)
  90. return false;
  91. using _ValueType1 = iter_value_t<_Iter1>;
  92. using _ValueType2 = iter_value_t<_Iter2>;
  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>(__first, __last, __result.base());
  223. return {std::move(__in), decltype(__result){__out}};
  224. }
  225. else if constexpr (sized_sentinel_for<_Sent, _Iter>)
  226. {
  227. #ifdef __cpp_lib_is_constant_evaluated
  228. if (!std::is_constant_evaluated())
  229. #endif
  230. {
  231. if constexpr (__memcpyable<_Iter, _Out>::__value)
  232. {
  233. using _ValueTypeI = iter_value_t<_Iter>;
  234. static_assert(_IsMove
  235. ? is_move_assignable_v<_ValueTypeI>
  236. : is_copy_assignable_v<_ValueTypeI>);
  237. auto __num = __last - __first;
  238. if (__num)
  239. __builtin_memmove(__result, __first,
  240. sizeof(_ValueTypeI) * __num);
  241. return {__first + __num, __result + __num};
  242. }
  243. }
  244. for (auto __n = __last - __first; __n > 0; --__n)
  245. {
  246. if constexpr (_IsMove)
  247. *__result = std::move(*__first);
  248. else
  249. *__result = *__first;
  250. ++__first;
  251. ++__result;
  252. }
  253. return {std::move(__first), std::move(__result)};
  254. }
  255. else
  256. {
  257. while (__first != __last)
  258. {
  259. if constexpr (_IsMove)
  260. *__result = std::move(*__first);
  261. else
  262. *__result = *__first;
  263. ++__first;
  264. ++__result;
  265. }
  266. return {std::move(__first), std::move(__result)};
  267. }
  268. }
  269. struct __copy_fn
  270. {
  271. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  272. weakly_incrementable _Out>
  273. requires indirectly_copyable<_Iter, _Out>
  274. constexpr copy_result<_Iter, _Out>
  275. operator()(_Iter __first, _Sent __last, _Out __result) const
  276. {
  277. return ranges::__copy_or_move<false>(std::move(__first),
  278. std::move(__last),
  279. std::move(__result));
  280. }
  281. template<input_range _Range, weakly_incrementable _Out>
  282. requires indirectly_copyable<iterator_t<_Range>, _Out>
  283. constexpr copy_result<borrowed_iterator_t<_Range>, _Out>
  284. operator()(_Range&& __r, _Out __result) const
  285. {
  286. return (*this)(ranges::begin(__r), ranges::end(__r),
  287. std::move(__result));
  288. }
  289. };
  290. inline constexpr __copy_fn copy{};
  291. struct __move_fn
  292. {
  293. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  294. weakly_incrementable _Out>
  295. requires indirectly_movable<_Iter, _Out>
  296. constexpr move_result<_Iter, _Out>
  297. operator()(_Iter __first, _Sent __last, _Out __result) const
  298. {
  299. return ranges::__copy_or_move<true>(std::move(__first),
  300. std::move(__last),
  301. std::move(__result));
  302. }
  303. template<input_range _Range, weakly_incrementable _Out>
  304. requires indirectly_movable<iterator_t<_Range>, _Out>
  305. constexpr move_result<borrowed_iterator_t<_Range>, _Out>
  306. operator()(_Range&& __r, _Out __result) const
  307. {
  308. return (*this)(ranges::begin(__r), ranges::end(__r),
  309. std::move(__result));
  310. }
  311. };
  312. inline constexpr __move_fn move{};
  313. template<bool _IsMove,
  314. bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  315. bidirectional_iterator _Out>
  316. requires (_IsMove
  317. ? indirectly_movable<_Iter, _Out>
  318. : indirectly_copyable<_Iter, _Out>)
  319. constexpr conditional_t<_IsMove,
  320. move_backward_result<_Iter, _Out>,
  321. copy_backward_result<_Iter, _Out>>
  322. __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result)
  323. {
  324. // TODO: implement more specializations to be at least on par with
  325. // std::copy_backward/std::move_backward.
  326. using __detail::__is_reverse_iterator;
  327. using __detail::__is_normal_iterator;
  328. if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
  329. && __is_reverse_iterator<_Out>)
  330. {
  331. auto [__in,__out]
  332. = ranges::__copy_or_move<_IsMove>(std::move(__last).base(),
  333. std::move(__first).base(),
  334. std::move(__result).base());
  335. return {reverse_iterator{std::move(__in)},
  336. reverse_iterator{std::move(__out)}};
  337. }
  338. else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
  339. {
  340. auto [__in,__out]
  341. = ranges::__copy_or_move_backward<_IsMove>(__first.base(),
  342. __last.base(),
  343. std::move(__result));
  344. return {decltype(__first){__in}, std::move(__out)};
  345. }
  346. else if constexpr (__is_normal_iterator<_Out>)
  347. {
  348. auto [__in,__out]
  349. = ranges::__copy_or_move_backward<_IsMove>(std::move(__first),
  350. std::move(__last),
  351. __result.base());
  352. return {std::move(__in), decltype(__result){__out}};
  353. }
  354. else if constexpr (sized_sentinel_for<_Sent, _Iter>)
  355. {
  356. #ifdef __cpp_lib_is_constant_evaluated
  357. if (!std::is_constant_evaluated())
  358. #endif
  359. {
  360. if constexpr (__memcpyable<_Out, _Iter>::__value)
  361. {
  362. using _ValueTypeI = iter_value_t<_Iter>;
  363. static_assert(_IsMove
  364. ? is_move_assignable_v<_ValueTypeI>
  365. : is_copy_assignable_v<_ValueTypeI>);
  366. auto __num = __last - __first;
  367. if (__num)
  368. __builtin_memmove(__result - __num, __first,
  369. sizeof(_ValueTypeI) * __num);
  370. return {__first + __num, __result - __num};
  371. }
  372. }
  373. auto __lasti = ranges::next(__first, __last);
  374. auto __tail = __lasti;
  375. for (auto __n = __last - __first; __n > 0; --__n)
  376. {
  377. --__tail;
  378. --__result;
  379. if constexpr (_IsMove)
  380. *__result = std::move(*__tail);
  381. else
  382. *__result = *__tail;
  383. }
  384. return {std::move(__lasti), std::move(__result)};
  385. }
  386. else
  387. {
  388. auto __lasti = ranges::next(__first, __last);
  389. auto __tail = __lasti;
  390. while (__first != __tail)
  391. {
  392. --__tail;
  393. --__result;
  394. if constexpr (_IsMove)
  395. *__result = std::move(*__tail);
  396. else
  397. *__result = *__tail;
  398. }
  399. return {std::move(__lasti), std::move(__result)};
  400. }
  401. }
  402. struct __copy_backward_fn
  403. {
  404. template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  405. bidirectional_iterator _Iter2>
  406. requires indirectly_copyable<_Iter1, _Iter2>
  407. constexpr copy_backward_result<_Iter1, _Iter2>
  408. operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
  409. {
  410. return ranges::__copy_or_move_backward<false>(std::move(__first),
  411. std::move(__last),
  412. std::move(__result));
  413. }
  414. template<bidirectional_range _Range, bidirectional_iterator _Iter>
  415. requires indirectly_copyable<iterator_t<_Range>, _Iter>
  416. constexpr copy_backward_result<borrowed_iterator_t<_Range>, _Iter>
  417. operator()(_Range&& __r, _Iter __result) const
  418. {
  419. return (*this)(ranges::begin(__r), ranges::end(__r),
  420. std::move(__result));
  421. }
  422. };
  423. inline constexpr __copy_backward_fn copy_backward{};
  424. struct __move_backward_fn
  425. {
  426. template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  427. bidirectional_iterator _Iter2>
  428. requires indirectly_movable<_Iter1, _Iter2>
  429. constexpr move_backward_result<_Iter1, _Iter2>
  430. operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
  431. {
  432. return ranges::__copy_or_move_backward<true>(std::move(__first),
  433. std::move(__last),
  434. std::move(__result));
  435. }
  436. template<bidirectional_range _Range, bidirectional_iterator _Iter>
  437. requires indirectly_movable<iterator_t<_Range>, _Iter>
  438. constexpr move_backward_result<borrowed_iterator_t<_Range>, _Iter>
  439. operator()(_Range&& __r, _Iter __result) const
  440. {
  441. return (*this)(ranges::begin(__r), ranges::end(__r),
  442. std::move(__result));
  443. }
  444. };
  445. inline constexpr __move_backward_fn move_backward{};
  446. template<typename _Iter, typename _Out>
  447. using copy_n_result = in_out_result<_Iter, _Out>;
  448. struct __copy_n_fn
  449. {
  450. template<input_iterator _Iter, weakly_incrementable _Out>
  451. requires indirectly_copyable<_Iter, _Out>
  452. constexpr copy_n_result<_Iter, _Out>
  453. operator()(_Iter __first, iter_difference_t<_Iter> __n,
  454. _Out __result) const
  455. {
  456. if constexpr (random_access_iterator<_Iter>)
  457. return ranges::copy(__first, __first + __n, std::move(__result));
  458. else
  459. {
  460. for (; __n > 0; --__n, (void)++__result, (void)++__first)
  461. *__result = *__first;
  462. return {std::move(__first), std::move(__result)};
  463. }
  464. }
  465. };
  466. inline constexpr __copy_n_fn copy_n{};
  467. struct __fill_n_fn
  468. {
  469. template<typename _Tp, output_iterator<const _Tp&> _Out>
  470. constexpr _Out
  471. operator()(_Out __first, iter_difference_t<_Out> __n,
  472. const _Tp& __value) const
  473. {
  474. // TODO: implement more specializations to be at least on par with
  475. // std::fill_n
  476. if (__n <= 0)
  477. return __first;
  478. // TODO: Generalize this optimization to contiguous iterators.
  479. if constexpr (is_pointer_v<_Out>
  480. // Note that __is_byte already implies !is_volatile.
  481. && __is_byte<remove_pointer_t<_Out>>::__value
  482. && integral<_Tp>)
  483. {
  484. __builtin_memset(__first, static_cast<unsigned char>(__value), __n);
  485. return __first + __n;
  486. }
  487. else if constexpr (is_scalar_v<_Tp>)
  488. {
  489. const auto __tmp = __value;
  490. for (; __n > 0; --__n, (void)++__first)
  491. *__first = __tmp;
  492. return __first;
  493. }
  494. else
  495. {
  496. for (; __n > 0; --__n, (void)++__first)
  497. *__first = __value;
  498. return __first;
  499. }
  500. }
  501. };
  502. inline constexpr __fill_n_fn fill_n{};
  503. struct __fill_fn
  504. {
  505. template<typename _Tp,
  506. output_iterator<const _Tp&> _Out, sentinel_for<_Out> _Sent>
  507. constexpr _Out
  508. operator()(_Out __first, _Sent __last, const _Tp& __value) const
  509. {
  510. // TODO: implement more specializations to be at least on par with
  511. // std::fill
  512. if constexpr (sized_sentinel_for<_Sent, _Out>)
  513. {
  514. const auto __len = __last - __first;
  515. return ranges::fill_n(__first, __len, __value);
  516. }
  517. else if constexpr (is_scalar_v<_Tp>)
  518. {
  519. const auto __tmp = __value;
  520. for (; __first != __last; ++__first)
  521. *__first = __tmp;
  522. return __first;
  523. }
  524. else
  525. {
  526. for (; __first != __last; ++__first)
  527. *__first = __value;
  528. return __first;
  529. }
  530. }
  531. template<typename _Tp, output_range<const _Tp&> _Range>
  532. constexpr borrowed_iterator_t<_Range>
  533. operator()(_Range&& __r, const _Tp& __value) const
  534. {
  535. return (*this)(ranges::begin(__r), ranges::end(__r), __value);
  536. }
  537. };
  538. inline constexpr __fill_fn fill{};
  539. }
  540. _GLIBCXX_END_NAMESPACE_VERSION
  541. } // namespace std
  542. #endif // concepts
  543. #endif // C++20
  544. #endif // _RANGES_ALGOBASE_H