#ifndef LLVM_ADT_SMALLBITVECTOR_H
#define LLVM_ADT_SMALLBITVECTOR_H
#include "llvm/ADT/BitVector.h"
#include "llvm/Support/MathExtras.h"
#include <cassert>
namespace llvm {
class SmallBitVector {
uintptr_t X;
enum {
NumBaseBits = sizeof(uintptr_t) * CHAR_BIT,
SmallNumRawBits = NumBaseBits - 1,
SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
NumBaseBits == 64 ? 6 :
SmallNumRawBits),
SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
};
public:
class reference {
SmallBitVector &TheVector;
unsigned BitPos;
public:
reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
reference& operator=(reference t) {
*this = bool(t);
return *this;
}
reference& operator=(bool t) {
if (t)
TheVector.set(BitPos);
else
TheVector.reset(BitPos);
return *this;
}
operator bool() const {
return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
}
};
private:
bool isSmall() const {
return X & uintptr_t(1);
}
BitVector *getPointer() const {
assert(!isSmall());
return reinterpret_cast<BitVector *>(X);
}
void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
X = 1;
setSmallSize(NewSize);
setSmallBits(NewSmallBits);
}
void switchToLarge(BitVector *BV) {
X = reinterpret_cast<uintptr_t>(BV);
assert(!isSmall() && "Tried to use an unaligned pointer");
}
uintptr_t getSmallRawBits() const {
assert(isSmall());
return X >> 1;
}
void setSmallRawBits(uintptr_t NewRawBits) {
assert(isSmall());
X = (NewRawBits << 1) | uintptr_t(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) << getSmallSize());
}
void setSmallBits(uintptr_t NewBits) {
setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
(getSmallSize() << SmallNumDataBits));
}
public:
SmallBitVector() : X(1) {}
explicit SmallBitVector(unsigned s, bool t = false) {
if (s <= SmallNumDataBits)
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.getPointer()));
}
~SmallBitVector() {
if (!isSmall())
delete getPointer();
}
bool empty() const {
return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
}
size_t size() const {
return isSmall() ? getSmallSize() : 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 getPointer()->count();
}
bool any() const {
if (isSmall())
return getSmallBits() != 0;
return getPointer()->any();
}
bool all() const {
if (isSmall())
return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
return getPointer()->all();
}
bool none() const {
if (isSmall())
return getSmallBits() == 0;
return getPointer()->none();
}
int find_first() const {
if (isSmall()) {
uintptr_t Bits = getSmallBits();
if (Bits == 0)
return -1;
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 getPointer()->find_first();
}
int find_next(unsigned Prev) const {
if (isSmall()) {
uintptr_t Bits = getSmallBits();
Bits &= ~uintptr_t(0) << (Prev + 1);
if (Bits == 0 || Prev + 1 >= getSmallSize())
return -1;
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 getPointer()->find_next(Prev);
}
void clear() {
if (!isSmall())
delete getPointer();
switchToSmall(0, 0);
}
void resize(unsigned N, bool t = false) {
if (!isSmall()) {
getPointer()->resize(N, t);
} else if (SmallNumDataBits >= N) {
uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
setSmallSize(N);
setSmallBits(NewBits | 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 {
getPointer()->reserve(N);
}
}
SmallBitVector &set() {
if (isSmall())
setSmallBits(~uintptr_t(0));
else
getPointer()->set();
return *this;
}
SmallBitVector &set(unsigned Idx) {
if (isSmall())
setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
else
getPointer()->set(Idx);
return *this;
}
SmallBitVector &reset() {
if (isSmall())
setSmallBits(0);
else
getPointer()->reset();
return *this;
}
SmallBitVector &reset(unsigned Idx) {
if (isSmall())
setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
else
getPointer()->reset(Idx);
return *this;
}
SmallBitVector &flip() {
if (isSmall())
setSmallBits(~getSmallBits());
else
getPointer()->flip();
return *this;
}
SmallBitVector &flip(unsigned Idx) {
if (isSmall())
setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
else
getPointer()->flip(Idx);
return *this;
}
SmallBitVector operator~() const {
return SmallBitVector(*this).flip();
}
reference operator[](unsigned Idx) {
assert(Idx < size() && "Out-of-bounds Bit access.");
return reference(*this, Idx);
}
bool operator[](unsigned Idx) const {
assert(Idx < size() && "Out-of-bounds Bit access.");
if (isSmall())
return ((getSmallBits() >> Idx) & 1) != 0;
return 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 *getPointer() == *RHS.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())
getPointer()->operator&=(*RHS.getPointer());
else {
SmallBitVector Copy = RHS;
Copy.resize(size());
getPointer()->operator&=(*Copy.getPointer());
}
return *this;
}
SmallBitVector &operator|=(const SmallBitVector &RHS) {
resize(std::max(size(), RHS.size()));
if (isSmall())
setSmallBits(getSmallBits() | RHS.getSmallBits());
else if (!RHS.isSmall())
getPointer()->operator|=(*RHS.getPointer());
else {
SmallBitVector Copy = RHS;
Copy.resize(size());
getPointer()->operator|=(*Copy.getPointer());
}
return *this;
}
SmallBitVector &operator^=(const SmallBitVector &RHS) {
resize(std::max(size(), RHS.size()));
if (isSmall())
setSmallBits(getSmallBits() ^ RHS.getSmallBits());
else if (!RHS.isSmall())
getPointer()->operator^=(*RHS.getPointer());
else {
SmallBitVector Copy = RHS;
Copy.resize(size());
getPointer()->operator^=(*Copy.getPointer());
}
return *this;
}
const SmallBitVector &operator=(const SmallBitVector &RHS) {
if (isSmall()) {
if (RHS.isSmall())
X = RHS.X;
else
switchToLarge(new BitVector(*RHS.getPointer()));
} else {
if (!RHS.isSmall())
*getPointer() = *RHS.getPointer();
else {
delete 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