Implement operators |=, &=, and ^= for SmallBitVector, and remove the
authorDan Gohman <gohman@apple.com>
Wed, 10 Feb 2010 05:54:04 +0000 (05:54 +0000)
committerDan Gohman <gohman@apple.com>
Wed, 10 Feb 2010 05:54:04 +0000 (05:54 +0000)
restriction in BitVector for |= and ^= that the operand must be the
same length.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@95768 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/BitVector.h
include/llvm/ADT/SmallBitVector.h
unittests/ADT/BitVectorTest.cpp
unittests/ADT/SmallBitVectorTest.cpp

index 45108c8cc5198ac5ae83fc60c38d44c6359e38eb..b9f2d8332263945acc126759799bfec0a4bbbd47 100644 (file)
@@ -307,15 +307,17 @@ public:
   }
 
   BitVector &operator|=(const BitVector &RHS) {
-    assert(Size == RHS.Size && "Illegal operation!");
-    for (unsigned i = 0; i < NumBitWords(size()); ++i)
+    if (size() < RHS.size())
+      resize(RHS.size());
+    for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
       Bits[i] |= RHS.Bits[i];
     return *this;
   }
 
   BitVector &operator^=(const BitVector &RHS) {
-    assert(Size == RHS.Size && "Illegal operation!");
-    for (unsigned i = 0; i < NumBitWords(size()); ++i)
+    if (size() < RHS.size())
+      resize(RHS.size());
+    for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
       Bits[i] ^= RHS.Bits[i];
     return *this;
   }
index 346fb1ca43dcd2232c21581ea5d8df9a05eee5e4..5c774b90ee3df3aefb0d612c46e2febbcc32644f 100644 (file)
@@ -310,11 +310,47 @@ public:
   }
 
   // Intersection, union, disjoint union.
-  BitVector &operator&=(const SmallBitVector &RHS); // TODO: implement
+  SmallBitVector &operator&=(const SmallBitVector &RHS) {
+    resize(std::max(size(), RHS.size()));
+    if (isSmall())
+      setSmallBits(getSmallBits() & RHS.getSmallBits());
+    else if (!RHS.isSmall())
+      X.getPointer()->operator&=(*RHS.X.getPointer());
+    else {
+      SmallBitVector Copy = RHS;
+      Copy.resize(size());
+      X.getPointer()->operator&=(*Copy.X.getPointer());
+    }
+    return *this;
+  }
 
-  BitVector &operator|=(const SmallBitVector &RHS); // TODO: implement
+  SmallBitVector &operator|=(const SmallBitVector &RHS) {
+    resize(std::max(size(), RHS.size()));
+    if (isSmall())
+      setSmallBits(getSmallBits() | RHS.getSmallBits());
+    else if (!RHS.isSmall())
+      X.getPointer()->operator|=(*RHS.X.getPointer());
+    else {
+      SmallBitVector Copy = RHS;
+      Copy.resize(size());
+      X.getPointer()->operator|=(*Copy.X.getPointer());
+    }
+    return *this;
+  }
 
-  BitVector &operator^=(const SmallBitVector &RHS); // TODO: implement
+  SmallBitVector &operator^=(const SmallBitVector &RHS) {
+    resize(std::max(size(), RHS.size()));
+    if (isSmall())
+      setSmallBits(getSmallBits() ^ RHS.getSmallBits());
+    else if (!RHS.isSmall())
+      X.getPointer()->operator^=(*RHS.X.getPointer());
+    else {
+      SmallBitVector Copy = RHS;
+      Copy.resize(size());
+      X.getPointer()->operator^=(*Copy.X.getPointer());
+    }
+    return *this;
+  }
 
   // Assignment operator.
   const SmallBitVector &operator=(const SmallBitVector &RHS) {
index f04eb60179eb618bad352a467ac360d7bd46fa89..4d4f5fd63c39e722e79eb802cb692dfcf18f5f22 100644 (file)
@@ -138,5 +138,45 @@ TEST(BitVectorTest, TrivialOperation) {
   EXPECT_TRUE(Vec.empty());
 }
 
+TEST(BitVectorTest, CompoundAssignment) {
+  BitVector A;
+  A.resize(10);
+  A.set(4);
+  A.set(7);
+
+  BitVector B;
+  B.resize(50);
+  B.set(5);
+  B.set(18);
+
+  A |= B;
+  EXPECT_TRUE(A.test(4));
+  EXPECT_TRUE(A.test(5));
+  EXPECT_TRUE(A.test(7));
+  EXPECT_TRUE(A.test(18));
+  EXPECT_EQ(A.count(), 4);
+  EXPECT_EQ(A.size(), 50);
+
+  B.resize(10);
+  B.set();
+  B.reset(2);
+  B.reset(7);
+  A &= B;
+  EXPECT_FALSE(A.test(2));
+  EXPECT_FALSE(A.test(7));
+  EXPECT_EQ(A.size(), 50);
+  EXPECT_EQ(A.count(), 2);
+
+  B.resize(100);
+  B.set();
+
+  A ^= B;
+  EXPECT_TRUE(A.test(2));
+  EXPECT_TRUE(A.test(7));
+  EXPECT_EQ(A.size(), 100);
+  EXPECT_EQ(A.count(), 98);
 }
+
+}
+
 #endif
index a5c60dec9ba2abad99d6c5b87152061f9f90d5a3..8048b61d388dd054ecfb62be3b7f2b6169bdf114 100644 (file)
@@ -137,4 +137,43 @@ TEST(SmallBitVectorTest, TrivialOperation) {
   EXPECT_TRUE(Vec.empty());
 }
 
+TEST(SmallBitVectorTest, CompoundAssignment) {
+  SmallBitVector A;
+  A.resize(10);
+  A.set(4);
+  A.set(7);
+
+  SmallBitVector B;
+  B.resize(50);
+  B.set(5);
+  B.set(18);
+
+  A |= B;
+  EXPECT_TRUE(A.test(4));
+  EXPECT_TRUE(A.test(5));
+  EXPECT_TRUE(A.test(7));
+  EXPECT_TRUE(A.test(18));
+  EXPECT_EQ(A.count(), 4);
+  EXPECT_EQ(A.size(), 50);
+
+  B.resize(10);
+  B.set();
+  B.reset(2);
+  B.reset(7);
+  A &= B;
+  EXPECT_FALSE(A.test(2));
+  EXPECT_FALSE(A.test(7));
+  EXPECT_EQ(A.size(), 50);
+  EXPECT_EQ(A.count(), 2);
+
+  B.resize(100);
+  B.set();
+
+  A ^= B;
+  EXPECT_TRUE(A.test(2));
+  EXPECT_TRUE(A.test(7));
+  EXPECT_EQ(A.size(), 100);
+  EXPECT_EQ(A.count(), 98);
+}
+
 }