mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
Implement basic List.drop that satisfies tests
Issues with uniqueness and copying which leaks memory obviously needs to be fixed.
This commit is contained in:
parent
b7b04344f3
commit
f45d89270b
11 changed files with 130 additions and 3 deletions
|
@ -665,6 +665,33 @@ pub fn listAppend(list: RocList, alignment: usize, element: Opaque, element_widt
|
|||
return output;
|
||||
}
|
||||
|
||||
pub fn listDrop(list: RocList, alignment: usize, element_width: usize, count: usize) callconv(.C) RocList {
|
||||
const size = list.len();
|
||||
if (size <= count) {
|
||||
return RocList.empty();
|
||||
}
|
||||
if (list.bytes) |source_ptr| {
|
||||
var i: usize = 0;
|
||||
|
||||
const output = RocList.allocate(std.heap.c_allocator, alignment, size - count, element_width);
|
||||
const target_ptr = output.bytes orelse unreachable;
|
||||
|
||||
while (i < size - count) : (i += 1) {
|
||||
@memcpy(target_ptr + (i * element_width), source_ptr + ((i + count) * element_width), element_width);
|
||||
}
|
||||
|
||||
// if (list.isUnique()) {
|
||||
// std.heap.c_allocator.free(source_ptr);
|
||||
// } else {
|
||||
// utils.decref(std.heap.c_allocator, alignment, list.bytes, size * element_width);
|
||||
// }
|
||||
|
||||
return output;
|
||||
} else {
|
||||
return RocList.empty();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn listRange(width: utils.IntWidth, low: Opaque, high: Opaque) callconv(.C) RocList {
|
||||
const allocator = std.heap.c_allocator;
|
||||
const IntWidth = utils.IntWidth;
|
||||
|
|
|
@ -25,6 +25,7 @@ comptime {
|
|||
exportListFn(list.listReverse, "reverse");
|
||||
exportListFn(list.listSortWith, "sort_with");
|
||||
exportListFn(list.listConcat, "concat");
|
||||
exportListFn(list.listDrop, "drop");
|
||||
}
|
||||
|
||||
// Dict Module
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue