[libc-commits] [libc] ce8bb9b - [libc] Implement forward iterators for libc fixed_vector (#93916)
    via libc-commits 
    libc-commits at lists.llvm.org
       
    Wed Jun  5 07:04:29 PDT 2024
    
    
  
Author: jameshu15869
Date: 2024-06-05T09:04:24-05:00
New Revision: ce8bb9b5acea9f8fb5392ae717e7d7b7683230ea
URL: https://github.com/llvm/llvm-project/commit/ce8bb9b5acea9f8fb5392ae717e7d7b7683230ea
DIFF: https://github.com/llvm/llvm-project/commit/ce8bb9b5acea9f8fb5392ae717e7d7b7683230ea.diff
LOG: [libc] Implement forward iterators for libc fixed_vector (#93916)
- Implements forward iterators for `cpp::fixed_vector` to use in
https://github.com/llvm/llvm-project/pull/92009
Added: 
    
Modified: 
    libc/src/__support/fixedvector.h
    libc/test/src/__support/fixedvector_test.cpp
Removed: 
    
################################################################################
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/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