Skip to content

Commit cb8699f

Browse files
committed
[libc][FixedVector] Add more helper methods
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 cf3b37c commit cb8699f

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

libc/src/__support/fixedvector.h

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

27+
template <typename It> constexpr FixedVector(It begin, It end) {
28+
for (; begin != end; ++begin)
29+
push_back(*begin);
30+
}
31+
32+
constexpr FixedVector(size_t count, const T &value) {
33+
for (size_t i = 0; i < count; ++i)
34+
push_back(value);
35+
}
36+
2737
bool push_back(const T &obj) {
2838
if (item_count == CAPACITY)
2939
return false;
@@ -43,8 +53,14 @@ template <typename T, size_t CAPACITY> class FixedVector {
4353
return true;
4454
}
4555

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

62+
size_t size() const { return item_count; }
63+
4864
// Empties the store for all practical purposes.
4965
void reset() { item_count = 0; }
5066

@@ -63,6 +79,10 @@ template <typename T, size_t CAPACITY> class FixedVector {
6379
return reverse_iterator{&store[item_count]};
6480
}
6581
LIBC_INLINE constexpr reverse_iterator rend() { return store.rend(); }
82+
83+
using iterator = typename cpp::array<T, CAPACITY>::iterator;
84+
LIBC_INLINE constexpr iterator begin() { return store.begin(); }
85+
LIBC_INLINE constexpr iterator end() { return iterator{&store[item_count]}; }
6686
};
6787

6888
} // namespace LIBC_NAMESPACE

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: 35 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

@@ -59,3 +60,37 @@ TEST(LlvmLibcFixedVectorTest, Iteration) {
5960
for (auto it = v.rbegin(), e = v.rend(); it != e; ++it)
6061
ASSERT_GT(*it, -1);
6162
}
63+
64+
TEST(LlvmLibcFixedVectorTest, ConstructionFromIterators) {
65+
LIBC_NAMESPACE::cpp::array<int, 4> arr{1, 2, 3, 4};
66+
LIBC_NAMESPACE::FixedVector<int, 5> vec(arr.begin(), arr.end());
67+
ASSERT_EQ(vec.size(), arr.size());
68+
for (size_t i = 0; i < arr.size(); ++i)
69+
ASSERT_EQ(vec[i], arr[i]);
70+
}
71+
72+
TEST(LlvmLibcFixedVectorTest, ConstructionFromCountAndValue) {
73+
constexpr int kVal = 10;
74+
// TODO: If the first argument here were just `4`, then we'd have no way to
75+
// disambiguate between the FixedVector ctor that uses iterators vs the one
76+
// taking a count and `cosnt T &`. Using `4` would result in a compile error.
77+
// Formally, we can ensure the count + reference ctor is used if we gate the
78+
// iterator ctor on checking if the type has the `input_iterator_tag` via
79+
// iterator_traits, but we'd have to plumb that through which can be done
80+
// separately. Note the snafu we hit here only happens because we happen to
81+
// test with containters using integral types.
82+
LIBC_NAMESPACE::FixedVector<int, 5> vec(size_t(4), kVal);
83+
ASSERT_EQ(vec.size(), size_t(4));
84+
for (size_t i = 0; i < vec.size(); ++i)
85+
ASSERT_EQ(vec[i], kVal);
86+
}
87+
88+
TEST(LlvmLibcFixedVectorTest, ForwardIteration) {
89+
LIBC_NAMESPACE::cpp::array<int, 4> arr{1, 2, 3, 4};
90+
LIBC_NAMESPACE::FixedVector<int, 5> vec(arr.begin(), arr.end());
91+
ASSERT_EQ(vec.size(), arr.size());
92+
for (auto it = vec.begin(); it != vec.end(); ++it) {
93+
auto idx = it - vec.begin();
94+
ASSERT_EQ(*it, arr[idx]);
95+
}
96+
}

0 commit comments

Comments
 (0)