diff --git a/hmap.c b/hmap.c index 4a7a714..f3763db 100644 --- a/hmap.c +++ b/hmap.c @@ -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; + 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; }