llvm.org GIT mirror llvm / e7962c9
Implement operators |=, &=, and ^= for SmallBitVector, and remove the 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 Dan Gohman 9 years ago
4 changed file(s) with 126 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
306306 }
307307
308308 BitVector &operator|=(const BitVector &RHS) {
309 assert(Size == RHS.Size && "Illegal operation!");
310 for (unsigned i = 0; i < NumBitWords(size()); ++i)
309 if (size() < RHS.size())
310 resize(RHS.size());
311 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
311312 Bits[i] |= RHS.Bits[i];
312313 return *this;
313314 }
314315
315316 BitVector &operator^=(const BitVector &RHS) {
316 assert(Size == RHS.Size && "Illegal operation!");
317 for (unsigned i = 0; i < NumBitWords(size()); ++i)
317 if (size() < RHS.size())
318 resize(RHS.size());
319 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
318320 Bits[i] ^= RHS.Bits[i];
319321 return *this;
320322 }
309309 }
310310
311311 // Intersection, union, disjoint union.
312 BitVector &operator&=(const SmallBitVector &RHS); // TODO: implement
313
314 BitVector &operator|=(const SmallBitVector &RHS); // TODO: implement
315
316 BitVector &operator^=(const SmallBitVector &RHS); // TODO: implement
312 SmallBitVector &operator&=(const SmallBitVector &RHS) {
313 resize(std::max(size(), RHS.size()));
314 if (isSmall())
315 setSmallBits(getSmallBits() & RHS.getSmallBits());
316 else if (!RHS.isSmall())
317 X.getPointer()->operator&=(*RHS.X.getPointer());
318 else {
319 SmallBitVector Copy = RHS;
320 Copy.resize(size());
321 X.getPointer()->operator&=(*Copy.X.getPointer());
322 }
323 return *this;
324 }
325
326 SmallBitVector &operator|=(const SmallBitVector &RHS) {
327 resize(std::max(size(), RHS.size()));
328 if (isSmall())
329 setSmallBits(getSmallBits() | RHS.getSmallBits());
330 else if (!RHS.isSmall())
331 X.getPointer()->operator|=(*RHS.X.getPointer());
332 else {
333 SmallBitVector Copy = RHS;
334 Copy.resize(size());
335 X.getPointer()->operator|=(*Copy.X.getPointer());
336 }
337 return *this;
338 }
339
340 SmallBitVector &operator^=(const SmallBitVector &RHS) {
341 resize(std::max(size(), RHS.size()));
342 if (isSmall())
343 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
344 else if (!RHS.isSmall())
345 X.getPointer()->operator^=(*RHS.X.getPointer());
346 else {
347 SmallBitVector Copy = RHS;
348 Copy.resize(size());
349 X.getPointer()->operator^=(*Copy.X.getPointer());
350 }
351 return *this;
352 }
317353
318354 // Assignment operator.
319355 const SmallBitVector &operator=(const SmallBitVector &RHS) {
137137 EXPECT_TRUE(Vec.empty());
138138 }
139139
140 TEST(BitVectorTest, CompoundAssignment) {
141 BitVector A;
142 A.resize(10);
143 A.set(4);
144 A.set(7);
145
146 BitVector B;
147 B.resize(50);
148 B.set(5);
149 B.set(18);
150
151 A |= B;
152 EXPECT_TRUE(A.test(4));
153 EXPECT_TRUE(A.test(5));
154 EXPECT_TRUE(A.test(7));
155 EXPECT_TRUE(A.test(18));
156 EXPECT_EQ(A.count(), 4);
157 EXPECT_EQ(A.size(), 50);
158
159 B.resize(10);
160 B.set();
161 B.reset(2);
162 B.reset(7);
163 A &= B;
164 EXPECT_FALSE(A.test(2));
165 EXPECT_FALSE(A.test(7));
166 EXPECT_EQ(A.size(), 50);
167 EXPECT_EQ(A.count(), 2);
168
169 B.resize(100);
170 B.set();
171
172 A ^= B;
173 EXPECT_TRUE(A.test(2));
174 EXPECT_TRUE(A.test(7));
175 EXPECT_EQ(A.size(), 100);
176 EXPECT_EQ(A.count(), 98);
140177 }
178
179 }
180
141181 #endif
136136 EXPECT_TRUE(Vec.empty());
137137 }
138138
139 TEST(SmallBitVectorTest, CompoundAssignment) {
140 SmallBitVector A;
141 A.resize(10);
142 A.set(4);
143 A.set(7);
144
145 SmallBitVector B;
146 B.resize(50);
147 B.set(5);
148 B.set(18);
149
150 A |= B;
151 EXPECT_TRUE(A.test(4));
152 EXPECT_TRUE(A.test(5));
153 EXPECT_TRUE(A.test(7));
154 EXPECT_TRUE(A.test(18));
155 EXPECT_EQ(A.count(), 4);
156 EXPECT_EQ(A.size(), 50);
157
158 B.resize(10);
159 B.set();
160 B.reset(2);
161 B.reset(7);
162 A &= B;
163 EXPECT_FALSE(A.test(2));
164 EXPECT_FALSE(A.test(7));
165 EXPECT_EQ(A.size(), 50);
166 EXPECT_EQ(A.count(), 2);
167
168 B.resize(100);
169 B.set();
170
171 A ^= B;
172 EXPECT_TRUE(A.test(2));
173 EXPECT_TRUE(A.test(7));
174 EXPECT_EQ(A.size(), 100);
175 EXPECT_EQ(A.count(), 98);
139176 }
177
178 }