diff --git a/llvm/include/llvm/ADT/BitVector.h b/llvm/include/llvm/ADT/BitVector.h --- a/llvm/include/llvm/ADT/BitVector.h +++ b/llvm/include/llvm/ADT/BitVector.h @@ -452,6 +452,9 @@ return (Bits[Idx / BITWORD_SIZE] & Mask) != 0; } + /// Return the last element in the vector. + bool back() const { return (*this)[size() - 1]; } + bool test(unsigned Idx) const { return (*this)[Idx]; } @@ -473,6 +476,9 @@ set(OldSize); } + /// Pop one bit from the end of the vector. + void pop_back() { resize(size() - 1); } + /// Test if any common bits are set. bool anyCommon(const BitVector &RHS) const { unsigned ThisWords = Bits.size(); diff --git a/llvm/include/llvm/ADT/SmallBitVector.h b/llvm/include/llvm/ADT/SmallBitVector.h --- a/llvm/include/llvm/ADT/SmallBitVector.h +++ b/llvm/include/llvm/ADT/SmallBitVector.h @@ -471,6 +471,9 @@ return getPointer()->operator[](Idx); } + /// Return the last element in the vector. + bool back() const { return (*this)[size() - 1]; } + bool test(unsigned Idx) const { return (*this)[Idx]; } @@ -480,6 +483,9 @@ resize(size() + 1, Val); } + /// Pop one bit from the end of the vector. + void pop_back() { resize(size() - 1); } + /// Test if any common bits are set. bool anyCommon(const SmallBitVector &RHS) const { if (isSmall() && RHS.isSmall()) diff --git a/llvm/unittests/ADT/BitVectorTest.cpp b/llvm/unittests/ADT/BitVectorTest.cpp --- a/llvm/unittests/ADT/BitVectorTest.cpp +++ b/llvm/unittests/ADT/BitVectorTest.cpp @@ -1180,21 +1180,25 @@ EXPECT_EQ(-1, Vec.find_first()); EXPECT_EQ(10U, Vec.size()); EXPECT_EQ(0U, Vec.count()); + EXPECT_EQ(false, Vec.back()); Vec.push_back(true); EXPECT_EQ(10, Vec.find_first()); EXPECT_EQ(11U, Vec.size()); EXPECT_EQ(1U, Vec.count()); + EXPECT_EQ(true, Vec.back()); Vec.push_back(false); EXPECT_EQ(10, Vec.find_first()); EXPECT_EQ(12U, Vec.size()); EXPECT_EQ(1U, Vec.count()); + EXPECT_EQ(false, Vec.back()); Vec.push_back(true); EXPECT_EQ(10, Vec.find_first()); EXPECT_EQ(13U, Vec.size()); EXPECT_EQ(2U, Vec.count()); + EXPECT_EQ(true, Vec.back()); // Add a lot of values to cause reallocation. for (int i = 0; i != 100; ++i) { @@ -1206,6 +1210,28 @@ EXPECT_EQ(102U, Vec.count()); } +TYPED_TEST(BitVectorTest, PopBack) { + TypeParam Vec(10, true); + EXPECT_EQ(10U, Vec.size()); + EXPECT_EQ(10U, Vec.count()); + EXPECT_EQ(true, Vec.back()); + + Vec.pop_back(); + EXPECT_EQ(9U, Vec.size()); + EXPECT_EQ(9U, Vec.count()); + EXPECT_EQ(true, Vec.back()); + + Vec.push_back(false); + EXPECT_EQ(10U, Vec.size()); + EXPECT_EQ(9U, Vec.count()); + EXPECT_EQ(false, Vec.back()); + + Vec.pop_back(); + EXPECT_EQ(9U, Vec.size()); + EXPECT_EQ(9U, Vec.count()); + EXPECT_EQ(true, Vec.back()); +} + TYPED_TEST(BitVectorTest, DenseSet) { DenseSet Set; TypeParam A(10, true);