2024-05-26 10:35:08 -07:00
|
|
|
/*
|
|
|
|
Based on gencpp's and thus zpl's Array implementation
|
|
|
|
Made becasue of the map issue with fonts during hot-reload.
|
|
|
|
I didn't want to make the HMapZPL impl with the [dynamic] array for now to isolate the hot-reload issue (when I was diagnoising)
|
|
|
|
|
|
|
|
Update 5-26-2024:
|
|
|
|
TODO(Ed): Raw_Dynamic_Array is defined within base:runtime/core.odin and exposes what we need for worst case hot-reloads.
|
|
|
|
So its best to go back to regular dynamic arrays at some point.
|
|
|
|
*/
|
2024-05-31 08:26:52 -07:00
|
|
|
package grime
|
2024-02-13 23:29:28 -08:00
|
|
|
|
|
|
|
import "core:c/libc"
|
|
|
|
import "core:mem"
|
2024-02-22 20:15:29 -08:00
|
|
|
import "core:slice"
|
2024-02-13 23:29:28 -08:00
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
ArrayHeader :: struct ( $ Type : typeid ) {
|
2024-03-11 23:32:16 -07:00
|
|
|
backing : Allocator,
|
2024-05-06 20:29:47 -07:00
|
|
|
dbg_name : string,
|
|
|
|
fixed_cap : b32,
|
2024-02-13 23:29:28 -08:00
|
|
|
capacity : u64,
|
|
|
|
num : u64,
|
|
|
|
data : [^]Type,
|
|
|
|
}
|
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
Array :: struct ( $ Type : typeid ) {
|
|
|
|
using header : ^ArrayHeader(Type),
|
|
|
|
}
|
|
|
|
|
|
|
|
array_underlying_slice :: proc(slice: []($ Type)) -> Array(Type)
|
|
|
|
{
|
2024-03-02 15:19:27 -08:00
|
|
|
if len(slice) == 0 {
|
2024-06-02 14:29:27 -07:00
|
|
|
return {nil}
|
2024-03-02 15:19:27 -08:00
|
|
|
}
|
2024-06-02 14:29:27 -07:00
|
|
|
header_size := size_of( ArrayHeader(Type))
|
|
|
|
raw_data := & slice[0]
|
|
|
|
array := transmute( Array(Type)) ( uintptr(raw_data) - uintptr(header_size))
|
|
|
|
return array
|
2024-03-02 15:19:27 -08:00
|
|
|
}
|
|
|
|
|
2024-05-31 16:31:27 -07:00
|
|
|
array_to_slice :: #force_inline proc( using self : Array($ Type) ) -> []Type { return slice_ptr( data, int(num)) }
|
|
|
|
array_to_slice_capacity :: #force_inline proc( using self : Array($ Type) ) -> []Type { return slice_ptr( data, int(capacity)) }
|
2024-03-02 17:22:28 -08:00
|
|
|
|
2024-02-13 23:29:28 -08:00
|
|
|
array_grow_formula :: proc( value : u64 ) -> u64 {
|
2024-03-20 19:34:18 -07:00
|
|
|
result := (2 * value) + 8
|
|
|
|
return result
|
2024-02-13 23:29:28 -08:00
|
|
|
}
|
|
|
|
|
2024-05-31 14:58:18 -07:00
|
|
|
array_init :: proc( $Array_Type : typeid/Array($Type), capacity : u64,
|
|
|
|
allocator := context.allocator, fixed_cap : b32 = false, dbg_name : string = ""
|
|
|
|
) -> ( result : Array(Type), alloc_error : AllocatorError )
|
2024-02-13 23:29:28 -08:00
|
|
|
{
|
2024-03-19 20:25:48 -07:00
|
|
|
header_size := size_of(ArrayHeader(Type))
|
|
|
|
array_size := header_size + int(capacity) * size_of(Type)
|
2024-03-05 07:40:28 -08:00
|
|
|
|
|
|
|
raw_mem : rawptr
|
2024-03-19 20:25:48 -07:00
|
|
|
raw_mem, alloc_error = alloc( array_size, allocator = allocator )
|
2024-03-20 10:34:47 -07:00
|
|
|
// log( str_fmt_tmp("array reserved: %d", header_size + int(capacity) * size_of(Type) ))
|
2024-03-05 07:40:28 -08:00
|
|
|
if alloc_error != AllocatorError.None do return
|
|
|
|
|
2024-05-14 08:47:44 -07:00
|
|
|
result.header = cast( ^ArrayHeader(Type)) raw_mem
|
2024-03-11 23:32:16 -07:00
|
|
|
result.backing = allocator
|
2024-05-20 19:05:52 -07:00
|
|
|
result.dbg_name = dbg_name
|
2024-05-06 20:29:47 -07:00
|
|
|
result.fixed_cap = fixed_cap
|
2024-02-13 23:29:28 -08:00
|
|
|
result.capacity = capacity
|
2024-03-05 07:40:28 -08:00
|
|
|
result.data = cast( [^]Type ) (cast( [^]ArrayHeader(Type)) result.header)[ 1:]
|
|
|
|
return
|
2024-02-13 23:29:28 -08:00
|
|
|
}
|
|
|
|
|
2024-05-18 22:21:51 -07:00
|
|
|
array_append_array :: proc( using self: ^Array( $ Type), other : Array(Type)) -> AllocatorError
|
|
|
|
{
|
|
|
|
if num + other.num > capacity
|
|
|
|
{
|
|
|
|
grow_result := array_grow( self, num + other.num )
|
|
|
|
if grow_result != AllocatorError.None {
|
|
|
|
return grow_result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note(Ed) : Original code from gencpp
|
|
|
|
// libc.memcpy( ptr_offset(data, num), raw_data(items), len(items) * size_of(Type) )
|
|
|
|
|
|
|
|
target := ptr_offset( data, num )
|
2024-06-11 19:20:19 -07:00
|
|
|
|
|
|
|
dst_slice := slice_ptr(target, int(capacity - num))
|
|
|
|
src_slice := array_to_slice(other)
|
|
|
|
copy( dst_slice, src_slice )
|
2024-05-18 22:21:51 -07:00
|
|
|
|
|
|
|
num += other.num
|
|
|
|
return AllocatorError.None
|
|
|
|
}
|
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
array_append_slice :: proc( using self : ^Array( $ Type ), items : []Type ) -> AllocatorError
|
2024-02-13 23:29:28 -08:00
|
|
|
{
|
2024-05-18 22:21:51 -07:00
|
|
|
items_num :=u64(len(items))
|
|
|
|
if num + items_num > capacity
|
2024-02-13 23:29:28 -08:00
|
|
|
{
|
2024-05-18 22:21:51 -07:00
|
|
|
grow_result := array_grow( self, num + items_num )
|
2024-02-13 23:29:28 -08:00
|
|
|
if grow_result != AllocatorError.None {
|
|
|
|
return grow_result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
target := ptr_offset( data, num )
|
2024-05-18 22:21:51 -07:00
|
|
|
copy( slice_ptr(target, int(capacity - num)), items )
|
2024-02-13 23:29:28 -08:00
|
|
|
|
2024-05-18 22:21:51 -07:00
|
|
|
num += items_num
|
2024-02-13 23:29:28 -08:00
|
|
|
return AllocatorError.None
|
|
|
|
}
|
|
|
|
|
2024-05-31 16:31:27 -07:00
|
|
|
array_append_value :: proc( self : ^Array( $ Type), value : Type ) -> AllocatorError
|
|
|
|
{
|
|
|
|
// profile(#procedure)
|
|
|
|
if self.header.num == self.header.capacity
|
|
|
|
{
|
|
|
|
grow_result := array_grow( self, self.header.capacity )
|
|
|
|
if grow_result != AllocatorError.None {
|
|
|
|
return grow_result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.header.data[ self.header.num ] = value
|
|
|
|
self.header.num += 1
|
|
|
|
return AllocatorError.None
|
|
|
|
}
|
|
|
|
|
|
|
|
array_append_at_value :: proc( using self : ^Array( $ Type ), item : Type, id : u64 ) -> AllocatorError
|
2024-02-13 23:29:28 -08:00
|
|
|
{
|
|
|
|
id := id
|
|
|
|
if id >= num {
|
|
|
|
id = num - 1
|
|
|
|
}
|
|
|
|
if id < 0 {
|
|
|
|
id = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if capacity < num + 1
|
|
|
|
{
|
2024-02-27 04:50:57 -08:00
|
|
|
grow_result := array_grow( self, capacity )
|
2024-02-13 23:29:28 -08:00
|
|
|
if grow_result != AllocatorError.None {
|
|
|
|
return grow_result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
target := & data[id]
|
2024-05-06 20:29:47 -07:00
|
|
|
libc.memmove( ptr_offset(target, 1), target, uint(num - id) * size_of(Type) )
|
2024-02-13 23:29:28 -08:00
|
|
|
|
|
|
|
data[id] = item
|
|
|
|
num += 1
|
|
|
|
return AllocatorError.None
|
|
|
|
}
|
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
array_append_at_slice :: proc( using self : ^Array( $ Type ), items : []Type, id : u64 ) -> AllocatorError
|
2024-02-13 23:29:28 -08:00
|
|
|
{
|
|
|
|
id := id
|
|
|
|
if id >= num {
|
|
|
|
return array_append_slice( items )
|
|
|
|
}
|
|
|
|
if len(items) > capacity
|
|
|
|
{
|
2024-02-27 04:50:57 -08:00
|
|
|
grow_result := array_grow( self, capacity )
|
2024-02-13 23:29:28 -08:00
|
|
|
if grow_result != AllocatorError.None {
|
|
|
|
return grow_result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note(Ed) : Original code from gencpp
|
|
|
|
// target := ptr_offset( data, id + len(items) )
|
|
|
|
// src := ptr_offset( data, id )
|
|
|
|
// libc.memmove( target, src, num - id * size_of(Type) )
|
|
|
|
// libc.memcpy ( src, raw_data(items), len(items) * size_of(Type) )
|
|
|
|
|
|
|
|
// TODO(Ed) : VERIFY VIA DEBUG THIS COPY IS FINE
|
2024-05-21 20:35:36 -07:00
|
|
|
ensure(false, "time to check....")
|
2024-02-13 23:29:28 -08:00
|
|
|
target := & data[id + len(items)]
|
|
|
|
dst := slice_ptr( target, num - id - len(items) )
|
|
|
|
src := slice_ptr( & data[id], num - id )
|
|
|
|
copy( dst, src )
|
|
|
|
copy( src, items )
|
|
|
|
|
|
|
|
num += len(items)
|
|
|
|
return AllocatorError.None
|
|
|
|
}
|
|
|
|
|
2024-06-02 14:29:27 -07:00
|
|
|
array_back :: proc( self : Array($Type) ) -> Type {
|
|
|
|
value := self.data[self.num - 1]
|
|
|
|
return value
|
|
|
|
}
|
2024-03-19 21:44:43 -07:00
|
|
|
|
2024-05-21 20:35:36 -07:00
|
|
|
// array_push_back :: proc( using self : Array( $ Type)) -> b32 {
|
|
|
|
// if num == capacity {
|
|
|
|
// return false
|
|
|
|
// }
|
2024-03-05 07:40:28 -08:00
|
|
|
|
2024-05-21 20:35:36 -07:00
|
|
|
// data[ num ] = value
|
|
|
|
// num += 1
|
|
|
|
// return true
|
|
|
|
// }
|
2024-03-05 07:40:28 -08:00
|
|
|
|
2024-05-06 20:29:47 -07:00
|
|
|
array_clear :: proc "contextless" ( using self : Array( $ Type ), zero_data : b32 = false ) {
|
2024-02-27 04:50:57 -08:00
|
|
|
if zero_data {
|
2024-05-06 20:29:47 -07:00
|
|
|
mem.set( data, 0, int(num * size_of(Type)) )
|
2024-02-27 04:50:57 -08:00
|
|
|
}
|
2024-05-06 20:29:47 -07:00
|
|
|
header.num = 0
|
2024-02-13 23:29:28 -08:00
|
|
|
}
|
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
array_fill :: proc( using self : Array( $ Type ), begin, end : u64, value : Type ) -> b32
|
2024-02-13 23:29:28 -08:00
|
|
|
{
|
|
|
|
if begin < 0 || end >= num {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-22 20:15:29 -08:00
|
|
|
// data_slice := slice_ptr( ptr_offset( data, begin ), end - begin )
|
|
|
|
// slice.fill( data_slice, cast(int) value )
|
|
|
|
|
2024-02-13 23:29:28 -08:00
|
|
|
for id := begin; id < end; id += 1 {
|
|
|
|
data[ id ] = value
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
array_free :: proc( using self : Array( $ Type ) ) {
|
2024-05-06 20:29:47 -07:00
|
|
|
free( self.header, backing )
|
2024-03-05 07:40:28 -08:00
|
|
|
self.data = nil
|
2024-02-13 23:29:28 -08:00
|
|
|
}
|
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
array_grow :: proc( using self : ^Array( $ Type ), min_capacity : u64 ) -> AllocatorError
|
2024-02-13 23:29:28 -08:00
|
|
|
{
|
2024-02-22 20:15:29 -08:00
|
|
|
new_capacity := array_grow_formula( capacity )
|
2024-02-13 23:29:28 -08:00
|
|
|
|
|
|
|
if new_capacity < min_capacity {
|
|
|
|
new_capacity = min_capacity
|
|
|
|
}
|
2024-02-27 04:50:57 -08:00
|
|
|
return array_set_capacity( self, new_capacity )
|
2024-02-13 23:29:28 -08:00
|
|
|
}
|
|
|
|
|
2024-06-02 22:04:24 -07:00
|
|
|
array_pop :: proc( self : Array( $ Type ) ) {
|
|
|
|
verify( self.num != 0, "Attempted to pop an array with no elements" )
|
|
|
|
self.num -= 1
|
2024-02-13 23:29:28 -08:00
|
|
|
}
|
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
array_remove_at :: proc( using self : Array( $ Type ), id : u64 )
|
2024-02-13 23:29:28 -08:00
|
|
|
{
|
2024-05-06 20:29:47 -07:00
|
|
|
verify( id < header.num, "Attempted to remove from an index larger than the array" )
|
2024-02-13 23:29:28 -08:00
|
|
|
|
2024-05-06 20:29:47 -07:00
|
|
|
left := & data[id]
|
|
|
|
right := & data[id + 1]
|
|
|
|
libc.memmove( left, right, uint(num - id) * size_of(Type) )
|
2024-02-13 23:29:28 -08:00
|
|
|
|
2024-05-06 20:29:47 -07:00
|
|
|
header.num -= 1
|
2024-02-13 23:29:28 -08:00
|
|
|
}
|
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
array_reserve :: proc( using self : ^Array( $ Type ), new_capacity : u64 ) -> AllocatorError
|
2024-02-13 23:29:28 -08:00
|
|
|
{
|
|
|
|
if capacity < new_capacity {
|
2024-02-27 04:50:57 -08:00
|
|
|
return array_set_capacity( self, new_capacity )
|
2024-02-13 23:29:28 -08:00
|
|
|
}
|
|
|
|
return AllocatorError.None
|
|
|
|
}
|
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
array_resize :: proc( array : ^Array( $ Type ), num : u64 ) -> AllocatorError
|
2024-02-13 23:29:28 -08:00
|
|
|
{
|
|
|
|
if array.capacity < num
|
|
|
|
{
|
2024-02-22 20:15:29 -08:00
|
|
|
grow_result := array_grow( array, array.capacity )
|
2024-02-13 23:29:28 -08:00
|
|
|
if grow_result != AllocatorError.None {
|
|
|
|
return grow_result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array.num = num
|
|
|
|
return AllocatorError.None
|
|
|
|
}
|
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
array_set_capacity :: proc( self : ^Array( $ Type ), new_capacity : u64 ) -> AllocatorError
|
2024-02-13 23:29:28 -08:00
|
|
|
{
|
2024-03-05 07:40:28 -08:00
|
|
|
if new_capacity == self.capacity {
|
2024-02-22 20:15:29 -08:00
|
|
|
return AllocatorError.None
|
2024-02-13 23:29:28 -08:00
|
|
|
}
|
2024-03-05 07:40:28 -08:00
|
|
|
if new_capacity < self.num {
|
|
|
|
self.num = new_capacity
|
2024-02-22 20:15:29 -08:00
|
|
|
return AllocatorError.None
|
2024-02-13 23:29:28 -08:00
|
|
|
}
|
|
|
|
|
2024-03-05 07:40:28 -08:00
|
|
|
header_size :: size_of(ArrayHeader(Type))
|
|
|
|
|
2024-03-19 20:25:48 -07:00
|
|
|
new_size := header_size + (cast(int) new_capacity ) * size_of(Type)
|
|
|
|
old_size := header_size + (cast(int) self.capacity) * size_of(Type)
|
2024-03-05 07:40:28 -08:00
|
|
|
|
2024-03-19 09:18:39 -07:00
|
|
|
new_mem, result_code := resize_non_zeroed( self.header, old_size, new_size, mem.DEFAULT_ALIGNMENT, allocator = self.backing )
|
2024-03-11 23:32:16 -07:00
|
|
|
|
2024-03-02 07:24:09 -08:00
|
|
|
if result_code != AllocatorError.None {
|
|
|
|
ensure( false, "Failed to allocate for new array capacity" )
|
2024-05-31 08:26:52 -07:00
|
|
|
log( "Failed to allocate for new array capacity", level = LogLevel.Warning )
|
2024-03-02 07:24:09 -08:00
|
|
|
return result_code
|
|
|
|
}
|
2024-03-11 23:32:16 -07:00
|
|
|
if new_mem == nil {
|
|
|
|
ensure(false, "new_mem is nil but no allocation error")
|
|
|
|
return result_code
|
|
|
|
}
|
2024-03-05 07:40:28 -08:00
|
|
|
|
2024-03-19 20:25:48 -07:00
|
|
|
self.header = cast( ^ArrayHeader(Type)) raw_data(new_mem);
|
|
|
|
self.header.data = cast( [^]Type ) (cast( [^]ArrayHeader(Type)) self.header)[ 1:]
|
|
|
|
self.header.capacity = new_capacity
|
|
|
|
self.header.num = self.num
|
2024-02-13 23:29:28 -08:00
|
|
|
return result_code
|
|
|
|
}
|
2024-05-06 20:29:47 -07:00
|
|
|
|
|
|
|
array_block_size :: proc "contextless" ( self : Array( $Type ) ) -> u64 {
|
|
|
|
header_size :: size_of(ArrayHeader(Type))
|
|
|
|
block_size := cast(u64) (header_size + self.capacity * size_of(Type))
|
|
|
|
return block_size
|
|
|
|
}
|