1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 03:52:19 +02:00
Beef/BeefySysLib/util/BitSet.h
2022-02-11 05:49:11 -05:00

61 lines
No EOL
1,004 B
C++

#pragma once
#include "BFPlatform.h"
NS_BF_BEGIN;
#define BF_BITSET_ELEM_SIZE (sizeof(uintptr)*8)
class BitSet
{
public:
uintptr* mBits;
int mNumInts;
public:
BitSet()
{
mNumInts = 0;
mBits = NULL;
}
BitSet(int numBits)
{
mNumInts = 0;
mBits = NULL;
this->Resize(numBits);
}
~BitSet()
{
delete this->mBits;
}
void Resize(int numBits)
{
int numInts = (numBits + BF_BITSET_ELEM_SIZE - 1) / BF_BITSET_ELEM_SIZE;
if (numInts == mNumInts)
return;
this->mNumInts = numInts;
delete this->mBits;
this->mBits = new uintptr[numInts];
memset(this->mBits, 0, numInts * sizeof(uintptr));
}
bool IsSet(int idx)
{
return (this->mBits[idx / BF_BITSET_ELEM_SIZE] & ((uintptr)1 << (idx % BF_BITSET_ELEM_SIZE))) != 0;
}
void Set(int idx)
{
this->mBits[idx / BF_BITSET_ELEM_SIZE] |= ((uintptr)1 << (idx % BF_BITSET_ELEM_SIZE));
}
void Clear(int idx)
{
this->mBits[idx / BF_BITSET_ELEM_SIZE] &= ~((uintptr)1 << (idx % BF_BITSET_ELEM_SIZE));
}
};
NS_BF_END;