1.A Bitvector is a specialized kind.Is meant to condense bit values(or booleans) into an array so that no space is wasted.
2.A Bitvector Class
[The Data]
typedef unsigned long int INT32;
class Bitvector
{
protected:
INT32* m_array;
int m_size;
};
[Constructor & DeCons]
Bitvector(int p_size)
{
m_array = 0;
m_size = 0;
Resize(p_size);
}
~Bitvector()
{
if (m_array != 0)
delete[] m_array;
m_array = 0;
}
[Resize]
void Resize(int p_size)
{
INT32* newvector = 0;
if (p_size % 32 == 0)
p_size = p_size / 32;
else
p_size = (p_size / 32) + 1;
newvector = new INT32[p_size];
if (newvector == 0)
return;
int min;
if (p_size < m_size)
min = p_size;
else
min = m_size;
int index;
for (index = 0; index < min; index++)
newvector[index] = m_array[index];
m_size = p_size;
if (m_array != 0)
delete[] m_array;
m_array = newvector;
}
[Access Operator]
Because I am playing around with individual bits and not actual datatypes, I am not allowed to return a reference to a specific bit.
bool operator[] (int p_index)
{
int cell = p_index / 32;
int bit = p_index % 32;
return (m_array[cell] & (1 << bit)) >> bit;
}
[Set Function]
void Set(int p_index, bool p_value)
{
int cell = p_index / 32;
int bit = p_index % 32;
if (p_value == true)
m_array[cell] = (m_array[cell] | (1 << bit));
else
m_array[cell] = (m_array[cell] & (~(1 << bit)));
}
[ClearAll & SetAll Func]
void ClearAll()
{
int index;
for (index =0; index < m_size; index++)
m_array[index] = 0;
}
void SetAll()
{
int index;
for (index =0; index < m_size; index++)
m_array[index] = 0xFFFFFFFF;
}
void Size()
{
return m_size;
}
[WriteFile & ReadFile]
bool WriteFile(const char* p_filename)
{
FILE* outfile = 0;
int written = 0;
outfile = fopen(p_filename, "wb");
if (outfile == 0)
return false;
written = fwrite(m_array, sizeof(INT32), m_size, outfile);
fclose(outfile);
if (written != m_size)
return false;
return true;
}
bool ReadFile(const char* p_filename)
{
FILE* infile = 0;
int read = 0;
infile = fopen(p_filename, "rb");
if (infile == 0)
return false;
read = fread(m_array, sizeof(INT32), m_size, infile);
fclose(infile);
if (read != m_size)
return false;
return true;
}
3.eg:
int main()
{
Bitvector bitv(32);
bool b;
bitv.Set(0, true);
b = bitv[0];
bitv.Set(31, false);
b = bitv[31];
bitv.ClearAll();
bitv.SetAll();
bitv.Resize(48);
int s = bitv.Size();
return 0;
}
4.Game Demo
Array<Player> g_playerarray(64);
Bitvector g_modifiedstates(64);
If any given bit is zero, that means that the corresponding player has not been modified since the last game save, but if the bit is 1, then the corresponding player has been modified since the last game save and thus needs to be written to disk.
5.Bitfields
C++ introduces the notion of bitfields, individual integer variables within a class or structure that contain a certain number of bits.
NOTE: A bitfield cannot be declared outside of a class or a structure, they can only exist within a class or a structure.
There are only two types of bitfields: signed and unsigned.Both types are assumed to be integers.
eg: signed a : 4;
[Using a Bitfield]
class Player
{
public:
unsigned m_state : 2;
unsigned m_haskey : 1;
} // sizeof(Player) = 4;