Skip to content

[libc][FixedVector] Add more helper methods #94278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion libc/src/__support/fixedvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ template <typename T, size_t CAPACITY> class FixedVector {
public:
constexpr FixedVector() = default;

using iterator = typename cpp::array<T, CAPACITY>::iterator;
constexpr FixedVector(iterator begin, iterator end) {
for (; begin != end; ++begin)
push_back(*begin);
}

constexpr FixedVector(size_t count, const T &value) {
for (size_t i = 0; i < count; ++i)
push_back(value);
}

bool push_back(const T &obj) {
if (item_count == CAPACITY)
return false;
Expand All @@ -43,8 +54,14 @@ template <typename T, size_t CAPACITY> class FixedVector {
return true;
}

T &operator[](size_t idx) { return store[idx]; }

const T &operator[](size_t idx) const { return store[idx]; }

bool empty() const { return item_count == 0; }

size_t size() const { return item_count; }

// Empties the store for all practical purposes.
void reset() { item_count = 0; }

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

using iterator = typename cpp::array<T, CAPACITY>::iterator;
LIBC_INLINE constexpr iterator begin() { return store.begin(); }
LIBC_INLINE constexpr iterator end() { return iterator{&store[item_count]}; }
};
Expand Down
1 change: 1 addition & 0 deletions libc/test/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ add_libc_test(
SRCS
fixedvector_test.cpp
DEPENDS
libc.src.__support.CPP.array
libc.src.__support.fixedvector
)

Expand Down
27 changes: 27 additions & 0 deletions libc/test/src/__support/fixedvector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/array.h"
#include "src/__support/fixedvector.h"
#include "test/UnitTest/Test.h"

Expand Down Expand Up @@ -69,3 +70,29 @@ TEST(LlvmLibcFixedVectorTest, Iteration) {
for (int &x : v)
ASSERT_GE(x, 0);
}

TEST(LlvmLibcFixedVectorTest, ConstructionFromIterators) {
LIBC_NAMESPACE::cpp::array<int, 4> arr{1, 2, 3, 4};
LIBC_NAMESPACE::FixedVector<int, 5> vec(arr.begin(), arr.end());
ASSERT_EQ(vec.size(), arr.size());
for (size_t i = 0; i < arr.size(); ++i)
ASSERT_EQ(vec[i], arr[i]);
}

TEST(LlvmLibcFixedVectorTest, ConstructionFromCountAndValue) {
constexpr int kVal = 10;
LIBC_NAMESPACE::FixedVector<int, 5> vec(4, kVal);
ASSERT_EQ(vec.size(), size_t(4));
for (size_t i = 0; i < vec.size(); ++i)
ASSERT_EQ(vec[i], kVal);
}

TEST(LlvmLibcFixedVectorTest, ForwardIteration) {
LIBC_NAMESPACE::cpp::array<int, 4> arr{1, 2, 3, 4};
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]);
}
}
Loading