mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Fix #586 segfault: Update the charmaps hashmap when an existing charmap is resized
This commit is contained in:
@@ -33,6 +33,16 @@ typedef struct HashMapEntry *HashMap[HASHMAP_NB_BUCKETS];
|
||||
*/
|
||||
bool hash_AddElement(HashMap map, char const *key, void *element);
|
||||
|
||||
/**
|
||||
* Replaces an element with an already-present key in a hashmap.
|
||||
* @warning Inserting a NULL will make `hash_GetElement`'s return ambiguous!
|
||||
* @param map The HashMap to replace the element in
|
||||
* @param key The key with which the element will be stored and retrieved
|
||||
* @param element The element to replace
|
||||
* @return True if the element was found and replaced
|
||||
*/
|
||||
bool hash_ReplaceElement(HashMap const map, char const *key, void *element);
|
||||
|
||||
/**
|
||||
* Removes an element from a hashmap.
|
||||
* @param map The HashMap to remove the element from
|
||||
|
||||
@@ -173,6 +173,7 @@ void charmap_Add(char *mapping, uint8_t value)
|
||||
if (currentCharmap->usedNodes == currentCharmap->capacity) {
|
||||
currentCharmap->capacity *= 2;
|
||||
currentCharmap = resizeCharmap(currentCharmap, currentCharmap->capacity);
|
||||
hash_ReplaceElement(charmaps, currentCharmap->name, currentCharmap);
|
||||
}
|
||||
|
||||
/* Switch to and init new node */
|
||||
|
||||
@@ -64,6 +64,22 @@ bool hash_AddElement(HashMap map, char const *key, void *element)
|
||||
return newEntry->next != NULL;
|
||||
}
|
||||
|
||||
bool hash_ReplaceElement(HashMap const map, char const *key, void *element)
|
||||
{
|
||||
HashType hashedKey = hash(key);
|
||||
struct HashMapEntry *ptr = map[(HalfHashType)hashedKey];
|
||||
|
||||
while (ptr) {
|
||||
if (hashedKey >> HALF_HASH_NB_BITS == ptr->hash
|
||||
&& !strcmp(ptr->key, key)) {
|
||||
ptr->content = element;
|
||||
return true;
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool hash_RemoveElement(HashMap map, char const *key)
|
||||
{
|
||||
HashType hashedKey = hash(key);
|
||||
|
||||
Reference in New Issue
Block a user