libstdc++
ranges_algobase.h
Go to the documentation of this file.
1// Core algorithmic facilities -*- C++ -*-
2
3// Copyright (C) 2020-2025 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#endif // __glibcxx_ranges_iota
96
97 struct __equal_fn
98 {
99 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
100 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
101 typename _Pred = ranges::equal_to,
102 typename _Proj1 = identity, typename _Proj2 = identity>
103 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
104 constexpr bool
105 operator()(_Iter1 __first1, _Sent1 __last1,
106 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
107 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
108 {
109 // TODO: implement more specializations to at least have parity with
110 // std::equal.
111 if constexpr (__detail::__is_normal_iterator<_Iter1>
112 && same_as<_Iter1, _Sent1>)
113 return (*this)(__first1.base(), __last1.base(),
114 std::move(__first2), std::move(__last2),
115 std::move(__pred),
116 std::move(__proj1), std::move(__proj2));
117 else if constexpr (__detail::__is_normal_iterator<_Iter2>
118 && same_as<_Iter2, _Sent2>)
119 return (*this)(std::move(__first1), std::move(__last1),
120 __first2.base(), __last2.base(),
121 std::move(__pred),
122 std::move(__proj1), std::move(__proj2));
123 else if constexpr (sized_sentinel_for<_Sent1, _Iter1>
124 && sized_sentinel_for<_Sent2, _Iter2>)
125 {
126 auto __d1 = ranges::distance(__first1, __last1);
127 auto __d2 = ranges::distance(__first2, __last2);
128 if (__d1 != __d2)
129 return false;
130
131 using _ValueType1 = iter_value_t<_Iter1>;
132 constexpr bool __use_memcmp
133 = ((is_integral_v<_ValueType1> || is_pointer_v<_ValueType1>)
134 && __memcmpable<_Iter1, _Iter2>::__value
135 && is_same_v<_Pred, ranges::equal_to>
136 && is_same_v<_Proj1, identity>
137 && is_same_v<_Proj2, identity>);
138 if constexpr (__use_memcmp)
139 {
140 if (const size_t __len = (__last1 - __first1))
141 return !std::__memcmp(__first1, __first2, __len);
142 return true;
143 }
144 else
145 {
146 for (; __first1 != __last1; ++__first1, (void)++__first2)
147 if (!(bool)std::__invoke(__pred,
148 std::__invoke(__proj1, *__first1),
149 std::__invoke(__proj2, *__first2)))
150 return false;
151 return true;
152 }
153 }
154 else
155 {
156 for (; __first1 != __last1 && __first2 != __last2;
157 ++__first1, (void)++__first2)
158 if (!(bool)std::__invoke(__pred,
159 std::__invoke(__proj1, *__first1),
160 std::__invoke(__proj2, *__first2)))
161 return false;
162 return __first1 == __last1 && __first2 == __last2;
163 }
164 }
165
166 template<input_range _Range1, input_range _Range2,
167 typename _Pred = ranges::equal_to,
168 typename _Proj1 = identity, typename _Proj2 = identity>
169 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
170 _Pred, _Proj1, _Proj2>
171 constexpr bool
172 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
173 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
174 {
175 // _GLIBCXX_RESOLVE_LIB_DEFECTS
176 // 3560. ranges::equal [...] should short-circuit for sized_ranges
177 if constexpr (sized_range<_Range1>)
178 if constexpr (sized_range<_Range2>)
179 if (ranges::distance(__r1) != ranges::distance(__r2))
180 return false;
181
182 return (*this)(ranges::begin(__r1), ranges::end(__r1),
183 ranges::begin(__r2), ranges::end(__r2),
184 std::move(__pred),
185 std::move(__proj1), std::move(__proj2));
186 }
187 };
188
189 inline constexpr __equal_fn equal{};
190
191 template<typename _Iter, typename _Out>
192 struct in_out_result
193 {
194 [[no_unique_address]] _Iter in;
195 [[no_unique_address]] _Out out;
196
197 template<typename _Iter2, typename _Out2>
198 requires convertible_to<const _Iter&, _Iter2>
199 && convertible_to<const _Out&, _Out2>
200 constexpr
201 operator in_out_result<_Iter2, _Out2>() const &
202 { return {in, out}; }
203
204 template<typename _Iter2, typename _Out2>
205 requires convertible_to<_Iter, _Iter2>
206 && convertible_to<_Out, _Out2>
207 constexpr
208 operator in_out_result<_Iter2, _Out2>() &&
209 { return {std::move(in), std::move(out)}; }
210 };
211
212 template<typename _Iter, typename _Out>
213 using copy_result = in_out_result<_Iter, _Out>;
214
215 template<typename _Iter, typename _Out>
216 using move_result = in_out_result<_Iter, _Out>;
217
218 template<typename _Iter1, typename _Iter2>
219 using move_backward_result = in_out_result<_Iter1, _Iter2>;
220
221 template<typename _Iter1, typename _Iter2>
222 using copy_backward_result = in_out_result<_Iter1, _Iter2>;
223
224 template<bool _IsMove,
225 bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
226 bidirectional_iterator _Out>
227 requires (_IsMove
228 ? indirectly_movable<_Iter, _Out>
229 : indirectly_copyable<_Iter, _Out>)
230 constexpr __conditional_t<_IsMove,
231 move_backward_result<_Iter, _Out>,
232 copy_backward_result<_Iter, _Out>>
233 __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result);
234
235 template<bool _IsMove,
236 input_iterator _Iter, sentinel_for<_Iter> _Sent,
237 weakly_incrementable _Out>
238 requires (_IsMove
239 ? indirectly_movable<_Iter, _Out>
240 : indirectly_copyable<_Iter, _Out>)
241 constexpr __conditional_t<_IsMove,
242 move_result<_Iter, _Out>,
243 copy_result<_Iter, _Out>>
244 __copy_or_move(_Iter __first, _Sent __last, _Out __result)
245 {
246 // TODO: implement more specializations to be at least on par with
247 // std::copy/std::move.
248 using __detail::__is_move_iterator;
249 using __detail::__is_reverse_iterator;
250 using __detail::__is_normal_iterator;
251 if constexpr (__is_move_iterator<_Iter> && same_as<_Iter, _Sent>)
252 {
253 auto [__in, __out]
254 = ranges::__copy_or_move<true>(std::move(__first).base(),
255 std::move(__last).base(),
256 std::move(__result));
257 return {move_iterator{std::move(__in)}, std::move(__out)};
258 }
259 else if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
260 && __is_reverse_iterator<_Out>)
261 {
262 auto [__in,__out]
263 = ranges::__copy_or_move_backward<_IsMove>(std::move(__last).base(),
264 std::move(__first).base(),
265 std::move(__result).base());
266 return {reverse_iterator{std::move(__in)},
267 reverse_iterator{std::move(__out)}};
268 }
269 else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
270 {
271 auto [__in,__out]
272 = ranges::__copy_or_move<_IsMove>(__first.base(), __last.base(),
273 std::move(__result));
274 return {decltype(__first){__in}, std::move(__out)};
275 }
276 else if constexpr (__is_normal_iterator<_Out>)
277 {
278 auto [__in,__out]
279 = ranges::__copy_or_move<_IsMove>(std::move(__first), __last, __result.base());
280 return {std::move(__in), decltype(__result){__out}};
281 }
282 else if constexpr (sized_sentinel_for<_Sent, _Iter>)
283 {
284 if (!std::__is_constant_evaluated())
285 {
286 if constexpr (__memcpyable<_Out, _Iter>::__value)
287 {
288 using _ValueTypeI = iter_value_t<_Iter>;
289 auto __num = __last - __first;
290 if (__num > 1) [[likely]]
291 __builtin_memmove(__result, __first,
292 sizeof(_ValueTypeI) * __num);
293 else if (__num == 1)
294 std::__assign_one<_IsMove>(__result, __first);
295 return {__first + __num, __result + __num};
296 }
297 }
298
299 for (auto __n = __last - __first; __n > 0; --__n)
300 {
301 std::__assign_one<_IsMove>(__result, __first);
302 ++__first;
303 ++__result;
304 }
305 return {std::move(__first), std::move(__result)};
306 }
307 else
308 {
309 while (__first != __last)
310 {
311 std::__assign_one<_IsMove>(__result, __first);
312 ++__first;
313 ++__result;
314 }
315 return {std::move(__first), std::move(__result)};
316 }
317 }
318
319 struct __copy_fn
320 {
321 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
322 weakly_incrementable _Out>
323 requires indirectly_copyable<_Iter, _Out>
324 constexpr copy_result<_Iter, _Out>
325 operator()(_Iter __first, _Sent __last, _Out __result) const
326 {
327 return ranges::__copy_or_move<false>(std::move(__first),
328 std::move(__last),
329 std::move(__result));
330 }
331
332 template<input_range _Range, weakly_incrementable _Out>
333 requires indirectly_copyable<iterator_t<_Range>, _Out>
334 constexpr copy_result<borrowed_iterator_t<_Range>, _Out>
335 operator()(_Range&& __r, _Out __result) const
336 {
337 return (*this)(ranges::begin(__r), ranges::end(__r),
338 std::move(__result));
339 }
340 };
341
342 inline constexpr __copy_fn copy{};
343
344 struct __move_fn
345 {
346 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
347 weakly_incrementable _Out>
348 requires indirectly_movable<_Iter, _Out>
349 constexpr move_result<_Iter, _Out>
350 operator()(_Iter __first, _Sent __last, _Out __result) const
351 {
352 return ranges::__copy_or_move<true>(std::move(__first),
353 std::move(__last),
354 std::move(__result));
355 }
356
357 template<input_range _Range, weakly_incrementable _Out>
358 requires indirectly_movable<iterator_t<_Range>, _Out>
359 constexpr move_result<borrowed_iterator_t<_Range>, _Out>
360 operator()(_Range&& __r, _Out __result) const
361 {
362 return (*this)(ranges::begin(__r), ranges::end(__r),
363 std::move(__result));
364 }
365 };
366
367 inline constexpr __move_fn move{};
368
369 template<bool _IsMove,
370 bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
371 bidirectional_iterator _Out>
372 requires (_IsMove
373 ? indirectly_movable<_Iter, _Out>
374 : indirectly_copyable<_Iter, _Out>)
375 constexpr __conditional_t<_IsMove,
376 move_backward_result<_Iter, _Out>,
377 copy_backward_result<_Iter, _Out>>
378 __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result)
379 {
380 // TODO: implement more specializations to be at least on par with
381 // std::copy_backward/std::move_backward.
382 using __detail::__is_reverse_iterator;
383 using __detail::__is_normal_iterator;
384 if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
385 && __is_reverse_iterator<_Out>)
386 {
387 auto [__in,__out]
388 = ranges::__copy_or_move<_IsMove>(std::move(__last).base(),
389 std::move(__first).base(),
390 std::move(__result).base());
391 return {reverse_iterator{std::move(__in)},
392 reverse_iterator{std::move(__out)}};
393 }
394 else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
395 {
396 auto [__in,__out]
397 = ranges::__copy_or_move_backward<_IsMove>(__first.base(),
398 __last.base(),
399 std::move(__result));
400 return {decltype(__first){__in}, std::move(__out)};
401 }
402 else if constexpr (__is_normal_iterator<_Out>)
403 {
404 auto [__in,__out]
405 = ranges::__copy_or_move_backward<_IsMove>(std::move(__first),
406 std::move(__last),
407 __result.base());
408 return {std::move(__in), decltype(__result){__out}};
409 }
410 else if constexpr (sized_sentinel_for<_Sent, _Iter>)
411 {
412 if (!std::__is_constant_evaluated())
413 {
414 if constexpr (__memcpyable<_Out, _Iter>::__value)
415 {
416 using _ValueTypeI = iter_value_t<_Iter>;
417 auto __num = __last - __first;
418 __result -= __num;
419 if (__num > 1) [[likely]]
420 __builtin_memmove(__result, __first,
421 sizeof(_ValueTypeI) * __num);
422 else if (__num == 1)
423 std::__assign_one<_IsMove>(__result, __first);
424 return {__first + __num, __result};
425 }
426 }
427
428 auto __lasti = ranges::next(__first, __last);
429 auto __tail = __lasti;
430
431 for (auto __n = __last - __first; __n > 0; --__n)
432 {
433 --__tail;
434 --__result;
435 std::__assign_one<_IsMove>(__result, __tail);
436 }
437 return {std::move(__lasti), std::move(__result)};
438 }
439 else
440 {
441 auto __lasti = ranges::next(__first, __last);
442 auto __tail = __lasti;
443
444 while (__first != __tail)
445 {
446 --__tail;
447 --__result;
448 std::__assign_one<_IsMove>(__result, __tail);
449 }
450 return {std::move(__lasti), std::move(__result)};
451 }
452 }
453
454 struct __copy_backward_fn
455 {
456 template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
457 bidirectional_iterator _Iter2>
458 requires indirectly_copyable<_Iter1, _Iter2>
459 constexpr copy_backward_result<_Iter1, _Iter2>
460 operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
461 {
462 return ranges::__copy_or_move_backward<false>(std::move(__first),
463 std::move(__last),
464 std::move(__result));
465 }
466
467 template<bidirectional_range _Range, bidirectional_iterator _Iter>
468 requires indirectly_copyable<iterator_t<_Range>, _Iter>
469 constexpr copy_backward_result<borrowed_iterator_t<_Range>, _Iter>
470 operator()(_Range&& __r, _Iter __result) const
471 {
472 return (*this)(ranges::begin(__r), ranges::end(__r),
473 std::move(__result));
474 }
475 };
476
477 inline constexpr __copy_backward_fn copy_backward{};
478
479 struct __move_backward_fn
480 {
481 template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
482 bidirectional_iterator _Iter2>
483 requires indirectly_movable<_Iter1, _Iter2>
484 constexpr move_backward_result<_Iter1, _Iter2>
485 operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
486 {
487 return ranges::__copy_or_move_backward<true>(std::move(__first),
488 std::move(__last),
489 std::move(__result));
490 }
491
492 template<bidirectional_range _Range, bidirectional_iterator _Iter>
493 requires indirectly_movable<iterator_t<_Range>, _Iter>
494 constexpr move_backward_result<borrowed_iterator_t<_Range>, _Iter>
495 operator()(_Range&& __r, _Iter __result) const
496 {
497 return (*this)(ranges::begin(__r), ranges::end(__r),
498 std::move(__result));
499 }
500 };
501
502 inline constexpr __move_backward_fn move_backward{};
503
504 template<typename _Iter, typename _Out>
505 using copy_n_result = in_out_result<_Iter, _Out>;
506
507 struct __copy_n_fn
508 {
509 template<input_iterator _Iter, weakly_incrementable _Out>
510 requires indirectly_copyable<_Iter, _Out>
511 constexpr copy_n_result<_Iter, _Out>
512 operator()(_Iter __first, iter_difference_t<_Iter> __n,
513 _Out __result) const
514 {
515 if constexpr (random_access_iterator<_Iter>)
516 {
517 if (__n > 0)
518 return ranges::copy(__first, __first + __n, std::move(__result));
519 }
520 else
521 {
522 for (; __n > 0; --__n, (void)++__result, (void)++__first)
523 *__result = *__first;
524 }
525 return {std::move(__first), std::move(__result)};
526 }
527 };
528
529 inline constexpr __copy_n_fn copy_n{};
530
531 struct __fill_n_fn
532 {
533 template<typename _Out,
534 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Out>)>
535 requires output_iterator<_Out, const _Tp&>
536 constexpr _Out
537 operator()(_Out __first, iter_difference_t<_Out> __n,
538 const _Tp& __value) const
539 {
540 // TODO: implement more specializations to be at least on par with
541 // std::fill_n
542 if (__n <= 0)
543 return __first;
544
545 if constexpr (is_scalar_v<_Tp>)
546 {
547 // TODO: Generalize this optimization to contiguous iterators.
548 if constexpr (is_pointer_v<_Out>
549 // Note that __is_byte already implies !is_volatile.
550 && __is_byte<remove_pointer_t<_Out>>::__value
551 && integral<_Tp>)
552 {
553 if (!std::__is_constant_evaluated())
554 {
555 __builtin_memset(__first,
556 static_cast<unsigned char>(__value),
557 __n);
558 return __first + __n;
559 }
560 }
561
562 const auto __tmp = __value;
563 for (; __n > 0; --__n, (void)++__first)
564 *__first = __tmp;
565 return __first;
566 }
567 else
568 {
569 for (; __n > 0; --__n, (void)++__first)
570 *__first = __value;
571 return __first;
572 }
573 }
574 };
575
576 inline constexpr __fill_n_fn fill_n{};
577
578 struct __fill_fn
579 {
580 template<typename _Out,
581 sentinel_for<_Out> _Sent,
582 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Out>)>
583 requires output_iterator<_Out, const _Tp&>
584 constexpr _Out
585 operator()(_Out __first, _Sent __last, const _Tp& __value) const
586 {
587 // TODO: implement more specializations to be at least on par with
588 // std::fill
589 if constexpr (sized_sentinel_for<_Sent, _Out>)
590 {
591 const auto __len = __last - __first;
592 return ranges::fill_n(std::move(__first), __len, __value);
593 }
594 else if constexpr (is_scalar_v<_Tp>)
595 {
596 const auto __tmp = __value;
597 for (; __first != __last; ++__first)
598 *__first = __tmp;
599 return __first;
600 }
601 else
602 {
603 for (; __first != __last; ++__first)
604 *__first = __value;
605 return __first;
606 }
607 }
608
609 template<typename _Range,
610 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>)>
611 requires output_range<_Range, const _Tp&>
612 constexpr borrowed_iterator_t<_Range>
613 operator()(_Range&& __r, const _Tp& __value) const
614 {
615 return (*this)(ranges::begin(__r), ranges::end(__r), __value);
616 }
617 };
618
619 inline constexpr __fill_fn fill{};
620}
621_GLIBCXX_END_NAMESPACE_VERSION
622} // namespace std
623#endif // concepts
624#endif // C++20
625#endif // _RANGES_ALGOBASE_H
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
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:92
constexpr _BI2 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
Moves the range [first,last) into result.
ISO C++ entities toplevel namespace is std.