[libc-commits] [libc] [libc][FixedVector] Add more helper methods (PR #94278)
via libc-commits
libc-commits at lists.llvm.org
Wed Jun 5 14:34:31 PDT 2024
https://github.com/PiJoules updated https://github.com/llvm/llvm-project/pull/94278
>From 0aedcbbf465b5ccc673a729b6a899cd476f544a3 Mon Sep 17 00:00:00 2001
From: Leonard Chan <leonardchan at google.com>
Date: Mon, 3 Jun 2024 13:25:33 -0700
Subject: [PATCH] [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
---
libc/src/__support/fixedvector.h | 18 ++++++++++++-
libc/test/src/__support/CMakeLists.txt | 1 +
libc/test/src/__support/fixedvector_test.cpp | 27 ++++++++++++++++++++
3 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/libc/src/__support/fixedvector.h b/libc/src/__support/fixedvector.h
index 6aeb4d56363e9..ddd0993a95272 100644
--- a/libc/src/__support/fixedvector.h
+++ b/libc/src/__support/fixedvector.h
@@ -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;
@@ -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; }
@@ -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]}; }
};
diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index 663aa2bb82cae..936cfe4e2a20c 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -132,6 +132,7 @@ add_libc_test(
SRCS
fixedvector_test.cpp
DEPENDS
+ libc.src.__support.CPP.array
libc.src.__support.fixedvector
)
diff --git a/libc/test/src/__support/fixedvector_test.cpp b/libc/test/src/__support/fixedvector_test.cpp
index e9ffdd0203c27..212e1aed20f7c 100644
--- a/libc/test/src/__support/fixedvector_test.cpp
+++ b/libc/test/src/__support/fixedvector_test.cpp
@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
+#include "src/__support/CPP/array.h"
#include "src/__support/fixedvector.h"
#include "test/UnitTest/Test.h"
@@ -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]);
+ }
+}
More information about the libc-commits
mailing list