1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_RANGES_TRANSFORM_H
10 #define _LIBCPP___ALGORITHM_RANGES_TRANSFORM_H
12 #include <__algorithm/in_in_out_result.h>
13 #include <__algorithm/in_out_result.h>
14 #include <__concepts/constructible.h>
16 #include <__functional/identity.h>
17 #include <__functional/invoke.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>
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 # pragma GCC system_header
30 #include <__undef_macros>
32 #if _LIBCPP_STD_VER >= 20
34 _LIBCPP_BEGIN_NAMESPACE_STD
38 template <class _Ip
, class _Op
>
39 using unary_transform_result
= in_out_result
<_Ip
, _Op
>;
41 template <class _I1
, class _I2
, class _O1
>
42 using binary_transform_result
= in_in_out_result
<_I1
, _I2
, _O1
>;
46 template <class _InIter
, class _Sent
, class _OutIter
, class _Func
, class _Proj
>
47 _LIBCPP_HIDE_FROM_ABI
static constexpr unary_transform_result
<_InIter
, _OutIter
>
48 __unary(_InIter __first
, _Sent __last
, _OutIter __result
, _Func
& __operation
, _Proj
& __projection
) {
49 while (__first
!= __last
) {
50 *__result
= std::invoke(__operation
, std::invoke(__projection
, *__first
));
55 return {std::move(__first
), std::move(__result
)};
58 template <class _InIter1
,
66 _LIBCPP_HIDE_FROM_ABI
static constexpr binary_transform_result
<_InIter1
, _InIter2
, _OutIter
>
67 __binary(_InIter1 __first1
,
72 _Func
& __binary_operation
,
73 _Proj1
& __projection1
,
74 _Proj2
& __projection2
) {
75 while (__first1
!= __last1
&& __first2
!= __last2
) {
77 std::invoke(__binary_operation
, std::invoke(__projection1
, *__first1
), std::invoke(__projection2
, *__first2
));
82 return {std::move(__first1
), std::move(__first2
), std::move(__result
)};
86 template <input_iterator _InIter
,
87 sentinel_for
<_InIter
> _Sent
,
88 weakly_incrementable _OutIter
,
89 copy_constructible _Func
,
90 class _Proj
= identity
>
91 requires indirectly_writable
<_OutIter
, indirect_result_t
<_Func
&, projected
<_InIter
, _Proj
>>>
92 _LIBCPP_HIDE_FROM_ABI
constexpr unary_transform_result
<_InIter
, _OutIter
>
93 operator()(_InIter __first
, _Sent __last
, _OutIter __result
, _Func __operation
, _Proj __proj
= {}) const {
94 return __unary(std::move(__first
), std::move(__last
), std::move(__result
), __operation
, __proj
);
97 template <input_range _Range
, weakly_incrementable _OutIter
, copy_constructible _Func
, class _Proj
= identity
>
98 requires indirectly_writable
<_OutIter
, indirect_result_t
<_Func
, projected
<iterator_t
<_Range
>, _Proj
>>>
99 _LIBCPP_HIDE_FROM_ABI
constexpr unary_transform_result
<borrowed_iterator_t
<_Range
>, _OutIter
>
100 operator()(_Range
&& __range
, _OutIter __result
, _Func __operation
, _Proj __projection
= {}) const {
101 return __unary(ranges::begin(__range
), ranges::end(__range
), std::move(__result
), __operation
, __projection
);
104 template <input_iterator _InIter1
,
105 sentinel_for
<_InIter1
> _Sent1
,
106 input_iterator _InIter2
,
107 sentinel_for
<_InIter2
> _Sent2
,
108 weakly_incrementable _OutIter
,
109 copy_constructible _Func
,
110 class _Proj1
= identity
,
111 class _Proj2
= identity
>
112 requires indirectly_writable
<_OutIter
,
113 indirect_result_t
<_Func
&, projected
<_InIter1
, _Proj1
>, projected
<_InIter2
, _Proj2
>>>
114 _LIBCPP_HIDE_FROM_ABI
constexpr binary_transform_result
<_InIter1
, _InIter2
, _OutIter
> operator()(
120 _Func __binary_operation
,
121 _Proj1 __projection1
= {},
122 _Proj2 __projection2
= {}) const {
134 template <input_range _Range1
,
136 weakly_incrementable _OutIter
,
137 copy_constructible _Func
,
138 class _Proj1
= identity
,
139 class _Proj2
= identity
>
140 requires indirectly_writable
<
142 indirect_result_t
<_Func
&, projected
<iterator_t
<_Range1
>, _Proj1
>, projected
<iterator_t
<_Range2
>, _Proj2
>>>
143 _LIBCPP_HIDE_FROM_ABI
constexpr binary_transform_result
<borrowed_iterator_t
<_Range1
>,
144 borrowed_iterator_t
<_Range2
>,
146 operator()(_Range1
&& __range1
,
149 _Func __binary_operation
,
150 _Proj1 __projection1
= {},
151 _Proj2 __projection2
= {}) const {
153 ranges::begin(__range1
),
154 ranges::end(__range1
),
155 ranges::begin(__range2
),
156 ranges::end(__range2
),
164 inline namespace __cpo
{
165 inline constexpr auto transform
= __transform
{};
167 } // namespace ranges
169 _LIBCPP_END_NAMESPACE_STD
171 #endif // _LIBCPP_STD_VER >= 20
175 #endif // _LIBCPP___ALGORITHM_RANGES_TRANSFORM_H