File indexing completed on 2026-05-03 08:13:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___ALGORITHM_RANGES_FOLD_H
0011 #define _LIBCPP___ALGORITHM_RANGES_FOLD_H
0012
0013 #include <__concepts/assignable.h>
0014 #include <__concepts/constructible.h>
0015 #include <__concepts/convertible_to.h>
0016 #include <__concepts/invocable.h>
0017 #include <__concepts/movable.h>
0018 #include <__config>
0019 #include <__functional/invoke.h>
0020 #include <__functional/reference_wrapper.h>
0021 #include <__iterator/concepts.h>
0022 #include <__iterator/iterator_traits.h>
0023 #include <__iterator/next.h>
0024 #include <__ranges/access.h>
0025 #include <__ranges/concepts.h>
0026 #include <__ranges/dangling.h>
0027 #include <__type_traits/decay.h>
0028 #include <__type_traits/invoke.h>
0029 #include <__utility/forward.h>
0030 #include <__utility/move.h>
0031
0032 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0033 # pragma GCC system_header
0034 #endif
0035
0036 _LIBCPP_PUSH_MACROS
0037 #include <__undef_macros>
0038
0039 _LIBCPP_BEGIN_NAMESPACE_STD
0040
0041 #if _LIBCPP_STD_VER >= 23
0042
0043 namespace ranges {
0044 template <class _Ip, class _Tp>
0045 struct in_value_result {
0046 _LIBCPP_NO_UNIQUE_ADDRESS _Ip in;
0047 _LIBCPP_NO_UNIQUE_ADDRESS _Tp value;
0048
0049 template <class _I2, class _T2>
0050 requires convertible_to<const _Ip&, _I2> && convertible_to<const _Tp&, _T2>
0051 _LIBCPP_HIDE_FROM_ABI constexpr operator in_value_result<_I2, _T2>() const& {
0052 return {in, value};
0053 }
0054
0055 template <class _I2, class _T2>
0056 requires convertible_to<_Ip, _I2> && convertible_to<_Tp, _T2>
0057 _LIBCPP_HIDE_FROM_ABI constexpr operator in_value_result<_I2, _T2>() && {
0058 return {std::move(in), std::move(value)};
0059 }
0060 };
0061
0062 template <class _Ip, class _Tp>
0063 using fold_left_with_iter_result = in_value_result<_Ip, _Tp>;
0064
0065 template <class _Fp, class _Tp, class _Ip, class _Rp, class _Up = decay_t<_Rp>>
0066 concept __indirectly_binary_left_foldable_impl =
0067 convertible_to<_Rp, _Up> &&
0068 movable<_Tp> &&
0069 movable<_Up> &&
0070 convertible_to<_Tp, _Up> &&
0071 invocable<_Fp&, _Up, iter_reference_t<_Ip>> &&
0072 assignable_from<_Up&, invoke_result_t<_Fp&, _Up, iter_reference_t<_Ip>>>;
0073
0074 template <class _Fp, class _Tp, class _Ip>
0075 concept __indirectly_binary_left_foldable =
0076 copy_constructible<_Fp> &&
0077 invocable<_Fp&, _Tp, iter_reference_t<_Ip>> &&
0078 __indirectly_binary_left_foldable_impl<_Fp, _Tp, _Ip, invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>;
0079
0080 struct __fold_left_with_iter {
0081 template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>
0082 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) {
0083 using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>;
0084
0085 if (__first == __last) {
0086 return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), _Up(std::move(__init))};
0087 }
0088
0089 _Up __result = std::invoke(__f, std::move(__init), *__first);
0090 for (++__first; __first != __last; ++__first) {
0091 __result = std::invoke(__f, std::move(__result), *__first);
0092 }
0093
0094 return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), std::move(__result)};
0095 }
0096
0097 template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>
0098 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) {
0099 auto __result = operator()(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f));
0100
0101 using _Up = decay_t<invoke_result_t<_Fp&, _Tp, range_reference_t<_Rp>>>;
0102 return fold_left_with_iter_result<borrowed_iterator_t<_Rp>, _Up>{std::move(__result.in), std::move(__result.value)};
0103 }
0104 };
0105
0106 inline constexpr auto fold_left_with_iter = __fold_left_with_iter();
0107
0108 struct __fold_left {
0109 template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>
0110 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) {
0111 return fold_left_with_iter(std::move(__first), std::move(__last), std::move(__init), std::ref(__f)).value;
0112 }
0113
0114 template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>
0115 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) {
0116 return fold_left_with_iter(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f)).value;
0117 }
0118 };
0119
0120 inline constexpr auto fold_left = __fold_left();
0121 }
0122
0123 #endif
0124
0125 _LIBCPP_END_NAMESPACE_STD
0126
0127 _LIBCPP_POP_MACROS
0128
0129 #endif