diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/hashtable.c | 52 | ||||
-rw-r--r-- | src/hashtable.h | 14 | ||||
-rw-r--r-- | src/main.c | 28 |
3 files changed, 94 insertions, 0 deletions
diff --git a/src/hashtable.c b/src/hashtable.c new file mode 100644 index 0000000..14ab58a --- /dev/null +++ b/src/hashtable.c @@ -0,0 +1,52 @@ +#include <stddef.h> +#include <stdlib.h> +#include <stdio.h> + +#include "hashtable.h" + +struct { + void** data; + size_t size; +} typedef HashTableImpl; + +static int hash(char key) { + return key % 7;//TODO +} + +HashTable hashtable_init() { + HashTableImpl* ht = (HashTableImpl*) malloc(sizeof(HashTableImpl)); + + int len = 8; + ht->data = (void**) malloc(sizeof(void*) * len); + ht->size = len; + + return (HashTable) ht; +} + +int hashtable_deinit(HashTable* ht) { + HashTableImpl* ht_impl = (HashTableImpl*) *ht; + free(ht_impl->data); + free(ht_impl); + ht = NULL; + return 0; +} + +void* hashtable_get(HashTable ht, char key) { + HashTableImpl* ht_impl = (HashTableImpl*) ht; + + int index = hash(key); + + void* res = ht_impl->data[index]; + + return res; +} + +int hashtable_put(HashTable ht, char key, void* val) { + HashTableImpl* ht_impl = (HashTableImpl*) ht; + + int index = hash(key); + + ht_impl->data[index] = val; + + return 0; +} diff --git a/src/hashtable.h b/src/hashtable.h new file mode 100644 index 0000000..2a31204 --- /dev/null +++ b/src/hashtable.h @@ -0,0 +1,14 @@ +#ifndef __HASHTABLE_H__ +#define __HASHTABLE_H__ + +void typedef *HashTable; + +HashTable hashtable_init(); + +int hashtable_deinit(HashTable*); + +int hashtable_put(HashTable, char, void*); + +void* hashtable_get(HashTable, char); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..d1d84bf --- /dev/null +++ b/src/main.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include "hashtable.h" + +int main(int argc, char** argv) { + printf("Testing hashing:\n"); + + HashTable ht = hashtable_init(); + + char res = hashtable_get(ht, 'a'); + + printf("Result: %c\n", res); + + hashtable_put(ht, 'a', 'x'); + + res = hashtable_get(ht, 'a'); + + printf("Result: %c\n", res); + + hashtable_put(ht, 'h', '1'); + + res = hashtable_get(ht, 'a'); + + printf("Result: %c\n", res); + + hashtable_deinit(&ht); + + return 0; +} |