Skip to content

[libc][support][FixedVector] add reverse iterator #86732

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 3 commits into from
Mar 28, 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
8 changes: 8 additions & 0 deletions libc/src/__support/fixedvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "src/__support/CPP/array.h"

#include "src/__support/CPP/iterator.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to update the bazel.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 05c923c


namespace LIBC_NAMESPACE {

// A fixed size data store backed by an underlying cpp::array data structure. It
Expand Down Expand Up @@ -55,6 +57,12 @@ template <typename T, size_t CAPACITY> class FixedVector {
// matches the `destroy` API of those other data structures so that users
// can easily swap one data structure for the other.
static void destroy(FixedVector<T, CAPACITY> *store) { store->reset(); }

using reverse_iterator = typename cpp::array<T, CAPACITY>::reverse_iterator;
LIBC_INLINE constexpr reverse_iterator rbegin() {
return reverse_iterator{&store[item_count]};
}
LIBC_INLINE constexpr reverse_iterator rend() { return store.rend(); }
};

} // namespace LIBC_NAMESPACE
Expand Down
16 changes: 16 additions & 0 deletions libc/test/src/__support/fixedvector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ TEST(LlvmLibcFixedVectorTest, Destroy) {
LIBC_NAMESPACE::FixedVector<int, 20>::destroy(&fixed_vector);
ASSERT_TRUE(fixed_vector.empty());
}

TEST(LlvmLibcFixedVectorTest, Iteration) {
LIBC_NAMESPACE::FixedVector<int, 20> v;
for (int i = 0; i < 3; i++)
v.push_back(i);
auto it = v.rbegin();
ASSERT_EQ(*it, 2);
ASSERT_EQ(*++it, 1);
ASSERT_EQ(*++it, 0);
// TODO: need an overload of Test::test for iterators?
// ASSERT_EQ(++it, v.rend());
// ASSERT_EQ(v.rbegin(), v.rbegin());
ASSERT_TRUE(++it == v.rend());
for (auto it = v.rbegin(), e = v.rend(); it != e; ++it)
ASSERT_GT(*it, -1);
}
1 change: 1 addition & 0 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ libc_support_library(
hdrs = ["src/__support/fixedvector.h"],
deps = [
":__support_cpp_array",
":__support_cpp_iterator",
],
)

Expand Down