Skip to content

[libc] Implement forward iterators for libc fixed_vector #93916

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 2 commits into from
Jun 5, 2024

Conversation

jameshu15869
Copy link
Contributor

@jameshu15869 jameshu15869 commented May 31, 2024

@jameshu15869 jameshu15869 marked this pull request as draft May 31, 2024 03:22
@llvmbot llvmbot added the libc label May 31, 2024
@llvmbot
Copy link
Member

llvmbot commented May 31, 2024

@llvm/pr-subscribers-libc

Author: None (jameshu15869)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/93916.diff

2 Files Affected:

  • (modified) libc/src/__support/fixedvector.h (+4)
  • (modified) libc/test/src/__support/CPP/array_test.cpp (+5)
diff --git a/libc/src/__support/fixedvector.h b/libc/src/__support/fixedvector.h
index 81747ee10067c..6aeb4d56363e9 100644
--- a/libc/src/__support/fixedvector.h
+++ b/libc/src/__support/fixedvector.h
@@ -63,6 +63,10 @@ template <typename T, size_t CAPACITY> class FixedVector {
     return reverse_iterator{&store[item_count]};
   }
   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]}; }
 };
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/__support/CPP/array_test.cpp b/libc/test/src/__support/CPP/array_test.cpp
index f2d7bff636e42..06991871f6172 100644
--- a/libc/test/src/__support/CPP/array_test.cpp
+++ b/libc/test/src/__support/CPP/array_test.cpp
@@ -28,6 +28,11 @@ TEST(LlvmLibcArrayTest, Basic) {
   ASSERT_EQ(*(++it), 1);
   ASSERT_EQ(*(++it), 0);
 
+  auto forward_it = a.begin();
+  ASSERT_EQ(*forward_it, 0);
+  ASSERT_EQ(*(++forward_it), 1);
+  ASSERT_EQ(*(++forward_it), 2);
+
   for (int &x : a)
     ASSERT_GE(x, 0);
 }


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]}; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Does returning store.end() not work?

Suggested change
LIBC_INLINE constexpr iterator end() { return iterator{&store[item_count]}; }
LIBC_INLINE constexpr iterator end() { return store.end(); }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think store.end() ends up using the entire space allocated to the array instead of the actual number of elements inside it. I was getting a memory error with store.end()but I'm not 100% sure that is the cause

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, that makes sense. I'm wondering if something like
return store.begin() + item_count would work better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would that account for the size of the objects themselves when doing the pointer arithmetic? i.e. if an int is 4 bytes and you have 4 elements would you want end() to be at store.begin() + 4*4?

Copy link
Contributor

Choose a reason for hiding this comment

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

The iterator type is T * from the template, so it should know how to do pointer arithmetic on it, but it should be caught in your tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha, I was waiting on rebuilding LLVM this morning but I can try it when I get home later today

Copy link
Contributor

Choose a reason for hiding this comment

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

I actually prefer &store[item_count] and let the compiler do the pointer arithmetic for you.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just using the iterator directly makes sure it's the correct iterator type though.

@jhuber6 jhuber6 marked this pull request as ready for review May 31, 2024 13:08
@lntue lntue requested a review from nickdesaulniers May 31, 2024 15:32
Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

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

LGTM

@nickdesaulniers nickdesaulniers requested a review from gchatelet May 31, 2024 16:42
@nickdesaulniers
Copy link
Member

Consider updating the commit message and PR description to note that this is only for fixedvector.

@jameshu15869 jameshu15869 changed the title [libc] Implement forward iterators [libc] Implement forward iterators for fixed vectors May 31, 2024
@jameshu15869 jameshu15869 changed the title [libc] Implement forward iterators for fixed vectors [libc] Implement forward iterators for libc support fixed vectors May 31, 2024
@jameshu15869
Copy link
Contributor Author

Changed!

@jameshu15869 jameshu15869 changed the title [libc] Implement forward iterators for libc support fixed vectors [libc] Implement forward iterators for libc fixed_vector May 31, 2024
@jhuber6 jhuber6 merged commit ce8bb9b into llvm:main Jun 5, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants