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