Index: include/llvm/ADT/BitVector.h =================================================================== --- include/llvm/ADT/BitVector.h +++ include/llvm/ADT/BitVector.h @@ -503,6 +503,23 @@ return (*this)[Idx]; } + // Push single bit to end of vector. + void push_back(bool Val) { + unsigned OldSize = Size; + unsigned NewSize = Size + 1; + + // Resize, which will insert zeros. + // If we already fit then the unused bits will be already zero. + if (NewSize > getBitCapacity()) + resize(NewSize, false); + else + Size = NewSize; + + // If true, set single bit. + if (Val) + set(OldSize); + } + /// Test if any common bits are set. bool anyCommon(const BitVector &RHS) const { unsigned ThisWords = NumBitWords(size()); Index: include/llvm/ADT/SmallBitVector.h =================================================================== --- include/llvm/ADT/SmallBitVector.h +++ include/llvm/ADT/SmallBitVector.h @@ -465,6 +465,11 @@ return (*this)[Idx]; } + // Push single bit to end of vector. + void push_back(bool Val) { + resize(size() + 1, Val); + } + /// Test if any common bits are set. bool anyCommon(const SmallBitVector &RHS) const { if (isSmall() && RHS.isSmall()) Index: unittests/ADT/BitVectorTest.cpp =================================================================== --- unittests/ADT/BitVectorTest.cpp +++ unittests/ADT/BitVectorTest.cpp @@ -836,5 +836,27 @@ for (unsigned Bit : ToFill.set_bits()) EXPECT_EQ(List[i++], Bit); } + +TYPED_TEST(BitVectorTest, PushBack) { + TypeParam Vec(10, false); + Vec.push_back(true); + EXPECT_EQ(10, Vec.find_first()); + EXPECT_EQ(10, Vec.find_last()); + + Vec.push_back(false); + EXPECT_EQ(10, Vec.find_first()); + EXPECT_EQ(10, Vec.find_last()); + + Vec.push_back(true); + EXPECT_EQ(10, Vec.find_first()); + EXPECT_EQ(12, Vec.find_last()); + + // Add a lot of values to cause reallocation. + for (int i = 0; i != 100; ++i) { + Vec.push_back(true); + Vec.push_back(false); + } + EXPECT_EQ(102, Vec.count()); +} } #endif