[flang-commits] [flang] [llvm] [clang-tools-extra] [lld] [libcxx] [compiler-rt] [clang] [libc++][ranges] P2116R9: Implements `views::enumerate` (PR #73617)

Christopher Di Bella via flang-commits flang-commits at lists.llvm.org
Tue Jan 2 14:17:02 PST 2024


================
@@ -0,0 +1,101 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <ranges>
+
+// class enumerate_view
+
+// class enumerate_view::iterator
+
+// constexpr auto operator*() const;
+
+#include <array>
+#include <cassert>
+#include <concepts>
+#include <cstddef>
+#include <utility>
+#include <tuple>
+
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "../types.h"
+
+template <class Iterator, class ValueType = int, class Sentinel = sentinel_wrapper<Iterator>>
+constexpr void test() {
+  using View              = MinimalView<Iterator, Sentinel>;
+  using EnumerateView     = std::ranges::enumerate_view<View>;
+  using EnumerateIterator = std::ranges::iterator_t<EnumerateView>;
+
+  using Result = std::tuple<typename EnumerateIterator::difference_type,
+                            std::ranges::range_reference_t<MinimalView<Iterator, Sentinel>>>;
+
+  std::array array{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+
+  View view{Iterator(array.begin()), Sentinel(Iterator(array.end()))};
+  EnumerateView ev{std::move(view)};
+
+  {
+    auto it = ev.begin();
+    for (std::size_t index = 0; index < array.size(); ++index) {
+      std::same_as<Result> decltype(auto) result = *it;
+
+      auto [resultIndex, resultValue] = result;
+      assert(std::cmp_equal(index, resultIndex));
+      assert(array[index] == resultValue);
+
+      ++it;
+    }
+
+    assert(it == ev.end());
+  }
+
+  // const
+  {
+    auto constIt = std::as_const(ev).begin();
+    for (std::size_t index = 0; index < array.size(); ++index) {
+      std::same_as<Result> decltype(auto) result = *constIt;
+
+      auto [resultIndex, resultValue] = result;
+      assert(std::cmp_equal(index, resultIndex));
+      assert(array[index] == resultValue);
+
+      ++constIt;
+    }
----------------
cjdb wrote:

Please hand-roll.

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


More information about the flang-commits mailing list