Eliminate new[0], just set Bits to NULL.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34311 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Evan Cheng 2007-02-15 18:59:15 +00:00
parent 924b1ca9ee
commit 5f92ce4696

View File

@ -69,7 +69,7 @@ public:
/// BitVector default ctor - Creates an empty bitvector. /// BitVector default ctor - Creates an empty bitvector.
BitVector() : Size(0), Capacity(0) { BitVector() : Size(0), Capacity(0) {
Bits = new BitWord[0]; Bits = NULL;
} }
/// BitVector ctor - Creates a bitvector of specified number of bits. All /// BitVector ctor - Creates a bitvector of specified number of bits. All
@ -154,10 +154,12 @@ public:
/// clear - Clear all bits. /// clear - Clear all bits.
void clear() { void clear() {
if (Capacity > 0) {
delete[] Bits; delete[] Bits;
Bits = new BitWord[0]; Bits = NULL;
Size = Capacity = 0; Size = Capacity = 0;
} }
}
/// resize - Grow or shrink the bitvector. /// resize - Grow or shrink the bitvector.
void resize(unsigned N) { void resize(unsigned N) {
@ -186,8 +188,10 @@ public:
// Set, reset, flip // Set, reset, flip
BitVector &set() { BitVector &set() {
if (Bits) {
init_words(Bits, Capacity, true); init_words(Bits, Capacity, true);
clear_unused_bits(); clear_unused_bits();
}
return *this; return *this;
} }
@ -197,6 +201,7 @@ public:
} }
BitVector &reset() { BitVector &reset() {
if (Bits)
init_words(Bits, Capacity, false); init_words(Bits, Capacity, false);
return *this; return *this;
} }
@ -303,9 +308,11 @@ private:
// Clear the unused top bits in the high word. // Clear the unused top bits in the high word.
void clear_unused_bits() { void clear_unused_bits() {
if (Size) {
unsigned ExtraBits = Size % BITS_PER_WORD; unsigned ExtraBits = Size % BITS_PER_WORD;
Bits[Size / BITS_PER_WORD] &= ~(~0 << ExtraBits); Bits[Size / BITS_PER_WORD] &= ~(~0 << ExtraBits);
} }
}
void grow(unsigned NewSize) { void grow(unsigned NewSize) {
unsigned OldCapacity = Capacity; unsigned OldCapacity = Capacity;
@ -317,11 +324,13 @@ private:
std::copy(Bits, &Bits[OldCapacity], NewBits); std::copy(Bits, &Bits[OldCapacity], NewBits);
// Destroy the old bits. // Destroy the old bits.
if (Bits)
delete[] Bits; delete[] Bits;
Bits = NewBits; Bits = NewBits;
} }
void init_words(BitWord *B, unsigned NumWords, bool t) { void init_words(BitWord *B, unsigned NumWords, bool t) {
if (B)
memset(B, 0 - (int)t, NumWords*sizeof(BitWord)); memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
} }
}; };