libstdc++
ranges_util.h
Go to the documentation of this file.
1// Utilities for representing and manipulating ranges -*- C++ -*-
2
3// Copyright (C) 2019-2026 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_util.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{ranges}
28 */
29
30#ifndef _RANGES_UTIL_H
31#define _RANGES_UTIL_H 1
32
33#if __cplusplus > 201703L
34# include <bits/ranges_base.h>
35# include <bits/utility.h>
36# include <bits/invoke.h>
37# include <bits/cpp_type_traits.h> // __can_use_memchr_for_find
38#if __glibcxx_tuple_like // >= C++23
39# include <bits/stl_pair.h> // __pair_like, __is_tuple_like_v
40#endif
41
42#ifdef __glibcxx_ranges
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
46namespace ranges
47{
48 // C++20 24.5 [range.utility] Range utilities
49
50 namespace __detail
51 {
52 template<typename _Range>
53 concept __simple_view = view<_Range> && range<const _Range>
54 && same_as<iterator_t<_Range>, iterator_t<const _Range>>
55 && same_as<sentinel_t<_Range>, sentinel_t<const _Range>>;
56
57 // _GLIBCXX_RESOLVE_LIB_DEFECTS
58 // 4112. has-arrow should required operator->() to be const-qualified
59 template<typename _It>
60 concept __has_arrow = input_iterator<_It>
61 && (is_pointer_v<_It>
62 || requires(const _It __it) { __it.operator->(); });
63
64 using std::__detail::__different_from;
65 } // namespace __detail
66
67 /// The ranges::view_interface class template
68 template<typename _Derived>
69 requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
71 {
72 private:
73 constexpr _Derived& _M_derived() noexcept
74 {
76 static_assert(view<_Derived>);
77 return static_cast<_Derived&>(*this);
78 }
79
80 constexpr const _Derived& _M_derived() const noexcept
81 {
83 static_assert(view<_Derived>);
84 return static_cast<const _Derived&>(*this);
85 }
86
87 static constexpr bool
88 _S_bool(bool) noexcept; // not defined
89
90 template<typename _Tp>
91 static constexpr bool
92 _S_empty(_Tp& __t)
93 noexcept(noexcept(_S_bool(ranges::begin(__t) == ranges::end(__t))))
94 { return ranges::begin(__t) == ranges::end(__t); }
95
96 template<typename _Tp>
97 static constexpr auto
98 _S_size(_Tp& __t)
99 noexcept(noexcept(ranges::end(__t) - ranges::begin(__t)))
100 { return ranges::end(__t) - ranges::begin(__t); }
101
102 public:
103 constexpr bool
104 empty()
105 noexcept(noexcept(_S_empty(_M_derived())))
107 { return _S_empty(_M_derived()); }
108
109 constexpr bool
110 empty()
111 noexcept(noexcept(ranges::size(_M_derived()) == 0))
112 requires sized_range<_Derived>
113 { return ranges::size(_M_derived()) == 0; }
114
115 constexpr bool
116 empty() const
117 noexcept(noexcept(_S_empty(_M_derived())))
119 { return _S_empty(_M_derived()); }
120
121 constexpr bool
122 empty() const
123 noexcept(noexcept(ranges::size(_M_derived()) == 0))
125 { return ranges::size(_M_derived()) == 0; }
126
127 constexpr explicit
128 operator bool() noexcept(noexcept(ranges::empty(_M_derived())))
129 requires requires { ranges::empty(_M_derived()); }
130 { return !ranges::empty(_M_derived()); }
131
132 constexpr explicit
133 operator bool() const noexcept(noexcept(ranges::empty(_M_derived())))
134 requires requires { ranges::empty(_M_derived()); }
135 { return !ranges::empty(_M_derived()); }
136
137 constexpr auto
138 data() noexcept(noexcept(ranges::begin(_M_derived())))
139 requires contiguous_iterator<iterator_t<_Derived>>
140 { return std::to_address(ranges::begin(_M_derived())); }
141
142 constexpr auto
143 data() const noexcept(noexcept(ranges::begin(_M_derived())))
144 requires range<const _Derived>
145 && contiguous_iterator<iterator_t<const _Derived>>
146 { return std::to_address(ranges::begin(_M_derived())); }
147
148 constexpr auto
149 size() noexcept(noexcept(_S_size(_M_derived())))
151 && sized_sentinel_for<sentinel_t<_Derived>, iterator_t<_Derived>>
152 { return _S_size(_M_derived()); }
153
154 constexpr auto
155 size() const noexcept(noexcept(_S_size(_M_derived())))
157 && sized_sentinel_for<sentinel_t<const _Derived>,
158 iterator_t<const _Derived>>
159 { return _S_size(_M_derived()); }
160
161 constexpr decltype(auto)
162 front() requires forward_range<_Derived>
163 {
164 __glibcxx_assert(!empty());
165 return *ranges::begin(_M_derived());
166 }
167
168 constexpr decltype(auto)
169 front() const requires forward_range<const _Derived>
170 {
171 __glibcxx_assert(!empty());
172 return *ranges::begin(_M_derived());
173 }
174
175 constexpr decltype(auto)
176 back()
178 {
179 __glibcxx_assert(!empty());
180 return *ranges::prev(ranges::end(_M_derived()));
181 }
182
183 constexpr decltype(auto)
184 back() const
187 {
188 __glibcxx_assert(!empty());
189 return *ranges::prev(ranges::end(_M_derived()));
190 }
191
192 template<random_access_range _Range = _Derived>
193 constexpr decltype(auto)
194 operator[](range_difference_t<_Range> __n)
195 { return ranges::begin(_M_derived())[__n]; }
196
197 template<random_access_range _Range = const _Derived>
198 constexpr decltype(auto)
199 operator[](range_difference_t<_Range> __n) const
200 { return ranges::begin(_M_derived())[__n]; }
201
202#if __cplusplus > 202002L
203 constexpr auto
204 cbegin() requires input_range<_Derived>
205 { return ranges::cbegin(_M_derived()); }
206
207 constexpr auto
208 cbegin() const requires input_range<const _Derived>
209 { return ranges::cbegin(_M_derived()); }
210
211 constexpr auto
212 cend() requires input_range<_Derived>
213 { return ranges::cend(_M_derived()); }
214
215 constexpr auto
216 cend() const requires input_range<const _Derived>
217 { return ranges::cend(_M_derived()); }
218#endif
219 };
220
221 namespace __detail
222 {
223 template<typename _From, typename _To>
224 concept __uses_nonqualification_pointer_conversion
225 = is_pointer_v<_From> && is_pointer_v<_To>
228
229 template<typename _From, typename _To>
230 concept __convertible_to_non_slicing = convertible_to<_From, _To>
231 && !__uses_nonqualification_pointer_conversion<decay_t<_From>,
233
234#if __glibcxx_tuple_like // >= C++23
235 // P2165R4 version of __pair_like is defined in <bits/stl_pair.h>.
236#else
237 // C++20 version of __pair_like from P2321R2.
238 template<typename _Tp>
239 concept __pair_like
240 = !is_reference_v<_Tp> && requires(_Tp __t)
241 {
242 typename tuple_size<_Tp>::type;
244 typename tuple_element_t<0, remove_const_t<_Tp>>;
245 typename tuple_element_t<1, remove_const_t<_Tp>>;
247 { get<1>(__t) } -> convertible_to<const tuple_element_t<1, _Tp>&>;
248 };
249#endif
250
251 template<typename _Tp, typename _Up, typename _Vp>
252 concept __pair_like_convertible_from
253 = !range<_Tp> && !is_reference_v<_Tp> && __pair_like<_Tp>
254 && constructible_from<_Tp, _Up, _Vp>
255 && __convertible_to_non_slicing<_Up, tuple_element_t<0, _Tp>>
256 && convertible_to<_Vp, tuple_element_t<1, _Tp>>;
257
258 } // namespace __detail
259
260 namespace views { struct _Drop; } // defined in <ranges>
261
262 enum class subrange_kind : bool { unsized, sized };
263
264 /// The ranges::subrange class template
265 template<input_or_output_iterator _It, sentinel_for<_It> _Sent = _It,
266 subrange_kind _Kind = sized_sentinel_for<_Sent, _It>
267 ? subrange_kind::sized : subrange_kind::unsized>
268 requires (_Kind == subrange_kind::sized || !sized_sentinel_for<_Sent, _It>)
269 class subrange : public view_interface<subrange<_It, _Sent, _Kind>>
270 {
271 private:
272 static constexpr bool _S_store_size
273 = _Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _It>;
274
275 friend struct views::_Drop; // Needs to inspect _S_store_size.
276
277 _It _M_begin = _It();
278 [[no_unique_address]] _Sent _M_end = _Sent();
279
280 using __size_type
281 = __detail::__make_unsigned_like_t<iter_difference_t<_It>>;
282
283 template<typename _Tp, bool = _S_store_size>
284 struct _Size
285 {
286 [[__gnu__::__always_inline__]]
287 constexpr _Size(_Tp = {}) { }
288 };
289
290 template<typename _Tp>
291 struct _Size<_Tp, true>
292 {
293 [[__gnu__::__always_inline__]]
294 constexpr _Size(_Tp __s = {}) : _M_size(__s) { }
295
296 _Tp _M_size;
297 };
298
299 [[no_unique_address]] _Size<__size_type> _M_size = {};
300
301 public:
302 subrange() requires default_initializable<_It> = default;
303
304 constexpr
305 subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s)
306 noexcept(is_nothrow_constructible_v<_It, decltype(__i)>
307 && is_nothrow_constructible_v<_Sent, _Sent&>)
308 requires (!_S_store_size)
309 : _M_begin(std::move(__i)), _M_end(__s)
310 { }
311
312 constexpr
313 subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s,
314 __size_type __n)
315 noexcept(is_nothrow_constructible_v<_It, decltype(__i)>
316 && is_nothrow_constructible_v<_Sent, _Sent&>)
317 requires (_Kind == subrange_kind::sized)
318 : _M_begin(std::move(__i)), _M_end(__s), _M_size(__n)
319 { }
320
321 template<__detail::__different_from<subrange> _Rng>
322 requires borrowed_range<_Rng>
323 && __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
325 constexpr
326 subrange(_Rng&& __r)
327 noexcept(noexcept(subrange(__r, ranges::size(__r))))
328 requires _S_store_size && sized_range<_Rng>
329 : subrange(__r, ranges::size(__r))
330 { }
331
332 template<__detail::__different_from<subrange> _Rng>
333 requires borrowed_range<_Rng>
334 && __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
336 constexpr
337 subrange(_Rng&& __r)
338 noexcept(noexcept(subrange(ranges::begin(__r), ranges::end(__r))))
339 requires (!_S_store_size)
340 : subrange(ranges::begin(__r), ranges::end(__r))
341 { }
342
343 template<borrowed_range _Rng>
344 requires __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
346 constexpr
347 subrange(_Rng&& __r, __size_type __n)
348 noexcept(noexcept(subrange(ranges::begin(__r), ranges::end(__r), __n)))
349 requires (_Kind == subrange_kind::sized)
350 : subrange{ranges::begin(__r), ranges::end(__r), __n}
351 { }
352
353 template<__detail::__different_from<subrange> _PairLike>
354 requires __detail::__pair_like_convertible_from<_PairLike, const _It&,
355 const _Sent&>
356 constexpr
357 operator _PairLike() const
358 { return _PairLike(_M_begin, _M_end); }
359
360 constexpr _It
361 begin() const requires copyable<_It>
362 { return _M_begin; }
363
364 [[nodiscard]] constexpr _It
365 begin() requires (!copyable<_It>)
366 { return std::move(_M_begin); }
367
368 constexpr _Sent end() const { return _M_end; }
369
370 constexpr bool empty() const { return _M_begin == _M_end; }
371
372 constexpr __size_type
373 size() const requires (_Kind == subrange_kind::sized)
374 {
375 if constexpr (_S_store_size)
376 return _M_size._M_size;
377 else
378 return __detail::__to_unsigned_like(_M_end - _M_begin);
379 }
380
381 [[nodiscard]] constexpr subrange
382 next(iter_difference_t<_It> __n = 1) const &
383 requires forward_iterator<_It>
384 {
385 auto __tmp = *this;
386 __tmp.advance(__n);
387 return __tmp;
388 }
389
390 [[nodiscard]] constexpr subrange
391 next(iter_difference_t<_It> __n = 1) &&
392 {
393 advance(__n);
394 return std::move(*this);
395 }
396
397 [[nodiscard]] constexpr subrange
398 prev(iter_difference_t<_It> __n = 1) const
399 requires bidirectional_iterator<_It>
400 {
401 auto __tmp = *this;
402 __tmp.advance(-__n);
403 return __tmp;
404 }
405
406 constexpr subrange&
407 advance(iter_difference_t<_It> __n)
408 {
409 // _GLIBCXX_RESOLVE_LIB_DEFECTS
410 // 3433. subrange::advance(n) has UB when n < 0
411 if constexpr (bidirectional_iterator<_It>)
412 if (__n < 0)
413 {
414 ranges::advance(_M_begin, __n);
415 if constexpr (_S_store_size)
416 _M_size._M_size += __detail::__to_unsigned_like(-__n);
417 return *this;
418 }
419
420 __glibcxx_assert(__n >= 0);
421 auto __d = __n - ranges::advance(_M_begin, __n, _M_end);
422 if constexpr (_S_store_size)
423 _M_size._M_size -= __detail::__to_unsigned_like(__d);
424 return *this;
425 }
426 };
427
428 template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
429 subrange(_It, _Sent) -> subrange<_It, _Sent>;
430
431 template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
432 subrange(_It, _Sent,
433 __detail::__make_unsigned_like_t<iter_difference_t<_It>>)
435
436 template<borrowed_range _Rng>
437 subrange(_Rng&&)
438 -> subrange<iterator_t<_Rng>, sentinel_t<_Rng>,
440 || sized_sentinel_for<sentinel_t<_Rng>, iterator_t<_Rng>>)
441 ? subrange_kind::sized : subrange_kind::unsized>;
442
443 template<borrowed_range _Rng>
444 subrange(_Rng&&,
445 __detail::__make_unsigned_like_t<range_difference_t<_Rng>>)
446 -> subrange<iterator_t<_Rng>, sentinel_t<_Rng>, subrange_kind::sized>;
447
448 // _GLIBCXX_RESOLVE_LIB_DEFECTS
449 // 3589. The const lvalue reference overload of get for subrange does not
450 // constrain I to be copyable when N == 0
451 template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
452 requires ((_Num == 0 && copyable<_It>) || _Num == 1)
453 constexpr auto
454 get(const subrange<_It, _Sent, _Kind>& __r)
455 {
456 if constexpr (_Num == 0)
457 return __r.begin();
458 else
459 return __r.end();
460 }
461
462 template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
463 requires (_Num < 2)
464 constexpr auto
465 get(subrange<_It, _Sent, _Kind>&& __r)
466 {
467 if constexpr (_Num == 0)
468 return __r.begin();
469 else
470 return __r.end();
471 }
472
473 template<typename _It, typename _Sent, subrange_kind _Kind>
474 inline constexpr bool
475 enable_borrowed_range<subrange<_It, _Sent, _Kind>> = true;
476
477 template<range _Range>
478 using borrowed_subrange_t = __conditional_t<borrowed_range<_Range>,
479 subrange<iterator_t<_Range>>,
480 dangling>;
481
482 // __is_subrange is defined in <bits/utility.h>.
483 template<typename _Iter, typename _Sent, subrange_kind _Kind>
484 inline constexpr bool __detail::__is_subrange<subrange<_Iter, _Sent, _Kind>> = true;
485} // namespace ranges
486
487 // Unpack a ranges::subrange into a type trait and use its value. We also verify that
488 // get is supported, i.e. iterator is copyable.
489 template<template<typename...> class _Trait, typename _Tp, typename _It, typename _St>
490 constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, ranges::subrange<_It, _St>>
491 = _Trait<_Tp, _It, _St>{};
492
493 template<template<typename...> class _Trait, typename _Tp, copyable _It, typename _St>
494 constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, ranges::subrange<_It, _St>&>
495 = _Trait<_Tp, _It, _St>{};
496
497 template<template<typename...> class _Trait, typename _Tp, copyable _It, typename _St>
498 constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, const ranges::subrange<_It, _St>>
499 = _Trait<_Tp, _It, _St>{};
500
501 template<template<typename...> class _Trait, typename _Tp, copyable _It, typename _St>
502 constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, const ranges::subrange<_It, _St>&>
503 = _Trait<_Tp, _It, _St>{};
504
505 // get(ranges::ubrange) are not marked noexcept, and thus always potentially throwing
506 template<template<typename...> class _Trait, typename _Tp, typename _It, typename _St>
507 constexpr bool __noexcept_unpack_tuple_like_v<_Trait, _Tp, ranges::subrange<_It, _St>>
508 = false;
509
510 template<template<typename...> class _Trait, typename _Tp, typename _It, typename _St>
511 constexpr bool __noexcept_unpack_tuple_like_v<_Trait, _Tp, ranges::subrange<_It, _St>&>
512 = false;
513
514 template<template<typename...> class _Trait, typename _Tp, typename _It, typename _St>
515 constexpr bool __noexcept_unpack_tuple_like_v<_Trait, _Tp, const ranges::subrange<_It, _St>>
516 = false;
517
518 template<template<typename...> class _Trait, typename _Tp, typename _It, typename _St>
519 constexpr bool __noexcept_unpack_tuple_like_v<_Trait, _Tp, const ranges::subrange<_It, _St>&>
520 = false;
521
522#if __glibcxx_tuple_like // >= C++23
523 // __is_tuple_like_v is defined in <bits/stl_pair.h>.
524 template<typename _It, typename _Sent, ranges::subrange_kind _Kind>
525 inline constexpr bool __is_tuple_like_v<ranges::subrange<_It, _Sent, _Kind>> = true;
526#endif
527
528// The following ranges algorithms are used by <ranges>, and are defined here
529// so that <ranges> can avoid including all of <bits/ranges_algo.h>.
530namespace ranges
531{
532 struct __find_fn
533 {
534 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
535 typename _Proj = identity,
536 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
537 requires indirect_binary_predicate<ranges::equal_to,
538 projected<_Iter, _Proj>, const _Tp*>
539 [[nodiscard]] constexpr _Iter
540 operator()(_Iter __first, _Sent __last,
541 const _Tp& __value, _Proj __proj = {}) const
542 {
543 if constexpr (is_same_v<_Proj, identity>)
544 if constexpr(__can_use_memchr_for_find<iter_value_t<_Iter>, _Tp>)
545 if constexpr (sized_sentinel_for<_Sent, _Iter>)
546 if constexpr (contiguous_iterator<_Iter>)
547 if (!is_constant_evaluated())
548 {
549 using _Vt = iter_value_t<_Iter>;
550 auto __n = __last - __first;
551 if (static_cast<_Vt>(__value) == __value) [[likely]]
552 if (__n > 0)
553 {
554 const size_t __nu = static_cast<size_t>(__n);
555 const int __ival = static_cast<int>(__value);
556 const void* __p0 = std::to_address(__first);
557 if (auto __p1 = __builtin_memchr(__p0, __ival, __nu))
558 __n = (const char*)__p1 - (const char*)__p0;
559 }
560 return __first + __n;
561 }
562
563 while (__first != __last
564 && !(std::__invoke(__proj, *__first) == __value))
565 ++__first;
566 return __first;
567 }
568
569 template<input_range _Range, typename _Proj = identity,
570 typename _Tp
571 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
572 requires indirect_binary_predicate<ranges::equal_to,
574 const _Tp*>
575 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
576 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
577 {
578 return (*this)(ranges::begin(__r), ranges::end(__r),
579 __value, std::move(__proj));
580 }
581 };
582
583 inline constexpr __find_fn find{};
584
585 struct __find_if_fn
586 {
587 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
588 typename _Proj = identity,
589 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
590 [[nodiscard]] constexpr _Iter
591 operator()(_Iter __first, _Sent __last,
592 _Pred __pred, _Proj __proj = {}) const
593 {
594 while (__first != __last
595 && !(bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
596 ++__first;
597 return __first;
598 }
599
600 template<input_range _Range, typename _Proj = identity,
601 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
602 _Pred>
603 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
604 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
605 {
606 return (*this)(ranges::begin(__r), ranges::end(__r),
607 std::move(__pred), std::move(__proj));
608 }
609 };
610
611 inline constexpr __find_if_fn find_if{};
612
613 struct __find_if_not_fn
614 {
615 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
616 typename _Proj = identity,
617 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
618 [[nodiscard]] constexpr _Iter
619 operator()(_Iter __first, _Sent __last,
620 _Pred __pred, _Proj __proj = {}) const
621 {
622 while (__first != __last
623 && (bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
624 ++__first;
625 return __first;
626 }
627
628 template<input_range _Range, typename _Proj = identity,
629 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
630 _Pred>
631 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
632 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
633 {
634 return (*this)(ranges::begin(__r), ranges::end(__r),
635 std::move(__pred), std::move(__proj));
636 }
637 };
638
639 inline constexpr __find_if_not_fn find_if_not{};
640
641 template<typename _Iter1, typename _Iter2>
642 struct in_in_result
643 {
644 [[no_unique_address]] _Iter1 in1;
645 [[no_unique_address]] _Iter2 in2;
646
647 template<typename _IIter1, typename _IIter2>
648 requires convertible_to<const _Iter1&, _IIter1>
649 && convertible_to<const _Iter2&, _IIter2>
650 constexpr
651 operator in_in_result<_IIter1, _IIter2>() const &
652 { return {in1, in2}; }
653
654 template<typename _IIter1, typename _IIter2>
655 requires convertible_to<_Iter1, _IIter1>
656 && convertible_to<_Iter2, _IIter2>
657 constexpr
658 operator in_in_result<_IIter1, _IIter2>() &&
659 { return {std::move(in1), std::move(in2)}; }
660 };
661
662 template<typename _Iter1, typename _Iter2>
663 using mismatch_result = in_in_result<_Iter1, _Iter2>;
664
665 struct __mismatch_fn
666 {
667 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
668 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
669 typename _Pred = ranges::equal_to,
670 typename _Proj1 = identity, typename _Proj2 = identity>
671 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
672 [[nodiscard]] constexpr mismatch_result<_Iter1, _Iter2>
673 operator()(_Iter1 __first1, _Sent1 __last1,
674 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
675 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
676 {
677 while (__first1 != __last1 && __first2 != __last2
678 && (bool)std::__invoke(__pred,
679 std::__invoke(__proj1, *__first1),
680 std::__invoke(__proj2, *__first2)))
681 {
682 ++__first1;
683 ++__first2;
684 }
685 return { std::move(__first1), std::move(__first2) };
686 }
687
688 template<input_range _Range1, input_range _Range2,
689 typename _Pred = ranges::equal_to,
690 typename _Proj1 = identity, typename _Proj2 = identity>
691 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
692 _Pred, _Proj1, _Proj2>
693 [[nodiscard]]
694 constexpr mismatch_result<borrowed_iterator_t<_Range1>,
695 borrowed_iterator_t<_Range2>>
696 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
697 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
698 {
699 return (*this)(ranges::begin(__r1), ranges::end(__r1),
700 ranges::begin(__r2), ranges::end(__r2),
701 std::move(__pred),
702 std::move(__proj1), std::move(__proj2));
703 }
704 };
705
706 inline constexpr __mismatch_fn mismatch{};
707
708 struct __search_fn
709 {
710 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
711 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
712 typename _Pred = ranges::equal_to,
713 typename _Proj1 = identity, typename _Proj2 = identity>
714 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
715 [[nodiscard]] constexpr subrange<_Iter1>
716 operator()(_Iter1 __first1, _Sent1 __last1,
717 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
718 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
719 {
720 if (__first1 == __last1 || __first2 == __last2)
721 return {__first1, __first1};
722
723 for (;;)
724 {
725 for (;;)
726 {
727 if (__first1 == __last1)
728 return {__first1, __first1};
729 if (std::__invoke(__pred,
730 std::__invoke(__proj1, *__first1),
731 std::__invoke(__proj2, *__first2)))
732 break;
733 ++__first1;
734 }
735 auto __cur1 = __first1;
736 auto __cur2 = __first2;
737 for (;;)
738 {
739 if (++__cur2 == __last2)
740 return {__first1, ++__cur1};
741 if (++__cur1 == __last1)
742 return {__cur1, __cur1};
743 if (!(bool)std::__invoke(__pred,
744 std::__invoke(__proj1, *__cur1),
745 std::__invoke(__proj2, *__cur2)))
746 {
747 ++__first1;
748 break;
749 }
750 }
751 }
752 }
753
754 template<forward_range _Range1, forward_range _Range2,
755 typename _Pred = ranges::equal_to,
756 typename _Proj1 = identity, typename _Proj2 = identity>
757 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
758 _Pred, _Proj1, _Proj2>
759 [[nodiscard]] constexpr borrowed_subrange_t<_Range1>
760 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
761 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
762 {
763 return (*this)(ranges::begin(__r1), ranges::end(__r1),
764 ranges::begin(__r2), ranges::end(__r2),
765 std::move(__pred),
766 std::move(__proj1), std::move(__proj2));
767 }
768 };
769
770 inline constexpr __search_fn search{};
771
772 struct __min_fn
773 {
774 template<typename _Tp, typename _Proj = identity,
775 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
776 _Comp = ranges::less>
777 [[nodiscard]] constexpr const _Tp&
778 operator()(const _Tp& __a, const _Tp& __b,
779 _Comp __comp = {}, _Proj __proj = {}) const
780 {
781 if (std::__invoke(__comp,
782 std::__invoke(__proj, __b),
783 std::__invoke(__proj, __a)))
784 return __b;
785 else
786 return __a;
787 }
788
789 template<input_range _Range, typename _Proj = identity,
790 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
791 _Comp = ranges::less>
792 requires indirectly_copyable_storable<iterator_t<_Range>,
793 range_value_t<_Range>*>
794 [[nodiscard]] constexpr range_value_t<_Range>
795 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
796 {
797 auto __first = ranges::begin(__r);
798 auto __last = ranges::end(__r);
799 __glibcxx_assert(__first != __last);
800 range_value_t<_Range> __result(*__first);
801 while (++__first != __last)
802 {
803 auto&& __tmp = *__first;
804 if (std::__invoke(__comp,
805 std::__invoke(__proj, __tmp),
806 std::__invoke(__proj, __result)))
807 __result = std::forward<decltype(__tmp)>(__tmp);
808 }
809 return __result;
810 }
811
812 template<copyable _Tp, typename _Proj = identity,
813 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
814 _Comp = ranges::less>
815 [[nodiscard]] constexpr _Tp
816 operator()(initializer_list<_Tp> __r,
817 _Comp __comp = {}, _Proj __proj = {}) const
818 {
819 return (*this)(ranges::subrange(__r),
820 std::move(__comp), std::move(__proj));
821 }
822 };
823
824 inline constexpr __min_fn min{};
825
826 struct __adjacent_find_fn
827 {
828 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
829 typename _Proj = identity,
830 indirect_binary_predicate<projected<_Iter, _Proj>,
831 projected<_Iter, _Proj>> _Pred
832 = ranges::equal_to>
833 [[nodiscard]] constexpr _Iter
834 operator()(_Iter __first, _Sent __last,
835 _Pred __pred = {}, _Proj __proj = {}) const
836 {
837 if (__first == __last)
838 return __first;
839 auto __next = __first;
840 for (; ++__next != __last; __first = __next)
841 {
842 if (std::__invoke(__pred,
843 std::__invoke(__proj, *__first),
844 std::__invoke(__proj, *__next)))
845 return __first;
846 }
847 return __next;
848 }
849
850 template<forward_range _Range, typename _Proj = identity,
851 indirect_binary_predicate<
853 projected<iterator_t<_Range>, _Proj>> _Pred = ranges::equal_to>
854 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
855 operator()(_Range&& __r, _Pred __pred = {}, _Proj __proj = {}) const
856 {
857 return (*this)(ranges::begin(__r), ranges::end(__r),
858 std::move(__pred), std::move(__proj));
859 }
860 };
861
862 inline constexpr __adjacent_find_fn adjacent_find{};
863
864} // namespace ranges
865
866 using ranges::get;
867
868 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
869 struct tuple_size<ranges::subrange<_Iter, _Sent, _Kind>>
870 : integral_constant<size_t, 2>
871 { };
872
873 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
874 struct tuple_element<0, ranges::subrange<_Iter, _Sent, _Kind>>
875 { using type = _Iter; };
876
877 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
878 struct tuple_element<1, ranges::subrange<_Iter, _Sent, _Kind>>
879 { using type = _Sent; };
880
881 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
882 struct tuple_element<0, const ranges::subrange<_Iter, _Sent, _Kind>>
883 { using type = _Iter; };
884
885 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
886 struct tuple_element<1, const ranges::subrange<_Iter, _Sent, _Kind>>
887 { using type = _Sent; };
888
889template<typename _Tp, typename _IndexType>
890 class _Repeat_iterator;
891
892 template<typename _Tp, typename _IndexType>
893 constexpr _Repeat_iterator<_Tp, _IndexType>
894 __make_repeat_iterator(_Tp&, _IndexType);
895
896 template<typename _Tp, typename _IndexType>
897 class _Repeat_iterator
898 {
899 static auto
900 _S_difference_type()
901 {
902 if constexpr (__detail::__is_signed_integer_like<_IndexType>)
903 return _IndexType{};
904 else
905 return ranges::__detail::__to_signed_like(_IndexType{});
906 }
907
908 _Tp* _M_value = nullptr;
909 _IndexType _M_current = _IndexType();
910
911 constexpr explicit
912 _Repeat_iterator(const _Tp* __value, _IndexType __bound = _IndexType())
913 : _M_value(__value), _M_current(__bound)
914 { }
915
916 public:
917 using iterator_concept = random_access_iterator_tag;
918 using iterator_category = random_access_iterator_tag;
919 using value_type = remove_cv_t<_Tp>;
920 using difference_type = decltype(_S_difference_type());
921
922 _Repeat_iterator() = default;
923
924 constexpr _Tp&
925 operator*() const noexcept
926 { return *_M_value; }
927
928 constexpr _Repeat_iterator&
929 operator++()
930 {
931 ++_M_current;
932 return *this;
933 }
934
935 constexpr _Repeat_iterator
936 operator++(int)
937 {
938 auto __tmp = *this;
939 ++*this;
940 return __tmp;
941 }
942
943 constexpr _Repeat_iterator&
944 operator--()
945 {
946 __glibcxx_assert(_M_current > 0);
947 --_M_current;
948 return *this;
949 }
950
951 constexpr _Repeat_iterator
952 operator--(int)
953 {
954 auto __tmp = *this;
955 --*this;
956 return __tmp;
957 }
958
959 constexpr _Repeat_iterator&
960 operator+=(difference_type __n)
961 {
962 __glibcxx_assert(_M_current + __n >= 0);
963 _M_current += __n;
964 return *this;
965 }
966
967 constexpr _Repeat_iterator&
968 operator-=(difference_type __n)
969 {
970 __glibcxx_assert(_M_current - __n >= 0);
971 _M_current -= __n;
972 return *this;
973 }
974
975 constexpr _Tp&
976 operator[](difference_type __n) const noexcept
977 { return *(*this + __n); }
978
979 friend constexpr bool
980 operator==(const _Repeat_iterator& __x, const _Repeat_iterator& __y)
981 { return __x._M_current == __y._M_current; }
982
983 friend constexpr auto
984 operator<=>(const _Repeat_iterator& __x, const _Repeat_iterator& __y)
985 { return __x._M_current <=> __y._M_current; }
986
987 friend constexpr _Repeat_iterator
988 operator+(_Repeat_iterator __i, difference_type __n)
989 {
990 __i += __n;
991 return __i;
992 }
993
994 friend constexpr _Repeat_iterator
995 operator+(difference_type __n, _Repeat_iterator __i)
996 { return __i + __n; }
997
998 friend constexpr _Repeat_iterator
999 operator-(_Repeat_iterator __i, difference_type __n)
1000 {
1001 __i -= __n;
1002 return __i;
1003 }
1004
1005 friend constexpr difference_type
1006 operator-(const _Repeat_iterator& __x, const _Repeat_iterator& __y)
1007 {
1008 return (static_cast<difference_type>(__x._M_current)
1009 - static_cast<difference_type>(__y._M_current));
1010 }
1011
1012 friend constexpr _Repeat_iterator<_Tp, _IndexType>
1013 __make_repeat_iterator<_Tp, _IndexType>(_Tp&, _IndexType);
1014 };
1015
1016 template<typename _Tp, typename _IndexType>
1017 constexpr _Repeat_iterator<_Tp, _IndexType>
1018 __make_repeat_iterator(_Tp& __tp, _IndexType __bound)
1019 { return _Repeat_iterator<_Tp, _IndexType>(std::addressof(__tp), __bound); }
1020
1021_GLIBCXX_END_NAMESPACE_VERSION
1022} // namespace std
1023#endif // library concepts
1024#endif // C++20
1025#endif // _RANGES_UTIL_H
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition ptr_traits.h:234
typename remove_pointer< _Tp >::type remove_pointer_t
Alias template for remove_pointer.
Definition type_traits:2381
typename decay< _Tp >::type decay_t
Alias template for decay.
Definition type_traits:2967
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
Definition move.h:176
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 _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
ISO C++ entities toplevel namespace is std.
constexpr auto end(_Container &__cont) noexcept(noexcept(__cont.end())) -> decltype(__cont.end())
Return an iterator pointing to one past the last element of the container.
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
typename __detail::__projected< _Iter, _Proj >::__type projected
[projected], projected
constexpr auto begin(_Container &__cont) noexcept(noexcept(__cont.begin())) -> decltype(__cont.begin())
Return an iterator pointing to the first element of the container.
integral_constant
Definition type_traits:98
The ranges::view_interface class template.
Definition ranges_util.h:71
The ranges::subrange class template.
Finds the size of a given tuple type.
Definition utility.h:55
Gives the type of the ith element of a given tuple type.
Definition utility.h:86
[concept.derived], concept derived_from
Definition concepts:76
[concept.convertible], concept convertible_to
Definition concepts:81
[concept.defaultinitializable], concept default_initializable
Definition concepts:166
[range.range] The range concept.
[range.range] The borrowed_range concept.
[range.sized] The sized_range concept.
[range.view] The ranges::view concept.
A range for which ranges::begin returns an input iterator.
A range for which ranges::begin returns a forward iterator.
A range for which ranges::begin returns a bidirectional iterator.
A range for which ranges::begin and ranges::end return the same type.