[libc-commits] [libc] 54ca5a8 - [libc][fixedvector] Add const_iterator begin/end (#96714)

via libc-commits libc-commits at lists.llvm.org
Tue Jun 25 16:33:40 PDT 2024


Author: PiJoules
Date: 2024-06-25T16:33:36-07:00
New Revision: 54ca5a800d12bf76dabc957163df02c3ea005627

URL: https://github.com/llvm/llvm-project/commit/54ca5a800d12bf76dabc957163df02c3ea005627
DIFF: https://github.com/llvm/llvm-project/commit/54ca5a800d12bf76dabc957163df02c3ea005627.diff

LOG: [libc][fixedvector] Add const_iterator begin/end (#96714)

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 403b1620d20df..5161c0d7a533c 100644
--- a/libc/src/__support/fixedvector.h
+++ b/libc/src/__support/fixedvector.h
@@ -90,6 +90,11 @@ template <typename T, size_t CAPACITY> class FixedVector {
 
   LIBC_INLINE constexpr iterator begin() { return store.begin(); }
   LIBC_INLINE constexpr iterator end() { return iterator{&store[item_count]}; }
+
+  LIBC_INLINE constexpr const_iterator begin() const { return store.begin(); }
+  LIBC_INLINE constexpr const_iterator end() const {
+    return const_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 212e1aed20f7c..b73df04d03746 100644
--- a/libc/test/src/__support/fixedvector_test.cpp
+++ b/libc/test/src/__support/fixedvector_test.cpp
@@ -96,3 +96,13 @@ TEST(LlvmLibcFixedVectorTest, ForwardIteration) {
     ASSERT_EQ(*it, arr[idx]);
   }
 }
+
+TEST(LlvmLibcFixedVectorTest, ConstForwardIteration) {
+  const LIBC_NAMESPACE::cpp::array<int, 4> arr{1, 2, 3, 4};
+  const LIBC_NAMESPACE::FixedVector<int, 5> vec(arr.begin(), arr.end());
+  ASSERT_EQ(vec.size(), arr.size());
+  for (auto it = vec.begin(); it != vec.end(); ++it) {
+    auto idx = it - vec.begin();
+    ASSERT_EQ(*it, arr[idx]);
+  }
+}


        


More information about the libc-commits mailing list