include/bedrock/dynmap.h file

Hash map, plus typed helpers for string and int keys.

Classes

struct DynMap
Generic dynamic hash map with separate chaining.
struct DynMapEntry
Represents an individual entry in a hash map.

Typedefs

using DynMap = struct DynMap
using DynMapEntry = struct DynMapEntry

Functions

static void dynmap_resize(DynMap* map)
Doubles the number of buckets and rehashes all entries.
static void dynmap_init(DynMap* map, const size_t buffer_size, unsigned long(*)(const void*) hash, int(*)(const void*, const void*) compare, void*(*)(const void*) key_copy, void(*)(void*) key_destroy, void*(*)(const void*) value_copy, void(*)(void*) value_destroy)
Initializes a DynMap instance.
static auto dynmap_insert(DynMap* map, const void* key, const void* value) -> int
Inserts or updates a key-value pair in the map.
static auto dynmap_get(const DynMap* map, const void* key) -> void*
Looks up the value associated with a key.
static auto dynmap_delete(DynMap* map, const void* key) -> int
Removes the entry associated with a key.
static void dynmap_destroy(DynMap* map)
Frees all resources associated with the map.
static auto string_hash(const void* key) -> BD_DYNMAP_UNUSED unsigned long
djb2 hash for a NUL-terminated string key.
static auto string_compare(const void* a, const void* b) -> BD_DYNMAP_UNUSED int
Compares two string keys with strcmp.
static auto string_copy(const void* key) -> BD_DYNMAP_UNUSED void*
Allocates and copies a string key.
static auto string_destroy(void* key) -> BD_DYNMAP_UNUSED void
Frees a string key allocated by string_copy.
static auto int_hash(const void* key) -> BD_DYNMAP_UNUSED unsigned long
Hashes an int key by casting its value.
static auto int_compare(const void* a, const void* b) -> BD_DYNMAP_UNUSED int
Compares two int keys; returns <0, 0, or >0.
static auto int_copy(const void* key) -> BD_DYNMAP_UNUSED void*
Allocates and copies an int key.
static auto int_destroy(void* key) -> BD_DYNMAP_UNUSED void
Frees an int key allocated by int_copy.
static auto no_op_copy(const void* value) -> BD_DYNMAP_UNUSED void*
Returns the value pointer unchanged (no allocation).
static auto no_op_destroy(void* value) -> BD_DYNMAP_UNUSED void
No-op destructor for values not owned by the map.

Defines

#define dynmap_clib_free
#define dynmap_clib_malloc
#define dynmap_clib_calloc
#define dynmap_clib_realloc
#define LOAD_FACTOR_THRESHOLD
#define BD_DYNMAP_UNUSED
#define DEFINE_STRING_KEY_MAP(map_name, value_type)
Defines a typed hash map with string keys and pointer values.
#define DEFINE_INT_KEY_MAP(map_name, value_type)
Defines a typed hash map with int keys and pointer values.

Function documentation

static void dynmap_resize(DynMap* map)

Doubles the number of buckets and rehashes all entries.

Parameters
map Map to resize (must not be NULL and must be initialized).

Allocates a new bucket array at twice the current size, reinserts every existing entry into its new bucket, then replaces the old array. On allocation failure the map is left unchanged.

static void dynmap_init(DynMap* map, const size_t buffer_size, unsigned long(*)(const void*) hash, int(*)(const void*, const void*) compare, void*(*)(const void*) key_copy, void(*)(void*) key_destroy, void*(*)(const void*) value_copy, void(*)(void*) value_destroy)

Initializes a DynMap instance.

Parameters
map Map instance to initialize (must not be NULL).
buffer_size Initial number of hash buckets.
hash Hash function for keys.
compare Key comparison function; return 0 when keys are equal.
key_copy Function that copies a key for map ownership.
key_destroy Function that destroys a key owned by the map.
value_copy Function that copies a value for map ownership.
value_destroy Function that destroys a value owned by the map.

Sets the initial bucket count, clears the entry count, allocates the bucket array, and installs the hash/compare/copy/destroy callbacks. When built with DYNMAP_THREAD_SAFE, also initializes the map mutex.

static int dynmap_insert(DynMap* map, const void* key, const void* value)

Inserts or updates a key-value pair in the map.

Parameters
map Map to modify (must not be NULL and must be initialized).
key Key to insert or update (passed to hash/compare/key_copy).
value Value to associate with key (passed to value_copy).
Returns 1 if a new entry was inserted, 0 if an existing entry's value was updated, -1 if allocation failed.

If the load factor would exceed the threshold, the map is resized first. When key already exists, the old value is destroyed and replaced. Otherwise, a new entry is allocated and linked into the target bucket. Keys and values are stored via the map's copy callbacks.

static void* dynmap_get(const DynMap* map, const void* key)

Looks up the value associated with a key.

Parameters
map Map to query (must not be NULL and must be initialized).
key Key to search for (passed to hash/compare).
Returns Pointer to the stored value if found, or NULL if key is absent.

Hashes key, walks the corresponding bucket chain, and returns the value of the first entry whose key compares equal.

static int dynmap_delete(DynMap* map, const void* key)

Removes the entry associated with a key.

Parameters
map Map to modify (must not be NULL and must be initialized).
key Key of the entry to remove (passed to hash/compare).
Returns 1 if an entry was deleted, 0 if key was not found.

Hashes key, walks the corresponding bucket chain, and unlinks the first matching entry. The entry's key and value are destroyed via the map callbacks, then the entry node is freed.

static void dynmap_destroy(DynMap* map)

Frees all resources associated with the map.

Parameters
map Map to destroy, or NULL.

Walks every bucket, destroys each entry's key and value via the map callbacks, frees entry nodes and the bucket array, then resets counts. When built with DYNMAP_THREAD_SAFE, also destroys the map mutex. A NULL map is ignored.

Define documentation

#define DEFINE_STRING_KEY_MAP(map_name, value_type)

Defines a typed hash map with string keys and pointer values.

Parameters
map_name Name of the generated map type and function prefix.
value_type Element type pointed to by stored values.

Values are stored by pointer (no_op copy/destroy); the caller owns them.

#define DEFINE_INT_KEY_MAP(map_name, value_type)

Defines a typed hash map with int keys and pointer values.

Parameters
map_name Name of the generated map type and function prefix.
value_type Element type pointed to by stored values.

Values are stored by pointer (no_op copy/destroy); the caller owns them.