Data.Structures.For.Game.Programmers.PART2.Basics.4.Bitvector

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;


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值