[libc-commits] [libc] [libc] Add front() and erase() to FixedVector (PR #213866)
via libc-commits
libc-commits at lists.llvm.org
Tue Aug 4 00:57:08 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Pavel Labath (labath)
<details>
<summary>Changes</summary>
Add front() accessors and erase() method to FixedVector. I use these to clean up the pop_front implementation in if_nameindex_test.cpp, which previously manually shifted elements and popped the back.
Assisted by Gemini.
---
Full diff: https://github.com/llvm/llvm-project/pull/213866.diff
3 Files Affected:
- (modified) libc/src/__support/fixedvector.h (+18)
- (modified) libc/test/src/__support/fixedvector_test.cpp (+37)
- (modified) libc/test/src/net/linux/if_nameindex_test.cpp (+2-5)
``````````diff
diff --git a/libc/src/__support/fixedvector.h b/libc/src/__support/fixedvector.h
index 34601f86dc017..daa6a4101916d 100644
--- a/libc/src/__support/fixedvector.h
+++ b/libc/src/__support/fixedvector.h
@@ -57,6 +57,16 @@ template <typename T, size_t CAPACITY> class FixedVector {
return true;
}
+ LIBC_INLINE constexpr const T &front() const {
+ LIBC_ASSERT(!empty());
+ return store[0];
+ }
+
+ LIBC_INLINE constexpr T &front() {
+ LIBC_ASSERT(!empty());
+ return store[0];
+ }
+
LIBC_INLINE constexpr const T &back() const {
LIBC_ASSERT(!empty());
return store[item_count - 1];
@@ -75,6 +85,14 @@ template <typename T, size_t CAPACITY> class FixedVector {
return true;
}
+ LIBC_INLINE constexpr iterator erase(iterator pos) {
+ LIBC_ASSERT(pos >= begin() && pos < end());
+ for (iterator it = pos; it + 1 != end(); ++it)
+ *it = *(it + 1);
+ pop_back();
+ return pos;
+ }
+
LIBC_INLINE constexpr T &operator[](size_t idx) {
LIBC_ASSERT(idx < item_count);
return store[idx];
diff --git a/libc/test/src/__support/fixedvector_test.cpp b/libc/test/src/__support/fixedvector_test.cpp
index 8be18a52ec015..3e5972f8869d4 100644
--- a/libc/test/src/__support/fixedvector_test.cpp
+++ b/libc/test/src/__support/fixedvector_test.cpp
@@ -106,3 +106,40 @@ TEST(LlvmLibcFixedVectorTest, ConstForwardIteration) {
ASSERT_EQ(*it, arr[idx]);
}
}
+
+TEST(LlvmLibcFixedVectorTest, Front) {
+ LIBC_NAMESPACE::FixedVector<int, 20> fixed_vector;
+ for (int i = 0; i < 5; i++)
+ ASSERT_TRUE(fixed_vector.push_back(i));
+ ASSERT_EQ(fixed_vector.front(), 0);
+ fixed_vector.front() = 10;
+ ASSERT_EQ(fixed_vector.front(), 10);
+ fixed_vector.front() = 0;
+
+ const auto &const_vector = fixed_vector;
+ ASSERT_EQ(const_vector.front(), 0);
+}
+
+TEST(LlvmLibcFixedVectorTest, Erase) {
+ LIBC_NAMESPACE::FixedVector<int, 20> fixed_vector;
+ for (int i = 0; i < 5; i++)
+ ASSERT_TRUE(fixed_vector.push_back(i));
+
+ auto it = fixed_vector.erase(fixed_vector.begin());
+ ASSERT_EQ(it, fixed_vector.begin());
+ ASSERT_EQ(fixed_vector.size(), size_t(4));
+ ASSERT_EQ(fixed_vector.front(), 1);
+ ASSERT_EQ(fixed_vector.back(), 4);
+
+ it = fixed_vector.erase(fixed_vector.begin() + 1);
+ ASSERT_EQ(it, fixed_vector.begin() + 1);
+ ASSERT_EQ(fixed_vector.size(), size_t(3));
+ ASSERT_EQ(fixed_vector[0], 1);
+ ASSERT_EQ(fixed_vector[1], 3);
+ ASSERT_EQ(fixed_vector[2], 4);
+
+ it = fixed_vector.erase(fixed_vector.end() - 1);
+ ASSERT_EQ(it, fixed_vector.end());
+ ASSERT_EQ(fixed_vector.size(), size_t(2));
+ ASSERT_EQ(fixed_vector.back(), 3);
+}
diff --git a/libc/test/src/net/linux/if_nameindex_test.cpp b/libc/test/src/net/linux/if_nameindex_test.cpp
index 1285f967ad0b1..f210584092ce0 100644
--- a/libc/test/src/net/linux/if_nameindex_test.cpp
+++ b/libc/test/src/net/linux/if_nameindex_test.cpp
@@ -49,11 +49,8 @@ template <typename T, size_t CAPACITY>
static optional<T> pop_front(FixedVector<T, CAPACITY> &vec) {
if (vec.empty())
return nullopt;
- // TODO: Add front() and erase() to FixedVector, then clean this up.
- T first = vec[0];
- for (size_t i = 1; i < vec.size(); ++i)
- vec[i - 1] = vec[i];
- vec.pop_back();
+ T first = vec.front();
+ vec.erase(vec.begin());
return first;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/213866
More information about the libc-commits
mailing list