Skip to content

Commit 1669921

Browse files
PiJoulesAlexisPerry
authored andcommitted
[libc][fixedvector] Add const_iterator begin/end (llvm#96714)
1 parent c464444 commit 1669921

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

libc/src/__support/fixedvector.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ template <typename T, size_t CAPACITY> class FixedVector {
9090

9191
LIBC_INLINE constexpr iterator begin() { return store.begin(); }
9292
LIBC_INLINE constexpr iterator end() { return iterator{&store[item_count]}; }
93+
94+
LIBC_INLINE constexpr const_iterator begin() const { return store.begin(); }
95+
LIBC_INLINE constexpr const_iterator end() const {
96+
return const_iterator{&store[item_count]};
97+
}
9398
};
9499

95100
} // namespace LIBC_NAMESPACE

libc/test/src/__support/fixedvector_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,13 @@ TEST(LlvmLibcFixedVectorTest, ForwardIteration) {
9696
ASSERT_EQ(*it, arr[idx]);
9797
}
9898
}
99+
100+
TEST(LlvmLibcFixedVectorTest, ConstForwardIteration) {
101+
const LIBC_NAMESPACE::cpp::array<int, 4> arr{1, 2, 3, 4};
102+
const LIBC_NAMESPACE::FixedVector<int, 5> vec(arr.begin(), arr.end());
103+
ASSERT_EQ(vec.size(), arr.size());
104+
for (auto it = vec.begin(); it != vec.end(); ++it) {
105+
auto idx = it - vec.begin();
106+
ASSERT_EQ(*it, arr[idx]);
107+
}
108+
}

0 commit comments

Comments
 (0)