Progress on hashmap

This commit is contained in:
Nathan Fisher 2023-08-12 00:31:12 -04:00
parent 1775fc5444
commit 99347fd433

46
hmap.c
View file

@ -55,9 +55,9 @@ uint64_t hash_fnv1a_64(uint8_t *key, size_t len) {
}
struct _map_node {
struct _map_node *lt;
struct _map_node *gt;
uint64_t key;
struct _map_node *next;
void * key;
uint64_t hash;
void *data;
};
@ -76,11 +76,31 @@ hmap_node* hmap_node_init(void *key, size_t keysize, void *data) {
hash = hash_fnv1a_64(key, keysize);
node = calloc(1, sizeof(hmap_node));
if (node == NULL) return NULL;
node->key = hash;
node->key = key;
node->hash = hash;
node->data = data;
return node;
}
void* hmap_node_attach(hmap_node *root, hmap_node *leaf) {
void *ret = NULL;
while (1) {
if (root->key == leaf->key) {
ret = root->data;
root->data = leaf->data;
return ret;
} else if (root->next == NULL) {
root->next = leaf;
break;
} else {
root = root->next;
}
}
return NULL;
// todo
}
hmap* hmap_init() {
hmap* map;
@ -96,10 +116,26 @@ hmap* hmap_init() {
return map;
}
int hmap_expand(hmap *map) {
return 0;
}
hmap_node* hmap_insert(hmap * map, void *key, size_t keysize, void *data) {
hmap_node *node;
size_t idx;
hmap_node *ret = NULL;
if (map->len >= map->capacity / 2) {
if (hmap_expand(map)) return NULL;
}
node = hmap_node_init(key, keysize, data);
if (node == NULL) return NULL;
// todo
idx = node->hash % map->capacity;
if (map->buckets[idx].key == NULL) {
map->buckets[idx] = *node;
} else {
ret = hmap_node_attach(&map->buckets[idx], node);
}
// todo
return ret;
}