43 lines
1.4 KiB
C
43 lines
1.4 KiB
C
// 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
|