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

via libc-commits libc-commits at lists.llvm.org
Fri May 31 05:54:52 PDT 2024


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

>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 1/2] 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);
 }

>From 6995e94f928f37d91c4339286eb2aac9e41f3780 Mon Sep 17 00:00:00 2001
From: jameshu15869 <jhudson15869 at gmail.com>
Date: Fri, 31 May 2024 08:54:06 -0400
Subject: [PATCH 2/2] add fixedvector iterator test

---
 libc/test/src/__support/CPP/array_test.cpp   |  5 -----
 libc/test/src/__support/fixedvector_test.cpp | 10 ++++++++++
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/libc/test/src/__support/CPP/array_test.cpp b/libc/test/src/__support/CPP/array_test.cpp
index 06991871f6172..f2d7bff636e42 100644
--- a/libc/test/src/__support/CPP/array_test.cpp
+++ b/libc/test/src/__support/CPP/array_test.cpp
@@ -28,11 +28,6 @@ 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);
 }
diff --git a/libc/test/src/__support/fixedvector_test.cpp b/libc/test/src/__support/fixedvector_test.cpp
index 4e92081321de7..e9ffdd0203c27 100644
--- a/libc/test/src/__support/fixedvector_test.cpp
+++ b/libc/test/src/__support/fixedvector_test.cpp
@@ -58,4 +58,14 @@ TEST(LlvmLibcFixedVectorTest, Iteration) {
   ASSERT_TRUE(++it == v.rend());
   for (auto it = v.rbegin(), e = v.rend(); it != e; ++it)
     ASSERT_GT(*it, -1);
+
+  auto forward_it = v.begin();
+  ASSERT_EQ(*forward_it, 0);
+  ASSERT_EQ(*++forward_it, 1);
+  ASSERT_EQ(*++forward_it, 2);
+  ASSERT_TRUE(++forward_it == v.end());
+  for (auto it = v.begin(), e = v.end(); it != e; ++it)
+    ASSERT_GT(*it, -1);
+  for (int &x : v)
+    ASSERT_GE(x, 0);
 }



More information about the libc-commits mailing list