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

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Feb 28 07:43:37 PST 2026


================
@@ -0,0 +1,339 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// REQUIRES: std-at-least-c++23
+
+// template<input_iterator I, sentinel_for<I> S,
+//          indirectly-binary-left-foldable<iter_value_t<I>, I> F>
+//   requires constructible_from<iter_value_t<I>, iter_reference_t<I>>
+//   constexpr see below ranges::fold_left_first_with_iter(I first, S last, F f);
+
+// template<input_range R, indirectly-binary-left-foldable<range_value_t<R>, iterator_t<R>> F>
+//   requires constructible_from<range_value_t<R>, range_reference_t<R>>
+//   constexpr see below ranges::fold_left_first_with_iter(R&& r, F f);
+
+// template<input_iterator I, sentinel_for<I> S,
+//          indirectly-binary-left-foldable<iter_value_t<I>, I> F>
+//   requires constructible_from<iter_value_t<I>, iter_reference_t<I>>
+//   constexpr auto ranges::fold_left_first(I first, S last, F f);
+
+// template<input_range R, indirectly-binary-left-foldable<range_value_t<R>, iterator_t<R>> F>
+//   requires constructible_from<range_value_t<R>, range_reference_t<R>>
+//   constexpr auto ranges::fold_left_first(R&& r, F f);
+
+#include <algorithm>
+#include <cassert>
+#include <concepts>
+#include <deque>
+#include <forward_list>
+#include <functional>
+#include <iterator>
+#include <list>
+#include <optional>
+#include <ranges>
+#include <set>
+#include <string_view>
+#include <string>
+#include <vector>
+
+#include "test_macros.h"
+#include "test_range.h"
+#include "invocable_with_telemetry.h"
+#include "maths.h"
+
+#if !defined(TEST_HAS_NO_LOCALIZATION)
+#  include <sstream>
+#endif
+
+using std::ranges::fold_left_first;
+using std::ranges::fold_left_first_with_iter;
+
+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>>;
+
+template <class Result, class T>
+concept is_dangling_with =
+    std::same_as<Result, std::ranges::fold_left_first_with_iter_result<std::ranges::dangling, T>>;
+
+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 (expected.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 (expected.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_lvalue_range(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, f);
+    assert(result.in == r.end());
+    assert(result.value == 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, f2);
----------------
eiytoq wrote:

```suggestion
    std::same_as<std::optional<Expected>> decltype(auto) result = fold_left_first_with_iter(r, f2);
```

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


More information about the libcxx-commits mailing list