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