Path: blob/main/contrib/llvm-project/libcxx/include/__algorithm/fold.h
35232 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___ALGORITHM_FOLD_H10#define _LIBCPP___ALGORITHM_FOLD_H1112#include <__concepts/assignable.h>13#include <__concepts/convertible_to.h>14#include <__concepts/invocable.h>15#include <__concepts/movable.h>16#include <__config>17#include <__functional/invoke.h>18#include <__functional/reference_wrapper.h>19#include <__iterator/concepts.h>20#include <__iterator/iterator_traits.h>21#include <__iterator/next.h>22#include <__ranges/access.h>23#include <__ranges/concepts.h>24#include <__ranges/dangling.h>25#include <__type_traits/decay.h>26#include <__type_traits/invoke.h>27#include <__utility/forward.h>28#include <__utility/move.h>2930#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)31# pragma GCC system_header32#endif3334_LIBCPP_PUSH_MACROS35#include <__undef_macros>3637_LIBCPP_BEGIN_NAMESPACE_STD3839#if _LIBCPP_STD_VER >= 234041namespace ranges {42template <class _Ip, class _Tp>43struct in_value_result {44_LIBCPP_NO_UNIQUE_ADDRESS _Ip in;45_LIBCPP_NO_UNIQUE_ADDRESS _Tp value;4647template <class _I2, class _T2>48requires convertible_to<const _Ip&, _I2> && convertible_to<const _Tp&, _T2>49_LIBCPP_HIDE_FROM_ABI constexpr operator in_value_result<_I2, _T2>() const& {50return {in, value};51}5253template <class _I2, class _T2>54requires convertible_to<_Ip, _I2> && convertible_to<_Tp, _T2>55_LIBCPP_HIDE_FROM_ABI constexpr operator in_value_result<_I2, _T2>() && {56return {std::move(in), std::move(value)};57}58};5960template <class _Ip, class _Tp>61using fold_left_with_iter_result = in_value_result<_Ip, _Tp>;6263template <class _Fp, class _Tp, class _Ip, class _Rp, class _Up = decay_t<_Rp>>64concept __indirectly_binary_left_foldable_impl =65convertible_to<_Rp, _Up> && //66movable<_Tp> && //67movable<_Up> && //68convertible_to<_Tp, _Up> && //69invocable<_Fp&, _Up, iter_reference_t<_Ip>> && //70assignable_from<_Up&, invoke_result_t<_Fp&, _Up, iter_reference_t<_Ip>>>;7172template <class _Fp, class _Tp, class _Ip>73concept __indirectly_binary_left_foldable =74copy_constructible<_Fp> && //75invocable<_Fp&, _Tp, iter_reference_t<_Ip>> && //76__indirectly_binary_left_foldable_impl<_Fp, _Tp, _Ip, invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>;7778struct __fold_left_with_iter {79template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>80[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) {81using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>;8283if (__first == __last) {84return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), _Up(std::move(__init))};85}8687_Up __result = std::invoke(__f, std::move(__init), *__first);88for (++__first; __first != __last; ++__first) {89__result = std::invoke(__f, std::move(__result), *__first);90}9192return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), std::move(__result)};93}9495template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>96[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) {97auto __result = operator()(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f));9899using _Up = decay_t<invoke_result_t<_Fp&, _Tp, range_reference_t<_Rp>>>;100return fold_left_with_iter_result<borrowed_iterator_t<_Rp>, _Up>{std::move(__result.in), std::move(__result.value)};101}102};103104inline constexpr auto fold_left_with_iter = __fold_left_with_iter();105106struct __fold_left {107template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>108[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) {109return fold_left_with_iter(std::move(__first), std::move(__last), std::move(__init), std::ref(__f)).value;110}111112template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>113[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) {114return fold_left_with_iter(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f)).value;115}116};117118inline constexpr auto fold_left = __fold_left();119} // namespace ranges120121#endif // _LIBCPP_STD_VER >= 23122123_LIBCPP_END_NAMESPACE_STD124125_LIBCPP_POP_MACROS126127#endif // _LIBCPP___ALGORITHM_FOLD_H128129130