aboutsummaryrefslogtreecommitdiff
path: root/hash_help.inc
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-01-19 19:06:09 +0100
committerHugo Hörnquist <hugo@hornquist.se>2019-01-19 19:06:14 +0100
commitc42c2834d8c7b5d81465b9d9d127d8384151b9cb (patch)
tree53b1a8eb0368d5f91525604eea2c3173083c1955 /hash_help.inc
parentCan now parse entire directory in one go. (diff)
downloadcalp-c42c2834d8c7b5d81465b9d9d127d8384151b9cb.tar.gz
calp-c42c2834d8c7b5d81465b9d9d127d8384151b9cb.tar.xz
[BROKEN] Work on adding hash tables.
Diffstat (limited to 'hash_help.inc')
-rw-r--r--hash_help.inc41
1 files changed, 41 insertions, 0 deletions
diff --git a/hash_help.inc b/hash_help.inc
new file mode 100644
index 00000000..19463be4
--- /dev/null
+++ b/hash_help.inc
@@ -0,0 +1,41 @@
+#ifndef TYPE
+#error "Set TYPE to something before including this header"
+#else
+
+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;
+
+ /* TODO conflict resolution */
+
+ ++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) {
+ return 0;
+ } else if (strcmp(mem->key.mem, key) == 0) {
+ return mem;
+ } else {
+ /* TODO fix retrival on invalid key */
+ }
+ return 0;
+}
+
+#endif /* TYPE */
+
+// vim: ft=c