Compare commits
6 Commits
c44dd0bee3
...
9f906bfb83
Author | SHA1 | Date | |
---|---|---|---|
9f906bfb83 | |||
9dac7bcc73 | |||
be504fc942 | |||
10ea3a7ce8 | |||
61699c7b97 | |||
66a1af511b |
66
pl_json.h
66
pl_json.h
@ -13,8 +13,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// library version (format XYYZZ)
|
// library version (format XYYZZ)
|
||||||
#define PL_JSON_VERSION "1.0.1"
|
#define PL_JSON_VERSION "1.0.0"
|
||||||
#define PL_JSON_VERSION_NUM 10001
|
#define PL_JSON_VERSION_NUM 10000
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Index of this file:
|
Index of this file:
|
||||||
@ -68,6 +68,7 @@ plJsonObject* pl_json_member_by_index(plJsonObject*, uint32_t uIndex);
|
|||||||
void pl_json_member_list (plJsonObject*, char** pcListOut, uint32_t* puSizeOut, uint32_t* puLength);
|
void pl_json_member_list (plJsonObject*, char** pcListOut, uint32_t* puSizeOut, uint32_t* puLength);
|
||||||
bool pl_json_member_exist (plJsonObject*, const char* pcName);
|
bool pl_json_member_exist (plJsonObject*, const char* pcName);
|
||||||
plJsonType pl_json_get_type (plJsonObject*);
|
plJsonType pl_json_get_type (plJsonObject*);
|
||||||
|
const char* pl_json_get_name (plJsonObject*);
|
||||||
|
|
||||||
// retrieve and cast values (default used if member isn't present)
|
// retrieve and cast values (default used if member isn't present)
|
||||||
int pl_json_int_member (plJsonObject*, const char* pcName, int iDefaultValue);
|
int pl_json_int_member (plJsonObject*, const char* pcName, int iDefaultValue);
|
||||||
@ -114,12 +115,12 @@ void pl_json_add_bool_member (plJsonObject*, const char* pcName, bool);
|
|||||||
void pl_json_add_string_member(plJsonObject*, const char* pcName, const char*);
|
void pl_json_add_string_member(plJsonObject*, const char* pcName, const char*);
|
||||||
|
|
||||||
// arrays
|
// arrays
|
||||||
void pl_json_add_int_array (plJsonObject*, const char* pcName, int*, uint32_t uCount);
|
void pl_json_add_int_array (plJsonObject*, const char* pcName, const int*, uint32_t uCount);
|
||||||
void pl_json_add_uint_array (plJsonObject*, const char* pcName, uint32_t*, uint32_t uCount);
|
void pl_json_add_uint_array (plJsonObject*, const char* pcName, const uint32_t*, uint32_t uCount);
|
||||||
void pl_json_add_float_array (plJsonObject*, const char* pcName, float*, uint32_t uCount);
|
void pl_json_add_float_array (plJsonObject*, const char* pcName, const float*, uint32_t uCount);
|
||||||
void pl_json_add_double_array(plJsonObject*, const char* pcName, double*, uint32_t uCount);
|
void pl_json_add_double_array(plJsonObject*, const char* pcName, const double*, uint32_t uCount);
|
||||||
void pl_json_add_bool_array (plJsonObject*, const char* pcName, bool*, uint32_t uCount);
|
void pl_json_add_bool_array (plJsonObject*, const char* pcName, const bool*, uint32_t uCount);
|
||||||
void pl_json_add_string_array(plJsonObject*, const char* pcName, char**, uint32_t uCount);
|
void pl_json_add_string_array(plJsonObject*, const char* pcName, const char**, uint32_t uCount);
|
||||||
|
|
||||||
// objects & object arrays
|
// objects & object arrays
|
||||||
plJsonObject* pl_json_add_member (plJsonObject*, const char* pcName); // returns object to be modified with above commands
|
plJsonObject* pl_json_add_member (plJsonObject*, const char* pcName); // returns object to be modified with above commands
|
||||||
@ -144,7 +145,7 @@ enum plJsonType_
|
|||||||
|
|
||||||
#ifdef PL_JSON_IMPLEMENTATION
|
#ifdef PL_JSON_IMPLEMENTATION
|
||||||
|
|
||||||
#ifndef PL_GLTF_EXTENSION_H
|
#ifndef CGLTF_IMPLEMENTATION
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// [SECTION] jsmn.h
|
// [SECTION] jsmn.h
|
||||||
@ -709,7 +710,7 @@ static void pl__check_json_object(plJsonObject* ptJson, uint32_t* puBuffer
|
|||||||
plJsonObject*
|
plJsonObject*
|
||||||
pl_json_new_root_object(const char* pcName)
|
pl_json_new_root_object(const char* pcName)
|
||||||
{
|
{
|
||||||
plJsonObject* ptJson = PL_JSON_ALLOC(sizeof(plJsonObject));
|
plJsonObject* ptJson = (plJsonObject*)PL_JSON_ALLOC(sizeof(plJsonObject));
|
||||||
memset(ptJson, 0, sizeof(plJsonObject));
|
memset(ptJson, 0, sizeof(plJsonObject));
|
||||||
ptJson->tType = PL_JSON_TYPE_OBJECT;
|
ptJson->tType = PL_JSON_TYPE_OBJECT;
|
||||||
ptJson->ptRootObject = ptJson;
|
ptJson->ptRootObject = ptJson;
|
||||||
@ -757,9 +758,14 @@ pl_load_json(const char* pcJson, plJsonObject** pptJsonOut)
|
|||||||
uint32_t uLayer = 0;
|
uint32_t uLayer = 0;
|
||||||
uint32_t uCurrentTokenIndex = 0;
|
uint32_t uCurrentTokenIndex = 0;
|
||||||
plJsonObject** sbtObjectStack = NULL;
|
plJsonObject** sbtObjectStack = NULL;
|
||||||
*pptJsonOut = PL_JSON_ALLOC(sizeof(plJsonObject));
|
*pptJsonOut = (plJsonObject*)PL_JSON_ALLOC(sizeof(plJsonObject));
|
||||||
memset(*pptJsonOut, 0, sizeof(plJsonObject));
|
memset(*pptJsonOut, 0, sizeof(plJsonObject));
|
||||||
plJsonObject* ptJsonOut = *pptJsonOut;
|
plJsonObject* ptJsonOut = *pptJsonOut;
|
||||||
|
if(ptJsonOut == NULL)
|
||||||
|
{
|
||||||
|
pl_sb_json_free(sbtTokens);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
ptJsonOut->ptRootObject = ptJsonOut;
|
ptJsonOut->ptRootObject = ptJsonOut;
|
||||||
pl_sb_json_reserve(ptJsonOut->sbcBuffer, strlen(pcJson));
|
pl_sb_json_reserve(ptJsonOut->sbcBuffer, strlen(pcJson));
|
||||||
ptJsonOut->tType = pl__get_json_token_object_type(pcJson, &sbtTokens[uCurrentTokenIndex]);
|
ptJsonOut->tType = pl__get_json_token_object_type(pcJson, &sbtTokens[uCurrentTokenIndex]);
|
||||||
@ -791,19 +797,19 @@ pl_load_json(const char* pcJson, plJsonObject** pptJsonOut)
|
|||||||
if(ptParentObject->tType == PL_JSON_TYPE_ARRAY)
|
if(ptParentObject->tType == PL_JSON_TYPE_ARRAY)
|
||||||
{
|
{
|
||||||
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
|
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
|
||||||
pl_sb_json_resize(ptJsonOut->sbcBuffer, uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1);
|
pl_sb_json_resize(ptJsonOut->sbcBuffer, (uint32_t)(uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1));
|
||||||
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
|
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
|
||||||
pl_sb_json_push(ptParentObject->sbuValueOffsets, uBufferLocation);
|
pl_sb_json_push(ptParentObject->sbuValueOffsets, uBufferLocation);
|
||||||
pl_sb_json_push(ptParentObject->sbuValueLength, ptCurrentToken->end - ptCurrentToken->start);
|
pl_sb_json_push(ptParentObject->sbuValueLength, (uint32_t)(ptCurrentToken->end - ptCurrentToken->start));
|
||||||
ptParentObject->uChildrenFound++;
|
ptParentObject->uChildrenFound++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
|
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
|
||||||
pl_sb_json_resize(ptJsonOut->sbcBuffer, uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1);
|
pl_sb_json_resize(ptJsonOut->sbcBuffer, (uint32_t)(uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1));
|
||||||
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
|
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
|
||||||
ptParentObject->uValueOffset = uBufferLocation;
|
ptParentObject->uValueOffset = uBufferLocation;
|
||||||
ptParentObject->uValueLength = ptCurrentToken->end - ptCurrentToken->start;
|
ptParentObject->uValueLength = (uint32_t)(ptCurrentToken->end - ptCurrentToken->start);
|
||||||
ptParentObject->uChildrenFound++;
|
ptParentObject->uChildrenFound++;
|
||||||
pl_sb_json_pop(sbtObjectStack);
|
pl_sb_json_pop(sbtObjectStack);
|
||||||
}
|
}
|
||||||
@ -817,19 +823,19 @@ pl_load_json(const char* pcJson, plJsonObject** pptJsonOut)
|
|||||||
if(ptParentObject->tType == PL_JSON_TYPE_ARRAY)
|
if(ptParentObject->tType == PL_JSON_TYPE_ARRAY)
|
||||||
{
|
{
|
||||||
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
|
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
|
||||||
pl_sb_json_resize(ptJsonOut->sbcBuffer, uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1);
|
pl_sb_json_resize(ptJsonOut->sbcBuffer, (uint32_t)(uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1));
|
||||||
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
|
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
|
||||||
pl_sb_json_push(ptParentObject->sbuValueOffsets, uBufferLocation);
|
pl_sb_json_push(ptParentObject->sbuValueOffsets, uBufferLocation);
|
||||||
pl_sb_json_push(ptParentObject->sbuValueLength, ptCurrentToken->end - ptCurrentToken->start);
|
pl_sb_json_push(ptParentObject->sbuValueLength, (uint32_t)(ptCurrentToken->end - ptCurrentToken->start));
|
||||||
ptParentObject->uChildrenFound++;
|
ptParentObject->uChildrenFound++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
|
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
|
||||||
pl_sb_json_resize(ptJsonOut->sbcBuffer, uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1);
|
pl_sb_json_resize(ptJsonOut->sbcBuffer, (uint32_t)(uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1));
|
||||||
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
|
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
|
||||||
ptParentObject->uValueOffset = uBufferLocation;
|
ptParentObject->uValueOffset = uBufferLocation;
|
||||||
ptParentObject->uValueLength = ptCurrentToken->end - ptCurrentToken->start;
|
ptParentObject->uValueLength = (uint32_t)(ptCurrentToken->end - ptCurrentToken->start);
|
||||||
ptParentObject->uChildrenFound++;
|
ptParentObject->uChildrenFound++;
|
||||||
pl_sb_json_pop(sbtObjectStack);
|
pl_sb_json_pop(sbtObjectStack);
|
||||||
}
|
}
|
||||||
@ -1042,6 +1048,12 @@ pl_json_get_type(plJsonObject* ptJson)
|
|||||||
return ptJson->tType;
|
return ptJson->tType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char*
|
||||||
|
pl_json_get_name(plJsonObject* ptJson)
|
||||||
|
{
|
||||||
|
return ptJson->acName;
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
pl_json_member_exist(plJsonObject* ptJson, const char* pcName)
|
pl_json_member_exist(plJsonObject* ptJson, const char* pcName)
|
||||||
{
|
{
|
||||||
@ -1535,7 +1547,7 @@ pl_json_add_member_array(plJsonObject* ptJson, const char* pcName, uint32_t uSiz
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pl_json_add_int_array(plJsonObject* ptJson, const char* pcName, int* piValues, uint32_t uSize)
|
pl_json_add_int_array(plJsonObject* ptJson, const char* pcName, const int* piValues, uint32_t uSize)
|
||||||
{
|
{
|
||||||
ptJson->uChildCount++;
|
ptJson->uChildCount++;
|
||||||
ptJson->uChildrenFound++;
|
ptJson->uChildrenFound++;
|
||||||
@ -1569,7 +1581,7 @@ pl_json_add_int_array(plJsonObject* ptJson, const char* pcName, int* piValues, u
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pl_json_add_uint_array(plJsonObject* ptJson, const char* pcName, uint32_t* puValues, uint32_t uSize)
|
pl_json_add_uint_array(plJsonObject* ptJson, const char* pcName, const uint32_t* puValues, uint32_t uSize)
|
||||||
{
|
{
|
||||||
ptJson->uChildCount++;
|
ptJson->uChildCount++;
|
||||||
ptJson->uChildrenFound++;
|
ptJson->uChildrenFound++;
|
||||||
@ -1603,7 +1615,7 @@ pl_json_add_uint_array(plJsonObject* ptJson, const char* pcName, uint32_t* puVal
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pl_json_add_float_array(plJsonObject* ptJson, const char* pcName, float* pfValues, uint32_t uSize)
|
pl_json_add_float_array(plJsonObject* ptJson, const char* pcName, const float* pfValues, uint32_t uSize)
|
||||||
{
|
{
|
||||||
ptJson->uChildCount++;
|
ptJson->uChildCount++;
|
||||||
ptJson->uChildrenFound++;
|
ptJson->uChildrenFound++;
|
||||||
@ -1637,7 +1649,7 @@ pl_json_add_float_array(plJsonObject* ptJson, const char* pcName, float* pfValue
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pl_json_add_double_array(plJsonObject* ptJson, const char* pcName, double* pdValues, uint32_t uSize)
|
pl_json_add_double_array(plJsonObject* ptJson, const char* pcName, const double* pdValues, uint32_t uSize)
|
||||||
{
|
{
|
||||||
ptJson->uChildCount++;
|
ptJson->uChildCount++;
|
||||||
ptJson->uChildrenFound++;
|
ptJson->uChildrenFound++;
|
||||||
@ -1671,7 +1683,7 @@ pl_json_add_double_array(plJsonObject* ptJson, const char* pcName, double* pdVal
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pl_json_add_bool_array(plJsonObject* ptJson, const char* pcName, bool* pbValues, uint32_t uSize)
|
pl_json_add_bool_array(plJsonObject* ptJson, const char* pcName, const bool* pbValues, uint32_t uSize)
|
||||||
{
|
{
|
||||||
ptJson->uChildCount++;
|
ptJson->uChildCount++;
|
||||||
ptJson->uChildrenFound++;
|
ptJson->uChildrenFound++;
|
||||||
@ -1705,7 +1717,7 @@ pl_json_add_bool_array(plJsonObject* ptJson, const char* pcName, bool* pbValues,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pl_json_add_string_array(plJsonObject* ptJson, const char* pcName, char** ppcBuffer, uint32_t uSize)
|
pl_json_add_string_array(plJsonObject* ptJson, const char* pcName, const char** ppcBuffer, uint32_t uSize)
|
||||||
{
|
{
|
||||||
ptJson->uChildCount++;
|
ptJson->uChildCount++;
|
||||||
ptJson->uChildrenFound++;
|
ptJson->uChildrenFound++;
|
||||||
@ -1860,7 +1872,7 @@ pl__write_json_object(plJsonObject* ptJson, char* pcBuffer, uint32_t* puBufferSi
|
|||||||
|
|
||||||
const char* pcPrevChar = &ptJson->ptRootObject->sbcBuffer[ptJson->sbuValueOffsets[i]];
|
const char* pcPrevChar = &ptJson->ptRootObject->sbcBuffer[ptJson->sbuValueOffsets[i]];
|
||||||
char cPreviousChar = ' ';
|
char cPreviousChar = ' ';
|
||||||
if(pcPrevChar)
|
// if(pcPrevChar)
|
||||||
{
|
{
|
||||||
const char* pcPrevCharAddr = pcPrevChar - 1;
|
const char* pcPrevCharAddr = pcPrevChar - 1;
|
||||||
cPreviousChar = pcPrevCharAddr[0];
|
cPreviousChar = pcPrevCharAddr[0];
|
||||||
@ -2000,7 +2012,7 @@ pl__check_json_object(plJsonObject* ptJson, uint32_t* puBufferSize, uint32_t* pu
|
|||||||
|
|
||||||
const char* pcPrevChar = &ptJson->ptRootObject->sbcBuffer[ptJson->sbuValueOffsets[i]];
|
const char* pcPrevChar = &ptJson->ptRootObject->sbcBuffer[ptJson->sbuValueOffsets[i]];
|
||||||
char cPreviousChar = ' ';
|
char cPreviousChar = ' ';
|
||||||
if(pcPrevChar)
|
// if(pcPrevChar)
|
||||||
{
|
{
|
||||||
const char* pcPrevCharAddr = pcPrevChar - 1;
|
const char* pcPrevCharAddr = pcPrevChar - 1;
|
||||||
cPreviousChar = pcPrevCharAddr[0];
|
cPreviousChar = pcPrevCharAddr[0];
|
||||||
|
4
pl_log.h
4
pl_log.h
@ -13,8 +13,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// library version (format XYYZZ)
|
// library version (format XYYZZ)
|
||||||
#define PL_LOG_VERSION "1.0.2"
|
#define PL_LOG_VERSION "1.0.0"
|
||||||
#define PL_LOG_VERSION_NUM 10002
|
#define PL_LOG_VERSION_NUM 10000
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Index of this file:
|
Index of this file:
|
||||||
|
193
pl_math.h
193
pl_math.h
@ -14,8 +14,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// library version (format XYYZZ)
|
// library version (format XYYZZ)
|
||||||
#define PL_MATH_VERSION "1.1.0"
|
#define PL_MATH_VERSION "1.0.0"
|
||||||
#define PL_MATH_VERSION_NUM 10100
|
#define PL_MATH_VERSION_NUM 10000
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Index of this file:
|
Index of this file:
|
||||||
@ -30,6 +30,7 @@ Index of this file:
|
|||||||
// [SECTION] matrix ops
|
// [SECTION] matrix ops
|
||||||
// [SECTION] quaternion ops
|
// [SECTION] quaternion ops
|
||||||
// [SECTION] rect ops
|
// [SECTION] rect ops
|
||||||
|
// [SECTION] aabb ops
|
||||||
// [SECTION] colors
|
// [SECTION] colors
|
||||||
// [SECTION] implementations
|
// [SECTION] implementations
|
||||||
*/
|
*/
|
||||||
@ -40,19 +41,28 @@ Index of this file:
|
|||||||
|
|
||||||
#ifndef PL_MATH_INC
|
#ifndef PL_MATH_INC
|
||||||
#define PL_MATH_INC
|
#define PL_MATH_INC
|
||||||
#define PL_MATH_DEFINED
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// [SECTION] forward declarations & basic types
|
// [SECTION] forward declarations & basic types
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
// forward declarations
|
// general math
|
||||||
typedef union _plVec2 plVec2;
|
typedef union _plVec2 plVec2;
|
||||||
typedef union _plVec3 plVec3;
|
typedef union _plVec3 plVec3;
|
||||||
typedef union _plVec4 plVec4;
|
typedef union _plVec4 plVec4;
|
||||||
|
typedef union _plMat3 plMat3;
|
||||||
typedef union _plMat4 plMat4;
|
typedef union _plMat4 plMat4;
|
||||||
typedef struct _plRect plRect;
|
|
||||||
typedef struct _plAABB plAABB;
|
// geometric primitives
|
||||||
|
typedef struct _plRect plRect;
|
||||||
|
typedef struct _plAABB plAABB;
|
||||||
|
typedef struct _plPlane plPlane;
|
||||||
|
typedef struct _plSphere plSphere;
|
||||||
|
typedef struct _plBox plBox;
|
||||||
|
typedef struct _plCone plCone;
|
||||||
|
typedef struct _plRay plRay;
|
||||||
|
typedef struct _plCylinder plCylinder;
|
||||||
|
typedef struct _plCapsule plCapsule;
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// [SECTION] defines
|
// [SECTION] defines
|
||||||
@ -141,6 +151,23 @@ typedef union _plVec4
|
|||||||
float d[4];
|
float d[4];
|
||||||
} plVec4;
|
} plVec4;
|
||||||
|
|
||||||
|
typedef union _plMat3
|
||||||
|
{
|
||||||
|
plVec3 col[3];
|
||||||
|
struct {
|
||||||
|
float x11;
|
||||||
|
float x21;
|
||||||
|
float x31;
|
||||||
|
float x12;
|
||||||
|
float x22;
|
||||||
|
float x32;
|
||||||
|
float x13;
|
||||||
|
float x23;
|
||||||
|
float x33;
|
||||||
|
};
|
||||||
|
float d[9];
|
||||||
|
} plMat3;
|
||||||
|
|
||||||
typedef union _plMat4
|
typedef union _plMat4
|
||||||
{
|
{
|
||||||
plVec4 col[4];
|
plVec4 col[4];
|
||||||
@ -177,6 +204,51 @@ typedef struct _plAABB
|
|||||||
plVec3 tMax;
|
plVec3 tMax;
|
||||||
} plAABB;
|
} plAABB;
|
||||||
|
|
||||||
|
typedef struct _plPlane
|
||||||
|
{
|
||||||
|
float fOffset; // opposite of direction
|
||||||
|
plVec3 tDirection; // normal
|
||||||
|
} plPlane;
|
||||||
|
|
||||||
|
typedef struct _plSphere
|
||||||
|
{
|
||||||
|
float fRadius;
|
||||||
|
plVec3 tCenter;
|
||||||
|
} plSphere;
|
||||||
|
|
||||||
|
typedef struct _plBox
|
||||||
|
{
|
||||||
|
plMat4 tTransform;
|
||||||
|
plVec3 tHalfSize;
|
||||||
|
} plBox;
|
||||||
|
|
||||||
|
typedef struct _plCone
|
||||||
|
{
|
||||||
|
plVec3 tBasePos;
|
||||||
|
plVec3 tTipPos;
|
||||||
|
float fRadius;
|
||||||
|
} plCone;
|
||||||
|
|
||||||
|
typedef struct _plCylinder
|
||||||
|
{
|
||||||
|
plVec3 tBasePos;
|
||||||
|
plVec3 tTipPos;
|
||||||
|
float fRadius;
|
||||||
|
} plCylinder;
|
||||||
|
|
||||||
|
typedef struct _plCapsule
|
||||||
|
{
|
||||||
|
plVec3 tBasePos;
|
||||||
|
plVec3 tTipPos;
|
||||||
|
float fRadius;
|
||||||
|
} plCapsule;
|
||||||
|
|
||||||
|
typedef struct _plRay
|
||||||
|
{
|
||||||
|
plVec3 tOrigin;
|
||||||
|
plVec3 tDirection;
|
||||||
|
} plRay;
|
||||||
|
|
||||||
#endif // PL_MATH_INC
|
#endif // PL_MATH_INC
|
||||||
|
|
||||||
#if defined(PL_MATH_INCLUDE_FUNCTIONS) && !defined(PL_MATH_INCLUDE_FUNCTIONS_H)
|
#if defined(PL_MATH_INCLUDE_FUNCTIONS) && !defined(PL_MATH_INCLUDE_FUNCTIONS_H)
|
||||||
@ -203,6 +275,8 @@ typedef struct _plAABB
|
|||||||
#define pl_create_vec2(XARG, YARG) {(XARG), (YARG)}
|
#define pl_create_vec2(XARG, YARG) {(XARG), (YARG)}
|
||||||
#define pl_create_vec3(XARG, YARG, ZARG) {(XARG), (YARG), (ZARG)}
|
#define pl_create_vec3(XARG, YARG, ZARG) {(XARG), (YARG), (ZARG)}
|
||||||
#define pl_create_vec4(XARG, YARG, ZARG, WARG) {(XARG), (YARG), (ZARG), (WARG)}
|
#define pl_create_vec4(XARG, YARG, ZARG, WARG) {(XARG), (YARG), (ZARG), (WARG)}
|
||||||
|
#define pl_create_mat3_diag(XARG, YARG, ZARG) {(XARG), 0.0, 0.0f, 0.0f, (YARG), 0.0f, 0.0f, 0.0f, (ZARG)}
|
||||||
|
#define pl_create_mat3_cols(XARG, YARG, ZARG) {(XARG).x, (XARG).y, (XARG).z, (YARG).x, (YARG).y, (YARG).z, (ZARG).x, (ZARG).y, (ZARG).z}
|
||||||
#define pl_create_mat4_diag(XARG, YARG, ZARG, WARG) {(XARG), 0.0, 0.0f, 0.0f, 0.0f, (YARG), 0.0f, 0.0f, 0.0f, 0.0f, (ZARG), 0.0f, 0.0f, 0.0f, 0.0f, (WARG)}
|
#define pl_create_mat4_diag(XARG, YARG, ZARG, WARG) {(XARG), 0.0, 0.0f, 0.0f, 0.0f, (YARG), 0.0f, 0.0f, 0.0f, 0.0f, (ZARG), 0.0f, 0.0f, 0.0f, 0.0f, (WARG)}
|
||||||
#define pl_create_mat4_cols(XARG, YARG, ZARG, WARG) {(XARG).x, (XARG).y, (XARG).z, (XARG).w, (YARG).x, (YARG).y, (YARG).z, (YARG).w, (ZARG).x, (ZARG).y, (ZARG).z, (ZARG).w, (WARG).x, (WARG).y, (WARG).z, (WARG).w}
|
#define pl_create_mat4_cols(XARG, YARG, ZARG, WARG) {(XARG).x, (XARG).y, (XARG).z, (XARG).w, (YARG).x, (YARG).y, (YARG).z, (YARG).w, (ZARG).x, (ZARG).y, (ZARG).z, (ZARG).w, (WARG).x, (WARG).y, (WARG).z, (WARG).w}
|
||||||
#define pl_create_rect_vec2(XARG, YARG) {(XARG), (YARG)}
|
#define pl_create_rect_vec2(XARG, YARG) {(XARG), (YARG)}
|
||||||
@ -211,6 +285,8 @@ typedef struct _plAABB
|
|||||||
#define pl_create_vec2(XARG, YARG) (plVec2){(XARG), (YARG)}
|
#define pl_create_vec2(XARG, YARG) (plVec2){(XARG), (YARG)}
|
||||||
#define pl_create_vec3(XARG, YARG, ZARG) (plVec3){(XARG), (YARG), (ZARG)}
|
#define pl_create_vec3(XARG, YARG, ZARG) (plVec3){(XARG), (YARG), (ZARG)}
|
||||||
#define pl_create_vec4(XARG, YARG, ZARG, WARG) (plVec4){(XARG), (YARG), (ZARG), (WARG)}
|
#define pl_create_vec4(XARG, YARG, ZARG, WARG) (plVec4){(XARG), (YARG), (ZARG), (WARG)}
|
||||||
|
#define pl_create_mat3_diag(XARG, YARG, ZARG) (plMat3){.x11 = (XARG), .x22 = (YARG), .x33 = (ZARG)}
|
||||||
|
#define pl_create_mat3_cols(XARG, YARG, ZARG) (plMat3){.col[0] = (XARG), .col[1] = (YARG), .col[2] = (ZARG)}
|
||||||
#define pl_create_mat4_diag(XARG, YARG, ZARG, WARG) (plMat4){.x11 = (XARG), .x22 = (YARG), .x33 = (ZARG), .x44 = (WARG)}
|
#define pl_create_mat4_diag(XARG, YARG, ZARG, WARG) (plMat4){.x11 = (XARG), .x22 = (YARG), .x33 = (ZARG), .x44 = (WARG)}
|
||||||
#define pl_create_mat4_cols(XARG, YARG, ZARG, WARG) (plMat4){.col[0] = (XARG), .col[1] = (YARG), .col[2] = (ZARG), .col[3] = (WARG)}
|
#define pl_create_mat4_cols(XARG, YARG, ZARG, WARG) (plMat4){.col[0] = (XARG), .col[1] = (YARG), .col[2] = (ZARG), .col[3] = (WARG)}
|
||||||
#define pl_create_rect_vec2(XARG, YARG) (plRect){.tMin = (XARG), .tMax = (YARG)}
|
#define pl_create_rect_vec2(XARG, YARG) (plRect){.tMin = (XARG), .tMax = (YARG)}
|
||||||
@ -319,7 +395,18 @@ static inline plVec4 pl_norm_vec4(plVec4 tVec) { float fLength = pl_length_vec4(
|
|||||||
// [SECTION] matrix ops
|
// [SECTION] matrix ops
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
// general ops
|
// general ops 3x3
|
||||||
|
static inline float pl_mat3_get (const plMat3* ptMat, int iRow, int iCol) { return ptMat->col[iCol].d[iRow];}
|
||||||
|
static inline void pl_mat3_set (plMat3* ptMat, int iRow, int iCol, float fValue) { ptMat->col[iCol].d[iRow] = fValue;}
|
||||||
|
static inline plMat3 pl_identity_mat3 (void) { return pl_create_mat3_diag(1.0f, 1.0f, 1.0f);}
|
||||||
|
static inline plMat3 pl_mat3_transpose (const plMat3* ptMat) { plMat3 tResult = {0}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) pl_mat3_set(&tResult, i, j, pl_mat3_get(ptMat, j, i)); return tResult;}
|
||||||
|
static inline plMat3 pl_mat3_invert (const plMat3* ptMat);
|
||||||
|
static inline plMat3 pl_mul_scalarf_mat3(float fLeft, const plMat3* ptRight) { plMat3 tResult = {0}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) pl_mat3_set(&tResult, i, j, fLeft * pl_mat3_get(ptRight, j, i)); return tResult;}
|
||||||
|
static inline plVec3 pl_mul_mat3_vec3 (const plMat3* ptLeft, plVec3 tRight);
|
||||||
|
static inline plMat3 pl_mul_mat3 (const plMat3* ptLeft, const plMat3* ptRight);
|
||||||
|
static inline plMat3 pl_add_mat3 (const plMat3* ptLeft, const plMat3* ptRight);
|
||||||
|
|
||||||
|
// general ops 4x4
|
||||||
static inline float pl_mat4_get (const plMat4* ptMat, int iRow, int iCol) { return ptMat->col[iCol].d[iRow];}
|
static inline float pl_mat4_get (const plMat4* ptMat, int iRow, int iCol) { return ptMat->col[iCol].d[iRow];}
|
||||||
static inline void pl_mat4_set (plMat4* ptMat, int iRow, int iCol, float fValue) { ptMat->col[iCol].d[iRow] = fValue;}
|
static inline void pl_mat4_set (plMat4* ptMat, int iRow, int iCol, float fValue) { ptMat->col[iCol].d[iRow] = fValue;}
|
||||||
static inline plMat4 pl_identity_mat4 (void) { return pl_create_mat4_diag(1.0f, 1.0f, 1.0f, 1.0f);}
|
static inline plMat4 pl_identity_mat4 (void) { return pl_create_mat4_diag(1.0f, 1.0f, 1.0f, 1.0f);}
|
||||||
@ -329,6 +416,8 @@ static inline plMat4 pl_mul_scalarf_mat4(float fLeft, const plMat4* ptRight)
|
|||||||
static inline plVec3 pl_mul_mat4_vec3 (const plMat4* ptLeft, plVec3 tRight);
|
static inline plVec3 pl_mul_mat4_vec3 (const plMat4* ptLeft, plVec3 tRight);
|
||||||
static inline plVec4 pl_mul_mat4_vec4 (const plMat4* ptLeft, plVec4 tRight);
|
static inline plVec4 pl_mul_mat4_vec4 (const plMat4* ptLeft, plVec4 tRight);
|
||||||
static inline plMat4 pl_mul_mat4 (const plMat4* ptLeft, const plMat4* ptRight);
|
static inline plMat4 pl_mul_mat4 (const plMat4* ptLeft, const plMat4* ptRight);
|
||||||
|
static inline plMat4 pl_add_mat4 (const plMat4* ptLeft, const plMat4* ptRight);
|
||||||
|
static inline plMat4 pl_mul_mat4_3 (const plMat4* ptLeft, const plMat4* ptMiddle, const plMat4* ptRight);
|
||||||
|
|
||||||
// translation, rotation, scaling
|
// translation, rotation, scaling
|
||||||
static inline plMat4 pl_mat4_translate_xyz (float fX, float fY, float fZ) { plMat4 tResult = pl_create_mat4_diag(1.0f, 1.0f, 1.0f, 1.0f); tResult.x14 = fX; tResult.x24 = fY; tResult.x34 = fZ; return tResult;}
|
static inline plMat4 pl_mat4_translate_xyz (float fX, float fY, float fZ) { plMat4 tResult = pl_create_mat4_diag(1.0f, 1.0f, 1.0f, 1.0f); tResult.x14 = fX; tResult.x24 = fY; tResult.x34 = fZ; return tResult;}
|
||||||
@ -391,6 +480,14 @@ static inline plRect pl_rect_move_start (const plRect* ptRect, float fX, floa
|
|||||||
static inline plRect pl_rect_move_start_x (const plRect* ptRect, float fX) { const plRect tResult = { { fX, ptRect->tMin.y}, { fX + ptRect->tMax.x - ptRect->tMin.x, ptRect->tMax.y} }; return tResult;}
|
static inline plRect pl_rect_move_start_x (const plRect* ptRect, float fX) { const plRect tResult = { { fX, ptRect->tMin.y}, { fX + ptRect->tMax.x - ptRect->tMin.x, ptRect->tMax.y} }; return tResult;}
|
||||||
static inline plRect pl_rect_move_start_y (const plRect* ptRect, float fY) { const plRect tResult = {{ ptRect->tMin.x, fY}, { ptRect->tMax.x, fY + ptRect->tMax.y - ptRect->tMin.y}}; return tResult;}
|
static inline plRect pl_rect_move_start_y (const plRect* ptRect, float fY) { const plRect tResult = {{ ptRect->tMin.x, fY}, { ptRect->tMax.x, fY + ptRect->tMax.y - ptRect->tMin.y}}; return tResult;}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// [SECTION] aabb ops
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static inline plAABB pl_aabb_merge (const plAABB* tA, const plAABB* tB) { plAABB tResult = {pl_min_vec3(tA->tMin, tB->tMin),pl_max_vec3(tA->tMax, tB->tMax)}; return tResult; }
|
||||||
|
static inline plVec3 pl_aabb_half_width(const plAABB* tA) { return pl_create_vec3(0.5f * (tA->tMax.x - tA->tMin.x), 0.5f * (tA->tMax.y - tA->tMin.y), 0.5f * (tA->tMax.z - tA->tMin.z)); }
|
||||||
|
static inline plVec3 pl_aabb_center (const plAABB* tA) { return pl_create_vec3(0.5f * (tA->tMax.x + tA->tMin.x), 0.5f * (tA->tMax.y + tA->tMin.y), 0.5f * (tA->tMax.z + tA->tMin.z)); }
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// [SECTION] colors
|
// [SECTION] colors
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@ -417,6 +514,15 @@ static inline plRect pl_rect_move_start_y (const plRect* ptRect, float fY)
|
|||||||
// [SECTION] implementations
|
// [SECTION] implementations
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static inline plVec3
|
||||||
|
pl_mul_mat3_vec3(const plMat3* ptLeft, plVec3 tRight)
|
||||||
|
{
|
||||||
|
float fX = ptLeft->col[0].d[0] * tRight.x + ptLeft->col[1].d[0] * tRight.y + ptLeft->col[2].d[0] * tRight.z;
|
||||||
|
float fY = ptLeft->col[0].d[1] * tRight.x + ptLeft->col[1].d[1] * tRight.y + ptLeft->col[2].d[1] * tRight.z;
|
||||||
|
float fZ = ptLeft->col[0].d[2] * tRight.x + ptLeft->col[1].d[2] * tRight.y + ptLeft->col[2].d[2] * tRight.z;
|
||||||
|
return pl_create_vec3(fX, fY, fZ);
|
||||||
|
}
|
||||||
|
|
||||||
static inline plVec3
|
static inline plVec3
|
||||||
pl_mul_mat4_vec3(const plMat4* ptLeft, plVec3 tRight)
|
pl_mul_mat4_vec3(const plMat4* ptLeft, plVec3 tRight)
|
||||||
{
|
{
|
||||||
@ -450,6 +556,38 @@ pl_mul_mat4_vec4(const plMat4* ptLeft, plVec4 tRight)
|
|||||||
return pl_add_vec4(Add0, Add1);
|
return pl_add_vec4(Add0, Add1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline plMat3
|
||||||
|
pl_mul_mat3(const plMat3* ptLeft, const plMat3* ptRight)
|
||||||
|
{
|
||||||
|
plMat3 tResult;
|
||||||
|
|
||||||
|
// row 0
|
||||||
|
tResult.x11 = ptLeft->col[0].d[0] * ptRight->col[0].d[0] + ptLeft->col[1].d[0] * ptRight->col[0].d[1] + ptLeft->col[2].d[0] * ptRight->col[0].d[2];
|
||||||
|
tResult.x12 = ptLeft->col[0].d[0] * ptRight->col[1].d[0] + ptLeft->col[1].d[0] * ptRight->col[1].d[1] + ptLeft->col[2].d[0] * ptRight->col[1].d[2];
|
||||||
|
tResult.x13 = ptLeft->col[0].d[0] * ptRight->col[2].d[0] + ptLeft->col[1].d[0] * ptRight->col[2].d[1] + ptLeft->col[2].d[0] * ptRight->col[2].d[2];
|
||||||
|
|
||||||
|
// row 1
|
||||||
|
tResult.x21 = ptLeft->col[0].d[1] * ptRight->col[0].d[0] + ptLeft->col[1].d[1] * ptRight->col[0].d[1] + ptLeft->col[2].d[1] * ptRight->col[0].d[2];
|
||||||
|
tResult.x22 = ptLeft->col[0].d[1] * ptRight->col[1].d[0] + ptLeft->col[1].d[1] * ptRight->col[1].d[1] + ptLeft->col[2].d[1] * ptRight->col[1].d[2];
|
||||||
|
tResult.x23 = ptLeft->col[0].d[1] * ptRight->col[2].d[0] + ptLeft->col[1].d[1] * ptRight->col[2].d[1] + ptLeft->col[2].d[1] * ptRight->col[2].d[2];
|
||||||
|
|
||||||
|
// row 2
|
||||||
|
tResult.x31 = ptLeft->col[0].d[2] * ptRight->col[0].d[0] + ptLeft->col[1].d[2] * ptRight->col[0].d[1] + ptLeft->col[2].d[2] * ptRight->col[0].d[2];
|
||||||
|
tResult.x32 = ptLeft->col[0].d[2] * ptRight->col[1].d[0] + ptLeft->col[1].d[2] * ptRight->col[1].d[1] + ptLeft->col[2].d[2] * ptRight->col[1].d[2];
|
||||||
|
tResult.x33 = ptLeft->col[0].d[2] * ptRight->col[2].d[0] + ptLeft->col[1].d[2] * ptRight->col[2].d[1] + ptLeft->col[2].d[2] * ptRight->col[2].d[2];
|
||||||
|
|
||||||
|
return tResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline plMat3
|
||||||
|
pl_add_mat3(const plMat3* ptLeft, const plMat3* ptRight)
|
||||||
|
{
|
||||||
|
plMat3 tResult;
|
||||||
|
for(uint32_t i = 0; i < 9; i++)
|
||||||
|
tResult.d[i] = ptLeft->d[i] + ptRight->d[i];
|
||||||
|
return tResult;
|
||||||
|
}
|
||||||
|
|
||||||
static inline plMat4
|
static inline plMat4
|
||||||
pl_mul_mat4(const plMat4* ptLeft, const plMat4* ptRight)
|
pl_mul_mat4(const plMat4* ptLeft, const plMat4* ptRight)
|
||||||
{
|
{
|
||||||
@ -482,6 +620,22 @@ pl_mul_mat4(const plMat4* ptLeft, const plMat4* ptRight)
|
|||||||
return tResult;
|
return tResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline plMat4
|
||||||
|
pl_mul_mat4_3(const plMat4* ptLeft, const plMat4* ptMiddle, const plMat4* ptRight)
|
||||||
|
{
|
||||||
|
plMat4 tIntermediateMatrix = pl_mul_mat4(ptMiddle, ptRight);
|
||||||
|
return pl_mul_mat4(ptLeft, &tIntermediateMatrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline plMat4
|
||||||
|
pl_add_mat4(const plMat4* ptLeft, const plMat4* ptRight)
|
||||||
|
{
|
||||||
|
plMat4 tResult;
|
||||||
|
for(uint32_t i = 0; i < 16; i++)
|
||||||
|
tResult.d[i] = ptLeft->d[i] + ptRight->d[i];
|
||||||
|
return tResult;
|
||||||
|
}
|
||||||
|
|
||||||
static inline plMat4
|
static inline plMat4
|
||||||
pl_mat4_rotate_vec3(float fAngle, plVec3 tVec)
|
pl_mat4_rotate_vec3(float fAngle, plVec3 tVec)
|
||||||
{
|
{
|
||||||
@ -514,6 +668,31 @@ pl_mat4_rotate_vec3(float fAngle, plVec3 tVec)
|
|||||||
tM.col[3]);
|
tM.col[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline plMat3
|
||||||
|
pl_mat3_invert(const plMat3* ptMat)
|
||||||
|
{
|
||||||
|
const plVec3 tA = ptMat->col[0];
|
||||||
|
const plVec3 tB = ptMat->col[1];
|
||||||
|
const plVec3 tC = ptMat->col[2];
|
||||||
|
|
||||||
|
plVec3 tR0 = pl_cross_vec3(tB, tC);
|
||||||
|
plVec3 tR1 = pl_cross_vec3(tC, tA);
|
||||||
|
plVec3 tR2 = pl_cross_vec3(tA, tB);
|
||||||
|
float fInvDet = 1.0f / pl_dot_vec3(tR2, tC);
|
||||||
|
|
||||||
|
plMat3 tResult;
|
||||||
|
tResult.x11 = tR0.x * fInvDet;
|
||||||
|
tResult.x21 = tR1.x * fInvDet;
|
||||||
|
tResult.x31 = tR2.x * fInvDet;
|
||||||
|
tResult.x12 = tR0.y * fInvDet;
|
||||||
|
tResult.x22 = tR1.y * fInvDet;
|
||||||
|
tResult.x32 = tR2.y * fInvDet;
|
||||||
|
tResult.x13 = tR0.z * fInvDet;
|
||||||
|
tResult.x23 = tR1.z * fInvDet;
|
||||||
|
tResult.x33 = tR2.z * fInvDet;
|
||||||
|
return tResult;
|
||||||
|
}
|
||||||
|
|
||||||
static inline plMat4
|
static inline plMat4
|
||||||
pl_mat4_invert(const plMat4* ptMat)
|
pl_mat4_invert(const plMat4* ptMat)
|
||||||
{
|
{
|
||||||
|
18
pl_memory.h
18
pl_memory.h
@ -125,12 +125,12 @@ void pl_pool_allocator_free (plPoolAllocator*, void* pItem);
|
|||||||
|
|
||||||
typedef struct _plTempAllocator
|
typedef struct _plTempAllocator
|
||||||
{
|
{
|
||||||
size_t szSize;
|
size_t szSize; // size in bytes of current working buffer
|
||||||
size_t szOffset;
|
size_t szOffset; // offset into pcBuffer
|
||||||
char* pcBuffer;
|
char* pcBuffer; // current working buffer (starts as acStackBuffer)
|
||||||
char acStackBuffer[PL_MEMORY_TEMP_STACK_SIZE];
|
char acStackBuffer[PL_MEMORY_TEMP_STACK_SIZE];
|
||||||
char** ppcMemoryBlocks;
|
char** ppcMemoryBlocks;
|
||||||
size_t szMemoryBlockCount;
|
size_t szMemoryBlockCount; // current number working heap alloc blocks
|
||||||
size_t szMemoryBlockCapacity;
|
size_t szMemoryBlockCapacity;
|
||||||
size_t szCurrentBlockSizes;
|
size_t szCurrentBlockSizes;
|
||||||
size_t szNextBlockSizes;
|
size_t szNextBlockSizes;
|
||||||
@ -347,6 +347,7 @@ pl_temp_allocator_alloc(plTempAllocator* ptAllocator, size_t szSize)
|
|||||||
ptAllocator->ppcMemoryBlocks = (char**)PL_MEMORY_ALLOC(sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1));
|
ptAllocator->ppcMemoryBlocks = (char**)PL_MEMORY_ALLOC(sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1));
|
||||||
memset(ptAllocator->ppcMemoryBlocks, 0, (sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1)));
|
memset(ptAllocator->ppcMemoryBlocks, 0, (sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1)));
|
||||||
memcpy(ptAllocator->ppcMemoryBlocks, ppcOldBlocks, sizeof(char*) * ptAllocator->szMemoryBlockCapacity);
|
memcpy(ptAllocator->ppcMemoryBlocks, ppcOldBlocks, sizeof(char*) * ptAllocator->szMemoryBlockCapacity);
|
||||||
|
PL_MEMORY_FREE(ppcOldBlocks);
|
||||||
ptAllocator->szMemoryBlockCapacity++;
|
ptAllocator->szMemoryBlockCapacity++;
|
||||||
ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount] = (char*)PL_MEMORY_ALLOC(szNewBlockSize);
|
ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount] = (char*)PL_MEMORY_ALLOC(szNewBlockSize);
|
||||||
ptAllocator->szSize = szNewBlockSize;
|
ptAllocator->szSize = szNewBlockSize;
|
||||||
@ -369,7 +370,10 @@ pl_temp_allocator_alloc(plTempAllocator* ptAllocator, size_t szSize)
|
|||||||
ptAllocator->ppcMemoryBlocks = (char**)PL_MEMORY_ALLOC(sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1));
|
ptAllocator->ppcMemoryBlocks = (char**)PL_MEMORY_ALLOC(sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1));
|
||||||
memset(ptAllocator->ppcMemoryBlocks, 0, (sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1)));
|
memset(ptAllocator->ppcMemoryBlocks, 0, (sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1)));
|
||||||
memcpy(ptAllocator->ppcMemoryBlocks, ppcOldBlocks, sizeof(char*) * ptAllocator->szMemoryBlockCapacity);
|
memcpy(ptAllocator->ppcMemoryBlocks, ppcOldBlocks, sizeof(char*) * ptAllocator->szMemoryBlockCapacity);
|
||||||
|
PL_MEMORY_FREE(ppcOldBlocks);
|
||||||
ptAllocator->szMemoryBlockCapacity++;
|
ptAllocator->szMemoryBlockCapacity++;
|
||||||
|
if(ptAllocator->szMemoryBlockCount == 0) // special case
|
||||||
|
PL_MEMORY_FREE(ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount]);
|
||||||
ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount] = (char*)PL_MEMORY_ALLOC(szNewBlockSize);
|
ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount] = (char*)PL_MEMORY_ALLOC(szNewBlockSize);
|
||||||
ptAllocator->szSize = szNewBlockSize;
|
ptAllocator->szSize = szNewBlockSize;
|
||||||
ptAllocator->pcBuffer = ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount];
|
ptAllocator->pcBuffer = ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount];
|
||||||
@ -434,9 +438,9 @@ pl__temp_allocator_sprintf_va(plTempAllocator* ptAllocator, const char* cPFormat
|
|||||||
|
|
||||||
pRequestedMemory = pl_temp_allocator_alloc(ptAllocator, n + 1);
|
pRequestedMemory = pl_temp_allocator_alloc(ptAllocator, n + 1);
|
||||||
memset(pRequestedMemory, 0, n + 1);
|
memset(pRequestedMemory, 0, n + 1);
|
||||||
pl_vnsprintf(pRequestedMemory, n + 1, cPFormat, args);
|
pl_vnsprintf((char*)pRequestedMemory, n + 1, cPFormat, args);
|
||||||
|
|
||||||
return pRequestedMemory;
|
return (char*)pRequestedMemory;
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
char*
|
||||||
@ -449,7 +453,7 @@ pl_temp_allocator_sprintf(plTempAllocator* ptAllocator, const char* cPFormat, ..
|
|||||||
pRequestedMemory = pl__temp_allocator_sprintf_va(ptAllocator, cPFormat, argptr);
|
pRequestedMemory = pl__temp_allocator_sprintf_va(ptAllocator, cPFormat, argptr);
|
||||||
va_end(argptr);
|
va_end(argptr);
|
||||||
|
|
||||||
return pRequestedMemory;
|
return (char*)pRequestedMemory;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -15,8 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// library version (format XYYZZ)
|
// library version (format XYYZZ)
|
||||||
#define PL_STRING_VERSION "1.1.0"
|
#define PL_STRING_VERSION "1.0.0"
|
||||||
#define PL_STRING_VERSION_NUM 10100
|
#define PL_STRING_VERSION_NUM 10000
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Index of this file:
|
Index of this file:
|
||||||
|
@ -17,8 +17,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// library version (format XYYZZ)
|
// library version (format XYYZZ)
|
||||||
#define PL_TEST_VERSION "1.0.1"
|
#define PL_TEST_VERSION "1.0.0"
|
||||||
#define PL_TEST_VERSION_NUM 10001
|
#define PL_TEST_VERSION_NUM 10000
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Index of this file:
|
Index of this file:
|
||||||
|
@ -9,101 +9,309 @@
|
|||||||
void
|
void
|
||||||
hashmap_test_0(void* pData)
|
hashmap_test_0(void* pData)
|
||||||
{
|
{
|
||||||
plHashMap* ptHashMap = NULL;
|
plHashMap tHashMap = {0};
|
||||||
|
|
||||||
int* sbiValues = NULL;
|
int* sbiValues = NULL;
|
||||||
pl_sb_push(sbiValues, 0);
|
pl_sb_push(sbiValues, 0);
|
||||||
pl_sb_push(sbiValues, 69);
|
pl_sb_push(sbiValues, 907);
|
||||||
pl_hm_insert(ptHashMap, pl_hm_hash_str("Dirty Number"), pl_sb_size(sbiValues) - 1);
|
pl_hm_insert(&tHashMap, pl_hm_hash_str("Dirty Number", 0), pl_sb_size(sbiValues) - 1);
|
||||||
pl_sb_push(sbiValues, 117);
|
pl_sb_push(sbiValues, 117);
|
||||||
pl_hm_insert(ptHashMap, pl_hm_hash_str("Spartan Number"), pl_sb_size(sbiValues) - 1);
|
pl_hm_insert(&tHashMap, pl_hm_hash_str("Spartan Number", 0), pl_sb_size(sbiValues) - 1);
|
||||||
|
|
||||||
for(uint32_t i = 0; i < 3000; i++)
|
for(uint32_t i = 0; i < 3000; i++)
|
||||||
{
|
{
|
||||||
pl_sb_push(sbiValues, i);
|
pl_sb_push(sbiValues, i);
|
||||||
pl_hm_insert(ptHashMap, pl_hm_hash("Spartan Number2", strlen("Spartan Number2"), i), pl_sb_size(sbiValues) - 1);
|
pl_hm_insert(&tHashMap, pl_hm_hash("Spartan Number2", strlen("Spartan Number2"), i), pl_sb_size(sbiValues) - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
pl_test_expect_int_equal(sbiValues[pl_hm_lookup(ptHashMap, pl_hm_hash_str("Dirty Number"))], 69, NULL);
|
pl_test_expect_int_equal(sbiValues[pl_hm_lookup(&tHashMap, pl_hm_hash_str("Dirty Number", 0))], 907, NULL);
|
||||||
pl_test_expect_int_equal(sbiValues[pl_hm_lookup(ptHashMap, pl_hm_hash_str("Spartan Number"))], 117, NULL);
|
pl_test_expect_int_equal(sbiValues[pl_hm_lookup(&tHashMap, pl_hm_hash_str("Spartan Number", 0))], 117, NULL);
|
||||||
|
|
||||||
pl_hm_remove(ptHashMap, pl_hm_hash_str("Dirty Number"));
|
pl_hm_remove(&tHashMap, pl_hm_hash_str("Dirty Number", 0));
|
||||||
|
|
||||||
uint64_t ulFreeIndex = pl_hm_get_free_index(ptHashMap);
|
uint64_t ulFreeIndex = pl_hm_get_free_index(&tHashMap);
|
||||||
if(ulFreeIndex == UINT64_MAX)
|
if(ulFreeIndex == PL_DS_HASH_INVALID)
|
||||||
{
|
{
|
||||||
pl_sb_add(sbiValues);
|
pl_sb_add(sbiValues);
|
||||||
ulFreeIndex = pl_sb_size(sbiValues) - 1;
|
ulFreeIndex = pl_sb_size(sbiValues) - 1;
|
||||||
}
|
}
|
||||||
sbiValues[ulFreeIndex] = 666999;
|
sbiValues[ulFreeIndex] = 123689;
|
||||||
pl_hm_insert(ptHashMap, pl_hm_hash_str("Extra dirty number"), ulFreeIndex);
|
pl_hm_insert(&tHashMap, pl_hm_hash_str("Extra dirty number", 0), ulFreeIndex);
|
||||||
pl_test_expect_int_equal(sbiValues[pl_hm_lookup(ptHashMap, pl_hm_hash_str("Extra dirty number"))], 666999, NULL);
|
pl_test_expect_int_equal(sbiValues[pl_hm_lookup(&tHashMap, pl_hm_hash_str("Extra dirty number", 0))], 123689, NULL);
|
||||||
|
|
||||||
pl_test_expect_int_equal(sbiValues[pl_hm_lookup(ptHashMap, pl_hm_hash_str("Extra dirty number"))], 666999, NULL);
|
pl_test_expect_int_equal(sbiValues[pl_hm_lookup(&tHashMap, pl_hm_hash_str("Extra dirty number", 0))], 123689, NULL);
|
||||||
pl_test_expect_int_equal(sbiValues[pl_hm_lookup(ptHashMap, pl_hm_hash_str("Spartan Number"))], 117, NULL);
|
pl_test_expect_int_equal(sbiValues[pl_hm_lookup(&tHashMap, pl_hm_hash_str("Spartan Number", 0))], 117, NULL);
|
||||||
|
|
||||||
pl_hm_free(ptHashMap);
|
pl_hm_free(&tHashMap);
|
||||||
pl_sb_free(sbiValues);
|
pl_sb_free(sbiValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
hashmap_test_1(void* pData)
|
hashmap_test_1(void* pData)
|
||||||
{
|
{
|
||||||
plHashMap* ptHashMap = NULL;
|
plHashMap tHashMap = {0};
|
||||||
|
|
||||||
pl_hm_insert(ptHashMap, pl_hm_hash_str("Dirty Number"), 69);
|
pl_hm_insert(&tHashMap, pl_hm_hash_str("Dirty Number", 0), 945);
|
||||||
pl_hm_insert(ptHashMap, pl_hm_hash_str("Spartan Number"), 117);
|
pl_hm_insert(&tHashMap, pl_hm_hash_str("Spartan Number", 0), 117);
|
||||||
|
|
||||||
pl_test_expect_int_equal((int)pl_hm_lookup(ptHashMap, pl_hm_hash_str("Dirty Number")), 69, NULL);
|
pl_test_expect_int_equal((int)pl_hm_lookup(&tHashMap, pl_hm_hash_str("Dirty Number", 0)), 945, NULL);
|
||||||
pl_test_expect_int_equal((int)pl_hm_lookup(ptHashMap, pl_hm_hash_str("Spartan Number")), 117, NULL);
|
pl_test_expect_int_equal((int)pl_hm_lookup(&tHashMap, pl_hm_hash_str("Spartan Number", 0)), 117, NULL);
|
||||||
|
|
||||||
pl_hm_free(ptHashMap);
|
pl_hm_free(&tHashMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
hashmap_test_2(void* pData)
|
hashmap_test_2(void* pData)
|
||||||
{
|
{
|
||||||
plHashMap* ptHashMap = NULL;
|
plHashMap tHashMap = {0};
|
||||||
|
|
||||||
int* sbiValues = NULL;
|
int* sbiValues = NULL;
|
||||||
pl_sb_push(sbiValues, 0);
|
pl_sb_push(sbiValues, 0);
|
||||||
pl_sb_push(sbiValues, 69);
|
pl_sb_push(sbiValues, 945);
|
||||||
pl_hm_insert_str(ptHashMap, "Dirty Number", pl_sb_size(sbiValues) - 1);
|
pl_hm_insert_str(&tHashMap, "Dirty Number", pl_sb_size(sbiValues) - 1);
|
||||||
pl_sb_push(sbiValues, 117);
|
pl_sb_push(sbiValues, 117);
|
||||||
pl_hm_insert_str(ptHashMap, "Spartan Number", pl_sb_size(sbiValues) - 1);
|
pl_hm_insert_str(&tHashMap, "Spartan Number", pl_sb_size(sbiValues) - 1);
|
||||||
|
|
||||||
for(uint32_t i = 0; i < 79; i++)
|
for(uint32_t i = 0; i < 79; i++)
|
||||||
{
|
{
|
||||||
pl_sb_push(sbiValues, 118);
|
pl_sb_push(sbiValues, 118);
|
||||||
pl_hm_insert_str(ptHashMap, "Spartan Number2", pl_sb_size(sbiValues) - 1);
|
pl_hm_insert_str(&tHashMap, "Spartan Number2", pl_sb_size(sbiValues) - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
pl_test_expect_int_equal(sbiValues[pl_hm_lookup_str(ptHashMap, "Dirty Number")], 69, NULL);
|
pl_test_expect_int_equal(sbiValues[pl_hm_lookup_str(&tHashMap, "Dirty Number")], 945, NULL);
|
||||||
pl_test_expect_int_equal(sbiValues[pl_hm_lookup_str(ptHashMap, "Spartan Number")], 117, NULL);
|
pl_test_expect_int_equal(sbiValues[pl_hm_lookup_str(&tHashMap, "Spartan Number")], 117, NULL);
|
||||||
|
|
||||||
pl_hm_remove_str(ptHashMap, "Dirty Number");
|
pl_hm_remove_str(&tHashMap, "Dirty Number");
|
||||||
|
|
||||||
uint64_t ulFreeIndex = pl_hm_get_free_index(ptHashMap);
|
uint64_t ulFreeIndex = pl_hm_get_free_index(&tHashMap);
|
||||||
if(ulFreeIndex == UINT64_MAX)
|
if(ulFreeIndex == PL_DS_HASH_INVALID)
|
||||||
{
|
{
|
||||||
pl_sb_add(sbiValues);
|
pl_sb_add(sbiValues);
|
||||||
ulFreeIndex = pl_sb_size(sbiValues) - 1;
|
ulFreeIndex = pl_sb_size(sbiValues) - 1;
|
||||||
}
|
}
|
||||||
sbiValues[ulFreeIndex] = 666999;
|
sbiValues[ulFreeIndex] = 945945;
|
||||||
pl_hm_insert_str(ptHashMap, "Extra dirty number", ulFreeIndex);
|
pl_hm_insert_str(&tHashMap, "Extra dirty number", ulFreeIndex);
|
||||||
pl_test_expect_int_equal(sbiValues[pl_hm_lookup_str(ptHashMap, "Extra dirty number")], 666999, NULL);
|
pl_test_expect_int_equal(sbiValues[pl_hm_lookup_str(&tHashMap, "Extra dirty number")], 945945, NULL);
|
||||||
|
|
||||||
pl_test_expect_int_equal(sbiValues[pl_hm_lookup_str(ptHashMap, "Extra dirty number")], 666999, NULL);
|
pl_test_expect_int_equal(sbiValues[pl_hm_lookup_str(&tHashMap, "Extra dirty number")], 945945, NULL);
|
||||||
pl_test_expect_int_equal(sbiValues[pl_hm_lookup_str(ptHashMap, "Spartan Number")], 117, NULL);
|
pl_test_expect_int_equal(sbiValues[pl_hm_lookup_str(&tHashMap, "Spartan Number")], 117, NULL);
|
||||||
|
|
||||||
pl_hm_free(ptHashMap);
|
pl_hm_free(&tHashMap);
|
||||||
pl_sb_free(sbiValues);
|
pl_sb_free(sbiValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
hashmap_test_3(void* pData)
|
||||||
|
{
|
||||||
|
plHashMap tHashMap = {0};
|
||||||
|
|
||||||
|
// test empty map
|
||||||
|
pl_test_expect_uint32_equal(pl_hm_size(&tHashMap), 0, NULL);
|
||||||
|
|
||||||
|
pl_hm_insert(&tHashMap, 417, 117);
|
||||||
|
pl_test_expect_uint32_equal(pl_hm_size(&tHashMap), 1, NULL);
|
||||||
|
|
||||||
|
// test removal
|
||||||
|
pl_hm_remove(&tHashMap, 417);
|
||||||
|
pl_test_expect_uint32_equal(pl_hm_size(&tHashMap), 0, NULL);
|
||||||
|
|
||||||
|
// test collisions
|
||||||
|
const uint32_t uMask = tHashMap._uBucketCapacity - 1; // assumes bucket count is power of 2
|
||||||
|
uint32_t uBucketIndex = 417 & uMask;
|
||||||
|
uint32_t uBucketIndex0 = (417 + tHashMap._uBucketCapacity) & uMask;
|
||||||
|
|
||||||
|
pl_hm_insert(&tHashMap, 417, 117);
|
||||||
|
pl_hm_insert(&tHashMap, 417 + tHashMap._uBucketCapacity, 118);
|
||||||
|
pl_hm_insert(&tHashMap, 417 + tHashMap._uBucketCapacity * 2, 119);
|
||||||
|
|
||||||
|
pl_test_expect_uint64_equal(pl_hm_lookup(&tHashMap, 417), 117, NULL);
|
||||||
|
pl_test_expect_uint64_equal(pl_hm_lookup(&tHashMap, 417 + tHashMap._uBucketCapacity), 118, NULL);
|
||||||
|
pl_test_expect_uint64_equal(pl_hm_lookup(&tHashMap, 417 + 2 * tHashMap._uBucketCapacity), 119, NULL);
|
||||||
|
|
||||||
|
pl_hm_remove(&tHashMap, 417 + tHashMap._uBucketCapacity * 2);
|
||||||
|
pl_test_expect_uint64_equal(pl_hm_lookup(&tHashMap, 417 + 2 * tHashMap._uBucketCapacity), UINT64_MAX, NULL);
|
||||||
|
pl_hm_remove(&tHashMap, 417);
|
||||||
|
pl_test_expect_uint64_equal(pl_hm_lookup(&tHashMap, 417), UINT64_MAX, NULL);
|
||||||
|
pl_hm_remove(&tHashMap, 417 + tHashMap._uBucketCapacity);
|
||||||
|
pl_test_expect_uint64_equal(pl_hm_lookup(&tHashMap, 417 + tHashMap._uBucketCapacity), UINT64_MAX, NULL);
|
||||||
|
|
||||||
|
pl_test_expect_uint32_equal(pl_hm_size(&tHashMap), 0, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
shashmap_test_0(void* pData)
|
||||||
|
{
|
||||||
|
uint64_t auKeys[1024] = {0};
|
||||||
|
uint32_t auValues[1024] = {0};
|
||||||
|
plHashMapStatic32 tHashMap = {
|
||||||
|
.auKeys = auKeys,
|
||||||
|
.auValueBucket = auValues,
|
||||||
|
.uBucketCount = 1024
|
||||||
|
};
|
||||||
|
|
||||||
|
pl_hms_clear32(&tHashMap);
|
||||||
|
|
||||||
|
int* sbiValues = NULL;
|
||||||
|
pl_sb_push(sbiValues, 0);
|
||||||
|
pl_sb_push(sbiValues, 907);
|
||||||
|
pl_hms_set_str32(&tHashMap, "Dirty Number", pl_sb_size(sbiValues) - 1);
|
||||||
|
pl_sb_push(sbiValues, 117);
|
||||||
|
pl_hms_set_str32(&tHashMap, "Spartan Number", pl_sb_size(sbiValues) - 1);
|
||||||
|
|
||||||
|
for(uint32_t i = 0; i < 256; i++)
|
||||||
|
{
|
||||||
|
pl_sb_push(sbiValues, i);
|
||||||
|
pl_hms_set32(&tHashMap, pl_hm_hash("Spartan Number2", strlen("Spartan Number2"), i), pl_sb_size(sbiValues) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hms_get_str32(&tHashMap, "Dirty Number")], 907, NULL);
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hms_get_str32(&tHashMap, "Spartan Number")], 117, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
hashmap32_test_0(void* pData)
|
||||||
|
{
|
||||||
|
plHashMap32 tHashMap = {0};
|
||||||
|
|
||||||
|
int* sbiValues = NULL;
|
||||||
|
pl_sb_push(sbiValues, 0);
|
||||||
|
pl_sb_push(sbiValues, 907);
|
||||||
|
pl_hm32_insert(&tHashMap, pl_hm_hash_str("Dirty Number", 0), pl_sb_size(sbiValues) - 1);
|
||||||
|
pl_sb_push(sbiValues, 117);
|
||||||
|
pl_hm32_insert(&tHashMap, pl_hm_hash_str("Spartan Number", 0), pl_sb_size(sbiValues) - 1);
|
||||||
|
|
||||||
|
for(uint32_t i = 0; i < 3000; i++)
|
||||||
|
{
|
||||||
|
pl_sb_push(sbiValues, i);
|
||||||
|
pl_hm32_insert(&tHashMap, pl_hm_hash("Spartan Number2", strlen("Spartan Number2"), i), pl_sb_size(sbiValues) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hm32_lookup(&tHashMap, pl_hm_hash_str("Dirty Number", 0))], 907, NULL);
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hm32_lookup(&tHashMap, pl_hm_hash_str("Spartan Number", 0))], 117, NULL);
|
||||||
|
|
||||||
|
pl_hm32_remove(&tHashMap, pl_hm_hash_str("Dirty Number", 0));
|
||||||
|
|
||||||
|
uint32_t ulFreeIndex = pl_hm32_get_free_index(&tHashMap);
|
||||||
|
if(ulFreeIndex == PL_DS_HASH32_INVALID)
|
||||||
|
{
|
||||||
|
pl_sb_add(sbiValues);
|
||||||
|
ulFreeIndex = pl_sb_size(sbiValues) - 1;
|
||||||
|
}
|
||||||
|
sbiValues[ulFreeIndex] = 123689;
|
||||||
|
pl_hm32_insert(&tHashMap, pl_hm_hash_str("Extra dirty number", 0), ulFreeIndex);
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hm32_lookup(&tHashMap, pl_hm_hash_str("Extra dirty number", 0))], 123689, NULL);
|
||||||
|
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hm32_lookup(&tHashMap, pl_hm_hash_str("Extra dirty number", 0))], 123689, NULL);
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hm32_lookup(&tHashMap, pl_hm_hash_str("Spartan Number", 0))], 117, NULL);
|
||||||
|
|
||||||
|
pl_hm32_free(&tHashMap);
|
||||||
|
pl_sb_free(sbiValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
hashmap32_test_1(void* pData)
|
||||||
|
{
|
||||||
|
plHashMap32 tHashMap = {0};
|
||||||
|
|
||||||
|
pl_hm32_insert(&tHashMap, pl_hm_hash_str("Dirty Number", 0), 945);
|
||||||
|
pl_hm32_insert(&tHashMap, pl_hm_hash_str("Spartan Number", 0), 117);
|
||||||
|
|
||||||
|
pl_test_expect_int_equal((int)pl_hm32_lookup(&tHashMap, pl_hm_hash_str("Dirty Number", 0)), 945, NULL);
|
||||||
|
pl_test_expect_int_equal((int)pl_hm32_lookup(&tHashMap, pl_hm_hash_str("Spartan Number", 0)), 117, NULL);
|
||||||
|
|
||||||
|
pl_hm32_free(&tHashMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
hashmap32_test_2(void* pData)
|
||||||
|
{
|
||||||
|
plHashMap32 tHashMap = {0};
|
||||||
|
|
||||||
|
int* sbiValues = NULL;
|
||||||
|
pl_sb_push(sbiValues, 0);
|
||||||
|
pl_sb_push(sbiValues, 945);
|
||||||
|
pl_hm32_insert_str(&tHashMap, "Dirty Number", pl_sb_size(sbiValues) - 1);
|
||||||
|
pl_sb_push(sbiValues, 117);
|
||||||
|
pl_hm32_insert_str(&tHashMap, "Spartan Number", pl_sb_size(sbiValues) - 1);
|
||||||
|
|
||||||
|
for(uint32_t i = 0; i < 79; i++)
|
||||||
|
{
|
||||||
|
pl_sb_push(sbiValues, 118);
|
||||||
|
pl_hm32_insert_str(&tHashMap, "Spartan Number2", pl_sb_size(sbiValues) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hm32_lookup_str(&tHashMap, "Dirty Number")], 945, NULL);
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hm32_lookup_str(&tHashMap, "Spartan Number")], 117, NULL);
|
||||||
|
|
||||||
|
pl_hm32_remove_str(&tHashMap, "Dirty Number");
|
||||||
|
|
||||||
|
uint32_t ulFreeIndex = pl_hm32_get_free_index(&tHashMap);
|
||||||
|
if(ulFreeIndex == PL_DS_HASH32_INVALID)
|
||||||
|
{
|
||||||
|
pl_sb_add(sbiValues);
|
||||||
|
ulFreeIndex = pl_sb_size(sbiValues) - 1;
|
||||||
|
}
|
||||||
|
sbiValues[ulFreeIndex] = 945945;
|
||||||
|
pl_hm32_insert_str(&tHashMap, "Extra dirty number", ulFreeIndex);
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hm32_lookup_str(&tHashMap, "Extra dirty number")], 945945, NULL);
|
||||||
|
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hm32_lookup_str(&tHashMap, "Extra dirty number")], 945945, NULL);
|
||||||
|
pl_test_expect_int_equal(sbiValues[pl_hm32_lookup_str(&tHashMap, "Spartan Number")], 117, NULL);
|
||||||
|
|
||||||
|
pl_hm32_free(&tHashMap);
|
||||||
|
pl_sb_free(sbiValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
hashmap32_test_3(void* pData)
|
||||||
|
{
|
||||||
|
plHashMap32 tHashMap = {0};
|
||||||
|
|
||||||
|
// test empty map
|
||||||
|
pl_test_expect_uint32_equal(pl_hm32_size(&tHashMap), 0, NULL);
|
||||||
|
|
||||||
|
pl_hm32_insert(&tHashMap, 417, 117);
|
||||||
|
pl_test_expect_uint32_equal(pl_hm32_size(&tHashMap), 1, NULL);
|
||||||
|
|
||||||
|
// test removal
|
||||||
|
pl_hm32_remove(&tHashMap, 417);
|
||||||
|
pl_test_expect_uint32_equal(pl_hm32_size(&tHashMap), 0, NULL);
|
||||||
|
|
||||||
|
// test collisions
|
||||||
|
const uint32_t uMask = tHashMap._uBucketCapacity - 1; // assumes bucket count is power of 2
|
||||||
|
uint32_t uBucketIndex = 417 & uMask;
|
||||||
|
uint32_t uBucketIndex0 = (417 + tHashMap._uBucketCapacity) & uMask;
|
||||||
|
|
||||||
|
pl_hm32_insert(&tHashMap, 417, 117);
|
||||||
|
pl_hm32_insert(&tHashMap, 417 + tHashMap._uBucketCapacity, 118);
|
||||||
|
pl_hm32_insert(&tHashMap, 417 + tHashMap._uBucketCapacity * 2, 119);
|
||||||
|
|
||||||
|
pl_test_expect_uint64_equal(pl_hm32_lookup(&tHashMap, 417), 117, NULL);
|
||||||
|
pl_test_expect_uint64_equal(pl_hm32_lookup(&tHashMap, 417 + tHashMap._uBucketCapacity), 118, NULL);
|
||||||
|
pl_test_expect_uint64_equal(pl_hm32_lookup(&tHashMap, 417 + 2 * tHashMap._uBucketCapacity), 119, NULL);
|
||||||
|
|
||||||
|
pl_hm32_remove(&tHashMap, 417 + tHashMap._uBucketCapacity * 2);
|
||||||
|
pl_test_expect_uint64_equal(pl_hm32_lookup(&tHashMap, 417 + 2 * tHashMap._uBucketCapacity), UINT32_MAX, NULL);
|
||||||
|
pl_hm32_remove(&tHashMap, 417);
|
||||||
|
pl_test_expect_uint64_equal(pl_hm32_lookup(&tHashMap, 417), UINT32_MAX, NULL);
|
||||||
|
pl_hm32_remove(&tHashMap, 417 + tHashMap._uBucketCapacity);
|
||||||
|
pl_test_expect_uint64_equal(pl_hm32_lookup(&tHashMap, 417 + tHashMap._uBucketCapacity), UINT32_MAX, NULL);
|
||||||
|
|
||||||
|
pl_test_expect_uint32_equal(pl_hm32_size(&tHashMap), 0, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pl_ds_tests(void* pData)
|
pl_ds_tests(void* pData)
|
||||||
{
|
{
|
||||||
pl_test_register_test(hashmap_test_0, NULL);
|
pl_test_register_test(hashmap_test_0, NULL);
|
||||||
pl_test_register_test(hashmap_test_1, NULL);
|
pl_test_register_test(hashmap_test_1, NULL);
|
||||||
pl_test_register_test(hashmap_test_2, NULL);
|
pl_test_register_test(hashmap_test_2, NULL);
|
||||||
|
pl_test_register_test(hashmap_test_3, NULL);
|
||||||
|
|
||||||
|
pl_test_register_test(shashmap_test_0, NULL);
|
||||||
|
|
||||||
|
pl_test_register_test(hashmap32_test_0, NULL);
|
||||||
|
pl_test_register_test(hashmap32_test_1, NULL);
|
||||||
|
pl_test_register_test(hashmap32_test_2, NULL);
|
||||||
|
pl_test_register_test(hashmap32_test_3, NULL);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user