File indexing completed on 2026-05-03 08:13:11
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___ALGORITHM_RANGES_TRANSFORM_H
0010 #define _LIBCPP___ALGORITHM_RANGES_TRANSFORM_H
0011
0012 #include <__algorithm/in_in_out_result.h>
0013 #include <__algorithm/in_out_result.h>
0014 #include <__concepts/constructible.h>
0015 #include <__config>
0016 #include <__functional/identity.h>
0017 #include <__functional/invoke.h>
0018 #include <__iterator/concepts.h>
0019 #include <__iterator/projected.h>
0020 #include <__ranges/access.h>
0021 #include <__ranges/concepts.h>
0022 #include <__ranges/dangling.h>
0023 #include <__utility/move.h>
0024
0025 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0026 # pragma GCC system_header
0027 #endif
0028
0029 _LIBCPP_PUSH_MACROS
0030 #include <__undef_macros>
0031
0032 #if _LIBCPP_STD_VER >= 20
0033
0034 _LIBCPP_BEGIN_NAMESPACE_STD
0035
0036 namespace ranges {
0037
0038 template <class _Ip, class _Op>
0039 using unary_transform_result = in_out_result<_Ip, _Op>;
0040
0041 template <class _I1, class _I2, class _O1>
0042 using binary_transform_result = in_in_out_result<_I1, _I2, _O1>;
0043
0044 struct __transform {
0045 private:
0046 template <class _InIter, class _Sent, class _OutIter, class _Func, class _Proj>
0047 _LIBCPP_HIDE_FROM_ABI static constexpr unary_transform_result<_InIter, _OutIter>
0048 __unary(_InIter __first, _Sent __last, _OutIter __result, _Func& __operation, _Proj& __projection) {
0049 while (__first != __last) {
0050 *__result = std::invoke(__operation, std::invoke(__projection, *__first));
0051 ++__first;
0052 ++__result;
0053 }
0054
0055 return {std::move(__first), std::move(__result)};
0056 }
0057
0058 template <class _InIter1,
0059 class _Sent1,
0060 class _InIter2,
0061 class _Sent2,
0062 class _OutIter,
0063 class _Func,
0064 class _Proj1,
0065 class _Proj2>
0066 _LIBCPP_HIDE_FROM_ABI static constexpr binary_transform_result<_InIter1, _InIter2, _OutIter>
0067 __binary(_InIter1 __first1,
0068 _Sent1 __last1,
0069 _InIter2 __first2,
0070 _Sent2 __last2,
0071 _OutIter __result,
0072 _Func& __binary_operation,
0073 _Proj1& __projection1,
0074 _Proj2& __projection2) {
0075 while (__first1 != __last1 && __first2 != __last2) {
0076 *__result =
0077 std::invoke(__binary_operation, std::invoke(__projection1, *__first1), std::invoke(__projection2, *__first2));
0078 ++__first1;
0079 ++__first2;
0080 ++__result;
0081 }
0082 return {std::move(__first1), std::move(__first2), std::move(__result)};
0083 }
0084
0085 public:
0086 template <input_iterator _InIter,
0087 sentinel_for<_InIter> _Sent,
0088 weakly_incrementable _OutIter,
0089 copy_constructible _Func,
0090 class _Proj = identity>
0091 requires indirectly_writable<_OutIter, indirect_result_t<_Func&, projected<_InIter, _Proj>>>
0092 _LIBCPP_HIDE_FROM_ABI constexpr unary_transform_result<_InIter, _OutIter>
0093 operator()(_InIter __first, _Sent __last, _OutIter __result, _Func __operation, _Proj __proj = {}) const {
0094 return __unary(std::move(__first), std::move(__last), std::move(__result), __operation, __proj);
0095 }
0096
0097 template <input_range _Range, weakly_incrementable _OutIter, copy_constructible _Func, class _Proj = identity>
0098 requires indirectly_writable<_OutIter, indirect_result_t<_Func, projected<iterator_t<_Range>, _Proj>>>
0099 _LIBCPP_HIDE_FROM_ABI constexpr unary_transform_result<borrowed_iterator_t<_Range>, _OutIter>
0100 operator()(_Range&& __range, _OutIter __result, _Func __operation, _Proj __projection = {}) const {
0101 return __unary(ranges::begin(__range), ranges::end(__range), std::move(__result), __operation, __projection);
0102 }
0103
0104 template <input_iterator _InIter1,
0105 sentinel_for<_InIter1> _Sent1,
0106 input_iterator _InIter2,
0107 sentinel_for<_InIter2> _Sent2,
0108 weakly_incrementable _OutIter,
0109 copy_constructible _Func,
0110 class _Proj1 = identity,
0111 class _Proj2 = identity>
0112 requires indirectly_writable<_OutIter,
0113 indirect_result_t<_Func&, projected<_InIter1, _Proj1>, projected<_InIter2, _Proj2>>>
0114 _LIBCPP_HIDE_FROM_ABI constexpr binary_transform_result<_InIter1, _InIter2, _OutIter> operator()(
0115 _InIter1 __first1,
0116 _Sent1 __last1,
0117 _InIter2 __first2,
0118 _Sent2 __last2,
0119 _OutIter __result,
0120 _Func __binary_operation,
0121 _Proj1 __projection1 = {},
0122 _Proj2 __projection2 = {}) const {
0123 return __binary(
0124 std::move(__first1),
0125 std::move(__last1),
0126 std::move(__first2),
0127 std::move(__last2),
0128 std::move(__result),
0129 __binary_operation,
0130 __projection1,
0131 __projection2);
0132 }
0133
0134 template <input_range _Range1,
0135 input_range _Range2,
0136 weakly_incrementable _OutIter,
0137 copy_constructible _Func,
0138 class _Proj1 = identity,
0139 class _Proj2 = identity>
0140 requires indirectly_writable<
0141 _OutIter,
0142 indirect_result_t<_Func&, projected<iterator_t<_Range1>, _Proj1>, projected<iterator_t<_Range2>, _Proj2>>>
0143 _LIBCPP_HIDE_FROM_ABI constexpr binary_transform_result<borrowed_iterator_t<_Range1>,
0144 borrowed_iterator_t<_Range2>,
0145 _OutIter>
0146 operator()(_Range1&& __range1,
0147 _Range2&& __range2,
0148 _OutIter __result,
0149 _Func __binary_operation,
0150 _Proj1 __projection1 = {},
0151 _Proj2 __projection2 = {}) const {
0152 return __binary(
0153 ranges::begin(__range1),
0154 ranges::end(__range1),
0155 ranges::begin(__range2),
0156 ranges::end(__range2),
0157 std::move(__result),
0158 __binary_operation,
0159 __projection1,
0160 __projection2);
0161 }
0162 };
0163
0164 inline namespace __cpo {
0165 inline constexpr auto transform = __transform{};
0166 }
0167 }
0168
0169 _LIBCPP_END_NAMESPACE_STD
0170
0171 #endif
0172
0173 _LIBCPP_POP_MACROS
0174
0175 #endif