12#define MAP_LOAD_FACTOR 75
16#define MAP_MAX_CAPACITY (1 << 30)
56 sz entry_size,
sz capacity) {
64 map->
buckets = allocator_alloc(allocator, size);
77 map_init_with_capacity(
map, allocator, entry_size, MAP_LOAD_FACTOR / 25 + 1);
83#define map_init_ty(ty, map, allocator) map_init(map, allocator, sizeof(ty))
113 while (capacity * MAP_LOAD_FACTOR / 100 <=
map->
size) {
115 if (capacity >= MAP_MAX_CAPACITY)
116 return MAP_MAX_CAPACITY;
155 "The size of an inserted entry (%d) must match the entry size defined "
156 "at compile time (%d)",
169 memcpy(head + 1, data, size);
177 memcpy(current + 1, data, size);
183 current = current->
next;
191 memcpy(new_node + 1, data, size);
192 current->
next = new_node;
196#define map_insert_ty(ty, map, key, data) map_insert(map, key, data, sizeof(ty))
208 for (; b; b = b->
next) {
218#define map_get_ty(ty, map, key) (ty *)map_get(map, key)
#define ASSERT(expr, msg,...)
Definition defs.h:35
size_t sz
Definition defs.h:51
uint32_t u32
Definition defs.h:45
static map_t map
Definition map.c:6
static void map_insert(map_t *map, map_key_t key, void *data, sz size)
map_key_t(* hash_function_f)(void *, sz)
Definition map.h:20
u32 map_key_t
Definition map.h:18
static sz map_calculate_next_capacity(const map_t *map)
Calculates the next capacity for the map. May return the same capacity if the MAP_LOAD_FACTOR is not ...
Definition map.h:111
static bool map_should_grow(const map_t *map)
Calculates if the map reached its MAP_LOAD_FACTOR. O(1)
Definition map.h:103
A generic allocator type passed by value. Contains a fallback allocator and a set of function pointer...
Definition memory.h:30
bool has_data
Definition map.h:23
map_key_t key
Definition map.h:24
struct map_bucket_t * next
Definition map.h:25
allocator_t allocator
The allocator used for all memory management, used for allocating both the main and the secondary sto...
Definition map.h:32
static void map_init(map_t *map, allocator_t allocator, sz entry_size)
Initializes the map with a fixed size and some initial capacity. O(1)
Definition map.h:76
static void map_init_with_capacity(map_t *map, allocator_t allocator, sz entry_size, sz capacity)
Initialize the map with a specific capacity and entry size. O(1)
Definition map.h:55
static void map_insert(map_t *map, map_key_t key, void *data, sz size)
Insert an element of the size size into the map. Does an additional check to verify that the size of ...
Definition map.h:153
sz size
The amount of elements currently in the map.
Definition map.h:38
sz capacity
The amount of buckets available.
Definition map.h:40
void * buckets
The raw byte storage used for the buckets.
Definition map.h:43
static void * map_get(map_t *map, map_key_t key)
Retrieves the element of the map at key map_key. Returns NULL if no element found....
Definition map.h:201
static void map_cleanup(map_t *map)
Cleanup the memory used by the map itself. Importantly, does not clean the internal types stored insi...
Definition map.h:89
static void map_grow(map_t *map)
Tries to grow the map. Doubles the size and reallocates all buckets. Will not grow if the MAP_LOAD_FA...
Definition map.h:127
sz entry_size
The size of a single entry in bytes. Serves as a guardrail for runtime checking.
Definition map.h:36