[libcxx-commits] [libcxx] [libc++] Add `ranges::fold_left_first` and ranges::`fold_left_first_with_iter` (PR #121558)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jan 3 05:29:54 PST 2025
================
@@ -118,6 +121,54 @@ struct __fold_left {
};
inline constexpr auto fold_left = __fold_left();
+
+struct __fold_left_first_with_iter {
+ template <input_iterator _Ip, sentinel_for<_Ip> _Sp, __indirectly_binary_left_foldable<iter_value_t<_Ip>, _Ip> _Fp>
+ requires constructible_from<iter_value_t<_Ip>, iter_reference_t<_Ip>>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Ip __first, _Sp __last, _Fp __f) {
+ using _Up = decltype(fold_left(std::move(__first), __last, iter_value_t<_Ip>(*__first), __f));
+
+ // case of empty range
+ if (__first == __last) {
+ return fold_left_first_with_iter_result<_Ip, optional<_Up>>{std::move(__first), optional<_Up>()};
+ }
+
+ optional<_Up> __result(std::in_place, *__first);
+ for (++__first; __first != __last; ++__first) {
+ *__result = std::invoke(__f, std::move(*__result), *__first);
+ }
+
+ return fold_left_first_with_iter_result<_Ip, optional<_Up>>{std::move(__first), optional<_Up>(std::move(__result))};
+ }
+
+ template <input_range _Rp, __indirectly_binary_left_foldable<range_value_t<_Rp>, iterator_t<_Rp>> _Fp>
+ requires constructible_from<range_value_t<_Rp>, range_reference_t<_Rp>>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Fp __f) {
+ auto __result = operator()(ranges::begin(__r), ranges::end(__r), std::ref(__f));
+
+ using _Up = decltype(fold_left(ranges::begin(__r), ranges::end(__r), range_value_t<_Rp>(*ranges::begin(__r)), __f));
+ return fold_left_first_with_iter_result<borrowed_iterator_t<_Rp>, optional<_Up>>{std::move(__result.in), optional(std::move(__result.value))};
----------------
frederick-vs-ja wrote:
Don't cast the `optional` value to it's own type.
```suggestion
return fold_left_first_with_iter_result<borrowed_iterator_t<_Rp>, optional<_Up>>{std::move(__result.in), std::move(__result.value)};
```
https://github.com/llvm/llvm-project/pull/121558
More information about the libcxx-commits
mailing list