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