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