#ifndef BitVector_h
#define BitVector_h
#include "wtf/Assertions.h"
#include "wtf/StdLibExtras.h"
#include "wtf/WTFExport.h"
namespace WTF {
class PrintStream;
class WTF_EXPORT BitVector {
public:
BitVector()
: m_bitsOrPointer(makeInlineBits(0))
{
}
explicit BitVector(size_t numBits)
: m_bitsOrPointer(makeInlineBits(0))
{
ensureSize(numBits);
}
BitVector(const BitVector& other)
: m_bitsOrPointer(makeInlineBits(0))
{
(*this) = other;
}
~BitVector()
{
if (isInline())
return;
OutOfLineBits::destroy(outOfLineBits());
}
BitVector& operator=(const BitVector& other)
{
if (isInline() && other.isInline())
m_bitsOrPointer = other.m_bitsOrPointer;
else
setSlow(other);
return *this;
}
size_t size() const
{
if (isInline())
return maxInlineBits();
return outOfLineBits()->numBits();
}
void ensureSize(size_t numBits)
{
if (numBits <= size())
return;
resizeOutOfLine(numBits);
}
void resize(size_t numBits);
void clearAll();
bool quickGet(size_t bit) const
{
ASSERT_WITH_SECURITY_IMPLICATION(bit < size());
return !!(bits()[bit / bitsInPointer()] & (static_cast<uintptr_t>(1) << (bit & (bitsInPointer() - 1))));
}
void quickSet(size_t bit)
{
ASSERT_WITH_SECURITY_IMPLICATION(bit < size());
bits()[bit / bitsInPointer()] |= (static_cast<uintptr_t>(1) << (bit & (bitsInPointer() - 1)));
}
void quickClear(size_t bit)
{
ASSERT_WITH_SECURITY_IMPLICATION(bit < size());
bits()[bit / bitsInPointer()] &= ~(static_cast<uintptr_t>(1) << (bit & (bitsInPointer() - 1)));
}
void quickSet(size_t bit, bool value)
{
if (value)
quickSet(bit);
else
quickClear(bit);
}
bool get(size_t bit) const
{
if (bit >= size())
return false;
return quickGet(bit);
}
void set(size_t bit)
{
ensureSize(bit + 1);
quickSet(bit);
}
void ensureSizeAndSet(size_t bit, size_t size)
{
ensureSize(size);
quickSet(bit);
}
void clear(size_t bit)
{
if (bit >= size())
return;
quickClear(bit);
}
void set(size_t bit, bool value)
{
if (value)
set(bit);
else
clear(bit);
}
void dump(PrintStream& out);
private:
static unsigned bitsInPointer()
{
return sizeof(void*) << 3;
}
static unsigned maxInlineBits()
{
return bitsInPointer() - 1;
}
static size_t byteCount(size_t bitCount)
{
return (bitCount + 7) >> 3;
}
static uintptr_t makeInlineBits(uintptr_t bits)
{
ASSERT(!(bits & (static_cast<uintptr_t>(1) << maxInlineBits())));
return bits | (static_cast<uintptr_t>(1) << maxInlineBits());
}
class OutOfLineBits {
public:
size_t numBits() const { return m_numBits; }
size_t numWords() const { return (m_numBits + bitsInPointer() - 1) / bitsInPointer(); }
uintptr_t* bits() { return bitwise_cast<uintptr_t*>(this + 1); }
const uintptr_t* bits() const { return bitwise_cast<const uintptr_t*>(this + 1); }
static OutOfLineBits* create(size_t numBits);
static void destroy(OutOfLineBits*);
private:
OutOfLineBits(size_t numBits)
: m_numBits(numBits)
{
}
size_t m_numBits;
};
bool isInline() const { return m_bitsOrPointer >> maxInlineBits(); }
const OutOfLineBits* outOfLineBits() const { return bitwise_cast<const OutOfLineBits*>(m_bitsOrPointer << 1); }
OutOfLineBits* outOfLineBits() { return bitwise_cast<OutOfLineBits*>(m_bitsOrPointer << 1); }
void resizeOutOfLine(size_t numBits);
void setSlow(const BitVector& other);
uintptr_t* bits()
{
if (isInline())
return &m_bitsOrPointer;
return outOfLineBits()->bits();
}
const uintptr_t* bits() const
{
if (isInline())
return &m_bitsOrPointer;
return outOfLineBits()->bits();
}
uintptr_t m_bitsOrPointer;
};
}
using WTF::BitVector;
#endif