summaryrefslogtreecommitdiff
path: root/hashtable.c
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-02-16 14:32:56 -0800
committerKai Stevenson <kai@kaistevenson.com>2023-02-16 14:32:56 -0800
commit05540fc6038f452f5dc5f18412c918c33ad96027 (patch)
treebb9f572a1cc6f3a66cbf9835593f62db87481e0a /hashtable.c
parent10a1f7e4ad1bc90436f58269a52cddfd0587be33 (diff)
changed project structure
Diffstat (limited to 'hashtable.c')
-rw-r--r--hashtable.c39
1 files changed, 0 insertions, 39 deletions
diff --git a/hashtable.c b/hashtable.c
deleted file mode 100644
index 7cb253c..0000000
--- a/hashtable.c
+++ /dev/null
@@ -1,39 +0,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include "hashtable.h"
-
-const unsigned int fnvPrime = 16777619;
-const unsigned int fnvOffset = 2166136261U;
-
-pair_t* init_pair(char* key, void* value) {
- pair_t* p = malloc(sizeof(pair_t));
- p->key = key;
- p->value = value;
- return p;
-}
-hashtable_t* init_hashtable(unsigned int capacity) {
- hashtable_t* ht = malloc(sizeof(hashtable_t));
- ht->pairs = calloc(capacity, sizeof(pair_t*));
- ht->capacity = capacity;
- return ht;
-}
-//FNV-1a hash
-unsigned int get_hash(char* str) {
- unsigned int hash = fnvOffset;
- while (*str != '\0') {
- hash = (hash ^ *str) * fnvPrime;
- str++;
- }
- return hash;
-}
-/* TODO: handle collision
-TODO: return 0 if something goes wrong */
-unsigned int hashtable_add_pair(hashtable_t* hashtable, pair_t* pair) {
- unsigned int hash = get_hash(pair->key);
- hashtable->pairs[hash % hashtable->capacity] = pair;
- return hash;
-}
-pair_t* hashtable_get_pair(hashtable_t* hashtable, char* key) {
- unsigned int hash = get_hash(key);
- return hashtable->pairs[hash % hashtable->capacity];
-}