⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dynamicstructchain.cpp

📁 My (so called) HiP compression algorithm as console mode utility. It s a hybrid of Lempel-Ziv 77 a
💻 CPP
字号:

#include "DynamicStructChain.h"

#pragma warning( push )
#pragma warning( disable:4311 ) // 'variable' : pointer truncation from 'type' to 'type'
#pragma warning( disable:4312 ) // 'variable' : conversion from 'type' to 'type' of greater size

// initialization routine for all constructor functions
void DynamicStructChain::InitClass()
{
	dwMemSize   = 0;
	pMem        = NULL;
	dwItemNum   = 0;
	dwMemStartItemNum  = DSC_MEM_START_ITEM_NUM;
	dwMemGrowthItemNum = DSC_MEM_ITEM_GROWTH_NUM;

	return;
}

DynamicStructChain::DynamicStructChain()
{
	dwStructSize = 0;
	InitClass();
}

DynamicStructChain::DynamicStructChain(DWORD dwStructureSize)
{
	dwStructSize = dwStructureSize;
	InitClass();
}

DynamicStructChain::~DynamicStructChain()
{
	// cleanup
	if (dwMemSize)
		free(pMem);
}

void DynamicStructChain::SetStructSize(DWORD dwSize)
{
	dwStructSize = dwSize;

	return;
}

void DynamicStructChain::SetMemStartItemNum(DWORD dwc)
{
	if (dwc == 0)
		dwMemStartItemNum  = DSC_MEM_START_ITEM_NUM;
	else
		dwMemStartItemNum  = dwc;

	return;
}

void DynamicStructChain::SetMemGrowthItemNum(DWORD dwc)
{
	if (dwc == 0)
		dwMemGrowthItemNum = DSC_MEM_ITEM_GROWTH_NUM;
	else
		dwMemGrowthItemNum = dwc;

	return;
}

DWORD DynamicStructChain::GetItemNum()
{
	return dwItemNum;
}

//
// Adds an a item at the end of the structure chain memory
//
// returns:
// FALSE - not enough memory / dwStructSize not set
//
BOOL DynamicStructChain::AddItem(void* pStruct)
{
	DWORD dwNewMemSize;

	if (!dwStructSize)
		return FALSE;

	// handle memory size
	if ((dwItemNum + 1) * dwStructSize > dwMemSize)
	{
		// need to get more memory
		if (dwMemSize == 0)
		{
			// it's the first item
			dwNewMemSize = dwMemStartItemNum*dwStructSize;
			pMem = malloc(dwNewMemSize);
		}
		else
		{
			// isn't the first item
			dwNewMemSize = dwMemGrowthItemNum*dwStructSize + dwMemSize;
			pMem = realloc(pMem, dwNewMemSize);
		}

		if (!pMem)
			return FALSE;

		dwMemSize = dwNewMemSize;
	}

	// add the item
	memcpy(
		(void*)((DWORD)pMem + dwStructSize*dwItemNum),
		pStruct,
		dwStructSize);
	++dwItemNum;

	return TRUE;
}

//
// NOT FULLY TESTED !
//
// wipes the specified structure out of the structure chain memory
//
// returns:
// FALSE - error (not in list)
//
BOOL DynamicStructChain::DeleteItem(UINT index)
{
	// out of range ?
	if (index >= dwItemNum)
		return FALSE; // ERR
	// last item ?
	if (index == dwItemNum - 1)
	{
		--dwItemNum;
		return TRUE; // OK
	}
	//
	// move memory - overwrite the unwanted struct with the following
	//
	memcpy(
		MakePtr(PVOID, pMem, index * dwStructSize),
		MakePtr(PVOID, pMem, (index + 1) * dwStructSize),
		sizeof(dwStructSize) * (dwItemNum - 1 - index));
	--dwItemNum; // adjust member variables

	return TRUE;
}

//
// returns:
// NULL if the item marked by "index" doesn't exist
//
void* DynamicStructChain::GetItem(UINT index)
{
	if (index >= dwItemNum)
		return NULL; // ERR

	return (void*)((DWORD)pMem + dwStructSize*index);
}

//
// Returns:
// FALSE if the item marked by "index" doesn't exist
//
BOOL DynamicStructChain::SetItem( UINT index, void* pStruct )
{
	if ( index >= dwItemNum )
		return FALSE; // ERR

	memcpy(
		(void*)((DWORD)pMem + dwStructSize*index),
		pStruct,
		dwStructSize);

	return TRUE; // OK
}

DWORD DynamicStructChain::GetStructSize()
{
	return dwStructSize;
}

DWORD DynamicStructChain::GetCurrentMemSize()
{
	return dwMemSize;
}

//
// Set the whole class to intialize state BUT "dwStructSize" is hold
//
BOOL DynamicStructChain::Free()
{
	if (dwMemSize)
	{
		free(pMem);
		InitClass();
		return TRUE;
	}

	return FALSE;
}

//
// Purpose:
//   Sorts the structures in the dynamic struct array in the
//   specified modus. 'dwFieldOff' is the offset, relative to
//   to the start of the structure, of a 4 byte unsigned (!)
//   area holding the value with which the structures are compared
//
// Remarks:
//   The InsertSort algorithm is used
//
void DynamicStructChain::Sort( DSC_SORT_TYPE type, DWORD dwFieldOff )
{
	UINT              i, i2;
	void*             pTmpStrct;

	if ( type == DSC_MIN_FIRST )
		return; // NOT IMPLEMENTED !

	// allocate space for temporary struct variable
	pTmpStrct = malloc( dwStructSize );

	//
	// sorting loop
	//
	for (i = 1; i < dwItemNum; i++) // start with 2nd item !
	{
		memcpy(
			pTmpStrct,
			MakePtr(LPVOID, pMem, i*dwStructSize),
			dwStructSize );
		i2 = i;
		while ((i2 > 0)
			&& *(PDWORD)((DWORD)pTmpStrct + dwFieldOff) > *(PDWORD)((DWORD)pMem + dwStructSize*(i2-1) + dwFieldOff)) // earlier item bigger ?
		{
			// ITEM[index] = ITEM[index - 1] 
			memcpy( 
				MakePtr( LPVOID, pMem, i2*dwStructSize ),
				MakePtr( LPVOID, pMem, (i2-1)*dwStructSize ),
				dwStructSize );
			--i2;
		}
		if (i2 < i) // anything swapped ?
			memcpy(
				MakePtr( LPVOID, pMem, i2*dwStructSize ),
				pTmpStrct,
				dwStructSize );
	}

	//
	// tidy up
	//
	free( pTmpStrct );

	return;
}

//
// Purpose:
//   Finds a item in the chain.
//
// Remarks:
//   'dwFieldOff' is the offset, relative to
//   to the start of the structure, of a 4 byte unsigned (!)
//   area holding the value whose item should be returned
//
//
void* DynamicStructChain::FindItem( DWORD dwFieldOff, DWORD dwFindThis )
{
	for ( UINT u = 0; u < dwItemNum; u++ )
		if ( *(PDWORD)((DWORD)pMem + u*dwStructSize + dwFieldOff) == dwFindThis )
			return MakePtr( PVOID, pMem, u*dwStructSize ); // OK

	return NULL; // ERR
}

#pragma warning( pop )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -