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