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