summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-02 15:28:06 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-03 01:03:24 +0100
commite37092c768ed6820641c0884e73e0c38dd00e9e8 (patch)
tree7ab40736601961da194c9f3629312fcc6b71e626 /src
downloadc-hashtable-e37092c768ed6820641c0884e73e0c38dd00e9e8.tar.gz
c-hashtable-e37092c768ed6820641c0884e73e0c38dd00e9e8.tar.bz2
c-hashtable-e37092c768ed6820641c0884e73e0c38dd00e9e8.zip
Initial commit
Diffstat (limited to 'src')
-rw-r--r--src/hashtable.c52
-rw-r--r--src/hashtable.h14
-rw-r--r--src/main.c28
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;
+}