X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FADT%2FBitVector.h;h=9dcb9e106f26b2a64630151a8f3c4f1798cfe6de;hb=c9fa2cd596b3c20c5f78aa8e1d227b81d52991fb;hp=23fde26e142ddfc1f88bcb0d26df2dc09a689435;hpb=8d8a0c36b70dacc73baa47ff6e92e5e774338117;p=oota-llvm.git diff --git a/include/llvm/ADT/BitVector.h b/include/llvm/ADT/BitVector.h index 23fde26e142..9dcb9e106f2 100644 --- a/include/llvm/ADT/BitVector.h +++ b/include/llvm/ADT/BitVector.h @@ -17,6 +17,7 @@ #include "llvm/Support/MathExtras.h" #include #include +#include #include namespace llvm { @@ -24,7 +25,7 @@ namespace llvm { class BitVector { typedef unsigned long BitWord; - enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * 8 }; + enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT }; BitWord *Bits; // Actual bits. unsigned Size; // Size of bitvector in bits. @@ -48,6 +49,11 @@ public: ~reference() {} + reference &operator=(reference t) { + *this = bool(t); + return *this; + } + reference& operator=(bool t) { if (t) *WordRef |= 1L << BitPos; @@ -94,6 +100,9 @@ public: delete[] Bits; } + /// empty - Tests whether there are no bits in this bitvector. + bool empty() const { return Size == 0; } + /// size - Returns the number of bits in this bitvector. unsigned size() const { return Size; } @@ -303,15 +312,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; } @@ -323,7 +334,8 @@ public: Size = RHS.size(); unsigned RHSWords = NumBitWords(Size); if (Size <= Capacity * BITWORD_SIZE) { - std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits); + if (Size) + std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits); clear_unused_bits(); return *this; } @@ -340,6 +352,12 @@ public: return *this; } + void swap(BitVector &RHS) { + std::swap(Bits, RHS.Bits); + std::swap(Size, RHS.Size); + std::swap(Capacity, RHS.Capacity); + } + private: unsigned NumBitWords(unsigned S) const { return (S + BITWORD_SIZE-1) / BITWORD_SIZE; @@ -405,4 +423,13 @@ inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) { } } // End llvm namespace + +namespace std { + /// Implement std::swap in terms of BitVector swap. + inline void + swap(llvm::BitVector &LHS, llvm::BitVector &RHS) { + LHS.swap(RHS); + } +} + #endif