1
0

feat: pl_ds.h hashmap insert can now replace item
All checks were successful
Tests / Ubuntu (push) Successful in 10s

This commit is contained in:
Jonathan Hoffstadt 2025-02-13 20:53:32 -06:00
parent 8e8da6a934
commit 0258683c74

22
pl_ds.h
View File

@ -4,8 +4,8 @@
*/ */
// library version (format XYYZZ) // library version (format XYYZZ)
#define PL_DS_VERSION "1.0.0" #define PL_DS_VERSION "1.1.0"
#define PL_DS_VERSION_NUM 10000 #define PL_DS_VERSION_NUM 10100
/* /*
Index of this file: Index of this file:
@ -176,7 +176,7 @@ COMPILE TIME OPTIONS
PL_DS_ALLOC(x) PL_DS_ALLOC(x)
PL_DS_FREE(x) PL_DS_FREE(x)
* Change initial hashmap size: * Change initial hashmap size:
PL_DS_HASHMAP_INITIAL_SIZE (default is 256) // should be power of 2 PL_DS_HASHMAP_INITIAL_SIZE (default is 1024) // should be power of 2
* Change assert by defining: * Change assert by defining:
PL_DS_ASSERT(x) PL_DS_ASSERT(x)
*/ */
@ -209,7 +209,7 @@ COMPILE TIME OPTIONS
#endif #endif
#ifndef PL_DS_HASHMAP_INITIAL_SIZE #ifndef PL_DS_HASHMAP_INITIAL_SIZE
#define PL_DS_HASHMAP_INITIAL_SIZE 256 #define PL_DS_HASHMAP_INITIAL_SIZE 1024
#endif #endif
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -536,6 +536,9 @@ pl__hm_insert(plHashMap** pptHashMap, uint64_t ulKey, uint64_t ulValue, const ch
uint64_t mask = ptHashMap->_uBucketCount - 1; uint64_t mask = ptHashMap->_uBucketCount - 1;
uint64_t ulModKey = ulKey & mask; uint64_t ulModKey = ulKey & mask;
uint64_t ulExistingKey = pl__hm_lookup(pptHashMap, ulKey);
if(ulExistingKey == UINT64_MAX)
{
while(ptHashMap->_aulKeys[ulModKey] != ulKey && ptHashMap->_aulKeys[ulModKey] != UINT64_MAX) while(ptHashMap->_aulKeys[ulModKey] != ulKey && ptHashMap->_aulKeys[ulModKey] != UINT64_MAX)
{ {
ulModKey = (ulModKey + 1) & mask; ulModKey = (ulModKey + 1) & mask;
@ -546,13 +549,22 @@ pl__hm_insert(plHashMap** pptHashMap, uint64_t ulKey, uint64_t ulValue, const ch
ptHashMap->_aulKeys[ulModKey] = ulKey; ptHashMap->_aulKeys[ulModKey] = ulKey;
ptHashMap->_aulValueIndices[ulModKey] = ulValue; ptHashMap->_aulValueIndices[ulModKey] = ulValue;
ptHashMap->_uItemCount++; ptHashMap->_uItemCount++;
}
else
{
while(ptHashMap->_aulKeys[ulModKey] != ulKey && ptHashMap->_aulKeys[ulModKey] != UINT64_MAX)
ulModKey = (ulModKey + 1) & mask;
ptHashMap->_aulValueIndices[ulModKey] = ulValue;
}
} }
static inline void static inline void
pl__hm_remove(plHashMap** pptHashMap, uint64_t ulKey) pl__hm_remove(plHashMap** pptHashMap, uint64_t ulKey)
{ {
plHashMap* ptHashMap = *pptHashMap; plHashMap* ptHashMap = *pptHashMap;
PL_DS_ASSERT(ptHashMap->_uBucketCount > 0 && "hashmap has no items"); if(ptHashMap == NULL)
return;
uint64_t mask = ptHashMap->_uBucketCount - 1; uint64_t mask = ptHashMap->_uBucketCount - 1;
uint64_t ulModKey = ulKey & mask; uint64_t ulModKey = ulKey & mask;