mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
write List.set in zig
This commit is contained in:
parent
e16436bdd0
commit
25ec057b2b
5 changed files with 93 additions and 37 deletions
|
@ -911,3 +911,33 @@ pub fn listConcat(list_a: RocList, list_b: RocList, alignment: usize, element_wi
|
|||
|
||||
return output;
|
||||
}
|
||||
|
||||
pub fn listSet(
|
||||
input: RocList,
|
||||
alignment: usize,
|
||||
index: usize,
|
||||
element: Opaque,
|
||||
element_width: usize,
|
||||
dec: Dec,
|
||||
) callconv(.C) RocList {
|
||||
if (index < input.len()) {
|
||||
var list = input.makeUnique(std.heap.c_allocator, alignment, element_width);
|
||||
|
||||
// the element we will replace
|
||||
var element_at_index = (list.bytes orelse undefined) + (index * element_width);
|
||||
|
||||
// decrement its refcount
|
||||
dec(element_at_index);
|
||||
|
||||
// copy in the new element
|
||||
@memcpy(element_at_index, element orelse undefined, element_width);
|
||||
|
||||
return list;
|
||||
} else {
|
||||
// we're out of bounds, and will not be using the `element`.
|
||||
// but we must consume the RC token, so explicitly decrement
|
||||
dec(element);
|
||||
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ comptime {
|
|||
exportListFn(list.listReverse, "reverse");
|
||||
exportListFn(list.listSortWith, "sort_with");
|
||||
exportListFn(list.listConcat, "concat");
|
||||
exportListFn(list.listSet, "set");
|
||||
}
|
||||
|
||||
// Dict Module
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue