#pragma once namespace NGL { template class BitSet { public: BitSet(T bitset = T()) : mBitSet(bitset) , mSize(sizeof(T) * 8) { } BitSet(const BitSet &other) : mBitSet(other.mBitSet) , mSize(other.mSize) { } BitSet &operator=(const BitSet &other) { if (this == &other) return *this; mBitSet = other.mBitSet; mSize = other.mSize; return *this; } bool operator==(const BitSet &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; }; }