#include <algorithm>
#include <cassert>
#include <climits>
+#include <cstdlib>
#include <cstring>
namespace llvm {
~reference() {}
+ reference &operator=(reference t) {
+ *this = bool(t);
+ return *this;
+ }
+
reference& operator=(bool t) {
if (t)
*WordRef |= 1L << BitPos;
/// bits are initialized to the specified value.
explicit BitVector(unsigned s, bool t = false) : Size(s) {
Capacity = NumBitWords(s);
- Bits = new BitWord[Capacity];
+ Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
init_words(Bits, Capacity, t);
if (t)
clear_unused_bits();
}
Capacity = NumBitWords(RHS.size());
- Bits = new BitWord[Capacity];
- std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
+ Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
+ std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
}
~BitVector() {
- delete[] Bits;
+ std::free(Bits);
}
/// empty - Tests whether there are no bits in this bitvector.
return false;
}
+ /// all - Returns true if all bits are set.
+ bool all() const {
+ // TODO: Optimize this.
+ return count() == size();
+ }
+
/// none - Returns true if none of the bits are set.
bool none() const {
return !any();
}
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;
}
Size = RHS.size();
unsigned RHSWords = NumBitWords(Size);
if (Size <= Capacity * BITWORD_SIZE) {
- std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
+ if (Size)
+ std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
clear_unused_bits();
return *this;
}
// Grow the bitvector to have enough elements.
Capacity = RHSWords;
- BitWord *NewBits = new BitWord[Capacity];
- std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
+ BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
+ std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
// Destroy the old bits.
- delete[] Bits;
+ std::free(Bits);
Bits = NewBits;
return *this;
}
void grow(unsigned NewSize) {
- unsigned OldCapacity = Capacity;
- Capacity = NumBitWords(NewSize);
- BitWord *NewBits = new BitWord[Capacity];
-
- // Copy the old bits over.
- if (OldCapacity != 0)
- std::copy(Bits, &Bits[OldCapacity], NewBits);
-
- // Destroy the old bits.
- delete[] Bits;
- Bits = NewBits;
+ Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
+ Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
clear_unused_bits();
}