xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/ranges_transform.h (revision b3edf4467982447620505a28fc82e38a414c07dc)
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_TRANSFORM_H
10 #define _LIBCPP___ALGORITHM_RANGES_TRANSFORM_H
11 
12 #include <__algorithm/in_in_out_result.h>
13 #include <__algorithm/in_out_result.h>
14 #include <__concepts/constructible.h>
15 #include <__config>
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>
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 _Ip, class _Op>
39 using unary_transform_result = in_out_result<_Ip, _Op>;
40 
41 template <class _I1, class _I2, class _O1>
42 using binary_transform_result = in_in_out_result<_I1, _I2, _O1>;
43 
44 namespace __transform {
45 struct __fn {
46 private:
47   template <class _InIter, class _Sent, class _OutIter, class _Func, class _Proj>
48   _LIBCPP_HIDE_FROM_ABI static constexpr unary_transform_result<_InIter, _OutIter>
49   __unary(_InIter __first, _Sent __last, _OutIter __result, _Func& __operation, _Proj& __projection) {
50     while (__first != __last) {
51       *__result = std::invoke(__operation, std::invoke(__projection, *__first));
52       ++__first;
53       ++__result;
54     }
55 
56     return {std::move(__first), std::move(__result)};
57   }
58 
59   template <class _InIter1,
60             class _Sent1,
61             class _InIter2,
62             class _Sent2,
63             class _OutIter,
64             class _Func,
65             class _Proj1,
66             class _Proj2>
67   _LIBCPP_HIDE_FROM_ABI static constexpr binary_transform_result<_InIter1, _InIter2, _OutIter>
68   __binary(_InIter1 __first1,
69            _Sent1 __last1,
70            _InIter2 __first2,
71            _Sent2 __last2,
72            _OutIter __result,
73            _Func& __binary_operation,
74            _Proj1& __projection1,
75            _Proj2& __projection2) {
76     while (__first1 != __last1 && __first2 != __last2) {
77       *__result =
78           std::invoke(__binary_operation, std::invoke(__projection1, *__first1), std::invoke(__projection2, *__first2));
79       ++__first1;
80       ++__first2;
81       ++__result;
82     }
83     return {std::move(__first1), std::move(__first2), std::move(__result)};
84   }
85 
86 public:
87   template <input_iterator _InIter,
88             sentinel_for<_InIter> _Sent,
89             weakly_incrementable _OutIter,
90             copy_constructible _Func,
91             class _Proj = identity>
92     requires indirectly_writable<_OutIter, indirect_result_t<_Func&, projected<_InIter, _Proj>>>
93   _LIBCPP_HIDE_FROM_ABI constexpr unary_transform_result<_InIter, _OutIter>
94   operator()(_InIter __first, _Sent __last, _OutIter __result, _Func __operation, _Proj __proj = {}) const {
95     return __unary(std::move(__first), std::move(__last), std::move(__result), __operation, __proj);
96   }
97 
98   template <input_range _Range, weakly_incrementable _OutIter, copy_constructible _Func, class _Proj = identity>
99     requires indirectly_writable<_OutIter, indirect_result_t<_Func, projected<iterator_t<_Range>, _Proj>>>
100   _LIBCPP_HIDE_FROM_ABI constexpr unary_transform_result<borrowed_iterator_t<_Range>, _OutIter>
101   operator()(_Range&& __range, _OutIter __result, _Func __operation, _Proj __projection = {}) const {
102     return __unary(ranges::begin(__range), ranges::end(__range), std::move(__result), __operation, __projection);
103   }
104 
105   template <input_iterator _InIter1,
106             sentinel_for<_InIter1> _Sent1,
107             input_iterator _InIter2,
108             sentinel_for<_InIter2> _Sent2,
109             weakly_incrementable _OutIter,
110             copy_constructible _Func,
111             class _Proj1 = identity,
112             class _Proj2 = identity>
113     requires indirectly_writable<_OutIter,
114                                  indirect_result_t<_Func&, projected<_InIter1, _Proj1>, projected<_InIter2, _Proj2>>>
115   _LIBCPP_HIDE_FROM_ABI constexpr binary_transform_result<_InIter1, _InIter2, _OutIter> operator()(
116       _InIter1 __first1,
117       _Sent1 __last1,
118       _InIter2 __first2,
119       _Sent2 __last2,
120       _OutIter __result,
121       _Func __binary_operation,
122       _Proj1 __projection1 = {},
123       _Proj2 __projection2 = {}) const {
124     return __binary(
125         std::move(__first1),
126         std::move(__last1),
127         std::move(__first2),
128         std::move(__last2),
129         std::move(__result),
130         __binary_operation,
131         __projection1,
132         __projection2);
133   }
134 
135   template <input_range _Range1,
136             input_range _Range2,
137             weakly_incrementable _OutIter,
138             copy_constructible _Func,
139             class _Proj1 = identity,
140             class _Proj2 = identity>
141     requires indirectly_writable<
142         _OutIter,
143         indirect_result_t<_Func&, projected<iterator_t<_Range1>, _Proj1>, projected<iterator_t<_Range2>, _Proj2>>>
144   _LIBCPP_HIDE_FROM_ABI constexpr binary_transform_result<borrowed_iterator_t<_Range1>,
145                                                           borrowed_iterator_t<_Range2>,
146                                                           _OutIter>
147   operator()(_Range1&& __range1,
148              _Range2&& __range2,
149              _OutIter __result,
150              _Func __binary_operation,
151              _Proj1 __projection1 = {},
152              _Proj2 __projection2 = {}) const {
153     return __binary(
154         ranges::begin(__range1),
155         ranges::end(__range1),
156         ranges::begin(__range2),
157         ranges::end(__range2),
158         std::move(__result),
159         __binary_operation,
160         __projection1,
161         __projection2);
162   }
163 };
164 } // namespace __transform
165 
166 inline namespace __cpo {
167 inline constexpr auto transform = __transform::__fn{};
168 } // namespace __cpo
169 } // namespace ranges
170 
171 _LIBCPP_END_NAMESPACE_STD
172 
173 #endif // _LIBCPP_STD_VER >= 20
174 
175 _LIBCPP_POP_MACROS
176 
177 #endif // _LIBCPP___ALGORITHM_RANGES_TRANSFORM_H
178