mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 04:22:20 +02:00
Experimental bitfields
This commit is contained in:
parent
f37fb2c1b7
commit
9e80281d1a
7 changed files with 416 additions and 26 deletions
61
BeefySysLib/util/BitSet.h
Normal file
61
BeefySysLib/util/BitSet.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
#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;
|
Loading…
Add table
Add a link
Reference in a new issue