#include "../../Core/Type/RuType_BitField.h"

// Memory manager
#include "../../Core/Memory/RuMemoryManager.h"

#pragma unmanaged

// ************************************************************************************************************************************************************

CRuBitField::CRuBitField()
:	m_numBits(0),
	m_bitFieldSize(0),
	m_bitFieldArray(NULL)
{
}

CRuBitField::~CRuBitField()
{
	delete[] m_bitFieldArray;
}

INT32 CRuBitField::GetNumBits() const
{
	return m_numBits;
}

void CRuBitField::SetNumBits(INT32 numBits)
{
	if(numBits > m_bitFieldSize)
	{
		// Allocate new bitfield
		INT32 newBitFieldSizeBytes = (numBits >> 5) + (numBits & 0x0000001F ? 1 : 0);
		UINT32 *newBitFieldArray = ruNEW UINT32 [newBitFieldSizeBytes];

		// Clear memory
		memset(newBitFieldArray, 0, (numBits >> 5) * sizeof(UINT32));

		if(m_bitFieldArray)
		{
			// Copy contents of old bitfield
			memcpy(newBitFieldArray, m_bitFieldArray, m_bitFieldSize >> 5);

			// Delete old bitfield
			delete[] m_bitFieldArray;
		}

		// Store new references
		m_numBits = numBits;
		m_bitFieldSize = newBitFieldSizeBytes << 5;
		m_bitFieldArray = newBitFieldArray;
	}
	else
	{
		m_numBits = numBits;
	}
}

// ************************************************************************************************************************************************************

#pragma managed
