From e37092c768ed6820641c0884e73e0c38dd00e9e8 Mon Sep 17 00:00:00 2001 From: Baitinq Date: Thu, 2 Jan 2025 15:28:06 +0100 Subject: Initial commit --- src/hashtable.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/hashtable.h | 14 ++++++++++++++ src/main.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/hashtable.c create mode 100644 src/hashtable.h create mode 100644 src/main.c (limited to 'src') 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 +#include +#include + +#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 +#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; +} -- cgit 1.4.1