Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
hashMap.h
Go to the documentation of this file.
1
370#ifndef _HASH_MAP_H_
371#define _HASH_MAP_H_
372
373#include <stdbool.h>
374#include <stddef.h>
375#include <stdint.h>
376
377#include "linked_list.h"
378
385typedef uint32_t (*hashFunction_t)(const void* data);
386
390typedef bool (*eqFunction_t)(const void* a, const void* b);
391
392// Forward-declared internal structs
395
399typedef struct
400{
402 const void* key;
404 void* value;
405
409
430
431// Default hash functions
432uint32_t hashString(const void* str);
433bool strEq(const void* a, const void* b);
434
435// Helper functions for using custom key types
436uint32_t hashBytes(const uint8_t* bytes, size_t length);
437bool bytesEq(const uint8_t* a, size_t aLength, const uint8_t* b, size_t bLength);
438
439// Helper functions for using int or pointer keys
440uint32_t hashInt(const void* intKey);
441bool intsEq(const void* keyA, const void* keyB);
442
443void hashPut(hashMap_t* map, const char* key, void* value);
444void* hashGet(hashMap_t* map, const char* key);
445void* hashRemove(hashMap_t* map, const char* key);
446
447void hashPutBin(hashMap_t* map, const void* key, void* value);
448void* hashGetBin(hashMap_t* map, const void* key);
449void* hashRemoveBin(hashMap_t* map, const void* key);
450
451void hashInit(hashMap_t* map, int initialSize);
452void hashInitBin(hashMap_t* map, int initialSize, hashFunction_t hashFunc, eqFunction_t eqFunc);
453void hashDeinit(hashMap_t* map);
454
455bool hashIterate(const hashMap_t* map, hashIterator_t* iterator);
456bool hashIterRemove(hashMap_t* map, hashIterator_t* iter);
457void hashIterReset(hashIterator_t* iterator);
458
459void hashReport(const hashMap_t* map);
460
461#endif
A single element of the hash map array, holding either one value or a list of values.
Definition hashMap.c:47
The internal state of a hash map iterator.
Definition hashMap.c:66
void * value
The value of the current key-value pair.
Definition hashMap.h:404
uint32_t hashBytes(const uint8_t *bytes, size_t length)
Convert an arbitrary byte sequence to a hash value.
Definition hashMap.c:489
eqFunction_t eqFunc
The key equality function to use, or NULL to use strEq()
Definition hashMap.h:428
uint32_t(* hashFunction_t)(const void *data)
A function that takes a pointer to key data and returns its hash value.
Definition hashMap.h:385
void * hashRemove(hashMap_t *map, const char *key)
Remove the value with a given string key from the hash map.
Definition hashMap.c:582
void hashDeinit(hashMap_t *map)
Deinitialize and free all memory associated with the given hash map.
Definition hashMap.c:722
int size
The total number of allocated buckets in the hash map.
Definition hashMap.h:416
void * hashGet(hashMap_t *map, const char *key)
Return the value in the hash map associated with the given string key.
Definition hashMap.c:570
void hashPut(hashMap_t *map, const char *key, void *value)
Create or update a key-value pair in the hash map with a string key.
Definition hashMap.c:558
void hashInit(hashMap_t *map, int initialSize)
Initialize a hash map for string keys.
Definition hashMap.c:692
hashFunction_t hashFunc
The key hash function to use, or NULL to use hashString()
Definition hashMap.h:425
bool bytesEq(const uint8_t *a, size_t aLength, const uint8_t *b, size_t bLength)
Compare two byte arrays.
Definition hashMap.c:512
bool hashIterRemove(hashMap_t *map, hashIterator_t *iter)
Remove the last item returned by the iterator from the hash map.
Definition hashMap.c:902
bool hashIterate(const hashMap_t *map, hashIterator_t *iterator)
Advance the given iterator to the next item, or return false if there is no next item.
Definition hashMap.c:847
void hashReport(const hashMap_t *map)
Prints out a detailed report on the hash map state.
Definition hashMap.c:967
hashIterState_t * _state
Definition hashMap.h:407
void hashInitBin(hashMap_t *map, int initialSize, hashFunction_t hashFunc, eqFunction_t eqFunc)
Initialize a hash map for non-string keys, using the given functions for hashing and comparison.
Definition hashMap.c:709
const void * key
The key of the current key-value pair.
Definition hashMap.h:402
uint32_t hashString(const void *str)
Convert a NULL-terminated string to a hash value.
Definition hashMap.c:453
bool strEq(const void *a, const void *b)
Compare two NUL-terminated strings.
Definition hashMap.c:475
void hashPutBin(hashMap_t *map, const void *key, void *value)
Create or update a key-value pair in the hash map with a non-string key.
Definition hashMap.c:594
void * hashRemoveBin(hashMap_t *map, const void *key)
Remove the value with a given non-string key from the hash map.
Definition hashMap.c:657
int count
The actual number of items stored in the hash map.
Definition hashMap.h:419
bool(* eqFunction_t)(const void *a, const void *b)
A function that takes two pointers to keys and returns true if the values they point to are equal.
Definition hashMap.h:390
void hashIterReset(hashIterator_t *iterator)
Reset the given iterator struct, freeing any memory associated with it.
Definition hashMap.c:948
hashBucket_t * values
The array of bucket values.
Definition hashMap.h:422
uint32_t hashInt(const void *intKey)
Convert a pointer address or integral value to a hash value.
Definition hashMap.c:525
void * hashGetBin(hashMap_t *map, const void *key)
Return the value in the hash map associated with the given key.
Definition hashMap.c:636
bool intsEq(const void *keyA, const void *keyB)
Compare two void pointers based on their address, or two integer types cast to a void pointer.
Definition hashMap.c:544
Struct used for iterating through a hash map efficiently.
Definition hashMap.h:400
A hash map for storing key-value pairs.
Definition hashMap.h:414