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