Started hash table

This commit is contained in:
Kyler Olsen 2025-11-28 15:52:40 -07:00
parent fb4fe5ad66
commit 404588d491
3 changed files with 51 additions and 2 deletions

View File

@ -0,0 +1,42 @@
// Kyler Olsen
// YREA SLS
// Hash Table Header
// November 2025
#ifndef SLS_HASH_TABLE_H
#define SLS_HASH_TABLE_H
#include <stddef.h>
#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

View File

@ -9,6 +9,7 @@
#include <stddef.h>
#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

6
SLS_C/src/hash_table.c Normal file
View File

@ -0,0 +1,6 @@
// Kyler Olsen
// YREA SLS
// Hash Table
// November 2025
#include "sls/hash_table.h"