}
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;
}
}
// 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) {
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
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);
+}
+
}