diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2023-02-14 16:52:54 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-02-14 16:52:54 -0800 |
| commit | 7e696a40e51aa35b003aa9c3fc72139b34cddc71 (patch) | |
| tree | e11bdb6734f7580157e8bedfec4ceb88a640cd5a /hashtable.c | |
| parent | 9df04950a492da2d51277a388bab7f1e87ae50f5 (diff) | |
a lot of stuff. exec/eval works now
Diffstat (limited to 'hashtable.c')
| -rw-r--r-- | hashtable.c | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/hashtable.c b/hashtable.c index ce4c65a..7cb253c 100644 --- a/hashtable.c +++ b/hashtable.c @@ -1,9 +1,22 @@ #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; @@ -13,19 +26,11 @@ unsigned int get_hash(char* str) { } return hash; } -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; -} /* 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); - pair_t* memPair = malloc(sizeof(pair_t)); - *memPair = pair; - hashtable->pairs[hash % hashtable->capacity] = memPair; +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) { |
