From 682b1413569efeda6489d2ad69a29dd92d107b07 Mon Sep 17 00:00:00 2001 From: Judah Caruso Date: Fri, 30 May 2025 00:59:43 -0600 Subject: [PATCH] add remove_ordered and remove_unordered to [array] --- array/dynamic_array.jai | 59 ++++++++++++++++++++++++++++++++++++++++- array/static_array.jai | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/array/dynamic_array.jai b/array/dynamic_array.jai index 12bb9da..ca2e780 100644 --- a/array/dynamic_array.jai +++ b/array/dynamic_array.jai @@ -25,6 +25,18 @@ resize :: inline (arr: *[..]$T, new_size: int) { basic.array_reserve(arr, new_size,, allocator = arr.allocator); } +remove_ordered :: inline (arr: *[..]$T, index: int, loc := #caller_location) #no_abc { + meta.check_bounds(index, arr.count, loc = loc); + memcpy(arr.data + index, arr.data + index + 1, (arr.count - index - 1) * size_of(T)); + arr.count -= 1; +} + +remove_unordered :: inline (arr: *[..]$T, index: int, loc := #caller_location) #no_abc { + meta.check_bounds(index, arr.count, loc = loc); + arr.data[index] = arr.data[arr.count - 1]; + arr.count -= 1; +} + reset :: inline (arr: *[..]$T) { arr.count = 0; } @@ -40,6 +52,51 @@ find_pointer :: (a: *[..]$T, $predicate: (T) -> bool) -> *T, bool, int { } #scope_file; - + mem :: #import "jc/memory"; +meta :: #import "jc/meta"; basic :: #import "Basic"; // @future + +#if RUN_TESTS #run { + test.run("remove_ordered", t => { + a: [..]int; + append(*a, 10, 20, 30); + + remove_ordered(*a, 1); + test.expect(t, a.count == 2); + test.expect(t, a.data[0] == 10); + test.expect(t, a.data[1] == 30); + + remove_ordered(*a, 0); + test.expect(t, a.count == 1); + test.expect(t, a.data[0] == 30); + + remove_ordered(*a, 0); + test.expect(t, a.count == 0); + + append(*a, 10); + test.expect(t, a.count == 1); + test.expect(t, a.data[0] == 10); + }); + + test.run("remove_unordered", t => { + a: [..]int; + append(*a, 10, 20, 30); + + remove_unordered(*a, 1); + test.expect(t, a.count == 2); + test.expect(t, a.data[0] == 10); + test.expect(t, a.data[1] == 30); + + remove_unordered(*a, 0); + test.expect(t, a.count == 1); + test.expect(t, a.data[0] == 30); + + remove_unordered(*a, 0); + test.expect(t, a.count == 0); + + append(*a, 10); + test.expect(t, a.count == 1); + test.expect(t, a.data[0] == 10); + }); +} diff --git a/array/static_array.jai b/array/static_array.jai index 4c241f3..05a07f9 100644 --- a/array/static_array.jai +++ b/array/static_array.jai @@ -28,6 +28,18 @@ append :: inline (a: *Static_Array, items: ..a.T) -> *a.T #no_abc { return first; } +remove_ordered :: inline (a: *Static_Array, index: int, loc := #caller_location) #no_abc { + meta.check_bounds(index, a.count, loc = loc); + memcpy(a.items.data + index, a.items.data + index + 1, (a.count - index - 1) * size_of(a.T)); + a.count -= 1; +} + +remove_unordered :: inline (a: *Static_Array, index: int, loc := #caller_location) #no_abc { + meta.check_bounds(index, a.count, loc = loc); + a.items[index] = a.items[a.count - 1]; + a.count -= 1; +} + reset :: inline (a: *Static_Array) #no_abc { for 0..a.count - 1 a.items[it] = a.Default; a.count = 0; @@ -114,4 +126,44 @@ basic :: #import "Basic"; // @future _, ok = find_pointer(*a, v => v == 20); test.expect(t, ok); }); + + test.run("remove_ordered", (t) => { + a: Static_Array(10, int); + append(*a, 10, 20, 30); + + remove_ordered(*a, 1); + test.expect(t, a.count == 2); + test.expect(t, a.items[0] == 10); + + remove_ordered(*a, 0); + test.expect(t, a.count == 1); + test.expect(t, a.items[0] == 30); + + remove_ordered(*a, 0); + test.expect(t, a.count == 0); + + append(*a, 10); + test.expect(t, a.count == 1); + test.expect(t, a.items[0] == 10); + }); + + test.run("remove_unordered", (t) => { + a: Static_Array(10, int); + append(*a, 10, 20, 30); + + remove_unordered(*a, 1); + test.expect(t, a.count == 2); + test.expect(t, a.items[1] == 30); + + remove_unordered(*a, 0); + test.expect(t, a.count == 1); + test.expect(t, a.items[0] == 30); + + remove_unordered(*a, 0); + test.expect(t, a.count == 0); + + append(*a, 10); + test.expect(t, a.count == 1); + test.expect(t, a.items[0] == 10); + }); }