[libcxx-commits] [libcxx] [libc++] Add `ranges::fold_left_first` and ranges::`fold_left_first_with_iter` (PR #121558)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jan 3 02:43:50 PST 2025
https://github.com/JCGoran created https://github.com/llvm/llvm-project/pull/121558
Implementation mostly borrowed from #75259.
Probably needs more tests, hence just a draft for now.
>From 6359ec7edda3d565facebc13a6578d9489cac8e1 Mon Sep 17 00:00:00 2001
From: JCGoran <jcgoran at protonmail.com>
Date: Thu, 2 Jan 2025 15:00:01 +0100
Subject: [PATCH 1/4] Initial impl of `fold_left_first_with_iter`
---
libcxx/include/__algorithm/ranges_fold.h | 51 +++++++++++++++++
libcxx/include/algorithm | 12 ++++
.../algorithm.nodiscard.verify.cpp | 4 ++
.../alg.fold/left_folds.pass.cpp | 57 +++++++++++++++++++
4 files changed, 124 insertions(+)
diff --git a/libcxx/include/__algorithm/ranges_fold.h b/libcxx/include/__algorithm/ranges_fold.h
index d2c39213985044..91873e6bd340a0 100644
--- a/libcxx/include/__algorithm/ranges_fold.h
+++ b/libcxx/include/__algorithm/ranges_fold.h
@@ -62,6 +62,9 @@ struct in_value_result {
template <class _Ip, class _Tp>
using fold_left_with_iter_result = in_value_result<_Ip, _Tp>;
+template <class _Ip, class _Tp>
+using fold_left_first_with_iter_result = in_value_result<_Ip, _Tp>;
+
template <class _Fp, class _Tp, class _Ip, class _Rp, class _Up = decay_t<_Rp>>
concept __indirectly_binary_left_foldable_impl =
convertible_to<_Rp, _Up> && //
@@ -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))};
+ }
+};
+
+inline constexpr auto fold_left_first_with_iter = __fold_left_first_with_iter();
+
+struct __fold_left_first {
+ 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) {
+ return fold_left_first_with_iter(std::move(__first), std::move(__last), std::ref(__f)).value;
+ }
+
+ 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) {
+ return fold_left_first_with_iter(ranges::begin(__r), ranges::end(__r), std::ref(__f)).value;
+ }
+};
+
+inline constexpr auto fold_left_first = __fold_left_first();
+
} // namespace ranges
#endif // _LIBCPP_STD_VER >= 23
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index e593ae26ed6e24..16728cbf0bcff2 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -948,6 +948,18 @@ namespace ranges {
template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
constexpr see below fold_left_with_iter(R&& r, T init, F f); // since C++23
+ template<class I, class T>
+ using fold_left_first_with_iter_result = in_value_result<I, optional<T>>; // since C++23
+
+ template<input_iterator I, sentinel_for<I> S,
+ indirectly-binary-left-foldable<T, I> F>
+ requires constructible_from<iter_value_t<I>, iter_reference_t<I>>
+ constexpr see below fold_left_first_with_iter(I first, S last, F f); // since C++23
+
+ template<input_range R, indirectly-binary-left-foldable<T, iterator_t<R>> F>
+ requires constructible_from<range_value_t<R>, range_reference_t<R>>
+ constexpr see below fold_left_first_with_iter(R&& r, F f); // since C++23
+
template<forward_iterator I1, sentinel_for<I1> S1, forward_iterator I2, sentinel_for<I2> S2,
class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
diff --git a/libcxx/test/libcxx/diagnostics/algorithm.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/algorithm.nodiscard.verify.cpp
index 14febc12a8a2d9..f4f14e513308ba 100644
--- a/libcxx/test/libcxx/diagnostics/algorithm.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/algorithm.nodiscard.verify.cpp
@@ -392,5 +392,9 @@ void test() {
// expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
std::ranges::fold_left_with_iter(iter, iter, 0, std::plus());
// expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::fold_left_first_with_iter(range, std::plus());
+ // expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::fold_left_first_with_iter(iter, iter, std::plus());
+ // expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
#endif
}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
index 4987ca9cac4ae0..22c5e1b8620a06 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
@@ -39,6 +39,7 @@
#include <string_view>
#include <string>
#include <vector>
+#include <optional>
#include "test_macros.h"
#include "test_range.h"
@@ -50,7 +51,9 @@
#endif
using std::ranges::fold_left;
+using std::ranges::fold_left_first;
using std::ranges::fold_left_with_iter;
+using std::ranges::fold_left_first_with_iter;
template <class Result, class Range, class T>
concept is_in_value_result =
@@ -105,6 +108,47 @@ constexpr void check_iterator(R& r, T const& init, F f, Expected const& expected
}
}
+template <std::ranges::input_range R, class F, std::equality_comparable Expected>
+ requires std::copyable<R>
+constexpr void check_iterator(R& r, F f, std::optional<Expected> const& expected) {
+ {
+ is_in_value_result<R, std::optional<Expected>> decltype(auto) result = fold_left_first_with_iter(r.begin(), r.end(), f);
+ assert(result.in == r.end());
+ assert(result.value == expected);
+ }
+
+ {
+ auto telemetry = invocable_telemetry();
+ auto f2 = invocable_with_telemetry(f, telemetry);
+ is_in_value_result<R, std::optional<Expected>> decltype(auto) result = fold_left_first_with_iter(r.begin(), r.end(), f2);
+ assert(result.in == r.end());
+ assert(result.value == expected);
+ if (result.value.has_value()) {
+ assert(telemetry.invocations == std::ranges::distance(r) - 1);
+ assert(telemetry.moves == 0);
+ assert(telemetry.copies == 1);
+ }
+ }
+
+ {
+ std::same_as<std::optional<Expected>> decltype(auto) result = fold_left_first(r.begin(), r.end(), f);
+ assert(result == expected);
+ }
+
+ {
+ auto telemetry = invocable_telemetry();
+ auto f2 = invocable_with_telemetry(f, telemetry);
+ std::same_as<std::optional<Expected>> decltype(auto) result = fold_left_first(r.begin(), r.end(), f2);
+ assert(result == expected);
+ if (result.has_value()) {
+ assert(telemetry.invocations == std::ranges::distance(r) - 1);
+ assert(telemetry.moves == 0);
+ assert(telemetry.copies == 1);
+ }
+ }
+}
+
+
template <std::ranges::input_range R, class T, class F, std::equality_comparable Expected>
requires std::copyable<R>
constexpr void check_lvalue_range(R& r, T const& init, F f, Expected const& expected) {
@@ -186,19 +230,29 @@ constexpr void check(R r, T const& init, F f, Expected const& expected) {
check_rvalue_range(r, init, f, expected);
}
+template <std::ranges::input_range R, class F, std::equality_comparable Expected>
+ requires std::copyable<R>
+constexpr void check(R r, F f, std::optional<Expected> const& expected) {
+ check_iterator(r, f, expected);
+}
+
constexpr void empty_range_test_case() {
auto const data = std::vector<int>{};
check(data, 100, std::plus(), 100);
check(data, -100, std::multiplies(), -100);
+ check(data, std::plus(), std::optional<int>());
check(data | std::views::take_while([](auto) { return false; }), 1.23, std::plus(), 1.23);
check(data, Integer(52), &Integer::plus, Integer(52));
+ check(data | std::views::take_while([](auto) { return false; }), std::plus(), std::optional<int>());
}
constexpr void common_range_test_case() {
auto const data = std::vector<int>{1, 2, 3, 4};
check(data, 0, std::plus(), triangular_sum(data));
check(data, 1, std::multiplies(), factorial(data.back()));
+ check(data, std::plus(), std::optional(triangular_sum(data)));
+ check(data, std::multiplies(), std::optional(factorial(data.back())));
auto multiply_with_prev = [n = 1](auto const x, auto const y) mutable {
auto const result = x * y * n;
@@ -206,6 +260,7 @@ constexpr void common_range_test_case() {
return static_cast<std::size_t>(result);
};
check(data, 1, multiply_with_prev, factorial(data.size()) * factorial(data.size() - 1));
+ check(data, multiply_with_prev, std::optional(factorial(data.size()) * factorial(data.size() - 1)));
auto fib = [n = 1](auto x, auto) mutable {
auto old_x = x;
@@ -237,6 +292,7 @@ constexpr void non_common_range_test_case() {
auto data = std::vector<std::string>{"five", "three", "two", "six", "one", "four"};
auto range = data | std::views::transform(parse);
check(range, 0, std::plus(), triangular_sum(range));
+ check(range, std::plus(), std::optional(triangular_sum(range)));
}
{
@@ -248,6 +304,7 @@ constexpr void non_common_range_test_case() {
auto range =
std::views::lazy_split(data, ' ') | std::views::transform(to_string_view) | std::views::transform(parse);
check(range, 0, std::plus(), triangular_sum(range));
+ check(range, std::plus(), std::optional(triangular_sum(range)));
}
}
>From 8eb9f22996e5b12c8567ac7840bb0ada90b5f16a Mon Sep 17 00:00:00 2001
From: JCGoran <jcgoran at protonmail.com>
Date: Thu, 2 Jan 2025 22:29:19 +0100
Subject: [PATCH 2/4] Update
---
libcxx/modules/std/algorithm.inc | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index 3c2139cd64ee49..fbf260241e89a8 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -164,13 +164,12 @@ export namespace std {
using std::ranges::fold_left;
using std::ranges::fold_left_with_iter;
using std::ranges::fold_left_with_iter_result;
-# if 0
using std::ranges::fold_left_first;
+ using std::ranges::fold_left_first_with_iter;
+ using std::ranges::fold_left_first_with_iter_result;
+# if 0
using std::ranges::fold_right;
using std::ranges::fold_right_last;
- using std::ranges::fold_left_with_iter;
- using std::ranges::fold_left_first_with_iter;
- using std::ranges::fold_left_first_with_iter;
# endif
#endif // _LIBCPP_STD_VER >= 23
} // namespace ranges
>From 8067ea1728a6861648c8a206324e7909e8695d77 Mon Sep 17 00:00:00 2001
From: JCGoran <jcgoran at protonmail.com>
Date: Thu, 2 Jan 2025 22:40:16 +0100
Subject: [PATCH 3/4] Update header
---
libcxx/include/algorithm | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 16728cbf0bcff2..5592387e454950 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -938,6 +938,15 @@ namespace ranges {
template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
constexpr auto fold_left(R&& r, T init, F f); // since C++23
+ template<input_iterator I, sentinel_for<I> S,
+ indirectly-binary-left-foldable<T, I> F>
+ requires constructible_from<iter_value_t<I>, iter_reference_t<I>>
+ constexpr see below fold_left_first(I first, S last, F f); // since C++23
+
+ template<input_range R, indirectly-binary-left-foldable<T, iterator_t<R>> F>
+ requires constructible_from<range_value_t<R>, range_reference_t<R>>
+ constexpr see below fold_left_first(R&& r, F f); // since C++23
+
template<class I, class T>
using fold_left_with_iter_result = in_value_result<I, T>; // since C++23
@@ -949,7 +958,7 @@ namespace ranges {
constexpr see below fold_left_with_iter(R&& r, T init, F f); // since C++23
template<class I, class T>
- using fold_left_first_with_iter_result = in_value_result<I, optional<T>>; // since C++23
+ using fold_left_first_with_iter_result = in_value_result<I, T>; // since C++23
template<input_iterator I, sentinel_for<I> S,
indirectly-binary-left-foldable<T, I> F>
>From a5515dba2f6d5fef5455d136d1dbccf20c340a4b Mon Sep 17 00:00:00 2001
From: JCGoran <jcgoran at protonmail.com>
Date: Fri, 3 Jan 2025 11:38:48 +0100
Subject: [PATCH 4/4] Formatting
---
libcxx/modules/std/algorithm.inc | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index fbf260241e89a8..20a3aea4fa8dab 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -162,11 +162,11 @@ export namespace std {
// [alg.fold], fold
using std::ranges::fold_left;
- using std::ranges::fold_left_with_iter;
- using std::ranges::fold_left_with_iter_result;
using std::ranges::fold_left_first;
using std::ranges::fold_left_first_with_iter;
using std::ranges::fold_left_first_with_iter_result;
+ using std::ranges::fold_left_with_iter;
+ using std::ranges::fold_left_with_iter_result;
# if 0
using std::ranges::fold_right;
using std::ranges::fold_right_last;
More information about the libcxx-commits
mailing list