From 404588d4911f9563849008bac33d61c733f10da4 Mon Sep 17 00:00:00 2001 From: Kyler Date: Fri, 28 Nov 2025 15:52:40 -0700 Subject: [PATCH] Started hash table --- SLS_C/include/sls/hash_table.h | 42 +++++++++++++++++++++++++++++++++ SLS_C/include/sls/interpreter.h | 5 ++-- SLS_C/src/hash_table.c | 6 +++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 SLS_C/include/sls/hash_table.h create mode 100644 SLS_C/src/hash_table.c diff --git a/SLS_C/include/sls/hash_table.h b/SLS_C/include/sls/hash_table.h new file mode 100644 index 0000000..5c4d1be --- /dev/null +++ b/SLS_C/include/sls/hash_table.h @@ -0,0 +1,42 @@ +// Kyler Olsen +// YREA SLS +// Hash Table Header +// November 2025 + +#ifndef SLS_HASH_TABLE_H +#define SLS_HASH_TABLE_H + +#include + +#include "sls/string.h" + +typedef struct Bucket { + size_t key_hash_b; + void *item; + struct Bucket *next; +} Bucket; + +typedef struct { + size_t buckets_count; + Bucket *buckets; +} HashTable; + +// Initializes a HashTable with buckets_count number of buckets. Returns NULL on Memory Allocation Error. +HashTable *init_hash_table(size_t buckets_count); +// Frees memory owned by the HashTable. +void del_hash_table(HashTable *ht); + +// The hash functions used by HashTable. Returns the hash of the string. +// static size_t sls_hash_a(SlsStr key); // For bucket index (in HashTable). +// static size_t sls_hash_b(SlsStr key); // For bucket key_id (in Bucket). +// Calculates the bucket index based on the number of buckets in the HashTable and the hash (a) of the string. +// static size_t bucket_index(const HashTable *ht, SlsStr key); + +// Inserts an item into the HashTable based on its key. Returns FALSE on Memory Allocation Error. +Boolean hash_table_put(HashTable *ht, SlsStr key, void *item); +// Gets the item associated with the given key. Returns default item if not found. +void *hash_table_get(const HashTable *ht, SlsStr key, void *default_item); +// Deletes the item associated with the given key. Returns FALSE if item is not found. +Boolean hash_table_del(HashTable *ht, SlsStr key); + +#endif // SLS_HASH_TABLE_H diff --git a/SLS_C/include/sls/interpreter.h b/SLS_C/include/sls/interpreter.h index a07a79a..1d3b5a0 100644 --- a/SLS_C/include/sls/interpreter.h +++ b/SLS_C/include/sls/interpreter.h @@ -9,6 +9,7 @@ #include #include "sls/lexer.h" +#include "sls/hash_table.h" typedef enum { STACK_IDENTIFIER, @@ -48,12 +49,12 @@ typedef struct StackItem { Boolean boolean; TokenString token_string; }; - StackItem *next; + struct StackItem *next; } StackItem; typedef struct { StackItem *stack; - + HashTable functions; } InterpreterState; #endif // SLS_INTERPRETER_H diff --git a/SLS_C/src/hash_table.c b/SLS_C/src/hash_table.c new file mode 100644 index 0000000..40bdb02 --- /dev/null +++ b/SLS_C/src/hash_table.c @@ -0,0 +1,6 @@ +// Kyler Olsen +// YREA SLS +// Hash Table +// November 2025 + +#include "sls/hash_table.h"