diff --git a/array/dynamic_array.jai b/array/dynamic_array.jai new file mode 100644 index 0000000..7d8758c --- /dev/null +++ b/array/dynamic_array.jai @@ -0,0 +1,30 @@ +// @todo(judah): replace array_add + +append :: inline (arr: *[..]$T, value: T) -> *T { + ptr := basic.array_add(arr); + ptr.* = value; + return ptr; +} + +append :: inline (arr: *[..]$T, values: ..T) -> *T { + count := arr.count; + basic.array_add(arr, ..values); + return *arr.data[count]; +} + +append :: inline (arr: *[..]$T) -> *T { + return basic.array_add(arr); +} + +reset :: inline (arr: *[..]$T, $keep_memory := true) { + arr.count = 0; + #if !keep_memory { + mem.release_memory(arr.data,, allocator = arr.allocator); + } +} + + +#scope_file; + +mem :: #import "jc/memory"; +basic :: #import "Basic"; // @future diff --git a/array/module.jai b/array/module.jai index 5887016..0405443 100644 --- a/array/module.jai +++ b/array/module.jai @@ -1,6 +1,7 @@ #module_parameters(RUN_TESTS := false); #load "static_array.jai"; +#load "dynamic_array.jai"; #scope_module; diff --git a/array/static_array.jai b/array/static_array.jai index 875092b..c78b611 100644 --- a/array/static_array.jai +++ b/array/static_array.jai @@ -22,7 +22,7 @@ operator []= :: inline (a: *Static_Array, index: int, value: a.T, loc := #caller for_expansion :: (a: *Static_Array, body: Code, flags: For_Flags) #expand { view := make_view(a); - for *=(flags & .POINTER == .POINTEr) <=(flags & .REVERSE == .REVERSE) `it, `it_index: iter { + for *=(flags & .POINTER == .POINTER) <=(flags & .REVERSE == .REVERSE) `it, `it_index: view { #insert,scope(body)(break = break it) body; } } @@ -78,7 +78,7 @@ make_dynamic :: (a: *Static_Array) -> [..]a.T { #scope_file; ensure_array_has_room :: (array: *Static_Array, count: int, loc := #caller_location) #expand { - basic.assert(array.count + count <= array.capacity, "attempt to add too many elements! want: %, Max: %", array.count + count, array.capacity, loc = loc); + basic.assert(array.count + count <= array.capacity, "attempt to add too many elements! want: %, max: %", array.count + count, array.capacity, loc = loc); } mem :: #import "jc/memory";