Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Util SP - Array.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_SP_ARRAY_INL)
#define TGS_COMMON_UTIL_SP_ARRAY_INL
#pragma once


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

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

TgEXTN TgRESULT                                      tgCM_UTS_AR_Internal_ReAllocate( PCU_STg2_UTS_AR psAR, C_TgSIZE uiCount );




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

/* ---- tgCM_UTS_AR_Free ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTS_AR_Free( PCU_STg2_UTS_AR psAR )
{
    TgPARAM_CHECK(psAR);
    if (psAR->m_bFixed)
        return;

    if (nullptr != psAR->m_puiStart)
    {
        TgFREE_POOL( psAR->m_puiStart );
        psAR->m_puiStart = nullptr;
    }
}


/* ---- tgCM_UTS_AR_Copy ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTS_AR_Copy( PCU_STg2_UTS_AR psAR0, CPCU_STg2_UTS_AR psAR1 )
{
    TgPARAM_CHECK(psAR0 && psAR1);
    if (psAR0 == psAR1)
        return (KTgS_OK);
    if (psAR0->m_uiStride != psAR1->m_uiStride)
        return (KTgE_FAIL);
    return (tgCM_UTS_AR_Assign_Range( psAR0, psAR1->m_puiStart, psAR1->m_puiLast ));
}


/* ---- tgCM_UTS_AR_Push_Back ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTS_AR_Push_Back( PCU_STg2_UTS_AR psAR, P_TgVOID pElement )
{
    TgPARAM_CHECK(psAR && pElement);
    if (psAR->m_puiLast != psAR->m_puiEnd)
    {
        if (psAR->m_pInitCopy)
        {
            psAR->m_pInitCopy( psAR->m_puiLast, pElement );
        }
        else
        {
            TgMEMCPY( psAR->m_puiLast, (TgSIZE)(psAR->m_puiEnd - psAR->m_puiLast), pElement, psAR->m_uiStride );
        }
        psAR->m_puiLast = psAR->m_puiLast + psAR->m_uiStride;
        return (KTgS_OK);
    }
    else
    {
        return (tgCM_UTS_AR_Insert_ElementN( psAR, ((TgSIZE)(psAR->m_puiLast - psAR->m_puiStart)) / psAR->m_uiStride, pElement, 1 ));
    }
}


/* ---- tgCM_UTS_AR_Insert_Element -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTS_AR_Insert_Element( PCU_STg2_UTS_AR psAR, C_TgSIZE uiIndex, P_TgVOID pElement )
{
    TgPARAM_CHECK(psAR && pElement);
    if (psAR->m_puiStart + uiIndex*psAR->m_uiStride == psAR->m_puiLast)
    {
        return (tgCM_UTS_AR_Push_Back( psAR, pElement ));
    }
    else
    {
        return (tgCM_UTS_AR_Insert_ElementN( psAR, uiIndex, pElement, 1 ));
    }
}


/* ---- tgCM_UTS_AR_Insert_Copy ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTS_AR_Insert_Copy( PCU_STg2_UTS_AR psAR, C_TgSIZE uiIndex, CPCU_STg2_UTS_AR psAR1 )
{
    TgPARAM_CHECK(psAR && psAR1);
    return (tgCM_UTS_AR_Insert_Range( psAR, uiIndex, psAR1->m_puiStart, psAR1->m_puiLast ));
}


/* ---- tgCM_UTS_AR_Pop_Back -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTS_AR_Pop_Back( PCU_STg2_UTS_AR psAR )
{
    TgPARAM_CHECK(psAR);
    if (psAR->m_puiStart == psAR->m_puiLast)
        return;

    if (psAR->m_pFree)
    {
        psAR->m_pFree( psAR->m_puiLast - psAR->m_uiStride );
    };

    psAR->m_puiLast -= psAR->m_uiStride;
}


/* ---- tgCM_UTS_AR_Erase_Element --------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTS_AR_Erase_Element( PCU_STg2_UTS_AR psAR, C_TgSIZE uiIndex )
{
    P_TgUINT08                          puiData;
    P_TgUINT08                          puiLast;

    TgPARAM_CHECK(psAR);
    puiData = psAR->m_puiStart + uiIndex*psAR->m_uiStride;
    if (nullptr == psAR->m_puiStart || psAR->m_puiLast <= puiData)
        return;

    puiLast = psAR->m_puiLast - psAR->m_uiStride;
    if (psAR->m_pInitCopy)
    {
        for (; puiData < puiLast; puiData += psAR->m_uiStride)
        {
            psAR->m_pInitCopy( puiData, puiData + psAR->m_uiStride );
        };
    }
    else
    {
        TgMEMMOVE( puiData, (TgSIZE)(psAR->m_puiEnd - puiData), puiData + psAR->m_uiStride, (TgSIZE)(puiLast - puiData) );
    };

    if (psAR->m_pFree)
    {
        psAR->m_pFree( puiLast );
    };

    psAR->m_puiLast -= psAR->m_uiStride;
}


/* ---- tgCM_UTS_AR_Clear ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTS_AR_Clear( PCU_STg2_UTS_AR psAR )
{
    P_TgUINT08                          puiData;
    TgSIZE                              nuiElement;
    TgSIZE                              uiIndex;

    TgPARAM_CHECK(psAR);
    puiData = (P_TgUINT08)tgCM_UTS_AR_Get_Data( psAR );
    nuiElement = tgCM_UTS_AR_Count( psAR );

    if (psAR->m_pFree)
    {
        for (uiIndex = 0; uiIndex < nuiElement; ++uiIndex)
        {
            psAR->m_pFree( puiData + uiIndex*psAR->m_uiStride );
        };
    };

    if (psAR->m_bFixed)
    {
        psAR->m_puiLast = psAR->m_puiStart;
    }
    else
    {
        if (nullptr != psAR->m_puiStart)
            TgFREE_POOL( psAR->m_puiStart );
        psAR->m_puiStart = nullptr;
        psAR->m_puiLast = nullptr;
        psAR->m_puiEnd = nullptr;
    }
}


/* ---- tgCM_UTS_AR_Get_Data -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE P_TgVOID tgCM_UTS_AR_Get_Data( PCU_STg2_UTS_AR psAR )
{
    TgPARAM_CHECK(psAR);
    return (psAR->m_puiStart);
}


/* ---- tgCM_UTS_AR_Get_Index ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE P_TgVOID tgCM_UTS_AR_Get_Index( PCU_STg2_UTS_AR psAR, C_TgSIZE uiIndex )
{
    P_TgUINT08                          puiData;

    TgPARAM_CHECK(psAR);
    puiData = psAR->m_puiStart + uiIndex*psAR->m_uiStride;
    if (nullptr == psAR->m_puiStart || psAR->m_puiLast <= puiData)
        return (nullptr);
    return (puiData);
}


/* ---- tgCM_UTS_AR_Get_Front ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE P_TgVOID tgCM_UTS_AR_Get_Front( PCU_STg2_UTS_AR psAR )
{
    TgPARAM_CHECK(psAR);
    return (psAR->m_puiStart == psAR->m_puiLast ? nullptr : psAR->m_puiStart);
}


/* ---- tgCM_UTS_AR_Get_Back -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE P_TgVOID tgCM_UTS_AR_Get_Back( PCU_STg2_UTS_AR psAR )
{
    TgPARAM_CHECK(psAR);
    return (psAR->m_puiStart == psAR->m_puiLast ? nullptr : psAR->m_puiLast - psAR->m_uiStride);
}


/* ---- tgCM_UTS_AR_Query_Index ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTS_AR_Query_Index( P_TgVOID pElement, C_TgSIZE uiSize, PCU_STg2_UTS_AR psAR, C_TgSIZE uiIndex )
{
    P_TgUINT08                          puiData;

    TgPARAM_CHECK(psAR && pElement);
    if (nullptr == pElement || uiSize < psAR->m_uiStride)
        return (KTgE_FAIL);
    puiData = (P_TgUINT08)tgCM_UTS_AR_Get_Index( psAR, uiIndex );
    if (nullptr == puiData)
        return (KTgE_FAIL);
    TgMEMCPY( pElement, psAR->m_uiStride, puiData, psAR->m_uiStride );
    return (KTgS_OK);
}


/* ---- tgCM_UTS_AR_Query_Front ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTS_AR_Query_Front( P_TgVOID pElement, C_TgSIZE uiSize, PCU_STg2_UTS_AR psAR )
{
    P_TgUINT08                          puiData;

    TgPARAM_CHECK(psAR && pElement);
    if (nullptr == pElement || uiSize < psAR->m_uiStride)
        return (KTgE_FAIL);
    puiData = (P_TgUINT08)tgCM_UTS_AR_Get_Front( psAR );
    if (nullptr == puiData)
        return (KTgE_FAIL);
    TgMEMCPY( pElement, psAR->m_uiStride, puiData, psAR->m_uiStride );
    return (KTgS_OK);
}


/* ---- tgCM_UTS_AR_Query_Back ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTS_AR_Query_Back( P_TgVOID pElement, C_TgSIZE uiSize, PCU_STg2_UTS_AR psAR )
{
    P_TgUINT08                          puiData;

    TgPARAM_CHECK(psAR && pElement);
    if (nullptr == pElement || uiSize < psAR->m_uiStride)
        return (KTgE_FAIL);
    puiData = (P_TgUINT08)tgCM_UTS_AR_Get_Back( psAR );
    if (nullptr == puiData)
        return (KTgE_FAIL);
    TgMEMCPY( pElement, psAR->m_uiStride, puiData, psAR->m_uiStride );
    return (KTgS_OK);
}


/* ---- tgCM_UTS_AR_Empty ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL tgCM_UTS_AR_Empty( PCU_STg2_UTS_AR psAR )
{
    TgPARAM_CHECK(psAR);
    return (psAR->m_puiStart == psAR->m_puiLast ? TgTRUE : TgFALSE);
}


/* ---- tgCM_UTS_AR_Capacity -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgSIZE tgCM_UTS_AR_Capacity( PCU_STg2_UTS_AR psAR )
{
    TgPARAM_CHECK(psAR);
    return (nullptr == psAR->m_puiStart ? 0 : ((TgSIZE)(psAR->m_puiEnd - psAR->m_puiStart)) / psAR->m_uiStride);
}


/* ---- tgCM_UTS_AR_Count ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgSIZE tgCM_UTS_AR_Count( PCU_STg2_UTS_AR psAR )
{
    TgPARAM_CHECK(psAR);
    return (nullptr == psAR->m_puiStart ? 0 : ((TgSIZE)(psAR->m_puiLast - psAR->m_puiStart)) / psAR->m_uiStride);
}


/* ---- tgCM_UTS_AR_Resize ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTS_AR_Resize( PCU_STg2_UTS_AR psAR, C_TgSIZE uiCount, P_TgVOID pElement )
{
    TgSIZE                              nuiElement;

    TgPARAM_CHECK(psAR);
    nuiElement = tgCM_UTS_AR_Count( psAR );
    if (uiCount < nuiElement)
    {
        tgCM_UTS_AR_Erase_Range( psAR, uiCount, nuiElement );
        return (KTgS_OK);
    }
    else if (uiCount - nuiElement > 0)
    {
        if (nullptr == pElement)
            return (KTgE_FAIL);
        return (tgCM_UTS_AR_Insert_ElementN( psAR, nuiElement, pElement, uiCount - nuiElement ));
    };
    return (KTgS_OK);
}


/* ---- tgCM_UTS_AR_Reserve --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTS_AR_Reserve( PCU_STg2_UTS_AR psAR, C_TgSIZE uiCount )
{
    TgPARAM_CHECK(psAR);
    if (tgCM_UTS_AR_Capacity( psAR ) >= uiCount)
    {
        return (KTgS_OK);
    };
    if (psAR->m_bFixed)
    {
        return (KTgE_FAIL);
    };
    return (tgCM_UTS_AR_Internal_ReAllocate( psAR, uiCount ));
}


/* ---- tgCM_UTS_AR_Shrink ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTS_AR_Shrink( PCU_STg2_UTS_AR psAR, C_TgSIZE uiCount )
{
    TgPARAM_CHECK(psAR);
    if (tgCM_UTS_AR_Capacity( psAR ) <= uiCount)
    {
        return (KTgS_OK);
    };
    if (tgCM_UTS_AR_Count( psAR ) > uiCount)
    {
        return (KTgE_FAIL);
    };
    if (psAR->m_bFixed)
    {
        return (KTgE_FAIL);
    };
    return (tgCM_UTS_AR_Internal_ReAllocate( psAR, uiCount ));
}


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