Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Util MP - Hash Table.inl
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, Andrew Aye.  All Rights Reserved.
    This software is free for non-commercial use.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
      following conditions are met:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution in binary form must reproduce this copyright notice, this list of conditions and the following disclaimers in the documentation and other materials
          provided with the distribution.
    The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
    The intellectual property rights of the algorithms used reside with Andrew Aye.
    You may not use this software, in whole or in part, in support of any commercial product without the express written consent of the author.
    There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
#if !defined(TGS_COMMON_UTIL_MP_HASH_TABLE_INL)
#define TGS_COMMON_UTIL_MP_HASH_TABLE_INL
#pragma once


/* == Common ============================================================================================================================================================ */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Internal Functions                                                                                                                                                    */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

TgINLINE TgRESULT                           tgCM_UTM_HT_Find_Internal( P_TgUINTPTR, C_TgUINTXX, PCU_STg2_UTM_AM_HT );
TgINLINE TgRESULT                           tgCM_UTM_HT_Insert_Internal( PCU_STg2_UTM_AM_HT, C_TgUINTXX, C_TgUINT64 );
TgEXTN TgRESULT                             tgCM_UTM_HT_Remove_Internal( PCU_STg2_UTM_AM_HT, C_TgUINTXX );




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Functions                                                                                                                                                      */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgCM_UTM_HT_Free ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_HT_Free( PCU_STg2_UTM_AM_HT psHash_Table )
{
    if (psHash_Table->m_nuiNode_Virtual > 0)
    {
        TgFREE_VIRTUAL( psHash_Table->m_psNode_List );
    }
    psHash_Table->m_psHash_List = nullptr;
    psHash_Table->m_psNode_List = nullptr;
    psHash_Table->m_uiFree_Node = 0;
    psHash_Table->m_nuiEntries = 0;
    psHash_Table->m_nuiNode_Commit = 0;
    psHash_Table->m_nuiNode_Virtual = 0;

    tgCM_UTM_AM_RW_Free( &psHash_Table->m_sLock.m_sLock );
}


/* ---- tgCM_UTM_HT_Clear ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_HT_Clear( PCU_STg2_UTM_AM_HT psHash_Table )
{
    TgUINTXX                            nuiEntry_Node;
    TgUINTXX                            uiIndex;

    nuiEntry_Node = 1 + psHash_Table->m_nuiEntries * sizeof( P_STg2_UTM_Hash_Node ) / sizeof( STg2_UTM_Hash_Node );

    memset( psHash_Table->m_psHash_List, 0x0, nuiEntry_Node*sizeof( P_STg2_UTM_Hash_Node ) );

    for (uiIndex = nuiEntry_Node; uiIndex < psHash_Table->m_nuiNode_Commit - 1; ++uiIndex)
    {
        psHash_Table->m_psNode_List[uiIndex].m_uiNext = uiIndex + 1;
    };
    psHash_Table->m_psNode_List[uiIndex].m_uiNext = 0;
}


/* ---- tgCM_UTM_HT_Find_Internal --------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Find_Internal( P_TgUINTPTR pOutVal, C_TgUINTXX uiHash, PCU_STg2_UTM_AM_HT psHash_Table )
{
    TgUINTXX                            uiIndex;
    P_STg2_UTM_Hash_Node                psNode;

    uiIndex = uiHash % psHash_Table->m_nuiEntries;
    psNode = psHash_Table->m_psHash_List[uiIndex];

    if (nullptr == psNode)
        return (KTgE_FAIL);

    while (1)
    {
        if (psNode->m_uiHash == uiHash)
        {
            *pOutVal = psNode->m_uiVal;
            return (KTgS_OK);
        };

        /* Failed to find the requested hash in the array | table */
        if (psNode->m_uiNext == 0)
        {
            return (KTgE_FAIL);
        };

        psNode = psHash_Table->m_psNode_List + psNode->m_uiNext;
    };
}


/* ---- tgCM_UTM_HT_Find_Yield ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Find_Yield( P_TgUINTPTR pOutVal, C_TgUINTXX uiHash, PCU_STg2_UTM_AM_HT psHash_Table )
{
    TgRESULT                            iResult;

    tgCM_UTM_AM_RW_Enter_Read_Wait_Yield( &psHash_Table->m_sLock.m_sLock );
    iResult = tgCM_UTM_HT_Find_Internal( pOutVal, uiHash, psHash_Table );
    tgCM_UTM_AM_RW_Exit_Read( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ---- tgCM_UTM_HT_Find_Spin ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Find_Spin( P_TgUINTPTR pOutVal, C_TgUINTXX uiHash, PCU_STg2_UTM_AM_HT psHash_Table )
{
    TgRESULT                            iResult;

    tgCM_UTM_AM_RW_Enter_Read_Wait_Spin( &psHash_Table->m_sLock.m_sLock );
    iResult = tgCM_UTM_HT_Find_Internal( pOutVal, uiHash, psHash_Table );
    tgCM_UTM_AM_RW_Exit_Read( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ---- tgCM_UTM_HT_Find ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Find( P_TgUINTPTR pOutVal, C_TgUINTXX uiHash, PCU_STg2_UTM_AM_HT psHash_Table )
{
    TgRESULT                            iResult;

    if (TgFAILED( tgCM_UTM_AM_RW_Enter_Read( &psHash_Table->m_sLock.m_sLock ) ))
        return (KTgE_FAIL);
    iResult = tgCM_UTM_HT_Find_Internal( pOutVal, uiHash, psHash_Table );
    tgCM_UTM_AM_RW_Exit_Read( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ---- tgCM_UTM_HT_Insert_Internal ------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Insert_Internal( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash, C_TgUINT64 uiValue )
{
    TgUINTXX                            uiIndex;
    P_STg2_UTM_Hash_Node                psNode, psPrev_Node;

    if (0 == psHash_Table->m_uiFree_Node)
        return (KTgE_FAIL);

    uiIndex = uiHash % psHash_Table->m_nuiEntries;
    psNode = psHash_Table->m_psNode_List + psHash_Table->m_uiFree_Node;
    psHash_Table->m_uiFree_Node = psNode->m_uiNext;

    psPrev_Node = psHash_Table->m_psHash_List[uiIndex];
    psNode->m_uiHash = uiHash;
    psNode->m_uiNext = nullptr == psPrev_Node ? 0 : (TgUINT32)(psPrev_Node - psHash_Table->m_psNode_List);
    psNode->m_uiVal = uiValue;
    psHash_Table->m_psHash_List[uiIndex] = psNode;
    return (KTgS_OK);
}


/* ---- tgCM_UTM_HT_Insert_Yield ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Insert_Yield( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash, C_TgUINT64 uiValue )
{
    TgRESULT                            iResult;

    tgCM_UTM_AM_RW_Enter_Write_Wait_Yield( &psHash_Table->m_sLock.m_sLock );
    iResult = tgCM_UTM_HT_Insert_Internal( psHash_Table, uiHash, uiValue );
    tgCM_UTM_AM_RW_Exit_Write( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ---- tgCM_UTM_HT_Insert_Spin ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Insert_Spin( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash, C_TgUINT64 uiValue )
{
    TgRESULT                            iResult;

    tgCM_UTM_AM_RW_Enter_Write_Wait_Spin( &psHash_Table->m_sLock.m_sLock );
    iResult = tgCM_UTM_HT_Insert_Internal( psHash_Table, uiHash, uiValue );
    tgCM_UTM_AM_RW_Exit_Write( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ---- tgCM_UTM_HT_Insert ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Insert( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash, C_TgUINT64 uiValue )
{
    TgRESULT                            iResult;

    if (TgFAILED( tgCM_UTM_AM_RW_Enter_Write( &psHash_Table->m_sLock.m_sLock ) ))
        return (KTgE_FAIL);
    iResult = tgCM_UTM_HT_Insert_Internal( psHash_Table, uiHash, uiValue );
    tgCM_UTM_AM_RW_Exit_Write( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ---- tgCM_UTM_HT_Update_Internal ------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Update_Internal( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash, C_TgUINT64 uiValue )
{
    TgUINTXX                            uiIndex;
    P_STg2_UTM_Hash_Node                psNode;

    uiIndex = uiHash % psHash_Table->m_nuiEntries;
    psNode = psHash_Table->m_psHash_List[uiIndex];

    if (nullptr == psNode)
        return (KTgE_FAIL);

    while (1)
    {
        if (psNode->m_uiHash == uiHash)
        {
            psNode->m_uiVal = uiValue;
            return (KTgS_OK);
        };

        /* Failed to find the requested hash in the array | table */
        if (psNode->m_uiNext == 0)
        {
            return (KTgE_FAIL);
        };

        psNode = psHash_Table->m_psNode_List + psNode->m_uiNext;
    };
}


/* ---- tgCM_UTM_HT_Update_Yield ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Update_Yield( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash, C_TgUINT64 uiValue )
{
    TgRESULT                            iResult;

    tgCM_UTM_AM_RW_Enter_Write_Wait_Yield( &psHash_Table->m_sLock.m_sLock );
    iResult = tgCM_UTM_HT_Update_Internal( psHash_Table, uiHash, uiValue );
    tgCM_UTM_AM_RW_Exit_Write( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ---- tgCM_UTM_HT_Update_Spin ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Update_Spin( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash, C_TgUINT64 uiValue )
{
    TgRESULT                            iResult;

    tgCM_UTM_AM_RW_Enter_Write_Wait_Spin( &psHash_Table->m_sLock.m_sLock );
    iResult = tgCM_UTM_HT_Update_Internal( psHash_Table, uiHash, uiValue );
    tgCM_UTM_AM_RW_Exit_Write( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ---- tgCM_UTM_HT_Update ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Update( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash, C_TgUINT64 uiValue )
{
    TgRESULT                            iResult;

    if (TgFAILED( tgCM_UTM_AM_RW_Enter_Write( &psHash_Table->m_sLock.m_sLock ) ))
        return (KTgE_FAIL);
    iResult = tgCM_UTM_HT_Update_Internal( psHash_Table, uiHash, uiValue );
    tgCM_UTM_AM_RW_Exit_Write( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ---- tgCM_UTM_HT_Remove_Yield ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Remove_Yield( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash )
{
    TgRESULT                            iResult;

    tgCM_UTM_AM_RW_Enter_Write_Wait_Yield( &psHash_Table->m_sLock.m_sLock );
    iResult = tgCM_UTM_HT_Remove_Internal( psHash_Table, uiHash );
    tgCM_UTM_AM_RW_Exit_Write( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ---- tgCM_UTM_HT_Remove_Spin ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Remove_Spin( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash )
{
    TgRESULT                            iResult;

    tgCM_UTM_AM_RW_Enter_Write_Wait_Spin( &psHash_Table->m_sLock.m_sLock );
    iResult = tgCM_UTM_HT_Remove_Internal( psHash_Table, uiHash );
    tgCM_UTM_AM_RW_Exit_Write( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ---- tgCM_UTM_HT_Remove ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_HT_Remove( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash )
{
    TgRESULT                            iResult;

    if (TgFAILED( tgCM_UTM_AM_RW_Enter_Write( &psHash_Table->m_sLock.m_sLock ) ))
        return (KTgE_FAIL);
    iResult = tgCM_UTM_HT_Remove_Internal( psHash_Table, uiHash );
    tgCM_UTM_AM_RW_Exit_Write( &psHash_Table->m_sLock.m_sLock );
    return (iResult);
}


/* ====================================================================================================================================================================== */
#endif