libstdc++
ranges_util.h
Go to the documentation of this file.
1// Utilities for representing and manipulating ranges -*- C++ -*-
2
3// Copyright (C) 2019-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_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
38#ifdef __glibcxx_ranges
39namespace std _GLIBCXX_VISIBILITY(default)
40{
41_GLIBCXX_BEGIN_NAMESPACE_VERSION
42namespace ranges
43{
44 // C++20 24.5 [range.utility] Range utilities
45
46 namespace __detail
47 {
48 template<typename _Range>
49 concept __simple_view = view<_Range> && range<const _Range>
50 && same_as<iterator_t<_Range>, iterator_t<const _Range>>
51 && same_as<sentinel_t<_Range>, sentinel_t<const _Range>>;
52
53 template<typename _It>
54 concept __has_arrow = input_iterator<_It>
55 && (is_pointer_v<_It> || requires(_It __it) { __it.operator->(); });
56
57 using std::__detail::__different_from;
58 } // namespace __detail
59
60 /// The ranges::view_interface class template
61 template<typename _Derived>
62 requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
64 {
65 private:
66 constexpr _Derived& _M_derived() noexcept
67 {
68 static_assert(derived_from<_Derived, view_interface<_Derived>>);
69 static_assert(view<_Derived>);
70 return static_cast<_Derived&>(*this);
71 }
72
73 constexpr const _Derived& _M_derived() const noexcept
74 {
75 static_assert(derived_from<_Derived, view_interface<_Derived>>);
76 static_assert(view<_Derived>);
77 return static_cast<const _Derived&>(*this);
78 }
79
80 static constexpr bool
81 _S_bool(bool) noexcept; // not defined
82
83 template<typename _Tp>
84 static constexpr bool
85 _S_empty(_Tp& __t)
86 noexcept(noexcept(_S_bool(ranges::begin(__t) == ranges::end(__t))))
87 { return ranges::begin(__t) == ranges::end(__t); }
88
89 template<typename _Tp>
90 static constexpr auto
91 _S_size(_Tp& __t)
92 noexcept(noexcept(ranges::end(__t) - ranges::begin(__t)))
93 { return ranges::end(__t) - ranges::begin(__t); }
94
95 public:
96 constexpr bool
97 empty()
98 noexcept(noexcept(_S_empty(_M_derived())))
99 requires forward_range<_Derived> && (!sized_range<_Derived>)
100 { return _S_empty(_M_derived()); }
101
102 constexpr bool
103 empty()
104 noexcept(noexcept(ranges::size(_M_derived()) == 0))
105 requires sized_range<_Derived>
106 { return ranges::size(_M_derived()) == 0; }
107
108 constexpr bool
109 empty() const
110 noexcept(noexcept(_S_empty(_M_derived())))
111 requires forward_range<const _Derived> && (!sized_range<const _Derived>)
112 { return _S_empty(_M_derived()); }
113
114 constexpr bool
115 empty() const
116 noexcept(noexcept(ranges::size(_M_derived()) == 0))
117 requires sized_range<const _Derived>
118 { return ranges::size(_M_derived()) == 0; }
119
120 constexpr explicit
121 operator bool() noexcept(noexcept(ranges::empty(_M_derived())))
122 requires requires { ranges::empty(_M_derived()); }
123 { return !ranges::empty(_M_derived()); }
124
125 constexpr explicit
126 operator bool() const noexcept(noexcept(ranges::empty(_M_derived())))
127 requires requires { ranges::empty(_M_derived()); }
128 { return !ranges::empty(_M_derived()); }
129
130 constexpr auto
131 data() noexcept(noexcept(ranges::begin(_M_derived())))
132 requires contiguous_iterator<iterator_t<_Derived>>
133 { return std::to_address(ranges::begin(_M_derived())); }
134
135 constexpr auto
136 data() const noexcept(noexcept(ranges::begin(_M_derived())))
137 requires range<const _Derived>
138 && contiguous_iterator<iterator_t<const _Derived>>
139 { return std::to_address(ranges::begin(_M_derived())); }
140
141 constexpr auto
142 size() noexcept(noexcept(_S_size(_M_derived())))
143 requires forward_range<_Derived>
144 && sized_sentinel_for<sentinel_t<_Derived>, iterator_t<_Derived>>
145 { return _S_size(_M_derived()); }
146
147 constexpr auto
148 size() const noexcept(noexcept(_S_size(_M_derived())))
149 requires forward_range<const _Derived>
150 && sized_sentinel_for<sentinel_t<const _Derived>,
151 iterator_t<const _Derived>>
152 { return _S_size(_M_derived()); }
153
154 constexpr decltype(auto)
155 front() requires forward_range<_Derived>
156 {
157 __glibcxx_assert(!empty());
158 return *ranges::begin(_M_derived());
159 }
160
161 constexpr decltype(auto)
162 front() const requires forward_range<const _Derived>
163 {
164 __glibcxx_assert(!empty());
165 return *ranges::begin(_M_derived());
166 }
167
168 constexpr decltype(auto)
169 back()
170 requires bidirectional_range<_Derived> && common_range<_Derived>
171 {
172 __glibcxx_assert(!empty());
173 return *ranges::prev(ranges::end(_M_derived()));
174 }
175
176 constexpr decltype(auto)
177 back() const
178 requires bidirectional_range<const _Derived>
179 && common_range<const _Derived>
180 {
181 __glibcxx_assert(!empty());
182 return *ranges::prev(ranges::end(_M_derived()));
183 }
184
185 template<random_access_range _Range = _Derived>
186 constexpr decltype(auto)
187 operator[](range_difference_t<_Range> __n)
188 { return ranges::begin(_M_derived())[__n]; }
189
190 template<random_access_range _Range = const _Derived>
191 constexpr decltype(auto)
192 operator[](range_difference_t<_Range> __n) const
193 { return ranges::begin(_M_derived())[__n]; }
194
195#if __cplusplus > 202002L
196 constexpr auto
197 cbegin() requires input_range<_Derived>
198 { return ranges::cbegin(_M_derived()); }
199
200 constexpr auto
201 cbegin() const requires input_range<const _Derived>
202 { return ranges::cbegin(_M_derived()); }
203
204 constexpr auto
205 cend() requires input_range<_Derived>
206 { return ranges::cend(_M_derived()); }
207
208 constexpr auto
209 cend() const requires input_range<const _Derived>
210 { return ranges::cend(_M_derived()); }
211#endif
212 };
213
214 namespace __detail
215 {
216 template<typename _From, typename _To>
217 concept __uses_nonqualification_pointer_conversion
218 = is_pointer_v<_From> && is_pointer_v<_To>
219 && !convertible_to<remove_pointer_t<_From>(*)[],
221
222 template<typename _From, typename _To>
223 concept __convertible_to_non_slicing = convertible_to<_From, _To>
224 && !__uses_nonqualification_pointer_conversion<decay_t<_From>,
226
227 template<typename _Tp>
228 concept __pair_like
229 = !is_reference_v<_Tp> && requires(_Tp __t)
230 {
231 typename tuple_size<_Tp>::type;
232 requires derived_from<tuple_size<_Tp>, integral_constant<size_t, 2>>;
233 typename tuple_element_t<0, remove_const_t<_Tp>>;
234 typename tuple_element_t<1, remove_const_t<_Tp>>;
235 { get<0>(__t) } -> convertible_to<const tuple_element_t<0, _Tp>&>;
236 { get<1>(__t) } -> convertible_to<const tuple_element_t<1, _Tp>&>;
237 };
238
239 template<typename _Tp, typename _Up, typename _Vp>
240 concept __pair_like_convertible_from
241 = !range<_Tp> && __pair_like<_Tp>
242 && constructible_from<_Tp, _Up, _Vp>
243 && __convertible_to_non_slicing<_Up, tuple_element_t<0, _Tp>>
244 && convertible_to<_Vp, tuple_element_t<1, _Tp>>;
245
246 } // namespace __detail
247
248 namespace views { struct _Drop; } // defined in <ranges>
249
250 enum class subrange_kind : bool { unsized, sized };
251
252 /// The ranges::subrange class template
253 template<input_or_output_iterator _It, sentinel_for<_It> _Sent = _It,
254 subrange_kind _Kind = sized_sentinel_for<_Sent, _It>
255 ? subrange_kind::sized : subrange_kind::unsized>
256 requires (_Kind == subrange_kind::sized || !sized_sentinel_for<_Sent, _It>)
257 class subrange : public view_interface<subrange<_It, _Sent, _Kind>>
258 {
259 private:
260 static constexpr bool _S_store_size
261 = _Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _It>;
262
263 friend struct views::_Drop; // Needs to inspect _S_store_size.
264
265 _It _M_begin = _It();
266 [[no_unique_address]] _Sent _M_end = _Sent();
267
268 using __size_type
269 = __detail::__make_unsigned_like_t<iter_difference_t<_It>>;
270
271 template<typename _Tp, bool = _S_store_size>
272 struct _Size
273 {
274 [[__gnu__::__always_inline__]]
275 constexpr _Size(_Tp = {}) { }
276 };
277
278 template<typename _Tp>
279 struct _Size<_Tp, true>
280 {
281 [[__gnu__::__always_inline__]]
282 constexpr _Size(_Tp __s = {}) : _M_size(__s) { }
283
284 _Tp _M_size;
285 };
286
287 [[no_unique_address]] _Size<__size_type> _M_size = {};
288
289 public:
290 subrange() requires default_initializable<_It> = default;
291
292 constexpr
293 subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s)
294 noexcept(is_nothrow_constructible_v<_It, decltype(__i)>
295 && is_nothrow_constructible_v<_Sent, _Sent&>)
296 requires (!_S_store_size)
297 : _M_begin(std::move(__i)), _M_end(__s)
298 { }
299
300 constexpr
301 subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s,
302 __size_type __n)
303 noexcept(is_nothrow_constructible_v<_It, decltype(__i)>
304 && is_nothrow_constructible_v<_Sent, _Sent&>)
305 requires (_Kind == subrange_kind::sized)
306 : _M_begin(std::move(__i)), _M_end(__s), _M_size(__n)
307 { }
308
309 template<__detail::__different_from<subrange> _Rng>
310 requires borrowed_range<_Rng>
311 && __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
312 && convertible_to<sentinel_t<_Rng>, _Sent>
313 constexpr
314 subrange(_Rng&& __r)
315 noexcept(noexcept(subrange(__r, ranges::size(__r))))
316 requires _S_store_size && sized_range<_Rng>
317 : subrange(__r, ranges::size(__r))
318 { }
319
320 template<__detail::__different_from<subrange> _Rng>
321 requires borrowed_range<_Rng>
322 && __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
323 && convertible_to<sentinel_t<_Rng>, _Sent>
324 constexpr
325 subrange(_Rng&& __r)
326 noexcept(noexcept(subrange(ranges::begin(__r), ranges::end(__r))))
327 requires (!_S_store_size)
328 : subrange(ranges::begin(__r), ranges::end(__r))
329 { }
330
331 template<borrowed_range _Rng>
332 requires __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
333 && convertible_to<sentinel_t<_Rng>, _Sent>
334 constexpr
335 subrange(_Rng&& __r, __size_type __n)
336 noexcept(noexcept(subrange(ranges::begin(__r), ranges::end(__r), __n)))
337 requires (_Kind == subrange_kind::sized)
338 : subrange{ranges::begin(__r), ranges::end(__r), __n}
339 { }
340
341 template<__detail::__different_from<subrange> _PairLike>
342 requires __detail::__pair_like_convertible_from<_PairLike, const _It&,
343 const _Sent&>
344 constexpr
345 operator _PairLike() const
346 { return _PairLike(_M_begin, _M_end); }
347
348 constexpr _It
349 begin() const requires copyable<_It>
350 { return _M_begin; }
351
352 [[nodiscard]] constexpr _It
353 begin() requires (!copyable<_It>)
354 { return std::move(_M_begin); }
355
356 constexpr _Sent end() const { return _M_end; }
357
358 constexpr bool empty() const { return _M_begin == _M_end; }
359
360 constexpr __size_type
361 size() const requires (_Kind == subrange_kind::sized)
362 {
363 if constexpr (_S_store_size)
364 return _M_size._M_size;
365 else
366 return __detail::__to_unsigned_like(_M_end - _M_begin);
367 }
368
369 [[nodiscard]] constexpr subrange
370 next(iter_difference_t<_It> __n = 1) const &
371 requires forward_iterator<_It>
372 {
373 auto __tmp = *this;
374 __tmp.advance(__n);
375 return __tmp;
376 }
377
378 [[nodiscard]] constexpr subrange
379 next(iter_difference_t<_It> __n = 1) &&
380 {
381 advance(__n);
382 return std::move(*this);
383 }
384
385 [[nodiscard]] constexpr subrange
386 prev(iter_difference_t<_It> __n = 1) const
387 requires bidirectional_iterator<_It>
388 {
389 auto __tmp = *this;
390 __tmp.advance(-__n);
391 return __tmp;
392 }
393
394 constexpr subrange&
395 advance(iter_difference_t<_It> __n)
396 {
397 // _GLIBCXX_RESOLVE_LIB_DEFECTS
398 // 3433. subrange::advance(n) has UB when n < 0
399 if constexpr (bidirectional_iterator<_It>)
400 if (__n < 0)
401 {
402 ranges::advance(_M_begin, __n);
403 if constexpr (_S_store_size)
404 _M_size._M_size += __detail::__to_unsigned_like(-__n);
405 return *this;
406 }
407
408 __glibcxx_assert(__n >= 0);
409 auto __d = __n - ranges::advance(_M_begin, __n, _M_end);
410 if constexpr (_S_store_size)
411 _M_size._M_size -= __detail::__to_unsigned_like(__d);
412 return *this;
413 }
414 };
415
416 template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
417 subrange(_It, _Sent) -> subrange<_It, _Sent>;
418
419 template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
420 subrange(_It, _Sent,
421 __detail::__make_unsigned_like_t<iter_difference_t<_It>>)
423
424 template<borrowed_range _Rng>
425 subrange(_Rng&&)
426 -> subrange<iterator_t<_Rng>, sentinel_t<_Rng>,
427 (sized_range<_Rng>
428 || sized_sentinel_for<sentinel_t<_Rng>, iterator_t<_Rng>>)
429 ? subrange_kind::sized : subrange_kind::unsized>;
430
431 template<borrowed_range _Rng>
432 subrange(_Rng&&,
433 __detail::__make_unsigned_like_t<range_difference_t<_Rng>>)
434 -> subrange<iterator_t<_Rng>, sentinel_t<_Rng>, subrange_kind::sized>;
435
436 template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
437 requires (_Num < 2)
438 constexpr auto
439 get(const subrange<_It, _Sent, _Kind>& __r)
440 {
441 if constexpr (_Num == 0)
442 return __r.begin();
443 else
444 return __r.end();
445 }
446
447 template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
448 requires (_Num < 2)
449 constexpr auto
450 get(subrange<_It, _Sent, _Kind>&& __r)
451 {
452 if constexpr (_Num == 0)
453 return __r.begin();
454 else
455 return __r.end();
456 }
457
458 template<typename _It, typename _Sent, subrange_kind _Kind>
459 inline constexpr bool
460 enable_borrowed_range<subrange<_It, _Sent, _Kind>> = true;
461
462 template<range _Range>
463 using borrowed_subrange_t = __conditional_t<borrowed_range<_Range>,
464 subrange<iterator_t<_Range>>,
465 dangling>;
466} // namespace ranges
467
468// The following ranges algorithms are used by <ranges>, and are defined here
469// so that <ranges> can avoid including all of <bits/ranges_algo.h>.
470namespace ranges
471{
472 struct __find_fn
473 {
474 template<input_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Tp,
475 typename _Proj = identity>
476 requires indirect_binary_predicate<ranges::equal_to,
477 projected<_Iter, _Proj>, const _Tp*>
478 constexpr _Iter
479 operator()(_Iter __first, _Sent __last,
480 const _Tp& __value, _Proj __proj = {}) const
481 {
482 while (__first != __last
483 && !(std::__invoke(__proj, *__first) == __value))
484 ++__first;
485 return __first;
486 }
487
488 template<input_range _Range, typename _Tp, typename _Proj = identity>
489 requires indirect_binary_predicate<ranges::equal_to,
490 projected<iterator_t<_Range>, _Proj>,
491 const _Tp*>
492 constexpr borrowed_iterator_t<_Range>
493 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
494 {
495 return (*this)(ranges::begin(__r), ranges::end(__r),
496 __value, std::move(__proj));
497 }
498 };
499
500 inline constexpr __find_fn find{};
501
502 struct __find_if_fn
503 {
504 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
505 typename _Proj = identity,
506 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
507 constexpr _Iter
508 operator()(_Iter __first, _Sent __last,
509 _Pred __pred, _Proj __proj = {}) const
510 {
511 while (__first != __last
512 && !(bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
513 ++__first;
514 return __first;
515 }
516
517 template<input_range _Range, typename _Proj = identity,
518 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
519 _Pred>
520 constexpr borrowed_iterator_t<_Range>
521 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
522 {
523 return (*this)(ranges::begin(__r), ranges::end(__r),
524 std::move(__pred), std::move(__proj));
525 }
526 };
527
528 inline constexpr __find_if_fn find_if{};
529
530 struct __find_if_not_fn
531 {
532 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
533 typename _Proj = identity,
534 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
535 constexpr _Iter
536 operator()(_Iter __first, _Sent __last,
537 _Pred __pred, _Proj __proj = {}) const
538 {
539 while (__first != __last
540 && (bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
541 ++__first;
542 return __first;
543 }
544
545 template<input_range _Range, typename _Proj = identity,
546 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
547 _Pred>
548 constexpr borrowed_iterator_t<_Range>
549 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
550 {
551 return (*this)(ranges::begin(__r), ranges::end(__r),
552 std::move(__pred), std::move(__proj));
553 }
554 };
555
556 inline constexpr __find_if_not_fn find_if_not{};
557
558 template<typename _Iter1, typename _Iter2>
559 struct in_in_result
560 {
561 [[no_unique_address]] _Iter1 in1;
562 [[no_unique_address]] _Iter2 in2;
563
564 template<typename _IIter1, typename _IIter2>
565 requires convertible_to<const _Iter1&, _IIter1>
566 && convertible_to<const _Iter2&, _IIter2>
567 constexpr
568 operator in_in_result<_IIter1, _IIter2>() const &
569 { return {in1, in2}; }
570
571 template<typename _IIter1, typename _IIter2>
572 requires convertible_to<_Iter1, _IIter1>
573 && convertible_to<_Iter2, _IIter2>
574 constexpr
575 operator in_in_result<_IIter1, _IIter2>() &&
576 { return {std::move(in1), std::move(in2)}; }
577 };
578
579 template<typename _Iter1, typename _Iter2>
580 using mismatch_result = in_in_result<_Iter1, _Iter2>;
581
582 struct __mismatch_fn
583 {
584 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
585 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
586 typename _Pred = ranges::equal_to,
587 typename _Proj1 = identity, typename _Proj2 = identity>
588 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
589 constexpr mismatch_result<_Iter1, _Iter2>
590 operator()(_Iter1 __first1, _Sent1 __last1,
591 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
592 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
593 {
594 while (__first1 != __last1 && __first2 != __last2
595 && (bool)std::__invoke(__pred,
596 std::__invoke(__proj1, *__first1),
597 std::__invoke(__proj2, *__first2)))
598 {
599 ++__first1;
600 ++__first2;
601 }
602 return { std::move(__first1), std::move(__first2) };
603 }
604
605 template<input_range _Range1, input_range _Range2,
606 typename _Pred = ranges::equal_to,
607 typename _Proj1 = identity, typename _Proj2 = identity>
608 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
609 _Pred, _Proj1, _Proj2>
610 constexpr mismatch_result<iterator_t<_Range1>, iterator_t<_Range2>>
611 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
612 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
613 {
614 return (*this)(ranges::begin(__r1), ranges::end(__r1),
615 ranges::begin(__r2), ranges::end(__r2),
616 std::move(__pred),
617 std::move(__proj1), std::move(__proj2));
618 }
619 };
620
621 inline constexpr __mismatch_fn mismatch{};
622
623 struct __search_fn
624 {
625 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
626 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
627 typename _Pred = ranges::equal_to,
628 typename _Proj1 = identity, typename _Proj2 = identity>
629 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
630 constexpr subrange<_Iter1>
631 operator()(_Iter1 __first1, _Sent1 __last1,
632 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
633 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
634 {
635 if (__first1 == __last1 || __first2 == __last2)
636 return {__first1, __first1};
637
638 for (;;)
639 {
640 for (;;)
641 {
642 if (__first1 == __last1)
643 return {__first1, __first1};
644 if (std::__invoke(__pred,
645 std::__invoke(__proj1, *__first1),
646 std::__invoke(__proj2, *__first2)))
647 break;
648 ++__first1;
649 }
650 auto __cur1 = __first1;
651 auto __cur2 = __first2;
652 for (;;)
653 {
654 if (++__cur2 == __last2)
655 return {__first1, ++__cur1};
656 if (++__cur1 == __last1)
657 return {__cur1, __cur1};
658 if (!(bool)std::__invoke(__pred,
659 std::__invoke(__proj1, *__cur1),
660 std::__invoke(__proj2, *__cur2)))
661 {
662 ++__first1;
663 break;
664 }
665 }
666 }
667 }
668
669 template<forward_range _Range1, forward_range _Range2,
670 typename _Pred = ranges::equal_to,
671 typename _Proj1 = identity, typename _Proj2 = identity>
672 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
673 _Pred, _Proj1, _Proj2>
674 constexpr borrowed_subrange_t<_Range1>
675 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
676 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
677 {
678 return (*this)(ranges::begin(__r1), ranges::end(__r1),
679 ranges::begin(__r2), ranges::end(__r2),
680 std::move(__pred),
681 std::move(__proj1), std::move(__proj2));
682 }
683 };
684
685 inline constexpr __search_fn search{};
686
687 struct __min_fn
688 {
689 template<typename _Tp, typename _Proj = identity,
690 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
691 _Comp = ranges::less>
692 constexpr const _Tp&
693 operator()(const _Tp& __a, const _Tp& __b,
694 _Comp __comp = {}, _Proj __proj = {}) const
695 {
696 if (std::__invoke(__comp,
697 std::__invoke(__proj, __b),
698 std::__invoke(__proj, __a)))
699 return __b;
700 else
701 return __a;
702 }
703
704 template<input_range _Range, typename _Proj = identity,
705 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
706 _Comp = ranges::less>
707 requires indirectly_copyable_storable<iterator_t<_Range>,
708 range_value_t<_Range>*>
709 constexpr range_value_t<_Range>
710 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
711 {
712 auto __first = ranges::begin(__r);
713 auto __last = ranges::end(__r);
714 __glibcxx_assert(__first != __last);
715 auto __result = *__first;
716 while (++__first != __last)
717 {
718 auto __tmp = *__first;
719 if (std::__invoke(__comp,
720 std::__invoke(__proj, __tmp),
721 std::__invoke(__proj, __result)))
722 __result = std::move(__tmp);
723 }
724 return __result;
725 }
726
727 template<copyable _Tp, typename _Proj = identity,
728 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
729 _Comp = ranges::less>
730 constexpr _Tp
731 operator()(initializer_list<_Tp> __r,
732 _Comp __comp = {}, _Proj __proj = {}) const
733 {
734 return (*this)(ranges::subrange(__r),
735 std::move(__comp), std::move(__proj));
736 }
737 };
738
739 inline constexpr __min_fn min{};
740
741 struct __adjacent_find_fn
742 {
743 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
744 typename _Proj = identity,
745 indirect_binary_predicate<projected<_Iter, _Proj>,
746 projected<_Iter, _Proj>> _Pred
747 = ranges::equal_to>
748 constexpr _Iter
749 operator()(_Iter __first, _Sent __last,
750 _Pred __pred = {}, _Proj __proj = {}) const
751 {
752 if (__first == __last)
753 return __first;
754 auto __next = __first;
755 for (; ++__next != __last; __first = __next)
756 {
757 if (std::__invoke(__pred,
758 std::__invoke(__proj, *__first),
759 std::__invoke(__proj, *__next)))
760 return __first;
761 }
762 return __next;
763 }
764
765 template<forward_range _Range, typename _Proj = identity,
766 indirect_binary_predicate<
767 projected<iterator_t<_Range>, _Proj>,
768 projected<iterator_t<_Range>, _Proj>> _Pred = ranges::equal_to>
769 constexpr borrowed_iterator_t<_Range>
770 operator()(_Range&& __r, _Pred __pred = {}, _Proj __proj = {}) const
771 {
772 return (*this)(ranges::begin(__r), ranges::end(__r),
773 std::move(__pred), std::move(__proj));
774 }
775 };
776
777 inline constexpr __adjacent_find_fn adjacent_find{};
778
779} // namespace ranges
780
781 using ranges::get;
782
783 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
784 struct tuple_size<ranges::subrange<_Iter, _Sent, _Kind>>
785 : integral_constant<size_t, 2>
786 { };
787
788 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
789 struct tuple_element<0, ranges::subrange<_Iter, _Sent, _Kind>>
790 { using type = _Iter; };
791
792 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
793 struct tuple_element<1, ranges::subrange<_Iter, _Sent, _Kind>>
794 { using type = _Sent; };
795
796 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
797 struct tuple_element<0, const ranges::subrange<_Iter, _Sent, _Kind>>
798 { using type = _Iter; };
799
800 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
801 struct tuple_element<1, const ranges::subrange<_Iter, _Sent, _Kind>>
802 { using type = _Sent; };
803
804_GLIBCXX_END_NAMESPACE_VERSION
805} // namespace std
806#endif // library concepts
807#endif // C++20
808#endif // _RANGES_UTIL_H
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition ptr_traits.h:241
typename remove_pointer< _Tp >::type remove_pointer_t
Alias template for remove_pointer.
Definition type_traits:2148
typename decay< _Tp >::type decay_t
Alias template for decay.
Definition type_traits:2687
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:126
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
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
Definition valarray:1243
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition valarray:1221
ISO C++ entities toplevel namespace is std.
constexpr auto cend(const _Container &__cont) noexcept(noexcept(std::end(__cont))) -> decltype(std::end(__cont))
Return an iterator pointing to one past the last element of the const 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.
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
constexpr auto cbegin(const _Container &__cont) noexcept(noexcept(std::begin(__cont))) -> decltype(std::begin(__cont))
Return an iterator pointing to the first element of the const container.
integral_constant
Definition type_traits:88
The ranges::view_interface class template.
Definition ranges_util.h:64
The ranges::subrange class template.
Finds the size of a given tuple type.
Definition utility.h:49