2022-02-11 05:49:11 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "BFPlatform.h"
|
|
|
|
|
|
|
|
NS_BF_BEGIN;
|
|
|
|
|
2022-02-16 18:28:23 -05:00
|
|
|
#define BF_BITSET_ELEM_BITCOUNT (sizeof(uintptr)*8)
|
2022-02-11 05:49:11 -05:00
|
|
|
|
|
|
|
class BitSet
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uintptr* mBits;
|
2022-02-16 18:28:23 -05:00
|
|
|
int mNumBits;
|
2022-02-11 05:49:11 -05:00
|
|
|
|
|
|
|
public:
|
|
|
|
BitSet()
|
|
|
|
{
|
2022-02-16 18:28:23 -05:00
|
|
|
mNumBits = 0;
|
2022-02-11 05:49:11 -05:00
|
|
|
mBits = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitSet(int numBits)
|
|
|
|
{
|
2022-02-16 18:28:23 -05:00
|
|
|
mNumBits = 0;
|
2022-02-11 05:49:11 -05:00
|
|
|
mBits = NULL;
|
|
|
|
this->Resize(numBits);
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:28:23 -05:00
|
|
|
BitSet(BitSet&& other)
|
|
|
|
{
|
|
|
|
mNumBits = other.mNumBits;
|
|
|
|
mBits = other.mBits;
|
|
|
|
other.mNumBits = 0;
|
|
|
|
other.mBits = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitSet(const BitSet& other)
|
|
|
|
{
|
|
|
|
mNumBits = 0;
|
|
|
|
mBits = NULL;
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
2022-02-11 05:49:11 -05:00
|
|
|
~BitSet()
|
|
|
|
{
|
2022-03-18 18:06:14 -07:00
|
|
|
delete [] this->mBits;
|
2022-02-11 05:49:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resize(int numBits)
|
|
|
|
{
|
2022-02-16 18:28:23 -05:00
|
|
|
int numInts = (numBits + BF_BITSET_ELEM_BITCOUNT - 1) / BF_BITSET_ELEM_BITCOUNT;
|
|
|
|
int curNumInts = (mNumBits + BF_BITSET_ELEM_BITCOUNT - 1) / BF_BITSET_ELEM_BITCOUNT;
|
|
|
|
mNumBits = numBits;
|
|
|
|
if (numInts == curNumInts)
|
|
|
|
return;
|
|
|
|
this->mNumBits = numBits;
|
2022-02-11 05:49:11 -05:00
|
|
|
delete this->mBits;
|
|
|
|
this->mBits = new uintptr[numInts];
|
|
|
|
memset(this->mBits, 0, numInts * sizeof(uintptr));
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:28:23 -05:00
|
|
|
void Clear()
|
2022-02-11 05:49:11 -05:00
|
|
|
{
|
2022-02-16 18:28:23 -05:00
|
|
|
int curNumInts = (mNumBits + BF_BITSET_ELEM_BITCOUNT - 1) / BF_BITSET_ELEM_BITCOUNT;
|
|
|
|
memset(mBits, 0, curNumInts * sizeof(uintptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsSet(int idx) const
|
|
|
|
{
|
|
|
|
BF_ASSERT((uintptr)idx < (uintptr)mNumBits);
|
|
|
|
return (this->mBits[idx / BF_BITSET_ELEM_BITCOUNT] & ((uintptr)1 << (idx % BF_BITSET_ELEM_BITCOUNT))) != 0;
|
2022-02-11 05:49:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Set(int idx)
|
|
|
|
{
|
2022-02-16 18:28:23 -05:00
|
|
|
BF_ASSERT((uintptr)idx < (uintptr)mNumBits);
|
|
|
|
this->mBits[idx / BF_BITSET_ELEM_BITCOUNT] |= ((uintptr)1 << (idx % BF_BITSET_ELEM_BITCOUNT));
|
2022-02-11 05:49:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Clear(int idx)
|
|
|
|
{
|
2022-02-16 18:28:23 -05:00
|
|
|
BF_ASSERT((uintptr)idx < (uintptr)mNumBits);
|
|
|
|
this->mBits[idx / BF_BITSET_ELEM_BITCOUNT] &= ~((uintptr)1 << (idx % BF_BITSET_ELEM_BITCOUNT));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty()
|
|
|
|
{
|
|
|
|
return mNumBits == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const BitSet& other) const
|
|
|
|
{
|
|
|
|
int curNumInts = (mNumBits + BF_BITSET_ELEM_BITCOUNT - 1) / BF_BITSET_ELEM_BITCOUNT;
|
|
|
|
if (mNumBits != other.mNumBits)
|
|
|
|
return false;
|
|
|
|
for (int i = 0; i < curNumInts; i++)
|
|
|
|
if (mBits[i] != other.mBits[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const BitSet& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
BitSet& operator=(const BitSet& other)
|
|
|
|
{
|
|
|
|
Resize(other.mNumBits);
|
|
|
|
int curNumInts = (mNumBits + BF_BITSET_ELEM_BITCOUNT - 1) / BF_BITSET_ELEM_BITCOUNT;
|
|
|
|
memcpy(mBits, other.mBits, curNumInts * sizeof(uintptr));
|
|
|
|
return *this;
|
2022-02-11 05:49:11 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_BF_END;
|