Index: include/llvm/ADT/BitVector.h =================================================================== --- include/llvm/ADT/BitVector.h +++ include/llvm/ADT/BitVector.h @@ -295,6 +295,23 @@ return *this; } + /// Set bit \p Idx to true. This function allows BitVector to be used in + /// contexts expecting std::set/DenseSet like API. + std::pair insert(unsigned Idx) { + reference ref(*this, Idx); + bool Res = !ref; + ref = true; + return std::make_pair(ref, Res); + } + + /// Clear bit \p Idx. This function allows BitVector to be used in contexts + /// expecting std::set/DenseSet like API. + bool erase(unsigned Idx) { + bool Res = test(Idx); + reset(Idx); + return Res; + } + BitVector &flip() { for (unsigned i = 0; i < NumBitWords(size()); ++i) Bits[i] = ~Bits[i]; Index: include/llvm/ADT/SmallBitVector.h =================================================================== --- include/llvm/ADT/SmallBitVector.h +++ include/llvm/ADT/SmallBitVector.h @@ -452,6 +452,23 @@ return false; } + /// Set bit \p Idx to true. This function allows BitVector to be used in + /// contexts expecting std::set/DenseSet like API. + std::pair insert(unsigned Idx) { + reference ref(*this, Idx); + bool Res = !ref; + ref = true; + return std::make_pair(ref, Res); + } + + /// Clear bit \p Idx. This function allows BitVector to be used in contexts + /// expecting std::set/DenseSet like API. + bool erase(unsigned Idx) { + bool Res = test(Idx); + reset(Idx); + return Res; + } + SmallBitVector &operator|=(const SmallBitVector &RHS) { resize(std::max(size(), RHS.size())); if (isSmall()) Index: unittests/ADT/BitVectorTest.cpp =================================================================== --- unittests/ADT/BitVectorTest.cpp +++ unittests/ADT/BitVectorTest.cpp @@ -399,5 +399,50 @@ C.reset(C); EXPECT_TRUE(C.none()); } + +TYPED_TEST(BitVectorTest, Insert) { + TypeParam A(83); + + EXPECT_EQ(A.count(), 0u); + + EXPECT_TRUE(A.insert(7).second); + EXPECT_EQ(A.count(), 1u); + EXPECT_FALSE(A.insert(7).second); + EXPECT_EQ(A.count(), 1u); + EXPECT_TRUE(A[7]); + + A.set(10); + EXPECT_FALSE(A.insert(10).second); + EXPECT_TRUE(A[10]); + EXPECT_EQ(A.count(), 2u); + + std::pair P0 = A.insert(12); + EXPECT_TRUE(P0.second); + EXPECT_TRUE(A[12]); + P0.first = false; + EXPECT_FALSE(A[12]); + EXPECT_EQ(A.count(), 2u); + P0.first = true; + EXPECT_TRUE(A[12]); + EXPECT_EQ(A.count(), 3u); + + std::pair P1 = A.insert(10); + EXPECT_FALSE(P1.second); + EXPECT_TRUE(A[10]); + P1.first = false; + EXPECT_FALSE(A[10]); + EXPECT_EQ(A.count(), 2u); + P1.first = true; + EXPECT_TRUE(A[10]); + EXPECT_EQ(A.count(), 3u); + + EXPECT_FALSE(A.erase(3)); + EXPECT_EQ(A.count(), 3u); + A.set(77); + EXPECT_TRUE(A.erase(77)); + EXPECT_EQ(A.count(), 3u); + EXPECT_FALSE(A.erase(77)); + EXPECT_EQ(A.count(), 3u); } +} // end anonymous namespace #endif