xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/ranges_for_each.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_RANGES_FOR_EACH_H
10 #define _LIBCPP___ALGORITHM_RANGES_FOR_EACH_H
11 
12 #include <__algorithm/for_each.h>
13 #include <__algorithm/for_each_n.h>
14 #include <__algorithm/in_fun_result.h>
15 #include <__concepts/assignable.h>
16 #include <__config>
17 #include <__functional/identity.h>
18 #include <__iterator/concepts.h>
19 #include <__iterator/projected.h>
20 #include <__ranges/access.h>
21 #include <__ranges/concepts.h>
22 #include <__ranges/dangling.h>
23 #include <__utility/move.h>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 _LIBCPP_PUSH_MACROS
30 #include <__undef_macros>
31 
32 #if _LIBCPP_STD_VER >= 20
33 
34 _LIBCPP_BEGIN_NAMESPACE_STD
35 
36 namespace ranges {
37 
38 template <class _Iter, class _Func>
39 using for_each_result = in_fun_result<_Iter, _Func>;
40 
41 struct __for_each {
42 private:
43   template <class _Iter, class _Sent, class _Proj, class _Func>
44   _LIBCPP_HIDE_FROM_ABI constexpr static for_each_result<_Iter, _Func>
__for_each_impl__for_each45   __for_each_impl(_Iter __first, _Sent __last, _Func& __func, _Proj& __proj) {
46     // In the case where we have different iterator and sentinel types, the segmented iterator optimization
47     // in std::for_each will not kick in. Therefore, we prefer std::for_each_n in that case (whenever we can
48     // obtain the `n`).
49     if constexpr (!std::assignable_from<_Iter&, _Sent> && std::sized_sentinel_for<_Sent, _Iter>) {
50       auto __n   = __last - __first;
51       auto __end = std::__for_each_n(std::move(__first), __n, __func, __proj);
52       return {std::move(__end), std::move(__func)};
53     } else {
54       auto __end = std::__for_each(std::move(__first), std::move(__last), __func, __proj);
55       return {std::move(__end), std::move(__func)};
56     }
57   }
58 
59 public:
60   template <input_iterator _Iter,
61             sentinel_for<_Iter> _Sent,
62             class _Proj = identity,
63             indirectly_unary_invocable<projected<_Iter, _Proj>> _Func>
64   _LIBCPP_HIDE_FROM_ABI constexpr for_each_result<_Iter, _Func>
operator__for_each65   operator()(_Iter __first, _Sent __last, _Func __func, _Proj __proj = {}) const {
66     return __for_each_impl(std::move(__first), std::move(__last), __func, __proj);
67   }
68 
69   template <input_range _Range,
70             class _Proj = identity,
71             indirectly_unary_invocable<projected<iterator_t<_Range>, _Proj>> _Func>
72   _LIBCPP_HIDE_FROM_ABI constexpr for_each_result<borrowed_iterator_t<_Range>, _Func>
operator__for_each73   operator()(_Range&& __range, _Func __func, _Proj __proj = {}) const {
74     return __for_each_impl(ranges::begin(__range), ranges::end(__range), __func, __proj);
75   }
76 };
77 
78 inline namespace __cpo {
79 inline constexpr auto for_each = __for_each{};
80 } // namespace __cpo
81 } // namespace ranges
82 
83 _LIBCPP_END_NAMESPACE_STD
84 
85 #endif // _LIBCPP_STD_VER >= 20
86 
87 _LIBCPP_POP_MACROS
88 
89 #endif // _LIBCPP___ALGORITHM_RANGES_FOR_EACH_H
90