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 <__algorithm/specialized_algorithms.h>
16#include <__concepts/assignable.h>
17#include <__config>
18#include <__functional/identity.h>
19#include <__iterator/concepts.h>
20#include <__iterator/projected.h>
21#include <__ranges/access.h>
22#include <__ranges/concepts.h>
23#include <__ranges/dangling.h>
24#include <__type_traits/remove_cvref.h>
25#include <__utility/move.h>
26
27#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28# pragma GCC system_header
29#endif
30
31_LIBCPP_PUSH_MACROS
32#include <__undef_macros>
33
34#if _LIBCPP_STD_VER >= 20
35
36_LIBCPP_BEGIN_NAMESPACE_STD
37
38namespace ranges {
39
40template <class _Iter, class _Func>
41using for_each_result = in_fun_result<_Iter, _Func>;
42
43struct __for_each {
44private:
45 template <class _Iter, class _Sent, class _Proj, class _Func>
46 _LIBCPP_HIDE_FROM_ABI constexpr static for_each_result<_Iter, _Func>
47 __for_each_impl(_Iter __first, _Sent __last, _Func& __func, _Proj& __proj) {
48 // In the case where we have different iterator and sentinel types, the segmented iterator optimization
49 // in std::for_each will not kick in. Therefore, we prefer std::for_each_n in that case (whenever we can
50 // obtain the `n`).
51 if constexpr (!std::assignable_from<_Iter&, _Sent> && std::sized_sentinel_for<_Sent, _Iter>) {
52 auto __n = __last - __first;
53 auto __end = std::__for_each_n(std::move(__first), __n, __func, __proj);
54 return {std::move(__end), std::move(__func)};
55 } else {
56 auto __end = std::__for_each(std::move(__first), std::move(__last), __func, __proj);
57 return {std::move(__end), std::move(__func)};
58 }
59 }
60
61public:
62 template <input_iterator _Iter,
63 sentinel_for<_Iter> _Sent,
64 class _Proj = identity,
65 indirectly_unary_invocable<projected<_Iter, _Proj>> _Func>
66 _LIBCPP_HIDE_FROM_ABI constexpr for_each_result<_Iter, _Func>
67 operator()(_Iter __first, _Sent __last, _Func __func, _Proj __proj = {}) const {
68 return __for_each_impl(std::move(__first), std::move(__last), __func, __proj);
69 }
70
71 template <input_range _Range,
72 class _Proj = identity,
73 indirectly_unary_invocable<projected<iterator_t<_Range>, _Proj>> _Func>
74 _LIBCPP_HIDE_FROM_ABI constexpr for_each_result<borrowed_iterator_t<_Range>, _Func>
75 operator()(_Range&& __range, _Func __func, _Proj __proj = {}) const {
76 using _SpecialAlg = __specialized_algorithm<_Algorithm::__for_each, __single_range<remove_cvref_t<_Range>>>;
77 if constexpr (_SpecialAlg::__has_algorithm) {
78 auto [__iter, __func2] = _SpecialAlg()(__range, std::move(__func), std::move(__proj));
79 return {std::move(__iter), std::move(__func)};
80 } else {
81 return __for_each_impl(ranges::begin(__range), ranges::end(__range), __func, __proj);
82 }
83 }
84};
85
86inline namespace __cpo {
87inline constexpr auto for_each = __for_each{};
88} // namespace __cpo
89} // namespace ranges
90
91_LIBCPP_END_NAMESPACE_STD
92
93#endif // _LIBCPP_STD_VER >= 20
94
95_LIBCPP_POP_MACROS
96
97#endif // _LIBCPP___ALGORITHM_RANGES_FOR_EACH_H