aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-03-08 10:06:15 +0100
committerHugo Hörnquist <hugo@hornquist.se>2019-03-08 10:06:15 +0100
commit161163fdaf2b58cf725457112e5ea79b1dba0fee (patch)
tree6a17d7164d4339a03e2103835f6b8c8d3a281eac
parentRemove C vector library. (diff)
downloadcalp-161163fdaf2b58cf725457112e5ea79b1dba0fee.tar.gz
calp-161163fdaf2b58cf725457112e5ea79b1dba0fee.tar.xz
Remove C hash library.
-rw-r--r--hash.c15
-rw-r--r--hash.h37
-rw-r--r--hash.inc.h61
3 files changed, 0 insertions, 113 deletions
diff --git a/hash.c b/hash.c
deleted file mode 100644
index 47775b59..00000000
--- a/hash.c
+++ /dev/null
@@ -1,15 +0,0 @@
-#include "hash.h"
-
-/*
- * http://www.cse.yorku.ca/~oz/hash.html
- * djb2 from above url.
- */
-unsigned long hash(char* str) {
- unsigned long hash = 5381;
- int c;
-
- while ( (c = *str++) )
- hash = ((hash << 5) + hash) + c;
-
- return hash;
-}
diff --git a/hash.h b/hash.h
deleted file mode 100644
index e1ff7385..00000000
--- a/hash.h
+++ /dev/null
@@ -1,37 +0,0 @@
-#ifndef HASH_H
-#define HASH_H
-
-#include <string.h>
-
-#include "macro.h"
-
-unsigned long hash(char*);
-
-#define HASHT(T) TEMPL(hash_t, T)
-
-#endif /* HASH_H */
-#ifdef TYPE
-
-typedef struct {
- int size;
- int item_count;
- /* NOTE
- * Hash maps are always assumed to hold pointers to objects
- * Double pointer means a list of pointers.
- */
- TYPE** values;
-} TABLE(TYPE);
-
-int PUSH(HASHT(TYPE)) ( HASHT(TYPE)* table, TYPE* value );
-
-INIT_F(HASHT(TYPE), int init_size );
-
-TYPE* GET(HASHT(TYPE)) ( HASHT(TYPE)* table, char* key );
-
-/*
- * Free's all item's stored in table.
- * And finally frees table.
- */
-FREE_F(HASHT(TYPE));
-
-#endif /* HASH_H */
diff --git a/hash.inc.h b/hash.inc.h
deleted file mode 100644
index 0945dfc4..00000000
--- a/hash.inc.h
+++ /dev/null
@@ -1,61 +0,0 @@
-#ifndef TYPE
-#error "Set TYPE to something before including self header"
-#else
-
-#include "err.h"
-
-int PUT(HASHT(TYPE)) ( HASHT(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;
-}
-
-INIT_F(HASHT(TYPE), 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* GET(HASHT(TYPE)) ( HASHT(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;
- }
-}
-
-FREE(HASHT(TYPE)) {
- /*
- * 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 */