#ifndef LLVM_ADT_SMALLBITVECTOR_H
#define LLVM_ADT_SMALLBITVECTOR_H
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/Support/MathExtras.h"
#include <cassert>
namespace llvm {
class SmallBitVector {
PointerIntPair<BitVector *, 1, uintptr_t> X;
static const size_t NumBaseBits = sizeof(uintptr_t) * CHAR_BIT;
static const size_t SmallNumRawBits = NumBaseBits - 1;
static const size_t SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
NumBaseBits == 64 ? 6 :
SmallNumRawBits);
static const size_t SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits;
bool isSmall() const {
return X.getInt();
}
void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
X.setInt(true);
setSmallSize(NewSize);
setSmallBits(NewSmallBits);
}
void switchToLarge(BitVector *BV) {
X.setInt(false);
X.setPointer(BV);
}
uintptr_t getSmallRawBits() const {
return reinterpret_cast<uintptr_t>(X.getPointer()) >> 1;
}
void setSmallRawBits(uintptr_t NewRawBits) {
return X.setPointer(reinterpret_cast<BitVector *>(NewRawBits << 1));
}
size_t getSmallSize() const {
return getSmallRawBits() >> SmallNumDataBits;
}
void setSmallSize(size_t Size) {
setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
}
uintptr_t getSmallBits() const {
return getSmallRawBits() & ~(~uintptr_t(0) << SmallNumDataBits);
}
void setSmallBits(uintptr_t NewBits) {
setSmallRawBits((getSmallRawBits() & (~uintptr_t(0) << SmallNumDataBits)) |
(NewBits & ~(~uintptr_t(0) << getSmallSize())));
}
public:
SmallBitVector() : X(0, 1) {}
explicit SmallBitVector(unsigned s, bool t = false) : X(0, 1) {
if (s <= SmallNumRawBits)
switchToSmall(t ? ~uintptr_t(0) : 0, s);
else
switchToLarge(new BitVector(s, t));
}
SmallBitVector(const SmallBitVector &RHS) {
if (RHS.isSmall())
X = RHS.X;
else
switchToLarge(new BitVector(*RHS.X.getPointer()));
}
~SmallBitVector() {
if (!isSmall())
delete X.getPointer();
}
bool empty() const {
return isSmall() ? getSmallSize() == 0 : X.getPointer()->empty();
}
size_t size() const {
return isSmall() ? getSmallSize() : X.getPointer()->size();
}
unsigned count() const {
if (isSmall()) {
uintptr_t Bits = getSmallBits();
if (sizeof(uintptr_t) * CHAR_BIT == 32)
return CountPopulation_32(Bits);
if (sizeof(uintptr_t) * CHAR_BIT == 64)
return CountPopulation_64(Bits);
assert(0 && "Unsupported!");
}
return X.getPointer()->count();
}
bool any() const {
if (isSmall())
return getSmallBits() != 0;
return X.getPointer()->any();
}
bool none() const {
if (isSmall())
return getSmallBits() == 0;
return X.getPointer()->none();
}
int find_first() const {
if (isSmall()) {
uintptr_t Bits = getSmallBits();
if (sizeof(uintptr_t) * CHAR_BIT == 32)
return CountTrailingZeros_32(Bits);
if (sizeof(uintptr_t) * CHAR_BIT == 64)
return CountTrailingZeros_64(Bits);
assert(0 && "Unsupported!");
}
return X.getPointer()->find_first();
}
int find_next(unsigned Prev) const {
if (isSmall()) {
uintptr_t Bits = getSmallBits();
Bits &= ~uintptr_t(0) << Prev;
if (sizeof(uintptr_t) * CHAR_BIT == 32)
return CountTrailingZeros_32(Bits);
if (sizeof(uintptr_t) * CHAR_BIT == 64)
return CountTrailingZeros_64(Bits);
assert(0 && "Unsupported!");
}
return X.getPointer()->find_next(Prev);
}
void clear() {
if (!isSmall())
delete X.getPointer();
switchToSmall(0, 0);
}
void resize(unsigned N, bool t = false) {
if (!isSmall()) {
X.getPointer()->resize(N, t);
} else if (getSmallSize() >= N) {
setSmallSize(N);
setSmallBits(getSmallBits());
} else {
BitVector *BV = new BitVector(N, t);
uintptr_t OldBits = getSmallBits();
for (size_t i = 0, e = getSmallSize(); i != e; ++i)
(*BV)[i] = (OldBits >> i) & 1;
switchToLarge(BV);
}
}
void reserve(unsigned N) {
if (isSmall()) {
if (N > SmallNumDataBits) {
uintptr_t OldBits = getSmallRawBits();
size_t SmallSize = getSmallSize();
BitVector *BV = new BitVector(SmallSize);
for (size_t i = 0; i < SmallSize; ++i)
if ((OldBits >> i) & 1)
BV->set(i);
BV->reserve(N);
switchToLarge(BV);
}
} else {
X.getPointer()->reserve(N);
}
}
SmallBitVector &set() {
if (isSmall())
setSmallBits(~uintptr_t(0));
else
X.getPointer()->set();
return *this;
}
SmallBitVector &set(unsigned Idx) {
if (isSmall())
setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
else
X.getPointer()->set(Idx);
return *this;
}
SmallBitVector &reset() {
if (isSmall())
setSmallBits(0);
else
X.getPointer()->reset();
return *this;
}
SmallBitVector &reset(unsigned Idx) {
if (isSmall())
setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
else
X.getPointer()->reset(Idx);
return *this;
}
SmallBitVector &flip() {
if (isSmall())
setSmallBits(~getSmallBits());
else
X.getPointer()->flip();
return *this;
}
SmallBitVector &flip(unsigned Idx) {
if (isSmall())
setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
else
X.getPointer()->flip(Idx);
return *this;
}
SmallBitVector operator~() const {
return SmallBitVector(*this).flip();
}
bool operator[](unsigned Idx) const {
assert(Idx < size() && "Out-of-bounds Bit access.");
if (isSmall())
return ((getSmallBits() >> Idx) & 1) != 0;
return X.getPointer()->operator[](Idx);
}
bool test(unsigned Idx) const {
return (*this)[Idx];
}
bool operator==(const SmallBitVector &RHS) const {
if (size() != RHS.size())
return false;
if (isSmall())
return getSmallBits() == RHS.getSmallBits();
else
return *X.getPointer() == *RHS.X.getPointer();
}
bool operator!=(const SmallBitVector &RHS) const {
return !(*this == RHS);
}
SmallBitVector &operator&=(const SmallBitVector &RHS) {
resize(std::max(size(), RHS.size()));
if (isSmall())
setSmallBits(getSmallBits() & RHS.getSmallBits());
else if (!RHS.isSmall())
X.getPointer()->operator&=(*RHS.X.getPointer());
else {
SmallBitVector Copy = RHS;
Copy.resize(size());
X.getPointer()->operator&=(*Copy.X.getPointer());
}
return *this;
}
SmallBitVector &operator|=(const SmallBitVector &RHS) {
resize(std::max(size(), RHS.size()));
if (isSmall())
setSmallBits(getSmallBits() | RHS.getSmallBits());
else if (!RHS.isSmall())
X.getPointer()->operator|=(*RHS.X.getPointer());
else {
SmallBitVector Copy = RHS;
Copy.resize(size());
X.getPointer()->operator|=(*Copy.X.getPointer());
}
return *this;
}
SmallBitVector &operator^=(const SmallBitVector &RHS) {
resize(std::max(size(), RHS.size()));
if (isSmall())
setSmallBits(getSmallBits() ^ RHS.getSmallBits());
else if (!RHS.isSmall())
X.getPointer()->operator^=(*RHS.X.getPointer());
else {
SmallBitVector Copy = RHS;
Copy.resize(size());
X.getPointer()->operator^=(*Copy.X.getPointer());
}
return *this;
}
const SmallBitVector &operator=(const SmallBitVector &RHS) {
if (isSmall()) {
if (RHS.isSmall())
X = RHS.X;
else
switchToLarge(new BitVector(*RHS.X.getPointer()));
} else {
if (!RHS.isSmall())
*X.getPointer() = *RHS.X.getPointer();
else {
delete X.getPointer();
X = RHS.X;
}
}
return *this;
}
void swap(SmallBitVector &RHS) {
std::swap(X, RHS.X);
}
};
inline SmallBitVector
operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
SmallBitVector Result(LHS);
Result &= RHS;
return Result;
}
inline SmallBitVector
operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
SmallBitVector Result(LHS);
Result |= RHS;
return Result;
}
inline SmallBitVector
operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
SmallBitVector Result(LHS);
Result ^= RHS;
return Result;
}
}
namespace std {
inline void
swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
LHS.swap(RHS);
}
}
#endif