diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-01-22 14:32:27 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-01-22 14:32:27 +0100 |
commit | aa4fbbfe61da25f430784385e04299065f5d07d3 (patch) | |
tree | 7a5a461909a24bd0379658110b520336b135a9d4 /hash.inc | |
parent | Remove superfluous file trie.c. (diff) | |
download | calp-aa4fbbfe61da25f430784385e04299065f5d07d3.tar.gz calp-aa4fbbfe61da25f430784385e04299065f5d07d3.tar.xz |
Rename hash_help.inc => hash.inc.
Diffstat (limited to 'hash.inc')
-rw-r--r-- | hash.inc | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/hash.inc b/hash.inc new file mode 100644 index 00000000..34766b54 --- /dev/null +++ b/hash.inc @@ -0,0 +1,65 @@ +#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]; + + // 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; +} + +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 */ + +// vim: ft=c |