-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[ADT] Adding bidirectional iterator functionality + unit tests #160726
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
[ADT] Adding bidirectional iterator functionality + unit tests #160726
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-adt Author: Francisco Geiman Thiesen (FranciscoThiesen) ChangesThis feature is needed for #160415 , @kuhar suggested that I split that PR into 2 so that the ADT work is checking in first. Full diff: https://github.com/llvm/llvm-project/pull/160726.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/BitVector.h b/llvm/include/llvm/ADT/BitVector.h
index 72da2343fae13..ef619aef208f3 100644
--- a/llvm/include/llvm/ADT/BitVector.h
+++ b/llvm/include/llvm/ADT/BitVector.h
@@ -40,12 +40,20 @@ template <typename BitVectorT> class const_set_bits_iterator_impl {
Current = Parent.find_next(Current);
}
+ void retreat() {
+ if (Current == -1) {
+ Current = Parent.find_last();
+ } else {
+ Current = Parent.find_prev(Current);
+ }
+ }
+
public:
- using iterator_category = std::forward_iterator_tag;
+ using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
- using value_type = int;
- using pointer = value_type*;
- using reference = value_type&;
+ using value_type = unsigned;
+ using pointer = const value_type*;
+ using reference = value_type;
const_set_bits_iterator_impl(const BitVectorT &Parent, int Current)
: Parent(Parent), Current(Current) {}
@@ -64,6 +72,17 @@ template <typename BitVectorT> class const_set_bits_iterator_impl {
return *this;
}
+ const_set_bits_iterator_impl operator--(int) {
+ auto Prev = *this;
+ retreat();
+ return Prev;
+ }
+
+ const_set_bits_iterator_impl &operator--() {
+ retreat();
+ return *this;
+ }
+
unsigned operator*() const { return Current; }
bool operator==(const const_set_bits_iterator_impl &Other) const {
diff --git a/llvm/unittests/ADT/BitVectorTest.cpp b/llvm/unittests/ADT/BitVectorTest.cpp
index 6a4780c143e54..216e9a0c579f4 100644
--- a/llvm/unittests/ADT/BitVectorTest.cpp
+++ b/llvm/unittests/ADT/BitVectorTest.cpp
@@ -9,6 +9,7 @@
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallBitVector.h"
+#include "llvm/ADT/STLExtras.h"
#include "gtest/gtest.h"
using namespace llvm;
@@ -1177,6 +1178,98 @@ TYPED_TEST(BitVectorTest, Iterators) {
EXPECT_EQ(List[i++], Bit);
}
+TYPED_TEST(BitVectorTest, BidirectionalIterator) {
+ // Test decrement operators
+ TypeParam Vec(100, false);
+ Vec.set(10);
+ Vec.set(20);
+ Vec.set(30);
+ Vec.set(40);
+
+ // Test that we can decrement from end()
+ auto EndIt = Vec.set_bits_end();
+ auto LastIt = EndIt;
+ --LastIt;
+ EXPECT_EQ(*LastIt, 40U);
+
+ // Test post-decrement
+ auto It = Vec.set_bits_end();
+ auto PrevIt = It--;
+ EXPECT_EQ(PrevIt, Vec.set_bits_end());
+ EXPECT_EQ(*It, 40U);
+
+ // Test pre-decrement
+ --It;
+ EXPECT_EQ(*It, 30U);
+
+ // Test full backward iteration
+ std::vector<unsigned> BackwardBits;
+ for (auto RIt = Vec.set_bits_end(); RIt != Vec.set_bits_begin(); ) {
+ --RIt;
+ BackwardBits.push_back(*RIt);
+ }
+ EXPECT_EQ(BackwardBits.size(), 4U);
+ EXPECT_EQ(BackwardBits[0], 40U);
+ EXPECT_EQ(BackwardBits[1], 30U);
+ EXPECT_EQ(BackwardBits[2], 20U);
+ EXPECT_EQ(BackwardBits[3], 10U);
+}
+
+TYPED_TEST(BitVectorTest, ReverseIteration) {
+ // Test using llvm::reverse
+ TypeParam Vec(100, false);
+ Vec.set(5);
+ Vec.set(15);
+ Vec.set(25);
+ Vec.set(35);
+ Vec.set(45);
+
+ std::vector<unsigned> ReversedBits;
+ for (unsigned Bit : llvm::reverse(Vec.set_bits())) {
+ ReversedBits.push_back(Bit);
+ }
+
+ EXPECT_EQ(ReversedBits.size(), 5U);
+ EXPECT_EQ(ReversedBits[0], 45U);
+ EXPECT_EQ(ReversedBits[1], 35U);
+ EXPECT_EQ(ReversedBits[2], 25U);
+ EXPECT_EQ(ReversedBits[3], 15U);
+ EXPECT_EQ(ReversedBits[4], 5U);
+}
+
+TYPED_TEST(BitVectorTest, BidirectionalIteratorEdgeCases) {
+ // Test empty BitVector
+ TypeParam Empty;
+ EXPECT_EQ(Empty.set_bits_begin(), Empty.set_bits_end());
+
+ // Decrementing end() on empty should give -1 (no bits set)
+ auto EmptyEndIt = Empty.set_bits_end();
+ --EmptyEndIt;
+ // After decrement on empty, iterator should still be at "no bit" position
+ EXPECT_EQ(*EmptyEndIt, static_cast<unsigned>(-1));
+
+ // Test single bit
+ TypeParam Single(10, false);
+ Single.set(5);
+
+ auto SingleIt = Single.set_bits_end();
+ --SingleIt;
+ EXPECT_EQ(*SingleIt, 5U);
+ // After decrementing past the first element, the iterator is in an
+ // undefined state (before begin), so we don't test this case
+
+ // Test all bits set
+ TypeParam AllSet(10, true);
+ std::vector<unsigned> AllBitsReverse;
+ for (unsigned Bit : llvm::reverse(AllSet.set_bits())) {
+ AllBitsReverse.push_back(Bit);
+ }
+ EXPECT_EQ(AllBitsReverse.size(), 10U);
+ for (unsigned i = 0; i < 10; ++i) {
+ EXPECT_EQ(AllBitsReverse[i], 9 - i);
+ }
+}
+
TYPED_TEST(BitVectorTest, PushBack) {
TypeParam Vec(10, false);
EXPECT_EQ(-1, Vec.find_first());
|
@FranciscoThiesen FYI, I edited the commit description to remove a mention because of how many notification this generates: https://discourse.llvm.org/t/forbidding-username-in-commits/86997 |
✅ With the latest revision this PR passed the C/C++ code formatter. |
Sorry about that! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you undo unrelated formatting changes? These should be landed separately.
1c6abb3
to
6926cfb
Compare
Co-authored-by: Jakub Kuderski <kubakuderski@gmail.com>
Co-authored-by: Jakub Kuderski <kubakuderski@gmail.com>
@kuhar can you trigger the workflows? |
Head branch was pushed to by a user without write access
@kuhar fixed issue with formatting (no wide changes, just fixed the clang-format commented parts) |
@FranciscoThiesen Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/31940 Here is the relevant piece of the build log for the reference
|
…160726) This feature is needed for llvm#160415 , kuhar suggested that I split that PR into 2 so that the ADT work is checking in first. --------- Co-authored-by: Jakub Kuderski <kubakuderski@gmail.com>
…160726) This feature is needed for llvm#160415 , kuhar suggested that I split that PR into 2 so that the ADT work is checking in first. --------- Co-authored-by: Jakub Kuderski <kubakuderski@gmail.com>
This feature is needed for #160415 , kuhar suggested that I split that PR into 2 so that the ADT work is checking in first.