Add key to bucket, make iteratorFunc in HashMapIterate take it.

This commit is contained in:
Jordan Bancino 2022-07-25 16:26:30 -04:00
parent a85e4771e0
commit 580b036d26
3 changed files with 11 additions and 8 deletions

View file

@ -7,6 +7,7 @@
typedef struct HashMapBucket
{
uint32_t hash;
char *key;
void *value;
} HashMapBucket;
@ -211,7 +212,7 @@ HashMapGet(HashMap * map, const char *key)
}
void
HashMapIterate(HashMap * map, void (*iteratorFunc) (void *))
HashMapIterate(HashMap * map, void (*iteratorFunc) (char *, void *))
{
size_t i;
@ -226,7 +227,7 @@ HashMapIterate(HashMap * map, void (*iteratorFunc) (void *))
if (bucket)
{
iteratorFunc(bucket->value);
iteratorFunc(bucket->key, bucket->value);
}
}
}
@ -244,7 +245,7 @@ HashMapMaxLoadSet(HashMap * map, float load)
void *
HashMapSet(HashMap * map, const char *key, void *value)
HashMapSet(HashMap * map, char *key, void *value)
{
uint32_t hash;
size_t index;
@ -275,6 +276,7 @@ HashMapSet(HashMap * map, const char *key, void *value)
}
bucket->hash = hash;
bucket->key = key;
bucket->value = value;
map->entries[index] = bucket;
map->count++;
@ -284,6 +286,7 @@ HashMapSet(HashMap * map, const char *key, void *value)
if (!bucket->hash)
{
bucket->hash = hash;
bucket->key = key;
bucket->value = value;
break;
}

View file

@ -49,7 +49,7 @@ extern void
* use HashMapDelete.
*/
extern void *
HashMapSet(HashMap * map, const char *key, void *value);
HashMapSet(HashMap * map, char *key, void *value);
/*
* HashMapGet: Get the value for the given key.
@ -70,7 +70,7 @@ extern void *
HashMapDelete(HashMap * map, const char *key);
extern void
HashMapIterate(HashMap * map, void (*iteratorFunc) (void *));
HashMapIterate(HashMap * map, void (*iteratorFunc) (char *, void *));
/*
* HashMapFree: Free the hash map, returning its memory to the operating

View file

@ -63,10 +63,10 @@ extern JsonValue *
extern void *
JsonValueFree(JsonValue * value);
extern char *
JsonEncode(HashMap * object);
extern int
JsonEncode(HashMap * object, FILE * out);
extern HashMap *
JsonDecode(char *string);
JsonDecode(FILE * in);
#endif