diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-04-15 08:42:24 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-04-15 08:42:24 -0600 |
commit | 802ed1e3cd0252cafd1be2aada0addf4d3f7eb2e (patch) | |
tree | 70f49ed30f0f9839709a093c6af54661eeaad4ee /libmisc/map.c | |
parent | 0450e14b3a86e4448537c03253eeebf509f8909e (diff) | |
parent | 32a1b710b40ce9d53cd0a7bc0c183da87e07f397 (diff) |
Merge branch 'lukeshu/generics'
Diffstat (limited to 'libmisc/map.c')
-rw-r--r-- | libmisc/map.c | 73 |
1 files changed, 39 insertions, 34 deletions
diff --git a/libmisc/map.c b/libmisc/map.c index bb8a2d2..7629c8c 100644 --- a/libmisc/map.c +++ b/libmisc/map.c @@ -16,14 +16,19 @@ /* Internal utilities *********************************************************/ -#define cast(n) lm_dll_node_cast(struct _map_kv, n) - -static inline void *_map_kv_keyp(struct _map *m, struct _map_kv *kv) { +struct _map_kv { + uint8_t flags; + /* opaque key; */ + /* opaque val; */ +}; +DLIST_DECLARE_NODE(_map_kv_list, struct _map_kv); + +static inline void *_map_kv_keyp(struct _map *m, struct _map_kv_list_node *kv) { assert(m); assert(kv); return ((void*)kv)+m->offsetof_k; } -static inline void *_map_kv_valp(struct _map *m, struct _map_kv *kv) { +static inline void *_map_kv_valp(struct _map *m, struct _map_kv_list_node *kv) { assert(m); assert(kv); return ((void*)kv)+m->offsetof_v; @@ -31,8 +36,8 @@ static inline void *_map_kv_valp(struct _map *m, struct _map_kv *kv) { static inline void _map_lookup(struct _map *m, void *keyp, hash_t *ret_hash, - lm_dll_root **ret_bucket, - struct _map_kv **ret_kv) { + struct _map_kv_list **ret_bucket, + struct _map_kv_list_node **ret_kv) { assert(m); assert(keyp); assert(ret_hash); @@ -45,8 +50,8 @@ static inline void _map_lookup(struct _map *m, void *keyp, return; } *ret_bucket = &m->buckets[*ret_hash % m->nbuckets]; - for (struct _map_kv *kv = cast((*ret_bucket)->front); kv; kv = cast(kv->rear)) { - if (!(kv->flags & FLAG_DEL) && + for (struct _map_kv_list_node *kv = (*ret_bucket)->front; kv; kv = kv->rear) { + if (!(kv->val.flags & FLAG_DEL) && memcmp(_map_kv_keyp(m, kv), keyp, m->sizeof_k) == 0) { *ret_kv = kv; return; @@ -58,13 +63,13 @@ static inline void _map_lookup(struct _map *m, void *keyp, static inline void _map_resize(struct _map *m, size_t new_nbuckets) { assert(m); assert(new_nbuckets); - lm_dll_root *new_buckets = calloc(new_nbuckets, sizeof(lm_dll_root)); + struct _map_kv_list *new_buckets = calloc(new_nbuckets, sizeof(struct _map_kv_list)); for (size_t i = 0; i < m->nbuckets; i++) { while (m->buckets[i].front) { - struct _map_kv *kv = cast(m->buckets[i].front); - lm_dll_pop_from_front(&m->buckets[i]); + struct _map_kv_list_node *kv = m->buckets[i].front; + dlist_pop_from_front(&m->buckets[i]); hash_t h = hash(_map_kv_keyp(m, kv), m->sizeof_k); - lm_dll_push_to_rear(&new_buckets[h % new_nbuckets], kv); + dlist_push_to_rear(&new_buckets[h % new_nbuckets], kv); } } m->nbuckets = new_nbuckets; @@ -91,8 +96,8 @@ void *_map_load(struct _map *m, void *keyp) { assert(keyp); hash_t h; - lm_dll_root *bucket; - struct _map_kv *kv; + struct _map_kv_list *bucket; + struct _map_kv_list_node *kv; _map_lookup(m, keyp, &h, &bucket, &kv); if (!kv) @@ -105,16 +110,16 @@ bool _map_del(struct _map *m, void *keyp) { assert(keyp); hash_t h; - lm_dll_root *bucket; - struct _map_kv *kv; + struct _map_kv_list *bucket; + struct _map_kv_list_node *kv; _map_lookup(m, keyp, &h, &bucket, &kv); if (!kv) return false; - if (kv->flags & FLAG_ITER) { - kv->flags |= FLAG_DEL; + if (kv->val.flags & FLAG_ITER) { + kv->val.flags |= FLAG_DEL; } else { - lm_dll_remove(bucket, kv); + dlist_remove(bucket, kv); free(kv); } m->len--; @@ -127,12 +132,12 @@ void *_map_store(struct _map *m, void *keyp, void *valp) { assert(valp); hash_t h; - lm_dll_root *bucket; - struct _map_kv *old; + struct _map_kv_list *bucket; + struct _map_kv_list_node *old; _map_lookup(m, keyp, &h, &bucket, &old); if (old) { - lm_dll_remove(bucket, old); + dlist_remove(bucket, old); free(old); m->len--; } @@ -141,10 +146,10 @@ void *_map_store(struct _map *m, void *keyp, void *valp) { h = hash(keyp, m->sizeof_k); bucket = &m->buckets[h % m->nbuckets]; } - struct _map_kv *kv = calloc(1, m->sizeof_kv); + struct _map_kv_list_node *kv = calloc(1, m->sizeof_kv); memcpy(_map_kv_keyp(m, kv), keyp, m->sizeof_k); memcpy(_map_kv_valp(m, kv), valp, m->sizeof_v); - lm_dll_push_to_rear(bucket, kv); + dlist_push_to_rear(bucket, kv); return _map_kv_valp(m, kv); } @@ -153,8 +158,8 @@ void _map_free(struct _map *m) { for (size_t i = 0; i < m->nbuckets; i++) { while (m->buckets[i].front) { - struct _map_kv *kv = cast(m->buckets[i].front); - lm_dll_pop_from_front(&m->buckets[i]); + struct _map_kv_list_node *kv = m->buckets[i].front; + dlist_pop_from_front(&m->buckets[i]); free(kv); } } @@ -199,14 +204,14 @@ bool _map_iter_next(struct _map_iter *state) { return false; while (!state->m->buckets[state->i].front) state->i++; - state->kv = cast(state->m->buckets[state->i].front); + state->kv = state->m->buckets[state->i].front; } else { - struct _map_kv *old_kv = state->kv; - state->kv = cast(old_kv->rear); + struct _map_kv_list_node *old_kv = state->kv; + state->kv = old_kv->rear; - old_kv->flags &= ~FLAG_ITER; - if (old_kv->flags & FLAG_DEL) { - lm_dll_remove(&state->m->buckets[state->i], old_kv); + old_kv->val.flags &= ~FLAG_ITER; + if (old_kv->val.flags & FLAG_DEL) { + dlist_remove(&state->m->buckets[state->i], old_kv); free(old_kv); } @@ -214,10 +219,10 @@ bool _map_iter_next(struct _map_iter *state) { state->i++; if (state->i == state->m->nbuckets) return false; - state->kv = cast(state->m->buckets[state->i].front); + state->kv = state->m->buckets[state->i].front; } } - state->kv->flags |= FLAG_ITER; + state->kv->val.flags |= FLAG_ITER; memcpy(state->keyp, _map_kv_keyp(state->m, state->kv), state->m->sizeof_k); *(state->valpp) = _map_kv_valp(state->m, state->kv); return true; |