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

View file

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

View file

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