fold.h 4.8 KB

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