1cb14a3feSDimitry Andric // -*- C++ -*- 2cb14a3feSDimitry Andric //===----------------------------------------------------------------------===// 3cb14a3feSDimitry Andric // 4cb14a3feSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5cb14a3feSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6cb14a3feSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7cb14a3feSDimitry Andric // 8cb14a3feSDimitry Andric //===----------------------------------------------------------------------===// 9cb14a3feSDimitry Andric 10cb14a3feSDimitry Andric #ifndef _LIBCPP___ALGORITHM_FOLD_H 11cb14a3feSDimitry Andric #define _LIBCPP___ALGORITHM_FOLD_H 12cb14a3feSDimitry Andric 13cb14a3feSDimitry Andric #include <__concepts/assignable.h> 14cb14a3feSDimitry Andric #include <__concepts/convertible_to.h> 15cb14a3feSDimitry Andric #include <__concepts/invocable.h> 16cb14a3feSDimitry Andric #include <__concepts/movable.h> 17cb14a3feSDimitry Andric #include <__config> 18cb14a3feSDimitry Andric #include <__functional/invoke.h> 19cb14a3feSDimitry Andric #include <__functional/reference_wrapper.h> 20cb14a3feSDimitry Andric #include <__iterator/concepts.h> 21cb14a3feSDimitry Andric #include <__iterator/iterator_traits.h> 22cb14a3feSDimitry Andric #include <__iterator/next.h> 23cb14a3feSDimitry Andric #include <__ranges/access.h> 24cb14a3feSDimitry Andric #include <__ranges/concepts.h> 25cb14a3feSDimitry Andric #include <__ranges/dangling.h> 26cb14a3feSDimitry Andric #include <__type_traits/decay.h> 27cb14a3feSDimitry Andric #include <__type_traits/invoke.h> 28cb14a3feSDimitry Andric #include <__utility/forward.h> 29cb14a3feSDimitry Andric #include <__utility/move.h> 30cb14a3feSDimitry Andric 31cb14a3feSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 32cb14a3feSDimitry Andric # pragma GCC system_header 33cb14a3feSDimitry Andric #endif 34cb14a3feSDimitry Andric 35b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS 36b3edf446SDimitry Andric #include <__undef_macros> 37b3edf446SDimitry Andric 38cb14a3feSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 39cb14a3feSDimitry Andric 40cb14a3feSDimitry Andric #if _LIBCPP_STD_VER >= 23 41cb14a3feSDimitry Andric 42cb14a3feSDimitry Andric namespace ranges { 43cb14a3feSDimitry Andric template <class _Ip, class _Tp> 44cb14a3feSDimitry Andric struct in_value_result { 45cb14a3feSDimitry Andric _LIBCPP_NO_UNIQUE_ADDRESS _Ip in; 46cb14a3feSDimitry Andric _LIBCPP_NO_UNIQUE_ADDRESS _Tp value; 47cb14a3feSDimitry Andric 48cb14a3feSDimitry Andric template <class _I2, class _T2> 49cb14a3feSDimitry Andric requires convertible_to<const _Ip&, _I2> && convertible_to<const _Tp&, _T2> in_value_resultin_value_result50cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr operator in_value_result<_I2, _T2>() const& { 51cb14a3feSDimitry Andric return {in, value}; 52cb14a3feSDimitry Andric } 53cb14a3feSDimitry Andric 54cb14a3feSDimitry Andric template <class _I2, class _T2> 55cb14a3feSDimitry Andric requires convertible_to<_Ip, _I2> && convertible_to<_Tp, _T2> in_value_resultin_value_result56cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr operator in_value_result<_I2, _T2>() && { 57cb14a3feSDimitry Andric return {std::move(in), std::move(value)}; 58cb14a3feSDimitry Andric } 59cb14a3feSDimitry Andric }; 60cb14a3feSDimitry Andric 61cb14a3feSDimitry Andric template <class _Ip, class _Tp> 62cb14a3feSDimitry Andric using fold_left_with_iter_result = in_value_result<_Ip, _Tp>; 63cb14a3feSDimitry Andric 64cb14a3feSDimitry Andric template <class _Fp, class _Tp, class _Ip, class _Rp, class _Up = decay_t<_Rp>> 65cb14a3feSDimitry Andric concept __indirectly_binary_left_foldable_impl = 66cb14a3feSDimitry Andric convertible_to<_Rp, _Up> && // 67cb14a3feSDimitry Andric movable<_Tp> && // 68cb14a3feSDimitry Andric movable<_Up> && // 69cb14a3feSDimitry Andric convertible_to<_Tp, _Up> && // 70cb14a3feSDimitry Andric invocable<_Fp&, _Up, iter_reference_t<_Ip>> && // 71cb14a3feSDimitry Andric assignable_from<_Up&, invoke_result_t<_Fp&, _Up, iter_reference_t<_Ip>>>; 72cb14a3feSDimitry Andric 73cb14a3feSDimitry Andric template <class _Fp, class _Tp, class _Ip> 74cb14a3feSDimitry Andric concept __indirectly_binary_left_foldable = 75cb14a3feSDimitry Andric copy_constructible<_Fp> && // 76cb14a3feSDimitry Andric invocable<_Fp&, _Tp, iter_reference_t<_Ip>> && // 77cb14a3feSDimitry Andric __indirectly_binary_left_foldable_impl<_Fp, _Tp, _Ip, invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>; 78cb14a3feSDimitry Andric 79cb14a3feSDimitry Andric struct __fold_left_with_iter { 80cb14a3feSDimitry Andric template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp> operator__fold_left_with_iter81*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) { 82cb14a3feSDimitry Andric using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>; 83cb14a3feSDimitry Andric 84cb14a3feSDimitry Andric if (__first == __last) { 85cb14a3feSDimitry Andric return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), _Up(std::move(__init))}; 86cb14a3feSDimitry Andric } 87cb14a3feSDimitry Andric 88cb14a3feSDimitry Andric _Up __result = std::invoke(__f, std::move(__init), *__first); 89cb14a3feSDimitry Andric for (++__first; __first != __last; ++__first) { 90cb14a3feSDimitry Andric __result = std::invoke(__f, std::move(__result), *__first); 91cb14a3feSDimitry Andric } 92cb14a3feSDimitry Andric 93cb14a3feSDimitry Andric return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), std::move(__result)}; 94cb14a3feSDimitry Andric } 95cb14a3feSDimitry Andric 96cb14a3feSDimitry Andric template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp> operator__fold_left_with_iter97*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) { 98cb14a3feSDimitry Andric auto __result = operator()(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f)); 99cb14a3feSDimitry Andric 100cb14a3feSDimitry Andric using _Up = decay_t<invoke_result_t<_Fp&, _Tp, range_reference_t<_Rp>>>; 101cb14a3feSDimitry Andric return fold_left_with_iter_result<borrowed_iterator_t<_Rp>, _Up>{std::move(__result.in), std::move(__result.value)}; 102cb14a3feSDimitry Andric } 103cb14a3feSDimitry Andric }; 104cb14a3feSDimitry Andric 105cb14a3feSDimitry Andric inline constexpr auto fold_left_with_iter = __fold_left_with_iter(); 106cb14a3feSDimitry Andric 107cb14a3feSDimitry Andric struct __fold_left { 108cb14a3feSDimitry Andric template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp> operator__fold_left109*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) { 110cb14a3feSDimitry Andric return fold_left_with_iter(std::move(__first), std::move(__last), std::move(__init), std::ref(__f)).value; 111cb14a3feSDimitry Andric } 112cb14a3feSDimitry Andric 113cb14a3feSDimitry Andric template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp> operator__fold_left114*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) { 115cb14a3feSDimitry Andric return fold_left_with_iter(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f)).value; 116cb14a3feSDimitry Andric } 117cb14a3feSDimitry Andric }; 118cb14a3feSDimitry Andric 119cb14a3feSDimitry Andric inline constexpr auto fold_left = __fold_left(); 120cb14a3feSDimitry Andric } // namespace ranges 121cb14a3feSDimitry Andric 122cb14a3feSDimitry Andric #endif // _LIBCPP_STD_VER >= 23 123cb14a3feSDimitry Andric 124cb14a3feSDimitry Andric _LIBCPP_END_NAMESPACE_STD 125cb14a3feSDimitry Andric 126b3edf446SDimitry Andric _LIBCPP_POP_MACROS 127b3edf446SDimitry Andric 128cb14a3feSDimitry Andric #endif // _LIBCPP___ALGORITHM_FOLD_H 129