Implement operators |=, &=, and ^= for SmallBitVector, and remove the

restriction in BitVector for |= and ^= that the operand must be the
same length.

llvm-svn: 95768
This commit is contained in:
Dan Gohman 2010-02-10 05:54:04 +00:00
parent d2035fb242
commit 2dd0d36f14
4 changed files with 124 additions and 7 deletions

View 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;
}

View 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) {

View 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

View 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);
}
}