Skip to content

Commit ce8bb9b

Browse files
authored
[libc] Implement forward iterators for libc fixed_vector (#93916)
- Implements forward iterators for `cpp::fixed_vector` to use in #92009
1 parent 6c97303 commit ce8bb9b

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

libc/src/__support/fixedvector.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ template <typename T, size_t CAPACITY> class FixedVector {
6363
return reverse_iterator{&store[item_count]};
6464
}
6565
LIBC_INLINE constexpr reverse_iterator rend() { return store.rend(); }
66+
67+
using iterator = typename cpp::array<T, CAPACITY>::iterator;
68+
LIBC_INLINE constexpr iterator begin() { return store.begin(); }
69+
LIBC_INLINE constexpr iterator end() { return iterator{&store[item_count]}; }
6670
};
6771

6872
} // namespace LIBC_NAMESPACE

libc/test/src/__support/fixedvector_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,14 @@ TEST(LlvmLibcFixedVectorTest, Iteration) {
5858
ASSERT_TRUE(++it == v.rend());
5959
for (auto it = v.rbegin(), e = v.rend(); it != e; ++it)
6060
ASSERT_GT(*it, -1);
61+
62+
auto forward_it = v.begin();
63+
ASSERT_EQ(*forward_it, 0);
64+
ASSERT_EQ(*++forward_it, 1);
65+
ASSERT_EQ(*++forward_it, 2);
66+
ASSERT_TRUE(++forward_it == v.end());
67+
for (auto it = v.begin(), e = v.end(); it != e; ++it)
68+
ASSERT_GT(*it, -1);
69+
for (int &x : v)
70+
ASSERT_GE(x, 0);
6171
}

0 commit comments

Comments
 (0)