Skip to content

Commit 649edb8

Browse files
authored
[libc][FixedVector] Add more helper methods (#94278)
This adds: - A ctor accepting a start and end iterator - A ctor accepting a count and const T& - size() - subscript operators - begin() and end() iterators
1 parent 88cdd99 commit 649edb8

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

libc/src/__support/fixedvector.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ template <typename T, size_t CAPACITY> class FixedVector {
2424
public:
2525
constexpr FixedVector() = default;
2626

27+
using iterator = typename cpp::array<T, CAPACITY>::iterator;
28+
constexpr FixedVector(iterator begin, iterator end) {
29+
for (; begin != end; ++begin)
30+
push_back(*begin);
31+
}
32+
33+
constexpr FixedVector(size_t count, const T &value) {
34+
for (size_t i = 0; i < count; ++i)
35+
push_back(value);
36+
}
37+
2738
bool push_back(const T &obj) {
2839
if (item_count == CAPACITY)
2940
return false;
@@ -43,8 +54,14 @@ template <typename T, size_t CAPACITY> class FixedVector {
4354
return true;
4455
}
4556

57+
T &operator[](size_t idx) { return store[idx]; }
58+
59+
const T &operator[](size_t idx) const { return store[idx]; }
60+
4661
bool empty() const { return item_count == 0; }
4762

63+
size_t size() const { return item_count; }
64+
4865
// Empties the store for all practical purposes.
4966
void reset() { item_count = 0; }
5067

@@ -64,7 +81,6 @@ template <typename T, size_t CAPACITY> class FixedVector {
6481
}
6582
LIBC_INLINE constexpr reverse_iterator rend() { return store.rend(); }
6683

67-
using iterator = typename cpp::array<T, CAPACITY>::iterator;
6884
LIBC_INLINE constexpr iterator begin() { return store.begin(); }
6985
LIBC_INLINE constexpr iterator end() { return iterator{&store[item_count]}; }
7086
};

libc/test/src/__support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ add_libc_test(
132132
SRCS
133133
fixedvector_test.cpp
134134
DEPENDS
135+
libc.src.__support.CPP.array
135136
libc.src.__support.fixedvector
136137
)
137138

libc/test/src/__support/fixedvector_test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/__support/CPP/array.h"
910
#include "src/__support/fixedvector.h"
1011
#include "test/UnitTest/Test.h"
1112

@@ -69,3 +70,29 @@ TEST(LlvmLibcFixedVectorTest, Iteration) {
6970
for (int &x : v)
7071
ASSERT_GE(x, 0);
7172
}
73+
74+
TEST(LlvmLibcFixedVectorTest, ConstructionFromIterators) {
75+
LIBC_NAMESPACE::cpp::array<int, 4> arr{1, 2, 3, 4};
76+
LIBC_NAMESPACE::FixedVector<int, 5> vec(arr.begin(), arr.end());
77+
ASSERT_EQ(vec.size(), arr.size());
78+
for (size_t i = 0; i < arr.size(); ++i)
79+
ASSERT_EQ(vec[i], arr[i]);
80+
}
81+
82+
TEST(LlvmLibcFixedVectorTest, ConstructionFromCountAndValue) {
83+
constexpr int kVal = 10;
84+
LIBC_NAMESPACE::FixedVector<int, 5> vec(4, kVal);
85+
ASSERT_EQ(vec.size(), size_t(4));
86+
for (size_t i = 0; i < vec.size(); ++i)
87+
ASSERT_EQ(vec[i], kVal);
88+
}
89+
90+
TEST(LlvmLibcFixedVectorTest, ForwardIteration) {
91+
LIBC_NAMESPACE::cpp::array<int, 4> arr{1, 2, 3, 4};
92+
LIBC_NAMESPACE::FixedVector<int, 5> vec(arr.begin(), arr.end());
93+
ASSERT_EQ(vec.size(), arr.size());
94+
for (auto it = vec.begin(); it != vec.end(); ++it) {
95+
auto idx = it - vec.begin();
96+
ASSERT_EQ(*it, arr[idx]);
97+
}
98+
}

0 commit comments

Comments
 (0)