9#define PS_HASH_MAP_INITIAL_CAPACITY 16
15 const struct ps_hash_map_entry* entry = &map->
entries[i];
16 if (entry->key == NULL) {
22 usize local_count = 1;
28 "hash map: only the first entry can have a NULL key");
30 "hash map: all entries in a bucket should have the same "
34 calc_count += local_count;
52 +
sizeof(*map->
entries) * capacity);
80 struct ps_hash_map_entry* entry = &map->
entries[i];
82 struct ps_hash_map_entry* next = entry->
next;
103 struct ps_hash_map_entry* entry = &map->
entries[i];
104 while (entry && entry->key) {
130 struct ps_hash_map_entry* entry = &map->
entries[index];
135 if (entry->key != NULL && map->
is_equal(entry->key, key)) {
138 if (entry->next == NULL) {
140 struct ps_hash_map_entry* new_entry =
141 malloc(
sizeof(*new_entry));
142 CHECK(
if (!new_entry) {
146 new_entry->next = NULL;
147 entry->next = new_entry;
161 entry->value = value;
180 struct ps_hash_map_entry* entry = &map->
entries[index];
184 if (entry->key && map->
is_equal(entry->key, key)) {
199 struct ps_hash_map_entry* entry = &map->
entries[index];
200 struct ps_hash_map_entry* prev = NULL;
204 if (entry->key && map->
is_equal(entry->key, key)) {
214 entry->key = entry->next->key;
215 entry->value = entry->next->value;
216 auto_t afterward = entry->next->next;
218 entry->next = afterward;
226 prev->next = entry->next;
Defines assertion and abortion functionality.
#define CHECK(...)
All code inside CHECK will be run when PS_DEBUG is defined.
#define PS_NO_MEMORY()
Aborts the program due to exhaustion of virtual memory.
#define ps_assert(cond, msg)
Asserts the given condition cond, aborting via ps_abort with the given msg otherwise.
u64(* hasher_t)(void *)
Function that hashes an object.
bool(* is_equal_t)(void *, void *)
Function that compares whether two objects are equal.
u64 hash_t
Numeric type for hashes.
void * ps_hash_map_get(struct ps_hash_map *map, void *key)
Gets the value associated with the given key.
void ps_hash_map_redistribute(struct ps_hash_map **map_ptr)
struct ps_hash_map * _ps_hash_map_new(is_equal_t is_equal, hasher_t hasher, free_fn_t free_fn, u64 capacity)
struct ps_hash_map * ps_hash_map_new(is_equal_t is_equal, hasher_t hasher, free_fn_t free_fn)
Initializes a new hash map with the given comparison and memory functions.
u64 ps_hash_map_count(const struct ps_hash_map *map)
Gets the number of key-value pairs in the hash map.
void ps_hash_map_free(struct ps_hash_map *map)
Destroys all data owned by the hash map.
void ps_hash_map_insert(struct ps_hash_map **map_ptr, void *key, void *value)
Sets the object associated with the given key in the hash map, overwriting any existing value for tha...
bool ps_hash_map_contains(struct ps_hash_map *map, void *key)
Checks if the hash map contains the given key-value pair.
void ps_hash_map_remove(struct ps_hash_map *map, void *key)
Removes the key-value pair associated with the given key.
void ps_hash_map_assert_inv(const struct ps_hash_map *map)
Asserts the invariants of the data structure implementation.
#define PS_HASH_MAP_INITIAL_CAPACITY
void(* free_fn_t)(void *ptr)
Function that destroys an object and its owned resources.
Enables malloc tracking functionality.
struct ps_hash_map_entry * next
u64 length
Number of first-node entries.
u64 count
Number of total entries: load factor = count / length.
struct ps_hash_map::ps_hash_map_entry entries[]