From 1775fc5444d59ca8a8b68e92b26aa6d6e6e42300 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Fri, 11 Aug 2023 23:35:23 -0400 Subject: [PATCH] Begin implementing hashmap --- Makefile | 1 + hmap.c | 105 +++++++++++++++++++++++++++++ {test => include}/haggis_private.h | 3 + 3 files changed, 109 insertions(+) create mode 100644 hmap.c rename {test => include}/haggis_private.h (97%) diff --git a/Makefile b/Makefile index 5d2a42f..b494341 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,7 @@ hdrs += include/jobq.h hdrs += include/linklist.h srcs += bytes.c +srcs += hmap.c srcs += haggis.c srcs += jobq.c srcs += linklist.c diff --git a/hmap.c b/hmap.c new file mode 100644 index 0000000..4a7a714 --- /dev/null +++ b/hmap.c @@ -0,0 +1,105 @@ +/* _,.---._ .-._ .--.-. ,--.--------. + * _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\ + * /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./ + * |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \ + * |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \ + * |==|,| | -|==| , '=' |==| - _ | |==|- | + * |==| '=' /\==\ - ,_ /|==| /\ , | |==|, | + * |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/ + * `-.`.____.' `--`--'' `--`./ `--` `--`--` + * _ __ ,---. .-._ .=-.-. _,.----. + * .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \ + * /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-' + * |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | . + * |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \ + * |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , | + * |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. / + * /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-' + * `--`---' `--` `--`./ `--``--`-` + * + * @(#)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 +#include +#include +#include +#include + +#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 { + struct _map_node *lt; + struct _map_node *gt; + uint64_t key; + void *data; +}; + +typedef struct _map_node hmap_node; + +typedef struct { + size_t capacity; + size_t len; + hmap_node *buckets; +} hmap; + +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; + node->key = hash; + node->data = data; + return node; +} + +hmap* hmap_init() { + hmap* map; + + map = calloc(1, sizeof(hmap)); + if (map == NULL) return NULL; + map->len = 0; + map->capacity = 64; + map->buckets = calloc(64, sizeof(size_t)); + if (map->buckets == NULL) { + free(map); + return NULL; + } + return map; +} + +hmap_node* hmap_insert(hmap * map, void *key, size_t keysize, void *data) { + hmap_node *node; + + node = hmap_node_init(key, keysize, data); + if (node == NULL) return NULL; + // todo +} diff --git a/test/haggis_private.h b/include/haggis_private.h similarity index 97% rename from test/haggis_private.h rename to include/haggis_private.h index 03aff19..0d10897 100644 --- a/test/haggis_private.h +++ b/include/haggis_private.h @@ -33,6 +33,8 @@ #ifndef HAGGIS_PRIVATE_H #define HAGGIS_PRIVATE_H 1 +#include +#include #include #include @@ -59,5 +61,6 @@ int haggis_load_filename(FILE *stream, haggis_filename *n); int haggis_store_filename(FILE *stream, haggis_filename *n); int haggis_load_filetype(FILE *stream, haggis_typeflag tag, haggis_filetype *file); int haggis_store_filetype(FILE *stream, haggis_filetype *filetype); +uint64_t hash_fnv1a_64(uint8_t *key, size_t len); #endif // !HAGGIS_PRIVATE_H