[libc-commits] [libc] [libc] Implement forward iterators (PR #93916)

via libc-commits libc-commits at lists.llvm.org
Thu May 30 20:22:47 PDT 2024


https://github.com/jameshu15869 created https://github.com/llvm/llvm-project/pull/93916

- Implements forward iterators to use in https://github.com/llvm/llvm-project/pull/92009

>From 0dc44e7582b06e7e267b7a349e2c325c60374608 Mon Sep 17 00:00:00 2001
From: jameshu15869 <jhudson15869 at gmail.com>
Date: Thu, 30 May 2024 23:14:57 -0400
Subject: [PATCH] implement libc forward iterators

---
 libc/src/__support/fixedvector.h           | 4 ++++
 libc/test/src/__support/CPP/array_test.cpp | 5 +++++
 2 files changed, 9 insertions(+)

diff --git a/libc/src/__support/fixedvector.h b/libc/src/__support/fixedvector.h
index 81747ee10067c..6aeb4d56363e9 100644
--- a/libc/src/__support/fixedvector.h
+++ b/libc/src/__support/fixedvector.h
@@ -63,6 +63,10 @@ template <typename T, size_t CAPACITY> class FixedVector {
     return reverse_iterator{&store[item_count]};
   }
   LIBC_INLINE constexpr reverse_iterator rend() { return store.rend(); }
+
+  using iterator = typename cpp::array<T, CAPACITY>::iterator;
+  LIBC_INLINE constexpr iterator begin() { return store.begin(); }
+  LIBC_INLINE constexpr iterator end() { return iterator{&store[item_count]}; }
 };
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/__support/CPP/array_test.cpp b/libc/test/src/__support/CPP/array_test.cpp
index f2d7bff636e42..06991871f6172 100644
--- a/libc/test/src/__support/CPP/array_test.cpp
+++ b/libc/test/src/__support/CPP/array_test.cpp
@@ -28,6 +28,11 @@ TEST(LlvmLibcArrayTest, Basic) {
   ASSERT_EQ(*(++it), 1);
   ASSERT_EQ(*(++it), 0);
 
+  auto forward_it = a.begin();
+  ASSERT_EQ(*forward_it, 0);
+  ASSERT_EQ(*(++forward_it), 1);
+  ASSERT_EQ(*(++forward_it), 2);
+
   for (int &x : a)
     ASSERT_GE(x, 0);
 }



More information about the libc-commits mailing list