@@ -92,10 +92,6 @@ class SmallBitVector {
92
92
};
93
93
94
94
private:
95
- bool isSmall () const {
96
- return X & uintptr_t (1 );
97
- }
98
-
99
95
BitVector *getPointer () const {
100
96
assert (!isSmall ());
101
97
return reinterpret_cast <BitVector *>(X);
@@ -186,6 +182,8 @@ class SmallBitVector {
186
182
return make_range (set_bits_begin (), set_bits_end ());
187
183
}
188
184
185
+ bool isSmall () const { return X & uintptr_t (1 ); }
186
+
189
187
// / Tests whether there are no bits in this bitvector.
190
188
bool empty () const {
191
189
return isSmall () ? getSmallSize () == 0 : getPointer ()->empty ();
@@ -242,7 +240,7 @@ class SmallBitVector {
242
240
uintptr_t Bits = getSmallBits ();
243
241
if (Bits == 0 )
244
242
return -1 ;
245
- return NumBaseBits - countLeadingZeros (Bits);
243
+ return NumBaseBits - countLeadingZeros (Bits) - 1 ;
246
244
}
247
245
return getPointer ()->find_last ();
248
246
}
@@ -265,7 +263,9 @@ class SmallBitVector {
265
263
return -1 ;
266
264
267
265
uintptr_t Bits = getSmallBits ();
268
- return NumBaseBits - countLeadingOnes (Bits);
266
+ // Set unused bits.
267
+ Bits |= ~uintptr_t (0 ) << getSmallSize ();
268
+ return NumBaseBits - countLeadingOnes (Bits) - 1 ;
269
269
}
270
270
return getPointer ()->find_last_unset ();
271
271
}
@@ -487,27 +487,37 @@ class SmallBitVector {
487
487
bool operator ==(const SmallBitVector &RHS) const {
488
488
if (size () != RHS.size ())
489
489
return false ;
490
- if (isSmall ())
490
+ if (isSmall () && RHS. isSmall () )
491
491
return getSmallBits () == RHS.getSmallBits ();
492
- else
492
+ else if (! isSmall () && !RHS. isSmall ())
493
493
return *getPointer () == *RHS.getPointer ();
494
+ else {
495
+ for (size_t i = 0 , e = size (); i != e; ++i) {
496
+ if ((*this )[i] != RHS[i])
497
+ return false ;
498
+ }
499
+ return true ;
500
+ }
494
501
}
495
502
496
503
bool operator !=(const SmallBitVector &RHS) const {
497
504
return !(*this == RHS);
498
505
}
499
506
500
507
// Intersection, union, disjoint union.
508
+ // FIXME BitVector::operator&= does not resize the LHS but this does
501
509
SmallBitVector &operator &=(const SmallBitVector &RHS) {
502
510
resize (std::max (size (), RHS.size ()));
503
- if (isSmall ())
511
+ if (isSmall () && RHS. isSmall () )
504
512
setSmallBits (getSmallBits () & RHS.getSmallBits ());
505
- else if (!RHS.isSmall ())
513
+ else if (!isSmall () && ! RHS.isSmall ())
506
514
getPointer ()->operator &=(*RHS.getPointer ());
507
515
else {
508
- SmallBitVector Copy = RHS;
509
- Copy.resize (size ());
510
- getPointer ()->operator &=(*Copy.getPointer ());
516
+ size_t i, e;
517
+ for (i = 0 , e = std::min (size (), RHS.size ()); i != e; ++i)
518
+ (*this )[i] = test (i) && RHS.test (i);
519
+ for (e = size (); i != e; ++i)
520
+ reset (i);
511
521
}
512
522
return *this ;
513
523
}
@@ -547,28 +557,26 @@ class SmallBitVector {
547
557
548
558
SmallBitVector &operator |=(const SmallBitVector &RHS) {
549
559
resize (std::max (size (), RHS.size ()));
550
- if (isSmall ())
560
+ if (isSmall () && RHS. isSmall () )
551
561
setSmallBits (getSmallBits () | RHS.getSmallBits ());
552
- else if (!RHS.isSmall ())
562
+ else if (!isSmall () && ! RHS.isSmall ())
553
563
getPointer ()->operator |=(*RHS.getPointer ());
554
564
else {
555
- SmallBitVector Copy = RHS;
556
- Copy.resize (size ());
557
- getPointer ()->operator |=(*Copy.getPointer ());
565
+ for (size_t i = 0 , e = RHS.size (); i != e; ++i)
566
+ (*this )[i] = test (i) || RHS.test (i);
558
567
}
559
568
return *this ;
560
569
}
561
570
562
571
SmallBitVector &operator ^=(const SmallBitVector &RHS) {
563
572
resize (std::max (size (), RHS.size ()));
564
- if (isSmall ())
573
+ if (isSmall () && RHS. isSmall () )
565
574
setSmallBits (getSmallBits () ^ RHS.getSmallBits ());
566
- else if (!RHS.isSmall ())
575
+ else if (!isSmall () && ! RHS.isSmall ())
567
576
getPointer ()->operator ^=(*RHS.getPointer ());
568
577
else {
569
- SmallBitVector Copy = RHS;
570
- Copy.resize (size ());
571
- getPointer ()->operator ^=(*Copy.getPointer ());
578
+ for (size_t i = 0 , e = RHS.size (); i != e; ++i)
579
+ (*this )[i] = test (i) != RHS.test (i);
572
580
}
573
581
return *this ;
574
582
}
0 commit comments