seahag/hmap.c

214 lines
5.6 KiB
C
Raw Normal View History

2023-08-11 23:35:23 -04:00
/* _,.---._ .-._ .--.-. ,--.--------.
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
* |==|,| | -|==| , '=' |==| - _ | |==|- |
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
* _ __ ,---. .-._ .=-.-. _,.----.
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
* `--`---' `--` `--`./ `--``--`-`
*
* @(#)Copyright (c) 2023, Nathan D. Fisher.
*
* This is free software. It comes with NO WARRANTY.
* Permission to use, modify and distribute this source code
* is granted subject to the following conditions.
* 1/ that the above copyright notice and this notice
* are preserved in all copies and that due credit be given
* to the author.
* 2/ that any changes to this code are clearly commented
* as such so that the author does not get blamed for bugs
* other than his own.
*/
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
2023-08-11 23:35:23 -04:00
#include <sys/stat.h>
#include <sys/types.h>
#include "haggis.h"
#include "haggis_private.h"
#define FNV64_OFFSET_BASIS 14695981039346656037u
#define FNV64_PRIME 1099511628211u
uint64_t hash_fnv1a_64(uint8_t *key, size_t len) {
int i;
uint64_t hash = FNV64_OFFSET_BASIS;
for (i = 0; i < len; i++) {
hash = hash ^ *key;
hash = hash * FNV64_PRIME;
key++;
}
return hash;
}
struct _map_node {
2023-08-12 00:31:12 -04:00
struct _map_node *next;
void * key;
uint64_t hash;
2023-08-11 23:35:23 -04:00
void *data;
};
typedef struct _map_node hmap_node;
typedef struct {
size_t capacity;
size_t len;
2023-08-12 00:54:43 -04:00
size_t keysize;
2023-08-11 23:35:23 -04:00
hmap_node *buckets;
} hmap;
void* hmap_insert(hmap * map, void *key, void *data);
2023-08-12 00:54:43 -04:00
2023-08-11 23:35:23 -04:00
hmap_node* hmap_node_init(void *key, size_t keysize, void *data) {
hmap_node *node;
uint64_t hash;
hash = hash_fnv1a_64(key, keysize);
node = calloc(1, sizeof(hmap_node));
if (node == NULL) return NULL;
2023-08-12 00:31:12 -04:00
node->key = key;
node->hash = hash;
2023-08-11 23:35:23 -04:00
node->data = data;
return node;
}
void hmap_node_deinit(hmap_node *node) {
hmap_node *current = node;
while (current != NULL) {
current = node->next;
free(node);
node = current;
}
}
void* hmap_node_attach(hmap_node *root, hmap_node *leaf, size_t keysize) {
2023-08-12 00:31:12 -04:00
void *ret = NULL;
while (1) {
if (memcmp(root->key, leaf->key, keysize) == 0) {
2023-08-12 00:31:12 -04:00
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
}
void* hmap_node_search(hmap_node *root, void *key, size_t keysize) {
void *ret = NULL;
while (1) {
if (memcmp(root->key, key, keysize) == 0) {
ret = root->data;
return ret;
} else if (root->next == NULL) {
return NULL;
} else {
root = root->next;
}
}
}
2023-08-12 00:54:43 -04:00
hmap* hmap_init(size_t keysize) {
2023-08-11 23:35:23 -04:00
hmap* map;
map = calloc(1, sizeof(hmap));
if (map == NULL) return NULL;
map->len = 0;
map->capacity = 64;
2023-08-12 00:54:43 -04:00
map->keysize = keysize;
map->buckets = calloc(64, sizeof(hmap_node));
2023-08-11 23:35:23 -04:00
if (map->buckets == NULL) {
free(map);
return NULL;
}
return map;
}
void hmap_deinit(hmap *map) {
int i;
for (i = 0; i < map->capacity; i++) {
if (map->buckets[i].next != NULL) {
hmap_node_deinit(map->buckets[i].next);
}
}
free(map->buckets);
free(map);
}
2023-08-12 00:31:12 -04:00
int hmap_expand(hmap *map) {
2023-08-12 00:54:43 -04:00
hmap_node *old;
hmap_node *new;
hmap_node *current;
int i;
new = calloc(map->capacity + 64, sizeof(hmap_node));
if (new == NULL) return 1;
old = map->buckets;
map->buckets = new;
for (i = 0; i < map->capacity; i++) {
current = &old[i];
while (current->key != NULL) {
hmap_insert(map, current->key, current->data);
2023-08-12 00:54:43 -04:00
current = current->next;
}
}
free(old);
2023-08-12 00:31:12 -04:00
return 0;
}
void* hmap_insert(hmap *map, void *key, void *data) {
2023-08-11 23:35:23 -04:00
hmap_node *node;
2023-08-12 00:31:12 -04:00
size_t idx;
hmap_node *ret = NULL;
2023-08-11 23:35:23 -04:00
2023-08-12 00:31:12 -04:00
if (map->len >= map->capacity / 2) {
if (hmap_expand(map)) return NULL;
}
node = hmap_node_init(key, map->keysize, data);
2023-08-11 23:35:23 -04:00
if (node == NULL) return NULL;
2023-08-12 00:31:12 -04:00
idx = node->hash % map->capacity;
if (map->buckets[idx].key == NULL) {
map->buckets[idx] = *node;
} else {
ret = hmap_node_attach(&map->buckets[idx], node, map->keysize);
2023-08-12 00:31:12 -04:00
}
map->len += 1;
if (ret == NULL)
return NULL;
else
return ret->data;
}
void* hmap_get(hmap *map, void *key) {
uint64_t hash;
size_t idx;
void *ret;
hash = hash_fnv1a_64(key, map->keysize);
idx = hash % map->capacity;
ret = hmap_node_search(&map->buckets[idx], key, map->keysize);
2023-08-12 00:31:12 -04:00
return ret;
2023-08-11 23:35:23 -04:00
}