Lines Matching refs:hash_map

64   hash_map_t *hash_map = zeroed_allocator->alloc(sizeof(hash_map_t));  in hash_map_new_internal()  local
65 if (hash_map == NULL) in hash_map_new_internal()
68 hash_map->hash_fn = hash_fn; in hash_map_new_internal()
69 hash_map->key_fn = key_fn; in hash_map_new_internal()
70 hash_map->data_fn = data_fn; in hash_map_new_internal()
71 hash_map->allocator = zeroed_allocator; in hash_map_new_internal()
72 hash_map->keys_are_equal = equality_fn ? equality_fn : default_key_equality; in hash_map_new_internal()
74 hash_map->num_bucket = num_bucket; in hash_map_new_internal()
75 hash_map->bucket = zeroed_allocator->alloc(sizeof(hash_map_bucket_t) * num_bucket); in hash_map_new_internal()
76 if (hash_map->bucket == NULL) { in hash_map_new_internal()
77 zeroed_allocator->free(hash_map); in hash_map_new_internal()
80 return hash_map; in hash_map_new_internal()
92 void hash_map_free(hash_map_t *hash_map) { in hash_map_free() argument
93 if (hash_map == NULL) in hash_map_free()
95 hash_map_clear(hash_map); in hash_map_free()
96 hash_map->allocator->free(hash_map->bucket); in hash_map_free()
97 hash_map->allocator->free(hash_map); in hash_map_free()
100 bool hash_map_is_empty(const hash_map_t *hash_map) { in hash_map_is_empty() argument
101 assert(hash_map != NULL); in hash_map_is_empty()
102 return (hash_map->hash_size == 0); in hash_map_is_empty()
105 size_t hash_map_size(const hash_map_t *hash_map) { in hash_map_size() argument
106 assert(hash_map != NULL); in hash_map_size()
107 return hash_map->hash_size; in hash_map_size()
110 size_t hash_map_num_buckets(const hash_map_t *hash_map) { in hash_map_num_buckets() argument
111 assert(hash_map != NULL); in hash_map_num_buckets()
112 return hash_map->num_bucket; in hash_map_num_buckets()
115 bool hash_map_has_key(const hash_map_t *hash_map, const void *key) { in hash_map_has_key() argument
116 assert(hash_map != NULL); in hash_map_has_key()
118 hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket; in hash_map_has_key()
119 list_t *hash_bucket_list = hash_map->bucket[hash_key].list; in hash_map_has_key()
125 bool hash_map_set(hash_map_t *hash_map, const void *key, void *data) { in hash_map_set() argument
126 assert(hash_map != NULL); in hash_map_set()
129 hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket; in hash_map_set()
131 if (hash_map->bucket[hash_key].list == NULL) { in hash_map_set()
132 hash_map->bucket[hash_key].list = list_new_internal(bucket_free_, hash_map->allocator); in hash_map_set()
133 if (hash_map->bucket[hash_key].list == NULL) in hash_map_set()
136 list_t *hash_bucket_list = hash_map->bucket[hash_key].list; in hash_map_set()
145 hash_map->hash_size++; in hash_map_set()
147 hash_map_entry = hash_map->allocator->alloc(sizeof(hash_map_entry_t)); in hash_map_set()
153 hash_map_entry->hash_map = hash_map; in hash_map_set()
158 bool hash_map_erase(hash_map_t *hash_map, const void *key) { in hash_map_erase() argument
159 assert(hash_map != NULL); in hash_map_erase()
161 hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket; in hash_map_erase()
162 list_t *hash_bucket_list = hash_map->bucket[hash_key].list; in hash_map_erase()
169 hash_map->hash_size--; in hash_map_erase()
174 void *hash_map_get(const hash_map_t *hash_map, const void *key) { in hash_map_get() argument
175 assert(hash_map != NULL); in hash_map_get()
177 hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket; in hash_map_get()
178 list_t *hash_bucket_list = hash_map->bucket[hash_key].list; in hash_map_get()
187 void hash_map_clear(hash_map_t *hash_map) { in hash_map_clear() argument
188 assert(hash_map != NULL); in hash_map_clear()
190 for (hash_index_t i = 0; i < hash_map->num_bucket; i++){ in hash_map_clear()
191 if (hash_map->bucket[i].list == NULL) in hash_map_clear()
193 list_free(hash_map->bucket[i].list); in hash_map_clear()
194 hash_map->bucket[i].list = NULL; in hash_map_clear()
198 void hash_map_foreach(hash_map_t *hash_map, hash_map_iter_cb callback, void *context) { in hash_map_foreach() argument
199 assert(hash_map != NULL); in hash_map_foreach()
202 for (hash_index_t i = 0; i < hash_map->num_bucket; ++i){ in hash_map_foreach()
203 if (hash_map->bucket[i].list == NULL) in hash_map_foreach()
205 for (const list_node_t *iter = list_begin(hash_map->bucket[i].list); in hash_map_foreach()
206 iter != list_end(hash_map->bucket[i].list); in hash_map_foreach()
218 const hash_map_t *hash_map = hash_map_entry->hash_map; in bucket_free_() local
220 if (hash_map->key_fn) in bucket_free_()
221 hash_map->key_fn((void *)hash_map_entry->key); in bucket_free_()
222 if (hash_map->data_fn) in bucket_free_()
223 hash_map->data_fn(hash_map_entry->data); in bucket_free_()
224 hash_map->allocator->free(hash_map_entry); in bucket_free_()
237 if (hash_map_entry->hash_map->keys_are_equal(hash_map_entry->key, key)) { in find_bucket_entry_()