Doing some heavy refactors on the container interfaces

Trying to set up their use to be more ideomatic to the core containers
This commit is contained in:
2024-05-31 17:58:18 -04:00
parent 3db403017e
commit f415eada0d
20 changed files with 133 additions and 98 deletions

View File

@ -24,13 +24,13 @@ HMapChainedSlot :: struct( $Type : typeid ) {
occupied : b32,
}
HMapChained :: struct( $ Type : typeid ) {
HMapChainedHeader :: struct( $ Type : typeid ) {
pool : Pool,
lookup : [] ^HMapChainedSlot(Type),
}
HMapChainedPtr :: struct( $ Type : typeid) {
using header : ^HMapChained(Type),
HMapChained :: struct( $ Type : typeid) {
using header : ^HMapChainedHeader(Type),
}
// Provides the nearest prime number value for the given capacity
@ -50,21 +50,22 @@ hmap_closest_prime :: proc( capacity : uint ) -> uint
return prime_table[len(prime_table) - 1]
}
hmap_chained_init :: proc( $Type : typeid, lookup_capacity : uint, allocator : Allocator,
hmap_chained_init :: proc( $HMapChainedType : typeid/HMapChained($Type), lookup_capacity : uint,
allocator := context.allocator,
pool_bucket_cap : uint = 1 * Kilo,
pool_bucket_reserve_num : uint = 0,
pool_alignment : uint = mem.DEFAULT_ALIGNMENT,
dbg_name : string = ""
) -> (table : HMapChainedPtr(Type), error : AllocatorError)
) -> (table : HMapChained(Type), error : AllocatorError)
{
header_size := size_of(HMapChained(Type))
header_size := size_of(HMapChainedHeader(Type))
size := header_size + int(lookup_capacity) * size_of( ^HMapChainedSlot(Type)) + size_of(int)
raw_mem : rawptr
raw_mem, error = alloc( size, allocator = allocator )
if error != AllocatorError.None do return
table.header = cast( ^HMapChained(Type)) raw_mem
table.header = cast( ^HMapChainedHeader(Type)) raw_mem
table.pool, error = pool_init(
should_zero_buckets = false,
block_size = size_of(HMapChainedSlot(Type)),
@ -74,12 +75,12 @@ hmap_chained_init :: proc( $Type : typeid, lookup_capacity : uint, allocator : A
allocator = allocator,
dbg_name = str_intern(str_fmt("%v: pool", dbg_name)).str
)
data := transmute([^] ^HMapChainedSlot(Type)) (transmute( [^]HMapChained(Type)) table.header)[1:]
data := transmute([^] ^HMapChainedSlot(Type)) (transmute( [^]HMapChainedHeader(Type)) table.header)[1:]
table.lookup = slice_ptr( data, int(lookup_capacity) )
return
}
hmap_chained_clear :: proc( using self : HMapChainedPtr($Type))
hmap_chained_clear :: proc( using self : HMapChained($Type))
{
for slot in lookup
{
@ -93,19 +94,19 @@ hmap_chained_clear :: proc( using self : HMapChainedPtr($Type))
}
}
hmap_chained_destroy :: proc( using self : ^HMapChainedPtr($Type)) {
hmap_chained_destroy :: proc( using self : ^HMapChained($Type)) {
pool_destroy( pool )
free( self.header, backing)
self = nil
}
hmap_chained_lookup_id :: #force_inline proc( using self : HMapChainedPtr($Type), key : u64 ) -> u64
hmap_chained_lookup_id :: #force_inline proc( using self : HMapChained($Type), key : u64 ) -> u64
{
hash_index := key % u64( len(lookup) )
return hash_index
}
hmap_chained_get :: proc( using self : HMapChainedPtr($Type), key : u64) -> ^Type
hmap_chained_get :: proc( using self : HMapChained($Type), key : u64) -> ^Type
{
// profile(#procedure)
surface_slot := lookup[hmap_chained_lookup_id(self, key)]
@ -127,14 +128,14 @@ hmap_chained_get :: proc( using self : HMapChainedPtr($Type), key : u64) -> ^Typ
return nil
}
hmap_chained_reload :: proc( self : HMapChainedPtr($Type), allocator : Allocator )
hmap_chained_reload :: proc( self : HMapChained($Type), allocator : Allocator )
{
pool_reload(self.pool, allocator)
}
// Returns true if an slot was actually found and marked as vacant
// Entries already found to be vacant will not return true
hmap_chained_remove :: proc( self : HMapChainedPtr($Type), key : u64 ) -> b32
hmap_chained_remove :: proc( self : HMapChained($Type), key : u64 ) -> b32
{
surface_slot := lookup[hmap_chained_lookup_id(self, key)]
@ -160,12 +161,12 @@ hmap_chained_remove :: proc( self : HMapChainedPtr($Type), key : u64 ) -> b32
// Sets the value to a vacant slot
// Will preemptively allocate the next slot in the hashtable if its null for the slot.
hmap_chained_set :: proc( using self : HMapChainedPtr($Type), key : u64, value : Type ) -> (^ Type, AllocatorError)
hmap_chained_set :: proc( using self : HMapChained($Type), key : u64, value : Type ) -> (^ Type, AllocatorError)
{
// profile(#procedure)
hash_index := hmap_chained_lookup_id(self, key)
surface_slot := lookup[hash_index]
set_slot :: #force_inline proc( using self : HMapChainedPtr(Type),
set_slot :: #force_inline proc( using self : HMapChained(Type),
slot : ^HMapChainedSlot(Type),
key : u64,
value : Type

View File

@ -47,13 +47,13 @@ HMapZPL :: struct ( $ Type : typeid ) {
entries : Array( HMapZPL_Entry(Type) ),
}
hamp_zpl_init :: proc
( $ Type : typeid, allocator : Allocator, num : u64, dbg_name : string = "" ) -> ( HMapZPL( Type), AllocatorError )
hmap_zpl_init :: proc
( $HMapZPL_Type : typeid / HMapZPL($Type), num : u64, allocator := context.allocator, dbg_name : string = "" ) -> ( HMapZPL( Type), AllocatorError )
{
result : HMapZPL(Type)
table_result, entries_result : AllocatorError
result.table, table_result = array_init_reserve( i64, allocator, num, dbg_name = dbg_name )
result.table, table_result = make( Array(i64), num, dbg_name = dbg_name, allocator = allocator )
if table_result != AllocatorError.None {
ensure( false, "Failed to allocate table array" )
return result, table_result
@ -61,7 +61,7 @@ hamp_zpl_init :: proc
array_resize( & result.table, num )
slice.fill( slice_ptr( result.table.data, cast(int) result.table.num), -1 )
result.entries, entries_result = array_init_reserve( HMapZPL_Entry(Type), allocator, num, dbg_name = dbg_name )
result.entries, entries_result = make( Array( HMapZPL_Entry(Type) ), num, dbg_name = dbg_name, allocator = allocator )
if entries_result != AllocatorError.None {
ensure( false, "Failed to allocate entries array" )
return result, entries_result
@ -122,7 +122,7 @@ hamp_zpl_rehash :: proc( ht : ^ HMapZPL( $ Type ), new_num : u64 ) -> AllocatorE
ensure( false, "ZPL HMAP IS REHASHING" )
last_added_index : i64
new_ht, init_result := hamp_zpl_init( Type, ht.table.backing, new_num, ht.table.dbg_name )
new_ht, init_result := hmap_zpl_init( HMapZPL(Type), new_num, ht.table.backing, ht.table.dbg_name )
if init_result != AllocatorError.None {
ensure( false, "New hamp_zpl failed to allocate" )
return init_result

View File

@ -156,7 +156,7 @@ import "codebase:grime"
Array :: grime.Array
array_to_slice :: grime.array_to_slice
array_init_reserve :: grime.array_init_reserve
array_init :: grime.array_init
array_append :: grime.array_append
array_append_at :: grime.array_append_at
array_clear :: grime.array_clear
@ -165,6 +165,15 @@ import "codebase:grime"
array_remove_at :: grime.array_remove_at
array_resize :: grime.array_resize
StackFixed :: grime.StackFixed
stack_clear :: grime.stack_clear
stack_push :: grime.stack_push
stack_pop :: grime.stack_pop
stack_peek_ref :: grime.stack_peek_ref
stack_peek :: grime.stack_peek
stack_push_contextless :: grime.stack_push_contextless
// filesystem
file_exists :: grime.file_exists
file_rewind :: grime.file_rewind
@ -296,6 +305,20 @@ is_power_of_two :: proc {
is_power_of_two_uintptr,
}
make :: proc {
array_init,
hmap_chained_init,
hmap_zpl_init,
// Usual
make_slice,
make_dynamic_array,
make_dynamic_array_len,
make_dynamic_array_len_cap,
make_map,
make_multi_pointer,
}
// measure_text_size :: proc {
// measure_text_size_raylib,
// }

View File

@ -59,7 +59,7 @@ slab_allocator :: proc( slab : Slab ) -> ( allocator : Allocator ) {
return
}
slab_init :: proc( policy : ^SlabPolicy, bucket_reserve_num : uint = 0, allocator : Allocator, dbg_name : string = "", should_zero_buckets : b32 = false ) -> ( slab : Slab, alloc_error : AllocatorError )
slab_init :: proc( policy : ^SlabPolicy, bucket_reserve_num : uint = 0, allocator : Allocator = context.allocator, dbg_name : string = "", should_zero_buckets : b32 = false ) -> ( slab : Slab, alloc_error : AllocatorError )
{
header_size :: size_of( SlabHeader )

View File

@ -3,51 +3,6 @@ package sectr
import "core:mem"
import "core:slice"
//region Fixed Stack
StackFixed :: struct ( $ Type : typeid, $ Size : u32 ) {
idx : u32,
items : [ Size ] Type,
}
stack_clear :: #force_inline proc ( using stack : ^StackFixed( $Type, $Size)) {
idx = 0
}
stack_push :: #force_inline proc( using stack : ^ StackFixed( $ Type, $ Size ), value : Type ) {
verify( idx < len( items ), "Attempted to push on a full stack" )
items[ idx ] = value
idx += 1
}
stack_pop :: #force_inline proc( using stack : ^StackFixed( $ Type, $ Size ) ) {
verify( idx > 0, "Attempted to pop an empty stack" )
idx -= 1
if idx == 0 {
items[idx] = {}
}
}
stack_peek_ref :: #force_inline proc "contextless" ( using stack : ^StackFixed( $ Type, $ Size ) ) -> ( ^Type) {
last_idx := max( 0, idx - 1 ) if idx > 0 else 0
last := & items[last_idx]
return last
}
stack_peek :: #force_inline proc "contextless" ( using stack : ^StackFixed( $ Type, $ Size ) ) -> Type {
last := max( 0, idx - 1 ) if idx > 0 else 0
return items[last]
}
stack_push_contextless :: #force_inline proc "contextless" ( stack : ^StackFixed( $Type, $Size), value : Type ) {
items[idx] = value
idx += 1
}
//endregion Fixed Stack
//region Stack Allocator
// TODO(Ed) : This is untested and problably filled with bugs.

View File

@ -63,10 +63,10 @@ str_cache_init :: proc( /*allocator : Allocator*/ ) -> ( cache : StringCache ) {
state := get_state()
alloc_error : AllocatorError
cache.slab, alloc_error = slab_init( & policy, allocator = persistent_allocator(), dbg_name = dbg_name )
cache.slab, alloc_error = slab_init( & policy, dbg_name = dbg_name, allocator = persistent_allocator() )
verify(alloc_error == .None, "Failed to initialize the string cache" )
cache.table, alloc_error = hamp_zpl_init( StrRunesPair, persistent_allocator(), 4 * Megabyte, dbg_name )
cache.table, alloc_error = make( HMapZPL(StrRunesPair), 4 * Megabyte, persistent_allocator(), dbg_name )
return
}