Lines Matching refs:map

38 void hashmap__init(struct hashmap *map, hashmap_hash_fn hash_fn,  in hashmap__init()  argument
41 map->hash_fn = hash_fn; in hashmap__init()
42 map->equal_fn = equal_fn; in hashmap__init()
43 map->ctx = ctx; in hashmap__init()
45 map->buckets = NULL; in hashmap__init()
46 map->cap = 0; in hashmap__init()
47 map->cap_bits = 0; in hashmap__init()
48 map->sz = 0; in hashmap__init()
55 struct hashmap *map = malloc(sizeof(struct hashmap)); in hashmap__new() local
57 if (!map) in hashmap__new()
59 hashmap__init(map, hash_fn, equal_fn, ctx); in hashmap__new()
60 return map; in hashmap__new()
63 void hashmap__clear(struct hashmap *map) in hashmap__clear() argument
68 hashmap__for_each_entry_safe(map, cur, tmp, bkt) { in hashmap__clear()
71 free(map->buckets); in hashmap__clear()
72 map->buckets = NULL; in hashmap__clear()
73 map->cap = map->cap_bits = map->sz = 0; in hashmap__clear()
76 void hashmap__free(struct hashmap *map) in hashmap__free() argument
78 if (IS_ERR_OR_NULL(map)) in hashmap__free()
81 hashmap__clear(map); in hashmap__free()
82 free(map); in hashmap__free()
85 size_t hashmap__size(const struct hashmap *map) in hashmap__size() argument
87 return map->sz; in hashmap__size()
90 size_t hashmap__capacity(const struct hashmap *map) in hashmap__capacity() argument
92 return map->cap; in hashmap__capacity()
95 static bool hashmap_needs_to_grow(struct hashmap *map) in hashmap_needs_to_grow() argument
98 return (map->cap == 0) || ((map->sz + 1) * 4 / 3 > map->cap); in hashmap_needs_to_grow()
101 static int hashmap_grow(struct hashmap *map) in hashmap_grow() argument
108 new_cap_bits = map->cap_bits + 1; in hashmap_grow()
117 hashmap__for_each_entry_safe(map, cur, tmp, bkt) { in hashmap_grow()
118 h = hash_bits(map->hash_fn(cur->key, map->ctx), new_cap_bits); in hashmap_grow()
122 map->cap = new_cap; in hashmap_grow()
123 map->cap_bits = new_cap_bits; in hashmap_grow()
124 free(map->buckets); in hashmap_grow()
125 map->buckets = new_buckets; in hashmap_grow()
130 static bool hashmap_find_entry(const struct hashmap *map, in hashmap_find_entry() argument
137 if (!map->buckets) in hashmap_find_entry()
140 for (prev_ptr = &map->buckets[hash], cur = *prev_ptr; in hashmap_find_entry()
143 if (map->equal_fn(cur->key, key, map->ctx)) { in hashmap_find_entry()
154 int hashmap_insert(struct hashmap *map, long key, long value, in hashmap_insert() argument
167 h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits); in hashmap_insert()
169 hashmap_find_entry(map, key, h, NULL, &entry)) { in hashmap_insert()
187 if (hashmap_needs_to_grow(map)) { in hashmap_insert()
188 err = hashmap_grow(map); in hashmap_insert()
191 h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits); in hashmap_insert()
200 hashmap_add_entry(&map->buckets[h], entry); in hashmap_insert()
201 map->sz++; in hashmap_insert()
206 bool hashmap_find(const struct hashmap *map, long key, long *value) in hashmap_find() argument
211 h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits); in hashmap_find()
212 if (!hashmap_find_entry(map, key, h, NULL, &entry)) in hashmap_find()
220 bool hashmap_delete(struct hashmap *map, long key, in hashmap_delete() argument
226 h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits); in hashmap_delete()
227 if (!hashmap_find_entry(map, key, h, &pprev, &entry)) in hashmap_delete()
237 map->sz--; in hashmap_delete()