add remove_ordered and remove_unordered to [array]
This commit is contained in:
parent
1b6d48d14b
commit
682b141356
2 changed files with 110 additions and 1 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -42,4 +54,49 @@ 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);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue