Compare commits

..

2 commits

Author SHA1 Message Date
f05f49517f temp changes 2025-05-26 22:03:32 -06:00
ede74d7f6c start 065 2025-05-26 03:51:55 -06:00
5 changed files with 130 additions and 13 deletions

2
TODO
View file

@ -1,6 +1,7 @@
*** IN PROGRESS *** *** IN PROGRESS ***
[Judah] [Judah]
065 [array] add dynamic, but stable array implementation (values should not move in memory once appended to the array; should mirror procedures on 'Static_Array')
[Jesse] [Jesse]
011 [math] add more Vec math procedures 011 [math] add more Vec math procedures
@ -65,7 +66,6 @@
062 [bits] create bit utilities module (see: odin math/bits module) 062 [bits] create bit utilities module (see: odin math/bits module)
063 [x] creating import rewriting metaprogram to automatically import .C or .H files 063 [x] creating import rewriting metaprogram to automatically import .C or .H files
064 [thirdpaty/luau] create bindings module (see: https://luau.org) 064 [thirdpaty/luau] create bindings module (see: https://luau.org)
065 [array] add dynamic, but stable array implementation (values should not move in memory once appended to the array; should mirror procedures on 'Static_Array')
066 [x] add generational ids module(?); maybe this integrates with [map] or [array]? 066 [x] add generational ids module(?); maybe this integrates with [map] or [array]?
067 [reload, bug] fix issue with #runs being executed multiple times 067 [reload, bug] fix issue with #runs being executed multiple times
068 [reload, bug] fix issue where calling 'print' from the client hits the crash allocator at random times 068 [reload, bug] fix issue where calling 'print' from the client hits the crash allocator at random times

View file

@ -1,6 +1,7 @@
#module_parameters(RUN_TESTS := false); #module_parameters(RUN_TESTS := false);
#load "static_array.jai"; #load "static_array.jai";
#load "stable_array.jai";
#load "dynamic_array.jai"; #load "dynamic_array.jai";
#scope_module; #scope_module;

121
array/stable_array.jai Normal file
View file

@ -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);
// }
// }
// }

View file

@ -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 { append :: inline (a: *Static_Array, item: a.T) -> *a.T {
ensure_array_has_room(a, 1); ensure_array_has_room(a, 1);
ptr := *a[a.count]; ptr := *a.items[a.count];
ptr.* = item; ptr.* = item;
a.count += 1; a.count += 1;
return ptr; return ptr;
@ -37,7 +37,7 @@ append :: inline (a: *Static_Array, item: a.T) -> *a.T {
append :: inline (a: *Static_Array) -> *a.T { append :: inline (a: *Static_Array) -> *a.T {
ensure_array_has_room(a, 1); ensure_array_has_room(a, 1);
ptr := *a[a.count]; ptr := *a.items[a.count];
a.count += 1; a.count += 1;
return ptr; return ptr;
} }
@ -51,14 +51,9 @@ append :: inline (a: *Static_Array, items: ..a.T) -> *a.T {
} }
reset :: inline (a: *Static_Array, $keep_memory := true) { reset :: inline (a: *Static_Array, $keep_memory := true) {
#if keep_memory { #if !keep_memory for 0..a.count - 1 a.items[it] = a.Default;
a.count = 0; a.count = 0;
} }
else {
for 0..a.count - 1 a.items[it] = a.Default;
a.count = 0;
}
}
make_view :: (a: Static_Array) -> []a.T { make_view :: (a: Static_Array) -> []a.T {
view := a.items.([]a.T); view := a.items.([]a.T);

View file

@ -117,15 +117,15 @@ release_memory :: inline (ptr: *void) {
} }
release_memory :: inline (arr: []$T) { release_memory :: inline (arr: []$T) {
release_memory(arr.data); release_memory(arr.data.(*void));
} }
release_memory :: inline (arr: [..]$T) { 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 :: inline (str: string) {
release_memory(str.data); release_memory(str.data.(*void));
} }
#load "allocators.jai"; #load "allocators.jai";