From 7c7dd1a8b18b101e093df5eff6247acd94f25422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sun, 3 Feb 2019 00:00:05 +0100 Subject: Rework makefile, made .inc into .inc.h. --- hash.inc.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 hash.inc.h (limited to 'hash.inc.h') diff --git a/hash.inc.h b/hash.inc.h new file mode 100644 index 00000000..0b07629f --- /dev/null +++ b/hash.inc.h @@ -0,0 +1,61 @@ +#ifndef TYPE +#error "Set TYPE to something before including this header" +#else + +#include "err.h" + +int HASH_PUT(TYPE) ( TABLE(TYPE)* table, TYPE* value) { + // TODO genicify the hash function + unsigned long h = hash(value->key.mem) % table->size; + TYPE* mem = table->values[h]; + + /* TODO conflict resolution */ + if (mem != NULL) ERR("Hash collision"); + mem = value; + + ++table->item_count; + return 0; +} + +int HASH_INIT(TYPE) ( TABLE(TYPE)* table, int init_size ) { + /* + * TODO parts of table might not get properly initialized to 0 + */ + table->values = calloc(sizeof(table->values), init_size); + table->size = init_size; + table->item_count = 0; + return 0; +} + +TYPE* HASH_GET(TYPE) ( TABLE(TYPE)* table, char* key ) { + unsigned long h = hash(key) % table->size; + TYPE* mem = table->values[h]; + if (mem == NULL) { + fprintf(stderr, "Trying to access %s\n", key); + ERR("Nothing in field"); + return 0; + } else if (strcmp(mem->key.mem, key) == 0) { + return mem; + } else { + /* TODO fix retrival on invalid key */ + ERR("Other error"); + return 0; + } +} + +int HASH_FREE(TYPE) ( TABLE(TYPE)* table ) { + /* + * TODO an early return is possible by checking if all items have + * been found. table->item_count + */ + for (int i = 0; i < table->size; i++) { + TYPE* mem = table->values[i]; + if (mem == NULL) continue; + + free(mem); + } + free(table->values); + return 0; +} + +#endif /* TYPE */ -- cgit v1.2.3