1
0
Fork 0

Added: Hash store

Browse Source
This commit is contained in:
rhuibertsjr 2024-04-13 14:15:54 +02:00
parent ccda74a701
commit 1faad172e9

59
src/hash-store.c Normal file
View File

@ -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;
}