[libcxx-commits] [libcxx] [libc++] Add `ranges::fold_left_first` and `ranges::fold_left_first_with_iter` (PR #121558)

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 21 10:38:40 PDT 2025


================
@@ -0,0 +1,162 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// 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
+
+// REQUIRES: std-at-least-c++23
+
+// MSVC warning C4244: 'argument': conversion from 'double' to 'const int', possible loss of data
+// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4244
+
+#include <algorithm>
+#include <cassert>
+#include <concepts>
+#include <functional>
+#include <iterator>
+#include <ranges>
+#include <string_view>
+#include <string>
+#include <vector>
+#include <optional>
+
+#include "test_macros.h"
+#include "test_range.h"
+#include "invocable_with_telemetry.h"
+#include "maths.h"
+
+template <class Result, class Range, class T>
+concept is_in_value_result =
+    std::same_as<Result, std::ranges::fold_left_first_with_iter_result<std::ranges::iterator_t<Range>, T>>;
+
+struct Integer {
+  int value;
+
+  constexpr Integer(int const x) : value(x) {}
+
+  constexpr Integer plus(int const x) const { return Integer{value + x}; }
+
+  friend constexpr bool operator==(Integer const& x, Integer const& y) = default;
+};
+
+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) {
+  {
+    std::same_as<std::optional<Expected>> decltype(auto) result = std::ranges::fold_left_first(r.begin(), r.end(), f);
+    assert(result == expected);
+  }
+
+  {
+    std::same_as<std::optional<Expected>> decltype(auto) result = std::ranges::fold_left_first(r, f);
+    assert(result == expected);
+  }
+
+  {
+    auto telemetry                                              = invocable_telemetry();
+    auto f2                                                     = invocable_with_telemetry(f, telemetry);
+    std::same_as<std::optional<Expected>> decltype(auto) result = std::ranges::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);
+    }
+  }
+
+  {
+    auto telemetry                                              = invocable_telemetry();
+    auto f2                                                     = invocable_with_telemetry(f, telemetry);
+    std::same_as<std::optional<Expected>> decltype(auto) result = std::ranges::fold_left_first(r, 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 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, std::plus(), std::optional<int>());
+  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, std::plus(), std::optional(triangular_sum(data)));
+  check(data, std::multiplies(), std::optional(factorial(data.back())));
----------------
mordante wrote:

Sorry I missed this in the previous review. But I feel these tests are way too clever.
```suggestion
  check(data, std::multiplies(), std::optional(24));
```
makes the test a lot easier to inspect. It also allows to test the `multiplies` in the `transform` test below

```
    auto data  = std::vector<std::string>{"five", "three", "two", "six", "one", "four"};
    auto range = data | std::views::transform(parse);
    check(range, std::plus(), std::optional(triangular_sum(range)));
    check(data, std::multiplies(), std::optional(720)); // <<< This is now tested.
```



https://github.com/llvm/llvm-project/pull/121558


More information about the libcxx-commits mailing list