From f05f49517facc54c5cdf9cb81b2520a7384d5df5 Mon Sep 17 00:00:00 2001 From: Judah Caruso Date: Mon, 26 May 2025 22:03:32 -0600 Subject: [PATCH] temp changes --- array/module.jai | 1 + array/stable_array.jai | 121 +++++++++++++++++++++++++++++++++++++++++ array/static_array.jai | 13 ++--- memory/module.jai | 6 +- 4 files changed, 129 insertions(+), 12 deletions(-) create mode 100644 array/stable_array.jai diff --git a/array/module.jai b/array/module.jai index 0405443..cf5aacf 100644 --- a/array/module.jai +++ b/array/module.jai @@ -1,6 +1,7 @@ #module_parameters(RUN_TESTS := false); #load "static_array.jai"; +#load "stable_array.jai"; #load "dynamic_array.jai"; #scope_module; diff --git a/array/stable_array.jai b/array/stable_array.jai new file mode 100644 index 0000000..f26c5a0 --- /dev/null +++ b/array/stable_array.jai @@ -0,0 +1,121 @@ +Stable_Array :: struct(T: Type, ITEMS_PER_CHUNK := 32) { + allocator: Allocator; + chunks: [..]*Chunk; + count: int; + + Chunk :: Static_Array(ITEMS_PER_CHUNK, T); +} + +operator [] :: (a: Stable_Array, index: int, loc := #caller_location) -> a.T #no_abc { + b_idx := index / a.ITEMS_PER_CHUNK; + i_idx := index % a.ITEMS_PER_CHUNK; + assert(b_idx < a.chunks.count && i_idx < a.chunks[b_idx].count); + return a.chunks[b_idx].items[i_idx]; +} + +operator *[] :: (a: *Stable_Array, index: int, loc := #caller_location) -> *a.T #no_abc { + b_idx := index / a.ITEMS_PER_CHUNK; + i_idx := index % a.ITEMS_PER_CHUNK; + assert(b_idx < a.chunks.count && i_idx < a.chunks[b_idx].count); + return *a.chunks[b_idx].items[i_idx]; +} + +for_expansion :: (a: Stable_Array, body: Code, flags: For_Flags) #expand { + for i: 0..a.count - 1 { + `it := a[i]; + `it_index := i; + #insert,scope(body) body; + } +} + +append :: (a: *Stable_Array) -> *a.T { + chunk := get_chunk(a, 1); + item := append(chunk); + a.count += 1; + return item; +} + +append :: (a: *Stable_Array, value: a.T) -> *a.T { + chunk := get_chunk(a, 1); + item := append(chunk, value); + a.count += 1; + return item; +} + +append :: (a: *Stable_Array, values: ..a.T) -> *a.T { + first: *a.T; + for values { + if first == null { + first = append(a, it); + } + else { + append(a, it); + } + } + + return first; +} + +reset :: (a: *Stable_Array, $keep_memory := true) { + #if keep_memory { + for a.chunks it.count = 0; + } + else { + for a.chunks mem.release_memory(it,, allocator = a.allocator); + mem.release_memory(a.chunks); + } + + a.count = 0; + a.chunks.count = 0; +} + + +#scope_file; + +mem :: #import "jc/memory"; + +get_chunk :: (a: *Stable_Array, amount: int) -> *a.Chunk { + if a.chunks.count == 0 { + if a.allocator.proc == null { + a.allocator = context.allocator; + a.chunks.allocator = a.allocator; + } + + return make_chunk(a); + } + + last := a.chunks[a.chunks.count - 1]; + if amount > a.ITEMS_PER_CHUNK - last.count { + last = make_chunk(a); + } + + return last; +} + +make_chunk :: (a: *Stable_Array) -> *a.Chunk { + chunk := mem.request_memory(a.Chunk,, allocator = a.allocator); + append(*a.chunks, chunk); + return chunk; +} + +// #run { +// #import "Basic"; + +// { +// a: Stable_Array(int); +// for 0..64 { +// append(*a, it * it); +// } + +// reset(*a, keep_memory = false); + +// append(*a, 10); +// append(*a, 20); +// append(*a, 30); + +// for a { +// print("%: %\n", it_index, it); +// } +// } +// } + diff --git a/array/static_array.jai b/array/static_array.jai index c78b611..95c3ab3 100644 --- a/array/static_array.jai +++ b/array/static_array.jai @@ -29,7 +29,7 @@ for_expansion :: (a: *Static_Array, body: Code, flags: For_Flags) #expand { append :: inline (a: *Static_Array, item: a.T) -> *a.T { ensure_array_has_room(a, 1); - ptr := *a[a.count]; + ptr := *a.items[a.count]; ptr.* = item; a.count += 1; return ptr; @@ -37,7 +37,7 @@ append :: inline (a: *Static_Array, item: a.T) -> *a.T { append :: inline (a: *Static_Array) -> *a.T { ensure_array_has_room(a, 1); - ptr := *a[a.count]; + ptr := *a.items[a.count]; a.count += 1; return ptr; } @@ -51,13 +51,8 @@ append :: inline (a: *Static_Array, items: ..a.T) -> *a.T { } reset :: inline (a: *Static_Array, $keep_memory := true) { - #if keep_memory { - a.count = 0; - } - else { - for 0..a.count - 1 a.items[it] = a.Default; - a.count = 0; - } + #if !keep_memory for 0..a.count - 1 a.items[it] = a.Default; + a.count = 0; } make_view :: (a: Static_Array) -> []a.T { diff --git a/memory/module.jai b/memory/module.jai index ebcead4..0317b37 100644 --- a/memory/module.jai +++ b/memory/module.jai @@ -117,15 +117,15 @@ release_memory :: inline (ptr: *void) { } release_memory :: inline (arr: []$T) { - release_memory(arr.data); + release_memory(arr.data.(*void)); } release_memory :: inline (arr: [..]$T) { - release_memory(arr.data,, allocator = arr.allocator); + release_memory(arr.data.(*void),, allocator = arr.allocator); } release_memory :: inline (str: string) { - release_memory(str.data); + release_memory(str.data.(*void)); } #load "allocators.jai";