Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:17

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