aboutsummaryrefslogtreecommitdiff
path: root/hash_help.inc
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-01-22 11:42:58 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-01-22 11:42:58 +0100
commitebe5c7e428b57dca758b1b3a1d8b7d4b423b19a9 (patch)
treebf97a4ba3a05c24ae1ec176f42656ce1608ee8d1 /hash_help.inc
parentAdd very simple err header. (diff)
downloadcalp-ebe5c7e428b57dca758b1b3a1d8b7d4b423b19a9.tar.gz
calp-ebe5c7e428b57dca758b1b3a1d8b7d4b423b19a9.tar.xz
Start using trie's instead of hash-maps.
Diffstat (limited to '')
-rw-r--r--hash_help.inc27
1 files changed, 25 insertions, 2 deletions
diff --git a/hash_help.inc b/hash_help.inc
index ddaf4ae0..34766b54 100644
--- a/hash_help.inc
+++ b/hash_help.inc
@@ -2,12 +2,18 @@
#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;
- table->values[h] = value;
+ TYPE* mem = table->values[h];
+
+ // fprintf(stderr, "INFO: %i: [%s] := [%s]\n", table->item_count, value->key.mem, value->val.mem);
/* TODO conflict resolution */
+ if (mem != NULL) ERR("Hash collision");
+ mem = value;
++table->item_count;
return 0;
@@ -25,15 +31,32 @@ int HASH_INIT(TYPE) ( TABLE(TYPE)* table, int init_size ) {
TYPE* HASH_GET(TYPE) ( TABLE(TYPE)* table, char* key ) {
unsigned long h = hash(key) % table->size;
- // TYPE* mem = (table->values + h);
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;
}