libstdc++
ranges_algobase.h
Go to the documentation of this file.
1// Core algorithmic facilities -*- C++ -*-
2
3// Copyright (C) 2020-2024 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/ranges_algobase.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{algorithm}
28 */
29
30#ifndef _RANGES_ALGOBASE_H
31#define _RANGES_ALGOBASE_H 1
32
33#if __cplusplus > 201703L
34
35#include <compare>
37#include <bits/stl_iterator.h>
38#include <bits/ranges_base.h> // ranges::begin, ranges::range etc.
39#include <bits/invoke.h> // __invoke
40#include <bits/cpp_type_traits.h> // __is_byte
41#include <bits/stl_algobase.h> // __memcmp
42
43#if __cpp_lib_concepts
44namespace std _GLIBCXX_VISIBILITY(default)
45{
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
47namespace ranges
48{
49 namespace __detail
50 {
51 template<typename _Tp>
52 constexpr inline bool __is_normal_iterator = false;
53
54 template<typename _Iterator, typename _Container>
55 constexpr inline bool
56 __is_normal_iterator<__gnu_cxx::__normal_iterator<_Iterator,
57 _Container>> = true;
58
59 template<typename _Tp>
60 constexpr inline bool __is_reverse_iterator = false;
61
62 template<typename _Iterator>
63 constexpr inline bool
64 __is_reverse_iterator<reverse_iterator<_Iterator>> = true;
65
66 template<typename _Tp>
67 constexpr inline bool __is_move_iterator = false;
68
69 template<typename _Iterator>
70 constexpr inline bool
71 __is_move_iterator<move_iterator<_Iterator>> = true;
72 } // namespace __detail
73
74#if __glibcxx_ranges_iota >= 202202L // C++ >= 23
75 template<typename _Out, typename _Tp>
76 struct out_value_result
77 {
78 [[no_unique_address]] _Out out;
79 [[no_unique_address]] _Tp value;
80
81 template<typename _Out2, typename _Tp2>
82 requires convertible_to<const _Out&, _Out2>
83 && convertible_to<const _Tp&, _Tp2>
84 constexpr
85 operator out_value_result<_Out2, _Tp2>() const &
86 { return {out, value}; }
87
88 template<typename _Out2, typename _Tp2>
89 requires convertible_to<_Out, _Out2>
90 && convertible_to<_Tp, _Tp2>
91 constexpr
92 operator out_value_result<_Out2, _Tp2>() &&
93 { return {std::move(out), std::move(value)}; }
94 };
95
96 template<typename _Out, typename _Tp>
97 using iota_result = out_value_result<_Out, _Tp>;
98
99 struct __iota_fn
100 {
101 template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent, weakly_incrementable _Tp>
102 requires indirectly_writable<_Out, const _Tp&>
103 constexpr iota_result<_Out, _Tp>
104 operator()(_Out __first, _Sent __last, _Tp __value) const
105 {
106 while (__first != __last)
107 {
108 *__first = static_cast<const _Tp&>(__value);
109 ++__first;
110 ++__value;
111 }
112 return {std::move(__first), std::move(__value)};
113 }
114
115 template<weakly_incrementable _Tp, output_range<const _Tp&> _Range>
116 constexpr iota_result<borrowed_iterator_t<_Range>, _Tp>
117 operator()(_Range&& __r, _Tp __value) const
118 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__value)); }
119 };
120
121 inline constexpr __iota_fn iota{};
122#endif // __glibcxx_ranges_iota
123
124 struct __equal_fn
125 {
126 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
127 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
128 typename _Pred = ranges::equal_to,
129 typename _Proj1 = identity, typename _Proj2 = identity>
130 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
131 constexpr bool
132 operator()(_Iter1 __first1, _Sent1 __last1,
133 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
134 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
135 {
136 // TODO: implement more specializations to at least have parity with
137 // std::equal.
138 if constexpr (__detail::__is_normal_iterator<_Iter1>
139 && same_as<_Iter1, _Sent1>)
140 return (*this)(__first1.base(), __last1.base(),
141 std::move(__first2), std::move(__last2),
142 std::move(__pred),
143 std::move(__proj1), std::move(__proj2));
144 else if constexpr (__detail::__is_normal_iterator<_Iter2>
145 && same_as<_Iter2, _Sent2>)
146 return (*this)(std::move(__first1), std::move(__last1),
147 __first2.base(), __last2.base(),
148 std::move(__pred),
149 std::move(__proj1), std::move(__proj2));
150 else if constexpr (sized_sentinel_for<_Sent1, _Iter1>
151 && sized_sentinel_for<_Sent2, _Iter2>)
152 {
153 auto __d1 = ranges::distance(__first1, __last1);
154 auto __d2 = ranges::distance(__first2, __last2);
155 if (__d1 != __d2)
156 return false;
157
158 using _ValueType1 = iter_value_t<_Iter1>;
159 constexpr bool __use_memcmp
160 = ((is_integral_v<_ValueType1> || is_pointer_v<_ValueType1>)
161 && __memcmpable<_Iter1, _Iter2>::__value
162 && is_same_v<_Pred, ranges::equal_to>
163 && is_same_v<_Proj1, identity>
164 && is_same_v<_Proj2, identity>);
165 if constexpr (__use_memcmp)
166 {
167 if (const size_t __len = (__last1 - __first1))
168 return !std::__memcmp(__first1, __first2, __len);
169 return true;
170 }
171 else
172 {
173 for (; __first1 != __last1; ++__first1, (void)++__first2)
174 if (!(bool)std::__invoke(__pred,
175 std::__invoke(__proj1, *__first1),
176 std::__invoke(__proj2, *__first2)))
177 return false;
178 return true;
179 }
180 }
181 else
182 {
183 for (; __first1 != __last1 && __first2 != __last2;
184 ++__first1, (void)++__first2)
185 if (!(bool)std::__invoke(__pred,
186 std::__invoke(__proj1, *__first1),
187 std::__invoke(__proj2, *__first2)))
188 return false;
189 return __first1 == __last1 && __first2 == __last2;
190 }
191 }
192
193 template<input_range _Range1, input_range _Range2,
194 typename _Pred = ranges::equal_to,
195 typename _Proj1 = identity, typename _Proj2 = identity>
196 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
197 _Pred, _Proj1, _Proj2>
198 constexpr bool
199 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
200 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
201 {
202 // _GLIBCXX_RESOLVE_LIB_DEFECTS
203 // 3560. ranges::equal [...] should short-circuit for sized_ranges
204 if constexpr (sized_range<_Range1>)
205 if constexpr (sized_range<_Range2>)
206 if (ranges::distance(__r1) != ranges::distance(__r2))
207 return false;
208
209 return (*this)(ranges::begin(__r1), ranges::end(__r1),
210 ranges::begin(__r2), ranges::end(__r2),
211 std::move(__pred),
212 std::move(__proj1), std::move(__proj2));
213 }
214 };
215
216 inline constexpr __equal_fn equal{};
217
218namespace __detail
219{
220 template<bool _IsMove, typename _OutIter, typename _InIter>
221 [[__gnu__::__always_inline__]]
222 constexpr void
223 __assign_one(_OutIter& __out, _InIter& __in)
224 {
225 if constexpr (_IsMove)
226 *__out = ranges::iter_move(__in);
227 else
228 *__out = *__in;
229 }
230} // namespace __detail
231
232 template<typename _Iter, typename _Out>
233 struct in_out_result
234 {
235 [[no_unique_address]] _Iter in;
236 [[no_unique_address]] _Out out;
237
238 template<typename _Iter2, typename _Out2>
239 requires convertible_to<const _Iter&, _Iter2>
240 && convertible_to<const _Out&, _Out2>
241 constexpr
242 operator in_out_result<_Iter2, _Out2>() const &
243 { return {in, out}; }
244
245 template<typename _Iter2, typename _Out2>
246 requires convertible_to<_Iter, _Iter2>
247 && convertible_to<_Out, _Out2>
248 constexpr
249 operator in_out_result<_Iter2, _Out2>() &&
250 { return {std::move(in), std::move(out)}; }
251 };
252
253 template<typename _Iter, typename _Out>
254 using copy_result = in_out_result<_Iter, _Out>;
255
256 template<typename _Iter, typename _Out>
257 using move_result = in_out_result<_Iter, _Out>;
258
259 template<typename _Iter1, typename _Iter2>
260 using move_backward_result = in_out_result<_Iter1, _Iter2>;
261
262 template<typename _Iter1, typename _Iter2>
263 using copy_backward_result = in_out_result<_Iter1, _Iter2>;
264
265 template<bool _IsMove,
266 bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
267 bidirectional_iterator _Out>
268 requires (_IsMove
269 ? indirectly_movable<_Iter, _Out>
270 : indirectly_copyable<_Iter, _Out>)
271 constexpr __conditional_t<_IsMove,
272 move_backward_result<_Iter, _Out>,
273 copy_backward_result<_Iter, _Out>>
274 __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result);
275
276 template<bool _IsMove,
277 input_iterator _Iter, sentinel_for<_Iter> _Sent,
278 weakly_incrementable _Out>
279 requires (_IsMove
280 ? indirectly_movable<_Iter, _Out>
281 : indirectly_copyable<_Iter, _Out>)
282 constexpr __conditional_t<_IsMove,
283 move_result<_Iter, _Out>,
284 copy_result<_Iter, _Out>>
285 __copy_or_move(_Iter __first, _Sent __last, _Out __result)
286 {
287 // TODO: implement more specializations to be at least on par with
288 // std::copy/std::move.
289 using __detail::__is_move_iterator;
290 using __detail::__is_reverse_iterator;
291 using __detail::__is_normal_iterator;
292 if constexpr (__is_move_iterator<_Iter> && same_as<_Iter, _Sent>)
293 {
294 auto [__in, __out]
295 = ranges::__copy_or_move<true>(std::move(__first).base(),
296 std::move(__last).base(),
297 std::move(__result));
298 return {move_iterator{std::move(__in)}, std::move(__out)};
299 }
300 else if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
301 && __is_reverse_iterator<_Out>)
302 {
303 auto [__in,__out]
304 = ranges::__copy_or_move_backward<_IsMove>(std::move(__last).base(),
305 std::move(__first).base(),
306 std::move(__result).base());
307 return {reverse_iterator{std::move(__in)},
308 reverse_iterator{std::move(__out)}};
309 }
310 else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
311 {
312 auto [__in,__out]
313 = ranges::__copy_or_move<_IsMove>(__first.base(), __last.base(),
314 std::move(__result));
315 return {decltype(__first){__in}, std::move(__out)};
316 }
317 else if constexpr (__is_normal_iterator<_Out>)
318 {
319 auto [__in,__out]
320 = ranges::__copy_or_move<_IsMove>(std::move(__first), __last, __result.base());
321 return {std::move(__in), decltype(__result){__out}};
322 }
323 else if constexpr (sized_sentinel_for<_Sent, _Iter>)
324 {
325 if (!std::__is_constant_evaluated())
326 {
327 if constexpr (__memcpyable<_Out, _Iter>::__value)
328 {
329 using _ValueTypeI = iter_value_t<_Iter>;
330 auto __num = __last - __first;
331 if (__num > 1) [[likely]]
332 __builtin_memmove(__result, __first,
333 sizeof(_ValueTypeI) * __num);
334 else if (__num == 1)
335 __detail::__assign_one<_IsMove>(__result, __first);
336 return {__first + __num, __result + __num};
337 }
338 }
339
340 for (auto __n = __last - __first; __n > 0; --__n)
341 {
342 __detail::__assign_one<_IsMove>(__result, __first);
343 ++__first;
344 ++__result;
345 }
346 return {std::move(__first), std::move(__result)};
347 }
348 else
349 {
350 while (__first != __last)
351 {
352 __detail::__assign_one<_IsMove>(__result, __first);
353 ++__first;
354 ++__result;
355 }
356 return {std::move(__first), std::move(__result)};
357 }
358 }
359
360 struct __copy_fn
361 {
362 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
363 weakly_incrementable _Out>
364 requires indirectly_copyable<_Iter, _Out>
365 constexpr copy_result<_Iter, _Out>
366 operator()(_Iter __first, _Sent __last, _Out __result) const
367 {
368 return ranges::__copy_or_move<false>(std::move(__first),
369 std::move(__last),
370 std::move(__result));
371 }
372
373 template<input_range _Range, weakly_incrementable _Out>
374 requires indirectly_copyable<iterator_t<_Range>, _Out>
375 constexpr copy_result<borrowed_iterator_t<_Range>, _Out>
376 operator()(_Range&& __r, _Out __result) const
377 {
378 return (*this)(ranges::begin(__r), ranges::end(__r),
379 std::move(__result));
380 }
381 };
382
383 inline constexpr __copy_fn copy{};
384
385 struct __move_fn
386 {
387 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
388 weakly_incrementable _Out>
389 requires indirectly_movable<_Iter, _Out>
390 constexpr move_result<_Iter, _Out>
391 operator()(_Iter __first, _Sent __last, _Out __result) const
392 {
393 return ranges::__copy_or_move<true>(std::move(__first),
394 std::move(__last),
395 std::move(__result));
396 }
397
398 template<input_range _Range, weakly_incrementable _Out>
399 requires indirectly_movable<iterator_t<_Range>, _Out>
400 constexpr move_result<borrowed_iterator_t<_Range>, _Out>
401 operator()(_Range&& __r, _Out __result) const
402 {
403 return (*this)(ranges::begin(__r), ranges::end(__r),
404 std::move(__result));
405 }
406 };
407
408 inline constexpr __move_fn move{};
409
410 template<bool _IsMove,
411 bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
412 bidirectional_iterator _Out>
413 requires (_IsMove
414 ? indirectly_movable<_Iter, _Out>
415 : indirectly_copyable<_Iter, _Out>)
416 constexpr __conditional_t<_IsMove,
417 move_backward_result<_Iter, _Out>,
418 copy_backward_result<_Iter, _Out>>
419 __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result)
420 {
421 // TODO: implement more specializations to be at least on par with
422 // std::copy_backward/std::move_backward.
423 using __detail::__is_reverse_iterator;
424 using __detail::__is_normal_iterator;
425 if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
426 && __is_reverse_iterator<_Out>)
427 {
428 auto [__in,__out]
429 = ranges::__copy_or_move<_IsMove>(std::move(__last).base(),
430 std::move(__first).base(),
431 std::move(__result).base());
432 return {reverse_iterator{std::move(__in)},
433 reverse_iterator{std::move(__out)}};
434 }
435 else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
436 {
437 auto [__in,__out]
438 = ranges::__copy_or_move_backward<_IsMove>(__first.base(),
439 __last.base(),
440 std::move(__result));
441 return {decltype(__first){__in}, std::move(__out)};
442 }
443 else if constexpr (__is_normal_iterator<_Out>)
444 {
445 auto [__in,__out]
446 = ranges::__copy_or_move_backward<_IsMove>(std::move(__first),
447 std::move(__last),
448 __result.base());
449 return {std::move(__in), decltype(__result){__out}};
450 }
451 else if constexpr (sized_sentinel_for<_Sent, _Iter>)
452 {
453 if (!std::__is_constant_evaluated())
454 {
455 if constexpr (__memcpyable<_Out, _Iter>::__value)
456 {
457 using _ValueTypeI = iter_value_t<_Iter>;
458 auto __num = __last - __first;
459 __result -= __num;
460 if (__num > 1) [[likely]]
461 __builtin_memmove(__result, __first,
462 sizeof(_ValueTypeI) * __num);
463 else if (__num == 1)
464 __detail::__assign_one<_IsMove>(__result, __first);
465 return {__first + __num, __result};
466 }
467 }
468
469 auto __lasti = ranges::next(__first, __last);
470 auto __tail = __lasti;
471
472 for (auto __n = __last - __first; __n > 0; --__n)
473 {
474 --__tail;
475 --__result;
476 __detail::__assign_one<_IsMove>(__result, __tail);
477 }
478 return {std::move(__lasti), std::move(__result)};
479 }
480 else
481 {
482 auto __lasti = ranges::next(__first, __last);
483 auto __tail = __lasti;
484
485 while (__first != __tail)
486 {
487 --__tail;
488 --__result;
489 __detail::__assign_one<_IsMove>(__result, __tail);
490 }
491 return {std::move(__lasti), std::move(__result)};
492 }
493 }
494
495 struct __copy_backward_fn
496 {
497 template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
498 bidirectional_iterator _Iter2>
499 requires indirectly_copyable<_Iter1, _Iter2>
500 constexpr copy_backward_result<_Iter1, _Iter2>
501 operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
502 {
503 return ranges::__copy_or_move_backward<false>(std::move(__first),
504 std::move(__last),
505 std::move(__result));
506 }
507
508 template<bidirectional_range _Range, bidirectional_iterator _Iter>
509 requires indirectly_copyable<iterator_t<_Range>, _Iter>
510 constexpr copy_backward_result<borrowed_iterator_t<_Range>, _Iter>
511 operator()(_Range&& __r, _Iter __result) const
512 {
513 return (*this)(ranges::begin(__r), ranges::end(__r),
514 std::move(__result));
515 }
516 };
517
518 inline constexpr __copy_backward_fn copy_backward{};
519
520 struct __move_backward_fn
521 {
522 template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
523 bidirectional_iterator _Iter2>
524 requires indirectly_movable<_Iter1, _Iter2>
525 constexpr move_backward_result<_Iter1, _Iter2>
526 operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
527 {
528 return ranges::__copy_or_move_backward<true>(std::move(__first),
529 std::move(__last),
530 std::move(__result));
531 }
532
533 template<bidirectional_range _Range, bidirectional_iterator _Iter>
534 requires indirectly_movable<iterator_t<_Range>, _Iter>
535 constexpr move_backward_result<borrowed_iterator_t<_Range>, _Iter>
536 operator()(_Range&& __r, _Iter __result) const
537 {
538 return (*this)(ranges::begin(__r), ranges::end(__r),
539 std::move(__result));
540 }
541 };
542
543 inline constexpr __move_backward_fn move_backward{};
544
545 template<typename _Iter, typename _Out>
546 using copy_n_result = in_out_result<_Iter, _Out>;
547
548 struct __copy_n_fn
549 {
550 template<input_iterator _Iter, weakly_incrementable _Out>
551 requires indirectly_copyable<_Iter, _Out>
552 constexpr copy_n_result<_Iter, _Out>
553 operator()(_Iter __first, iter_difference_t<_Iter> __n,
554 _Out __result) const
555 {
556 if constexpr (random_access_iterator<_Iter>)
557 {
558 if (__n > 0)
559 return ranges::copy(__first, __first + __n, std::move(__result));
560 }
561 else
562 {
563 for (; __n > 0; --__n, (void)++__result, (void)++__first)
564 *__result = *__first;
565 }
566 return {std::move(__first), std::move(__result)};
567 }
568 };
569
570 inline constexpr __copy_n_fn copy_n{};
571
572 struct __fill_n_fn
573 {
574 template<typename _Tp, output_iterator<const _Tp&> _Out>
575 constexpr _Out
576 operator()(_Out __first, iter_difference_t<_Out> __n,
577 const _Tp& __value) const
578 {
579 // TODO: implement more specializations to be at least on par with
580 // std::fill_n
581 if (__n <= 0)
582 return __first;
583
584 if constexpr (is_scalar_v<_Tp>)
585 {
586 // TODO: Generalize this optimization to contiguous iterators.
587 if constexpr (is_pointer_v<_Out>
588 // Note that __is_byte already implies !is_volatile.
589 && __is_byte<remove_pointer_t<_Out>>::__value
590 && integral<_Tp>)
591 {
592 if (!std::__is_constant_evaluated())
593 {
594 __builtin_memset(__first,
595 static_cast<unsigned char>(__value),
596 __n);
597 return __first + __n;
598 }
599 }
600
601 const auto __tmp = __value;
602 for (; __n > 0; --__n, (void)++__first)
603 *__first = __tmp;
604 return __first;
605 }
606 else
607 {
608 for (; __n > 0; --__n, (void)++__first)
609 *__first = __value;
610 return __first;
611 }
612 }
613 };
614
615 inline constexpr __fill_n_fn fill_n{};
616
617 struct __fill_fn
618 {
619 template<typename _Tp,
620 output_iterator<const _Tp&> _Out, sentinel_for<_Out> _Sent>
621 constexpr _Out
622 operator()(_Out __first, _Sent __last, const _Tp& __value) const
623 {
624 // TODO: implement more specializations to be at least on par with
625 // std::fill
626 if constexpr (sized_sentinel_for<_Sent, _Out>)
627 {
628 const auto __len = __last - __first;
629 return ranges::fill_n(std::move(__first), __len, __value);
630 }
631 else if constexpr (is_scalar_v<_Tp>)
632 {
633 const auto __tmp = __value;
634 for (; __first != __last; ++__first)
635 *__first = __tmp;
636 return __first;
637 }
638 else
639 {
640 for (; __first != __last; ++__first)
641 *__first = __value;
642 return __first;
643 }
644 }
645
646 template<typename _Tp, output_range<const _Tp&> _Range>
647 constexpr borrowed_iterator_t<_Range>
648 operator()(_Range&& __r, const _Tp& __value) const
649 {
650 return (*this)(ranges::begin(__r), ranges::end(__r), __value);
651 }
652 };
653
654 inline constexpr __fill_fn fill{};
655}
656_GLIBCXX_END_NAMESPACE_VERSION
657} // namespace std
658#endif // concepts
659#endif // C++20
660#endif // _RANGES_ALGOBASE_H
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:137
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:90
constexpr _BI2 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
Moves the range [first,last) into result.
ISO C++ entities toplevel namespace is std.