Added: Hash store
This commit is contained in:
parent
ccda74a701
commit
1faad172e9
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user