From 1faad172e9fbe0a59cbb82a73b762a2a788ebb1b Mon Sep 17 00:00:00 2001 From: rhuibertsjr Date: Sat, 13 Apr 2024 14:15:54 +0200 Subject: [PATCH] Added: Hash store --- src/hash-store.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/hash-store.c diff --git a/src/hash-store.c b/src/hash-store.c new file mode 100644 index 0000000..8a6cb67 --- /dev/null +++ b/src/hash-store.c @@ -0,0 +1,59 @@ + +internal Table +hash_store_initialize(void) +{ + Table result = {0}; + + result.ptr_arena = arena_initialize(KB(1)); + result.str8_arena = arena_initialize(KB(1)); + + result.str8_ptr = arena_push(result.ptr_arena, String8); + result.str8_count = 1; + result.str8_ptr[0] = str8_lit(""); + + return result; +} + +internal Key +hash_store_string8_to_key(Table *table, String8 value) +{ + Key result = 0; + u32 count = table->str8_count; + result = count; + + String8 *str8_ptr = table->str8_ptr; + for (u32 index = 0; index < count; index += 1, str8_ptr += 1) + { + if (string8_match(*str8_ptr, value)) + { + result = index; + break; + } + } + + if (result == count) + { + String8 *new_str8 = arena_push(table->ptr_arena, String8); + new_str8->content = + (u8*) arena_allocate(table->str8_arena, sizeof(u8) * value.length); + new_str8->length = value.length; + + memory_copy(new_str8->content, value.content, value.length); + table->str8_count += 1; + } + + return result; +} + +internal String8 +hash_store_string8_from_key(Table *table, Key index) +{ + String8 result = {0}; + + if (index < table->str8_count) + { + result = table->str8_ptr[index]; + } + + return result; +}