jc/array/dynamic_array.jai
2025-05-27 19:35:14 -06:00

45 lines
1.1 KiB
Text

// @todo(judah): replace array_add
init :: inline (arr: *[..]$T, allocator: Allocator) {
arr.allocator = allocator;
}
append :: inline (arr: *[..]$T, value: T) -> *T {
ptr := basic.array_add(arr,, allocator = arr.allocator);
ptr.* = value;
return ptr;
}
append :: inline (arr: *[..]$T, values: ..T) -> *T {
count := arr.count;
basic.array_add(arr, ..values,, allocator = arr.allocator);
return *arr.data[count];
}
append :: inline (arr: *[..]$T) -> *T {
return basic.array_add(arr,, allocator = arr.allocator);
}
resize :: inline (arr: *[..]$T, new_size: int) {
if new_size <= arr.allocated return;
basic.array_reserve(arr, new_size,, allocator = arr.allocator);
}
reset :: inline (arr: *[..]$T) {
arr.count = 0;
}
find :: (a: [..]$T, $predicate: (T) -> bool) -> T, bool, int {
for a if inline predicate(it) return it, true, it_index;
return mem.undefined_of(T), false, -1;
}
find_pointer :: (a: *[..]$T, $predicate: (T) -> bool) -> *T, bool, int {
for * a if inline predicate(it.*) return it, true, it_index;
return null, false, -1;
}
#scope_file;
mem :: #import "jc/memory";
basic :: #import "Basic"; // @future