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 <cmath>
36 #include <compare>
37 #include <iterator>
38 // #include <bits/range_concepts.h>
39 #include <ranges>
40 #include <bits/invoke.h>
41 #include <bits/cpp_type_traits.h> // __is_byte
42 
43 #if __cpp_lib_concepts
44 namespace std _GLIBCXX_VISIBILITY(default)
45 {
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 namespace ranges
48 {
49  namespace __detail
50  {
51  template<typename _Tp>
52  constexpr inline bool __is_normal_iterator = false;
53 
54  template<typename _Iterator, typename _Container>
55  constexpr inline bool
56  __is_normal_iterator<__gnu_cxx::__normal_iterator<_Iterator,
57  _Container>> = true;
58 
59  template<typename _Tp>
60  constexpr inline bool __is_reverse_iterator = false;
61 
62  template<typename _Iterator>
63  constexpr inline bool
64  __is_reverse_iterator<reverse_iterator<_Iterator>> = true;
65 
66  template<typename _Tp>
67  constexpr inline bool __is_move_iterator = false;
68 
69  template<typename _Iterator>
70  constexpr inline bool
71  __is_move_iterator<move_iterator<_Iterator>> = true;
72  } // namespace __detail
73 
74  struct __equal_fn
75  {
76  template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
77  input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
78  typename _Pred = ranges::equal_to,
79  typename _Proj1 = identity, typename _Proj2 = identity>
80  requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
81  constexpr bool
82  operator()(_Iter1 __first1, _Sent1 __last1,
83  _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
84  _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
85  {
86  // TODO: implement more specializations to at least have parity with
87  // std::equal.
88  if constexpr (__detail::__is_normal_iterator<_Iter1>
89  || __detail::__is_normal_iterator<_Iter2>)
90  return (*this)(std::__niter_base(std::move(__first1)),
91  std::__niter_base(std::move(__last1)),
92  std::__niter_base(std::move(__first2)),
93  std::__niter_base(std::move(__last2)),
94  std::move(__pred),
95  std::move(__proj1), std::move(__proj2));
96  else if constexpr (sized_sentinel_for<_Sent1, _Iter1>
97  && sized_sentinel_for<_Sent2, _Iter2>)
98  {
99  auto __d1 = ranges::distance(__first1, __last1);
100  auto __d2 = ranges::distance(__first2, __last2);
101  if (__d1 != __d2)
102  return false;
103 
104  using _ValueType1 = iter_value_t<_Iter1>;
105  using _ValueType2 = iter_value_t<_Iter2>;
106  constexpr bool __use_memcmp
107  = ((is_integral_v<_ValueType1> || is_pointer_v<_ValueType1>)
108  && is_same_v<_ValueType1, _ValueType2>
109  && is_pointer_v<_Iter1>
110  && is_pointer_v<_Iter2>
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  return (*this)(ranges::begin(__r1), ranges::end(__r1),
152  ranges::begin(__r2), ranges::end(__r2),
153  std::move(__pred),
154  std::move(__proj1), std::move(__proj2));
155  }
156  };
157 
158  inline constexpr __equal_fn equal{};
159 
160  template<typename _Iter, typename _Out>
161  struct copy_result
162  {
163  [[no_unique_address]] _Iter in;
164  [[no_unique_address]] _Out out;
165 
166  template<typename _Iter2, typename _Out2>
167  requires convertible_to<const _Iter&, _Iter2>
168  && convertible_to<const _Out&, _Out2>
169  operator copy_result<_Iter2, _Out2>() const &
170  { return {in, out}; }
171 
172  template<typename _Iter2, typename _Out2>
173  requires convertible_to<_Iter, _Iter2>
174  && convertible_to<_Out, _Out2>
175  operator copy_result<_Iter2, _Out2>() &&
176  { return {std::move(in), std::move(out)}; }
177  };
178 
179  template<typename _Iter, typename _Out>
180  using move_result = copy_result<_Iter, _Out>;
181 
182  template<typename _Iter1, typename _Iter2>
183  using move_backward_result = copy_result<_Iter1, _Iter2>;
184 
185  template<typename _Iter1, typename _Iter2>
186  using copy_backward_result = copy_result<_Iter1, _Iter2>;
187 
188  template<bool _IsMove,
189  bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
190  bidirectional_iterator _Out>
191  requires (_IsMove
192  ? indirectly_movable<_Iter, _Out>
193  : indirectly_copyable<_Iter, _Out>)
194  constexpr conditional_t<_IsMove,
195  move_backward_result<_Iter, _Out>,
196  copy_backward_result<_Iter, _Out>>
197  __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result);
198 
199  template<bool _IsMove,
200  input_iterator _Iter, sentinel_for<_Iter> _Sent,
201  weakly_incrementable _Out>
202  requires (_IsMove
203  ? indirectly_movable<_Iter, _Out>
204  : indirectly_copyable<_Iter, _Out>)
205  constexpr conditional_t<_IsMove,
206  move_result<_Iter, _Out>,
207  copy_result<_Iter, _Out>>
208  __copy_or_move(_Iter __first, _Sent __last, _Out __result)
209  {
210  // TODO: implement more specializations to be at least on par with
211  // std::copy/std::move.
212  constexpr bool __normal_iterator_p
213  = (__detail::__is_normal_iterator<_Iter>
214  || __detail::__is_normal_iterator<_Out>);
215  constexpr bool __reverse_p
216  = (__detail::__is_reverse_iterator<_Iter>
217  && __detail::__is_reverse_iterator<_Out>);
218  constexpr bool __move_iterator_p = __detail::__is_move_iterator<_Iter>;
219  if constexpr (__move_iterator_p)
220  {
221  auto [__in, __out]
222  = ranges::__copy_or_move<true>(std::move(__first).base(),
223  std::move(__last).base(),
224  std::move(__result));
225  return {move_iterator{std::move(__in)}, std::move(__out)};
226  }
227  else if constexpr (__reverse_p)
228  {
229  auto [__in,__out]
230  = ranges::__copy_or_move_backward<_IsMove>(__last.base(),
231  __first.base(),
232  __result.base());
233  return {reverse_iterator{std::move(__in)},
234  reverse_iterator{std::move(__out)}};
235  }
236  else if constexpr (__normal_iterator_p)
237  {
238  auto [__in,__out]
239  = ranges::__copy_or_move<_IsMove>(std::__niter_base(__first),
240  std::__niter_base(__last),
241  std::__niter_base(__result));
242  return {std::__niter_wrap(__first, std::move(__in)),
243  std::__niter_wrap(__result, std::move(__out))};
244  }
245  else if constexpr (sized_sentinel_for<_Sent, _Iter>)
246  {
247  using _ValueTypeI = iter_value_t<_Iter>;
248  using _ValueTypeO = iter_value_t<_Out>;
249  constexpr bool __use_memmove
250  = (is_trivially_copyable_v<_ValueTypeI>
251  && is_same_v<_ValueTypeI, _ValueTypeO>
252  && is_pointer_v<_Iter>
253  && is_pointer_v<_Out>);
254 
255  if constexpr (__use_memmove)
256  {
257  static_assert(_IsMove
258  ? is_move_assignable_v<_ValueTypeI>
259  : is_copy_assignable_v<_ValueTypeI>);
260  auto __num = __last - __first;
261  if (__num)
262  std::__memmove<_IsMove>(__result, __first, __num);
263  return {__first + __num, __result + __num};
264  }
265  else
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  }
279  else
280  {
281  while (__first != __last)
282  {
283  if constexpr (_IsMove)
284  *__result = std::move(*__first);
285  else
286  *__result = *__first;
287  ++__first;
288  ++__result;
289  }
290  return {std::move(__first), std::move(__result)};
291  }
292  }
293 
294  struct __copy_fn
295  {
296  template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
297  weakly_incrementable _Out>
298  requires indirectly_copyable<_Iter, _Out>
299  constexpr copy_result<_Iter, _Out>
300  operator()(_Iter __first, _Sent __last, _Out __result) const
301  {
302  return ranges::__copy_or_move<false>(std::move(__first),
303  std::move(__last),
304  std::move(__result));
305  }
306 
307  template<input_range _Range, weakly_incrementable _Out>
308  requires indirectly_copyable<iterator_t<_Range>, _Out>
309  constexpr copy_result<safe_iterator_t<_Range>, _Out>
310  operator()(_Range&& __r, _Out __result) const
311  {
312  return (*this)(ranges::begin(__r), ranges::end(__r),
313  std::move(__result));
314  }
315  };
316 
317  inline constexpr __copy_fn copy{};
318 
319  struct __move_fn
320  {
321  template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
322  weakly_incrementable _Out>
323  requires indirectly_movable<_Iter, _Out>
324  constexpr move_result<_Iter, _Out>
325  operator()(_Iter __first, _Sent __last, _Out __result) const
326  {
327  return ranges::__copy_or_move<true>(std::move(__first),
328  std::move(__last),
329  std::move(__result));
330  }
331 
332  template<input_range _Range, weakly_incrementable _Out>
333  requires indirectly_movable<iterator_t<_Range>, _Out>
334  constexpr move_result<safe_iterator_t<_Range>, _Out>
335  operator()(_Range&& __r, _Out __result) const
336  {
337  return (*this)(ranges::begin(__r), ranges::end(__r),
338  std::move(__result));
339  }
340  };
341 
342  inline constexpr __move_fn move{};
343 
344  template<bool _IsMove,
345  bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
346  bidirectional_iterator _Out>
347  requires (_IsMove
348  ? indirectly_movable<_Iter, _Out>
349  : indirectly_copyable<_Iter, _Out>)
350  constexpr conditional_t<_IsMove,
351  move_backward_result<_Iter, _Out>,
352  copy_backward_result<_Iter, _Out>>
353  __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result)
354  {
355  // TODO: implement more specializations to be at least on par with
356  // std::copy_backward/std::move_backward.
357  constexpr bool __normal_iterator_p
358  = (__detail::__is_normal_iterator<_Iter>
359  || __detail::__is_normal_iterator<_Out>);
360  constexpr bool __reverse_p
361  = (__detail::__is_reverse_iterator<_Iter>
362  && __detail::__is_reverse_iterator<_Out>);
363  if constexpr (__reverse_p)
364  {
365  auto [__in,__out]
366  = ranges::__copy_or_move<_IsMove>(__last.base(),
367  __first.base(),
368  __result.base());
369  return {reverse_iterator{std::move(__in)},
370  reverse_iterator{std::move(__out)}};
371  }
372  else if constexpr (__normal_iterator_p)
373  {
374  auto [__in,__out]
375  = ranges::__copy_or_move_backward<_IsMove>
376  (std::__niter_base(__first),
377  std::__niter_base(__last),
378  std::__niter_base(__result));
379  return {std::__niter_wrap(__first, std::move(__in)),
380  std::__niter_wrap(__result, std::move(__out))};
381  }
382  else if constexpr (sized_sentinel_for<_Sent, _Iter>)
383  {
384  using _ValueTypeI = iter_value_t<_Iter>;
385  using _ValueTypeO = iter_value_t<_Out>;
386  constexpr bool __use_memmove
387  = (is_trivially_copyable_v<_ValueTypeI>
388  && is_same_v<_ValueTypeI, _ValueTypeO>
389  && is_pointer_v<_Iter>
390  && is_pointer_v<_Out>);
391  if constexpr (__use_memmove)
392  {
393  static_assert(_IsMove
394  ? is_move_assignable_v<_ValueTypeI>
395  : is_copy_assignable_v<_ValueTypeI>);
396  auto __num = __last - __first;
397  if (__num)
398  std::__memmove<_IsMove>(__result - __num, __first, __num);
399  return {__first + __num, __result - __num};
400  }
401  else
402  {
403  auto __lasti = ranges::next(__first, __last);
404  auto __tail = __lasti;
405 
406  for (auto __n = __last - __first; __n > 0; --__n)
407  {
408  --__tail;
409  --__result;
410  if constexpr (_IsMove)
411  *__result = std::move(*__tail);
412  else
413  *__result = *__tail;
414  }
415  return {std::move(__lasti), std::move(__result)};
416  }
417  }
418  else
419  {
420  auto __lasti = ranges::next(__first, __last);
421  auto __tail = __lasti;
422 
423  while (__first != __tail)
424  {
425  --__tail;
426  --__result;
427  if constexpr (_IsMove)
428  *__result = std::move(*__tail);
429  else
430  *__result = *__tail;
431  }
432  return {std::move(__lasti), std::move(__result)};
433  }
434  }
435 
436  struct __copy_backward_fn
437  {
438  template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
439  bidirectional_iterator _Iter2>
440  requires indirectly_copyable<_Iter1, _Iter2>
441  constexpr copy_backward_result<_Iter1, _Iter2>
442  operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
443  {
444  return ranges::__copy_or_move_backward<false>(std::move(__first),
445  std::move(__last),
446  std::move(__result));
447  }
448 
449  template<bidirectional_range _Range, bidirectional_iterator _Iter>
450  requires indirectly_copyable<iterator_t<_Range>, _Iter>
451  constexpr copy_backward_result<safe_iterator_t<_Range>, _Iter>
452  operator()(_Range&& __r, _Iter __result) const
453  {
454  return (*this)(ranges::begin(__r), ranges::end(__r),
455  std::move(__result));
456  }
457  };
458 
459  inline constexpr __copy_backward_fn copy_backward{};
460 
461  struct __move_backward_fn
462  {
463  template<bidirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
464  bidirectional_iterator _Iter2>
465  requires indirectly_movable<_Iter1, _Iter2>
466  constexpr move_backward_result<_Iter1, _Iter2>
467  operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result) const
468  {
469  return ranges::__copy_or_move_backward<true>(std::move(__first),
470  std::move(__last),
471  std::move(__result));
472  }
473 
474  template<bidirectional_range _Range, bidirectional_iterator _Iter>
475  requires indirectly_movable<iterator_t<_Range>, _Iter>
476  constexpr move_backward_result<safe_iterator_t<_Range>, _Iter>
477  operator()(_Range&& __r, _Iter __result) const
478  {
479  return (*this)(ranges::begin(__r), ranges::end(__r),
480  std::move(__result));
481  }
482  };
483 
484  inline constexpr __move_backward_fn move_backward{};
485 
486  template<typename _Iter, typename _Out>
487  using copy_n_result = copy_result<_Iter, _Out>;
488 
489  struct __copy_n_fn
490  {
491  template<input_iterator _Iter, weakly_incrementable _Out>
492  requires indirectly_copyable<_Iter, _Out>
493  constexpr copy_n_result<_Iter, _Out>
494  operator()(_Iter __first, iter_difference_t<_Iter> __n,
495  _Out __result) const
496  {
497  if constexpr (random_access_iterator<_Iter>)
498  return ranges::copy(__first, __first + __n, std::move(__result));
499  else
500  {
501  for (; __n > 0; --__n, (void)++__result, (void)++__first)
502  *__result = *__first;
503  return {std::move(__first), std::move(__result)};
504  }
505  }
506  };
507 
508  inline constexpr __copy_n_fn copy_n{};
509 
510  struct __fill_n_fn
511  {
512  template<typename _Tp, output_iterator<const _Tp&> _Out>
513  constexpr _Out
514  operator()(_Out __first, iter_difference_t<_Out> __n,
515  const _Tp& __value) const
516  {
517  // TODO: implement more specializations to be at least on par with
518  // std::fill_n
519  if (__n <= 0)
520  return __first;
521 
522  // TODO: is __is_byte the best condition?
523  if constexpr (is_pointer_v<_Out> && __is_byte<_Tp>::__value)
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 safe_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
ranges
std::begin
_Tp * begin(valarray< _Tp > &__va)
Return an iterator pointing to the first element of the valarray.
Definition: valarray:1214
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:101
std::conditional_t
typename conditional< _Cond, _Iftrue, _Iffalse >::type conditional_t
Alias template for conditional.
Definition: type_traits:2557
std::__invoke
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
cpp_type_traits.h
std
ISO C++ entities toplevel namespace is std.
std::distance
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Definition: stl_iterator_base_funcs.h:138
compare
std::move_backward
constexpr _BI2 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
Moves the range [first,last) into result.
Definition: stl_algobase.h:867
std::end
_Tp * end(valarray< _Tp > &__va)
Return an iterator pointing to one past the last element of the valarray.
Definition: valarray:1234
invoke.h