2024-11-30 22:40:14 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../project/gen.hpp"
|
|
|
|
#include "containers.array.hpp"
|
|
|
|
|
|
|
|
using namespace gen;
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
global s32 HashTable_DefinitionCounter = 0;
|
|
|
|
|
2024-11-30 22:40:14 -08:00
|
|
|
CodeBody gen_hashtable_base()
|
|
|
|
{
|
2024-12-04 23:53:14 -08:00
|
|
|
CodeBody struct_def = parse_global_body( code(
|
2024-12-05 20:02:26 -08:00
|
|
|
typedef struct HT_FindResult_Def HT_FindResult;
|
|
|
|
struct HT_FindResult_Def
|
2024-11-30 22:40:14 -08:00
|
|
|
{
|
|
|
|
ssize HashIndex;
|
|
|
|
ssize PrevIndex;
|
|
|
|
ssize EntryIndex;
|
|
|
|
};
|
|
|
|
));
|
2024-12-04 23:53:14 -08:00
|
|
|
|
|
|
|
Code define_type = untyped_str(txt(
|
2024-12-05 00:41:08 -08:00
|
|
|
R"(#define HashTable(_type) struct _type
|
2024-12-04 23:53:14 -08:00
|
|
|
)"
|
|
|
|
));
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
Code define_critical_load_scale = untyped_str(txt("#define HashTable_CriticalLoadScale 0.7f\n"));
|
|
|
|
return def_global_body(args(struct_def, define_type, define_critical_load_scale));
|
2024-11-30 22:40:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeBody gen_hashtable( StrC type, StrC hashtable_name )
|
|
|
|
{
|
|
|
|
|
2024-12-05 00:41:08 -08:00
|
|
|
String tbl_type = {(char*) hashtable_name.duplicate(GlobalAllocator).Ptr};
|
|
|
|
String fn = tbl_type.duplicate(GlobalAllocator);
|
2024-12-05 14:04:17 -08:00
|
|
|
// str_to_lower(fn.Data);
|
2024-11-30 22:40:14 -08:00
|
|
|
|
|
|
|
String name_lower = String::make( GlobalAllocator, hashtable_name );
|
2024-12-05 14:04:17 -08:00
|
|
|
// str_to_lower( name_lower.Data );
|
2024-11-30 22:40:14 -08:00
|
|
|
|
|
|
|
String hashtable_entry = String::fmt_buf( GlobalAllocator, "HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
|
|
|
|
String entry_array_name = String::fmt_buf( GlobalAllocator, "Arr_HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
|
|
|
|
String entry_array_fn_ns = String::fmt_buf( GlobalAllocator, "arr_hte_%.*s", name_lower.length(), name_lower.Data );
|
|
|
|
|
|
|
|
CodeBody hashtable_types = parse_global_body( token_fmt(
|
|
|
|
"type", (StrC) type,
|
|
|
|
"tbl_name", (StrC) hashtable_name,
|
|
|
|
"tbl_type", (StrC) tbl_type,
|
|
|
|
stringize(
|
2024-12-05 14:48:24 -08:00
|
|
|
typedef struct HashTable_<type> <tbl_type>;
|
2024-11-30 22:40:14 -08:00
|
|
|
typedef struct HTE_<tbl_name> HTE_<tbl_name>;
|
2024-12-05 20:02:26 -08:00
|
|
|
struct HTE_<tbl_name> {
|
2024-11-30 22:40:14 -08:00
|
|
|
u64 Key;
|
|
|
|
ssize Next;
|
|
|
|
<type> Value;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef void (* <tbl_type>_MapProc) ( <tbl_type> self, u64 key, <type> value );
|
|
|
|
typedef void (* <tbl_type>_MapMutProc) ( <tbl_type> self, u64 key, <type>* value );
|
|
|
|
)));
|
|
|
|
|
|
|
|
CodeBody entry_array = gen_array( hashtable_entry, entry_array_name );
|
|
|
|
|
|
|
|
#pragma push_macro( "GEN_ASSERT" )
|
|
|
|
#pragma push_macro( "GEN_ASSERT_NOT_NULL" )
|
2024-12-05 20:02:26 -08:00
|
|
|
#pragma push_macro( "rcast" )
|
|
|
|
#pragma push_macro( "cast" )
|
|
|
|
#pragma push_macro( "typeof" )
|
|
|
|
#pragma push_macro( "forceinline" )
|
2024-11-30 22:40:14 -08:00
|
|
|
#undef GEN_ASSERT
|
|
|
|
#undef GEN_ASSERT_NOT_NULL
|
2024-12-05 20:02:26 -08:00
|
|
|
#undef GEN_ASSERT
|
|
|
|
#undef rcast
|
|
|
|
#undef cast
|
|
|
|
#undef typeof
|
|
|
|
#undef forceinline
|
2024-11-30 22:40:14 -08:00
|
|
|
CodeBody hashtable_def = parse_global_body( token_fmt(
|
|
|
|
"type", (StrC) type,
|
|
|
|
"tbl_name", (StrC) hashtable_name,
|
|
|
|
"tbl_type", (StrC) tbl_type,
|
|
|
|
"fn", (StrC) fn,
|
|
|
|
"entry_type", (StrC) hashtable_entry,
|
|
|
|
"array_entry", (StrC) entry_array_name,
|
|
|
|
"fn_array", (StrC) entry_array_fn_ns,
|
|
|
|
stringize(
|
2024-12-05 20:02:26 -08:00
|
|
|
struct HashTable_<type> {
|
2024-11-30 22:40:14 -08:00
|
|
|
Array_ssize Hashes;
|
|
|
|
<array_entry> Entries;
|
|
|
|
};
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
<tbl_type> <fn>_init ( AllocatorInfo allocator );
|
|
|
|
<tbl_type> <fn>_init_reserve( AllocatorInfo allocator, ssize num );
|
|
|
|
void <fn>_clear ( <tbl_type> self );
|
|
|
|
void <fn>_destroy ( <tbl_type>* self );
|
|
|
|
<type>* <fn>_get ( <tbl_type> self, u64 key );
|
|
|
|
void <fn>_map ( <tbl_type> self, <tbl_type>_MapProc map_proc );
|
|
|
|
void <fn>_map_mut ( <tbl_type> self, <tbl_type>_MapMutProc map_proc );
|
2024-11-30 22:40:14 -08:00
|
|
|
void <fn>_grow ( <tbl_type>* self );
|
|
|
|
void <fn>_rehash ( <tbl_type>* self, ssize new_num );
|
2024-12-05 20:02:26 -08:00
|
|
|
void <fn>_rehash_fast ( <tbl_type> self );
|
|
|
|
void <fn>_remove ( <tbl_type> self, u64 key );
|
|
|
|
void <fn>_remove_entry( <tbl_type> self, ssize idx );
|
2024-11-30 22:40:14 -08:00
|
|
|
void <fn>_set ( <tbl_type>* self, u64 key, <type> value );
|
2024-12-05 20:02:26 -08:00
|
|
|
ssize <fn>_slot ( <tbl_type> self, u64 key );
|
2024-11-30 22:40:14 -08:00
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
ssize <fn>__add_entry( <tbl_type>* self, u64 key );
|
|
|
|
HT_FindResult <fn>__find ( <tbl_type> self, u64 key );
|
|
|
|
b32 <fn>__full ( <tbl_type> self );
|
2024-11-30 22:40:14 -08:00
|
|
|
|
2024-12-08 22:33:37 -08:00
|
|
|
<tbl_type> <fn>_init( AllocatorInfo allocator )
|
2024-11-30 22:40:14 -08:00
|
|
|
{
|
2024-12-08 13:37:04 -08:00
|
|
|
<tbl_type> result = hashtable_init_reserve(<type>, allocator, 8);
|
2024-11-30 22:40:14 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
<tbl_type> <fn>_init_reserve( AllocatorInfo allocator, ssize num )
|
2024-11-30 22:40:14 -08:00
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
<tbl_type> result = { NULL, NULL };
|
2024-12-08 13:37:04 -08:00
|
|
|
result.Hashes = array_init_reserve(ssize, allocator, num );
|
2024-12-05 20:02:26 -08:00
|
|
|
array_get_header(result.Hashes)->Num = num;
|
|
|
|
array_resize(result.Hashes, num);
|
|
|
|
array_fill(result.Hashes, 0, num, -1);
|
2024-11-30 22:40:14 -08:00
|
|
|
|
2024-12-08 13:37:04 -08:00
|
|
|
result.Entries = array_init_reserve(<entry_type>, allocator, num );
|
2024-11-30 22:40:14 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void <fn>_clear( <tbl_type> self )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self.Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self.Entries);
|
2024-12-05 14:48:24 -08:00
|
|
|
array_clear( self.Entries );
|
2024-12-05 20:02:26 -08:00
|
|
|
s32 what = array_num(self.Hashes);
|
|
|
|
array_fill( self.Hashes, 0, what, (ssize)-1 );
|
2024-11-30 22:40:14 -08:00
|
|
|
}
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
void <fn>_destroy( <tbl_type>* self )
|
2024-11-30 22:40:14 -08:00
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self);
|
|
|
|
GEN_ASSERT_NOT_NULL(self->Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self->Entries);
|
|
|
|
if ( self->Hashes && array_get_header(self->Hashes)->Capacity) {
|
|
|
|
array_free( self->Hashes );
|
|
|
|
array_free( self->Entries );
|
2024-11-30 22:40:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<type>* <fn>_get( <tbl_type> self, u64 key )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self.Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self.Entries);
|
2024-11-30 22:40:14 -08:00
|
|
|
ssize idx = <fn>__find( self, key ).EntryIndex;
|
|
|
|
if ( idx > 0 )
|
|
|
|
return & self.Entries[idx].Value;
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
return nullptr;
|
2024-11-30 22:40:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void <fn>_map( <tbl_type> self, <tbl_type>_MapProc map_proc )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self.Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self.Entries);
|
2024-11-30 22:40:14 -08:00
|
|
|
GEN_ASSERT_NOT_NULL( map_proc );
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
for ( ssize idx = 0; idx < array_get_header( self.Entries )->Num; idx++ ) {
|
2024-11-30 22:40:14 -08:00
|
|
|
map_proc( self, self.Entries[idx].Key, self.Entries[idx].Value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void <fn>_map_mut( <tbl_type> self, <tbl_type>_MapMutProc map_proc )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self.Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self.Entries);
|
2024-11-30 22:40:14 -08:00
|
|
|
GEN_ASSERT_NOT_NULL( map_proc );
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
for ( ssize idx = 0; idx < array_get_header( self.Entries )->Num; idx++ ) {
|
2024-11-30 22:40:14 -08:00
|
|
|
map_proc( self, self.Entries[idx].Key, & self.Entries[idx].Value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void <fn>_grow( <tbl_type>* self )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self);
|
|
|
|
GEN_ASSERT_NOT_NULL(self->Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self->Entries);
|
2024-12-05 14:48:24 -08:00
|
|
|
ssize new_num = array_grow_formula( array_get_header( self->Entries )->Num );
|
2024-12-05 20:02:26 -08:00
|
|
|
hashtable_rehash( self, new_num );
|
2024-11-30 22:40:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void <fn>_rehash( <tbl_type>* self, ssize new_num )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self);
|
|
|
|
GEN_ASSERT_NOT_NULL(self->Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self->Entries);
|
|
|
|
GEN_ASSERT( new_num > 0 );
|
2024-11-30 22:40:14 -08:00
|
|
|
ssize idx;
|
|
|
|
ssize last_added_index;
|
|
|
|
|
2024-12-05 14:48:24 -08:00
|
|
|
ArrayHeader* old_hash_header = array_get_header( self->Hashes );
|
|
|
|
ArrayHeader* old_entries_header = array_get_header( self->Entries );
|
2024-11-30 22:40:14 -08:00
|
|
|
|
2024-12-08 13:37:04 -08:00
|
|
|
<tbl_type> new_tbl = hashtable_init_reserve( <type>, old_hash_header->Allocator, old_hash_header->Num );
|
2024-11-30 22:40:14 -08:00
|
|
|
|
2024-12-05 14:48:24 -08:00
|
|
|
ArrayHeader* new_hash_header = array_get_header( new_tbl.Hashes );
|
2024-11-30 22:40:14 -08:00
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
for (ssize idx = 0; idx < cast(ssize, old_hash_header->Num); ++idx)
|
2024-11-30 22:40:14 -08:00
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
<entry_type>* entry = & self->Entries[idx];
|
|
|
|
HT_FindResult find_result;
|
2024-11-30 22:40:14 -08:00
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
find_result = <fn>__find( new_tbl, entry->Key);
|
|
|
|
last_added_index = <fn>__add_entry( & new_tbl, entry->Key);
|
2024-11-30 22:40:14 -08:00
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
if (find_result.PrevIndex < 0)
|
|
|
|
new_tbl.Hashes[find_result.HashIndex] = last_added_index;
|
2024-11-30 22:40:14 -08:00
|
|
|
else
|
2024-12-05 20:02:26 -08:00
|
|
|
new_tbl.Entries[find_result.PrevIndex].Next = last_added_index;
|
2024-11-30 22:40:14 -08:00
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
new_tbl.Entries[last_added_index].Next = find_result.EntryIndex;
|
|
|
|
new_tbl.Entries[last_added_index].Value = entry->Value;
|
2024-11-30 22:40:14 -08:00
|
|
|
}
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
<fn>_destroy( self );
|
2024-11-30 22:40:14 -08:00
|
|
|
* self = new_tbl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void <fn>_rehash_fast( <tbl_type> self )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self.Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self.Entries);
|
2024-11-30 22:40:14 -08:00
|
|
|
ssize idx;
|
|
|
|
|
2024-12-05 14:48:24 -08:00
|
|
|
for ( idx = 0; idx < array_get_header( self.Entries )->Num; idx++ )
|
2024-11-30 22:40:14 -08:00
|
|
|
self.Entries[ idx ].Next = -1;
|
|
|
|
|
2024-12-05 14:48:24 -08:00
|
|
|
for ( idx = 0; idx < array_get_header( self.Hashes )->Num; idx++ )
|
2024-11-30 22:40:14 -08:00
|
|
|
self.Hashes[ idx ] = -1;
|
|
|
|
|
2024-12-05 14:48:24 -08:00
|
|
|
for ( idx = 0; idx < array_get_header( self.Entries )->Num; idx++ )
|
2024-11-30 22:40:14 -08:00
|
|
|
{
|
|
|
|
<entry_type>* entry;
|
|
|
|
HT_FindResult find_result;
|
|
|
|
|
|
|
|
entry = & self.Entries[ idx ];
|
|
|
|
find_result = <fn>__find( self, entry->Key );
|
|
|
|
|
|
|
|
if ( find_result.PrevIndex < 0 )
|
|
|
|
self.Hashes[ find_result.HashIndex ] = idx;
|
|
|
|
else
|
|
|
|
self.Entries[ find_result.PrevIndex ].Next = idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void <fn>_remove( <tbl_type> self, u64 key )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self.Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self.Entries);
|
2024-11-30 22:40:14 -08:00
|
|
|
HT_FindResult find_result = <fn>__find( self, key );
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
if ( find_result.EntryIndex >= 0 ) {
|
2024-12-05 14:48:24 -08:00
|
|
|
array_remove_at( self.Entries, find_result.EntryIndex );
|
2024-12-05 20:02:26 -08:00
|
|
|
hashtable_rehash_fast( self );
|
2024-11-30 22:40:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void <fn>_remove_entry( <tbl_type> self, ssize idx )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self.Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self.Entries);
|
2024-12-05 14:48:24 -08:00
|
|
|
array_remove_at( self.Entries, idx );
|
2024-11-30 22:40:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void <fn>_set( <tbl_type>* self, u64 key, <type> value )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self);
|
|
|
|
GEN_ASSERT_NOT_NULL(self->Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self->Entries);
|
2024-12-05 14:48:24 -08:00
|
|
|
ssize idx;
|
2024-11-30 22:40:14 -08:00
|
|
|
HT_FindResult find_result;
|
|
|
|
|
2024-12-05 14:48:24 -08:00
|
|
|
if ( array_get_header( self->Hashes )->Num == 0 )
|
2024-12-05 20:02:26 -08:00
|
|
|
hashtable_grow( self );
|
2024-11-30 22:40:14 -08:00
|
|
|
|
|
|
|
find_result = <fn>__find( * self, key );
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
if ( find_result.EntryIndex >= 0 ) {
|
2024-11-30 22:40:14 -08:00
|
|
|
idx = find_result.EntryIndex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
idx = <fn>__add_entry( self, key );
|
2024-11-30 22:40:14 -08:00
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
if ( find_result.PrevIndex >= 0 ) {
|
2024-11-30 22:40:14 -08:00
|
|
|
self->Entries[ find_result.PrevIndex ].Next = idx;
|
|
|
|
}
|
2024-12-05 20:02:26 -08:00
|
|
|
else {
|
2024-11-30 22:40:14 -08:00
|
|
|
self->Hashes[ find_result.HashIndex ] = idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self->Entries[ idx ].Value = value;
|
|
|
|
|
|
|
|
if ( <fn>__full( * self ) )
|
2024-12-05 20:02:26 -08:00
|
|
|
hashtable_grow( self );
|
2024-11-30 22:40:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize <fn>_slot( <tbl_type> self, u64 key )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self.Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self.Entries);
|
2024-12-05 14:48:24 -08:00
|
|
|
for ( ssize idx = 0; idx < array_get_header( self.Hashes )->Num; ++idx )
|
2024-11-30 22:40:14 -08:00
|
|
|
if ( self.Hashes[ idx ] == key )
|
|
|
|
return idx;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
ssize <fn>__add_entry( <tbl_type>* self, u64 key )
|
2024-11-30 22:40:14 -08:00
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self);
|
|
|
|
GEN_ASSERT_NOT_NULL(self->Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self->Entries);
|
2024-11-30 22:40:14 -08:00
|
|
|
ssize idx;
|
|
|
|
<entry_type> entry = { key, -1 };
|
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
idx = array_get_header( self->Entries )->Num;
|
|
|
|
array_append( self->Entries, entry );
|
2024-11-30 22:40:14 -08:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
HT_FindResult <fn>__find( <tbl_type> self, u64 key )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self.Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self.Entries);
|
2024-11-30 22:40:14 -08:00
|
|
|
HT_FindResult result = { -1, -1, -1 };
|
|
|
|
|
2024-12-05 14:48:24 -08:00
|
|
|
ArrayHeader* hash_header = array_get_header( self.Hashes );
|
2024-11-30 22:40:14 -08:00
|
|
|
if ( hash_header->Num > 0 )
|
|
|
|
{
|
|
|
|
result.HashIndex = key % hash_header->Num;
|
|
|
|
result.EntryIndex = self.Hashes[ result.HashIndex ];
|
|
|
|
|
|
|
|
while ( result.EntryIndex >= 0 )
|
|
|
|
{
|
|
|
|
if ( self.Entries[ result.EntryIndex ].Key == key )
|
|
|
|
break;
|
|
|
|
|
|
|
|
result.PrevIndex = result.EntryIndex;
|
|
|
|
result.EntryIndex = self.Entries[ result.EntryIndex ].Next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
b32 <fn>__full( <tbl_type> self )
|
|
|
|
{
|
2024-12-05 20:02:26 -08:00
|
|
|
GEN_ASSERT_NOT_NULL(self.Hashes);
|
|
|
|
GEN_ASSERT_NOT_NULL(self.Entries);
|
2024-12-05 14:48:24 -08:00
|
|
|
ArrayHeader* hash_header = array_get_header( self.Hashes );
|
|
|
|
ArrayHeader* entries_header = array_get_header( self.Entries );
|
2024-11-30 22:40:14 -08:00
|
|
|
|
2024-12-05 20:02:26 -08:00
|
|
|
usize critical_load = cast(usize, HashTable_CriticalLoadScale * cast(f32, hash_header->Num));
|
|
|
|
b32 result = entries_header->Num > critical_load;
|
|
|
|
return result;
|
2024-11-30 22:40:14 -08:00
|
|
|
}
|
|
|
|
)));
|
|
|
|
#pragma pop_macro( "GEN_ASSERT" )
|
|
|
|
#pragma pop_macro( "GEN_ASSERT_NOT_NULL" )
|
2024-12-05 20:02:26 -08:00
|
|
|
#pragma pop_macro( "rcast" )
|
|
|
|
#pragma pop_macro( "cast" )
|
|
|
|
#pragma pop_macro( "typeof" )
|
|
|
|
#pragma pop_macro( "forceinline" )
|
|
|
|
|
|
|
|
++ HashTable_DefinitionCounter;
|
2024-12-08 20:10:10 -08:00
|
|
|
StrC slot_str = String::fmt_buf(GlobalAllocator, "%d", HashTable_DefinitionCounter).to_strc();
|
2024-12-05 20:02:26 -08:00
|
|
|
|
2024-12-08 13:37:04 -08:00
|
|
|
Code generic_interface_slot = untyped_str(token_fmt( "type", type, "tbl_type", (StrC)tbl_type, "slot", (StrC)slot_str,
|
|
|
|
R"(#define GENERIC_SLOT_<slot>__hashtable_init <type>, <tbl_type>_init
|
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_init_reserve <type>, <tbl_type>_init_reserve
|
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_clear <tbl_type>, <tbl_type>_clear
|
2024-12-08 22:33:37 -08:00
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_destroy <tbl_type>, <tbl_type>_destroy
|
2024-12-08 13:37:04 -08:00
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_get <tbl_type>, <tbl_type>_get
|
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_map <tbl_type>, <tbl_type>_map
|
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_map_mut <tbl_type>, <tbl_type>_map_mut
|
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_grow <tbl_type>*, <tbl_type>_grow
|
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_rehash <tbl_type>*, <tbl_type>_rehash
|
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_rehash_fast <tbl_type>, <tbl_type>_rehash_fast
|
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_remove_entry <tbl_type>, <tbl_type>_remove_entry
|
2024-12-08 22:33:37 -08:00
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_set <tbl_type>, <tbl_type>_set
|
2024-12-08 13:37:04 -08:00
|
|
|
#define GENERIC_SLOT_<slot>__hashtable_slot <tbl_type>, <tbl_type>_slot
|
|
|
|
|
|
|
|
#define GENERIC_SLOT_<slot>__hashtable__add_entry <tbl_type>*, <tbl_type>__add_entry
|
|
|
|
#define GENERIC_SLOT_<slot>__hashtable__find <tbl_type>, <tbl_type>__find
|
|
|
|
#define GENERIC_SLOT_<slot>__hashtable__full <tbl_type>, <tbl_type>__full
|
2024-12-05 20:02:26 -08:00
|
|
|
)"
|
|
|
|
));
|
2024-11-30 22:40:14 -08:00
|
|
|
|
|
|
|
char const* cmt_str = str_fmt_buf( "Name: %.*s Type: %.*s"
|
|
|
|
, tbl_type.length(), tbl_type.Data
|
|
|
|
, type.Len, type.Ptr );
|
|
|
|
|
|
|
|
return def_global_body(args(
|
2024-12-04 08:30:54 -08:00
|
|
|
def_pragma( string_to_strc( string_fmt_buf( GlobalAllocator, "region %S", tbl_type ))),
|
2024-11-30 22:40:14 -08:00
|
|
|
fmt_newline,
|
2024-12-05 20:02:26 -08:00
|
|
|
generic_interface_slot,
|
|
|
|
fmt_newline,
|
2024-11-30 22:40:14 -08:00
|
|
|
hashtable_types,
|
|
|
|
fmt_newline,
|
|
|
|
entry_array,
|
|
|
|
hashtable_def,
|
|
|
|
fmt_newline,
|
2024-12-04 08:30:54 -08:00
|
|
|
def_pragma( string_to_strc( string_fmt_buf( GlobalAllocator, "endregion %S", tbl_type ))),
|
2024-11-30 22:40:14 -08:00
|
|
|
fmt_newline
|
|
|
|
));
|
|
|
|
}
|
2024-12-05 20:02:26 -08:00
|
|
|
|
|
|
|
CodeBody gen_hashtable_generic_selection_interface()
|
|
|
|
{
|
|
|
|
CodeBody interface_defines = def_body(CT_Global_Body);
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_init"), GenericSel_Direct_Type ));
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_init_reserve"), GenericSel_Direct_Type ));
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_clear"), GenericSel_Default, GenericSel_One_Arg ));
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_destroy"), GenericSel_By_Ref, GenericSel_One_Arg ) );
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_get") ));
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_grow"), GenericSel_Default, GenericSel_One_Arg ));
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_rehash") ));
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_rehash_fast"), GenericSel_Default, GenericSel_One_Arg ));
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_remove") ));
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_remove_entry") ));
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_set"), GenericSel_By_Ref ));
|
|
|
|
interface_defines.append( gen_generic_selection_function_macro( HashTable_DefinitionCounter, txt("hashtable_slot") ));
|
|
|
|
return interface_defines;
|
|
|
|
}
|