mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
hashmap: add hash_GetNode
This commit is contained in:
committed by
Eldred Habert
parent
08bdbd1949
commit
f97663aa37
@@ -51,6 +51,14 @@ bool hash_ReplaceElement(HashMap const map, char const *key, void *element);
|
|||||||
*/
|
*/
|
||||||
bool hash_RemoveElement(HashMap map, char const *key);
|
bool hash_RemoveElement(HashMap map, char const *key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds an element in a hashmap, and returns a pointer to its value field.
|
||||||
|
* @param map The map to consider the elements of
|
||||||
|
* @param key The key to search an element for
|
||||||
|
* @return A pointer to the pointer to the element, or NULL if not found.
|
||||||
|
*/
|
||||||
|
void **hash_GetNode(HashMap const map, char const *key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds an element in a hashmap.
|
* Finds an element in a hashmap.
|
||||||
* @param map The map to consider the elements of
|
* @param map The map to consider the elements of
|
||||||
|
|||||||
@@ -66,18 +66,14 @@ bool hash_AddElement(HashMap map, char const *key, void *element)
|
|||||||
|
|
||||||
bool hash_ReplaceElement(HashMap const map, char const *key, void *element)
|
bool hash_ReplaceElement(HashMap const map, char const *key, void *element)
|
||||||
{
|
{
|
||||||
HashType hashedKey = hash(key);
|
void **node = hash_GetNode(map, key);
|
||||||
struct HashMapEntry *ptr = map[(HalfHashType)hashedKey];
|
|
||||||
|
|
||||||
while (ptr) {
|
if (node) {
|
||||||
if (hashedKey >> HALF_HASH_NB_BITS == ptr->hash
|
*node = element;
|
||||||
&& !strcmp(ptr->key, key)) {
|
|
||||||
ptr->content = element;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
} else {
|
||||||
ptr = ptr->next;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hash_RemoveElement(HashMap map, char const *key)
|
bool hash_RemoveElement(HashMap map, char const *key)
|
||||||
@@ -99,7 +95,7 @@ bool hash_RemoveElement(HashMap map, char const *key)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *hash_GetElement(HashMap const map, char const *key)
|
void **hash_GetNode(HashMap const map, char const *key)
|
||||||
{
|
{
|
||||||
HashType hashedKey = hash(key);
|
HashType hashedKey = hash(key);
|
||||||
struct HashMapEntry *ptr = map[(HalfHashType)hashedKey];
|
struct HashMapEntry *ptr = map[(HalfHashType)hashedKey];
|
||||||
@@ -107,13 +103,20 @@ void *hash_GetElement(HashMap const map, char const *key)
|
|||||||
while (ptr) {
|
while (ptr) {
|
||||||
if (hashedKey >> HALF_HASH_NB_BITS == ptr->hash
|
if (hashedKey >> HALF_HASH_NB_BITS == ptr->hash
|
||||||
&& !strcmp(ptr->key, key)) {
|
&& !strcmp(ptr->key, key)) {
|
||||||
return ptr->content;
|
return &ptr->content;
|
||||||
}
|
}
|
||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *hash_GetElement(HashMap const map, char const *key)
|
||||||
|
{
|
||||||
|
void **node = hash_GetNode(map, key);
|
||||||
|
|
||||||
|
return node ? *node : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
void hash_ForEach(HashMap const map, void (*func)(void *, void *), void *arg)
|
void hash_ForEach(HashMap const map, void (*func)(void *, void *), void *arg)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < HASHMAP_NB_BUCKETS; i++) {
|
for (size_t i = 0; i < HASHMAP_NB_BUCKETS; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user