149 lines
5.5 KiB
C
149 lines
5.5 KiB
C
/*
|
|
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
* SCR_Hash.c
|
|
* To manage hash table.
|
|
*
|
|
* Scripts, Beaudet Christophe
|
|
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
*/
|
|
|
|
/*
|
|
*=================================================================================================
|
|
* Includes.
|
|
*=================================================================================================
|
|
*/
|
|
|
|
#include <ctype.h>
|
|
#include <memory.h>
|
|
#include "SCR.h"
|
|
|
|
/*
|
|
*=================================================================================================
|
|
* Functions.
|
|
*=================================================================================================
|
|
*/
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* Init a hash table.
|
|
* _p_stHash : Array to init.
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
void fn_v_Hash_InitTable(SCR_tda_st_Hash_Table _p_stHash)
|
|
{
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
unsigned int uiEntry;
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
SCR_M_Dbg_Assert_P(_p_stHash != NULL);
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
for(uiEntry = 0 ; uiEntry < SCR_C_ui_Hash_Size; uiEntry++)
|
|
{
|
|
fn_v_DyAr_InitArray(&_p_stHash[uiEntry]);
|
|
}
|
|
}
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* Close a hash table.
|
|
* _p_stHash : Array to close.
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
void fn_v_Hash_CloseTable(SCR_tda_st_Hash_Table _p_stHash)
|
|
{
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
unsigned int uiEntry;
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
SCR_M_Dbg_Assert_P(_p_stHash != NULL);
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
for(uiEntry = 0 ; uiEntry < SCR_C_ui_Hash_Size; uiEntry++)
|
|
{
|
|
SCR_M_DyAr_DeleteAllElements(SCR_tdst_Hash_Value, &_p_stHash[uiEntry], ;);
|
|
}
|
|
}
|
|
|
|
/*===============================================================================================*/
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* Compute a hash value for a string
|
|
* _p_szString : String to get hash value.
|
|
* Returns hash value computed.
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
SCR_tdx_Hash_Key SCR_fn_x_Hash_ComputeHashKeyForString(char *_p_szString)
|
|
{
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
unsigned int uiKey = 0;
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
SCR_M_Dbg_Assert_P(_p_szString != NULL);
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
/*
|
|
* We compute an hash value for any strings.
|
|
*/
|
|
while(*_p_szString)
|
|
{
|
|
uiKey = (uiKey << 5) + uiKey + tolower(*_p_szString);
|
|
_p_szString++;
|
|
}
|
|
|
|
return uiKey % SCR_C_ui_Hash_Modulo;
|
|
}
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* Compute a hash value for a long
|
|
* _lValue : Long to get hash value.
|
|
* Returns hash value computed.
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
SCR_tdx_Hash_Key SCR_fn_x_Hash_ComputeHashKeyForLong(long _lValue)
|
|
{
|
|
return(((SCR_tdx_Hash_Key) _lValue >> 4) % SCR_C_ui_Hash_Modulo);
|
|
}
|
|
|
|
/*===============================================================================================*/
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* Add a value at a specified Hash position in a table.
|
|
* _xHashKey : Hash key
|
|
* _p_stHashTable : Hash table
|
|
* _ulValue : Value to add
|
|
* _p_uiIndexOfHashValue : To receive index of hash value in table.
|
|
* Returns address of allocated structure.
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
SCR_tdst_Hash_Value *fnp_st_Hash_AddValue
|
|
(
|
|
SCR_tdx_Hash_Key _xHashKey,
|
|
SCR_tda_st_Hash_Table _p_stHashTable,
|
|
unsigned long _ulValue,
|
|
unsigned int *_p_uiIndexOfHashValue
|
|
)
|
|
{
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
SCR_tdst_Hash_Value *p_stHashValue;
|
|
unsigned int uiIndex;
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
SCR_M_Dbg_Assert_P(_p_stHashTable != NULL);
|
|
SCR_M_Dbg_Assert_P(_xHashKey < SCR_C_ui_Hash_Size);
|
|
SCR_M_Dbg_Assert_P(_p_uiIndexOfHashValue != NULL);
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
/*
|
|
* Add element in Hash table
|
|
*/
|
|
SCR_M_DyAr_AddElement(SCR_tdst_Hash_Value, uiIndex, p_stHashValue, _p_stHashTable[_xHashKey]);
|
|
|
|
/*
|
|
* We set value at the returned position.
|
|
*/
|
|
p_stHashValue->ulValue = _ulValue;
|
|
*_p_uiIndexOfHashValue = uiIndex;
|
|
|
|
return p_stHashValue;
|
|
}
|