Skip to content

Commit

Permalink
[libc][fixedvector] Add const_iterator begin/end (#96714)
Browse files Browse the repository at this point in the history
  • Loading branch information
PiJoules authored Jun 25, 2024
1 parent 0d53366 commit 54ca5a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libc/src/__support/fixedvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions libc/test/src/__support/fixedvector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

0 comments on commit 54ca5a8

Please sign in to comment.