make hash table use an arena

This commit is contained in:
Matthew 2025-08-28 06:04:25 +10:00
parent 94b9561545
commit 6a757dd36b

48
util.d
View File

@ -13,40 +13,6 @@ import std.string;
import core.stdc.string : memset; import core.stdc.string : memset;
import core.simd; import core.simd;
struct DynSlice(T)
{
T[][] slices;
u32 length;
u32 capacity;
u32 grow_size;
}
DynSlice!(T)
CreateDynSlice(T)(u32 size)
{
DynSlice!(T) dslice = {
slices: MAllocArray!(T[])(size),
length: 0,
capacity: size,
grow_size: size,
};
dslice.slices[0] = MAllocArray!(T)(size);
return dslice;
}
u32
Next(T)(DynSlice!(T)* slice)
{
if (slice.length < slice.capacity)
{
}
return 0;
}
void void
Logf(Args...)(string fmt, Args args) Logf(Args...)(string fmt, Args args)
{ {
@ -248,6 +214,7 @@ struct HashTable(K, V)
SLList!(P) free_lists; SLList!(P) free_lists;
SLList!(P)[] lists; SLList!(P)[] lists;
Node!(P)* nil; Node!(P)* nil;
Arena arena;
u64 node_count; u64 node_count;
u64 list_count; u64 list_count;
@ -270,20 +237,17 @@ struct HashTable(K, V)
return result; return result;
} }
Result!(V) opIndexUnary(string s: "~")(K key)
{
return Delete(&this, key);
}
} }
HashTable!(K, V) HashTable!(K, V)
CreateHashTable(K, V)(u64 size) CreateHashTable(K, V)(u64 size)
{ {
auto nil = Alloc!(Node!(KVPair!(K, V))); Arena arena = CreateArena(MB(4));
auto lists = AllocArray!(SLList!(KVPair!(K, V)))(size); auto nil = Alloc!(Node!(KVPair!(K, V)))(&arena);
auto lists = AllocArray!(SLList!(KVPair!(K, V)))(&arena, size);
HashTable!(K, V) table = { HashTable!(K, V) table = {
arena: arena,
lists: lists, lists: lists,
list_count: size, list_count: size,
nil: nil, nil: nil,
@ -326,7 +290,7 @@ Push(K, V)(HashTable!(K, V)* ht, K key, V value)
} }
else else
{ {
node = Alloc!(N); node = Alloc!(N)(&ht.arena);
} }
node.next = ht.nil; node.next = ht.nil;