92 lines
1.8 KiB
C++
92 lines
1.8 KiB
C++
|
|
|
|
|
|
#pragma once
|
|
|
|
namespace NGL
|
|
{
|
|
template <class T> class BitSet
|
|
{
|
|
public:
|
|
BitSet(T bitset = T())
|
|
: mBitSet(bitset)
|
|
, mSize(sizeof(T) * 8)
|
|
{
|
|
}
|
|
BitSet(const BitSet<T> &other)
|
|
: mBitSet(other.mBitSet)
|
|
, mSize(other.mSize)
|
|
{
|
|
}
|
|
BitSet &operator=(const BitSet<T> &other)
|
|
{
|
|
if (this == &other)
|
|
return *this;
|
|
mBitSet = other.mBitSet;
|
|
mSize = other.mSize;
|
|
return *this;
|
|
}
|
|
bool operator==(const BitSet<T> &other) const { return (mBitSet == other.mBitSet && mSize == other.mSize); }
|
|
bool setBit(unsigned int bitNum)
|
|
{
|
|
if (bitNum >= mSize)
|
|
return false;
|
|
mBitSet |= getBit(bitNum);
|
|
return true;
|
|
}
|
|
|
|
bool clearBit(unsigned int bitNum)
|
|
{
|
|
if (bitNum >= mSize)
|
|
return false;
|
|
mBitSet &= ~(getBit(bitNum));
|
|
return true;
|
|
}
|
|
|
|
bool at(unsigned int bitNum) const
|
|
{
|
|
if (bitNum >= sizeof(T) * 8)
|
|
return false;
|
|
T compVal = getBit(bitNum);
|
|
return ((mBitSet & compVal) == compVal);
|
|
}
|
|
|
|
bool operator[](unsigned int bitNum) const { return at(bitNum); }
|
|
|
|
size_t size() const { return mSize; }
|
|
|
|
bool resize(size_t newSize)
|
|
{
|
|
if (newSize >= sizeof(T) * 8)
|
|
return false;
|
|
for (unsigned int i = mSize; i < newSize; ++i)
|
|
clearBit(i);
|
|
mSize = newSize;
|
|
return true;
|
|
}
|
|
|
|
void clear() { fill(false); }
|
|
|
|
void fill(bool bBitsSet)
|
|
{
|
|
if (bBitsSet)
|
|
mBitSet = ~T();
|
|
else
|
|
mBitSet = T();
|
|
}
|
|
|
|
T operator()() const { return mBitSet; }
|
|
|
|
protected:
|
|
static T getBit(unsigned int bitNum_)
|
|
{
|
|
T bitNum = bitNum_;
|
|
const T one = 1;
|
|
return (one << bitNum);
|
|
}
|
|
|
|
T mBitSet;
|
|
size_t mSize;
|
|
};
|
|
}
|