mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
implement list.concat in zig
This commit is contained in:
parent
4375026c7b
commit
eda462c6e9
5 changed files with 61 additions and 194 deletions
|
@ -848,7 +848,6 @@ fn swapElements(source_ptr: [*]u8, element_width: usize, index_1: usize, index_2
|
|||
pub fn listJoin(list_of_lists: RocList, alignment: usize, element_width: usize) callconv(.C) RocList {
|
||||
var total_length: usize = 0;
|
||||
|
||||
const size = list_of_lists.len();
|
||||
const slice_of_lists = @ptrCast([*]RocList, @alignCast(@alignOf(RocList), list_of_lists.bytes));
|
||||
|
||||
var i: usize = 0;
|
||||
|
@ -873,3 +872,44 @@ pub fn listJoin(list_of_lists: RocList, alignment: usize, element_width: usize)
|
|||
|
||||
return output;
|
||||
}
|
||||
|
||||
pub fn listConcat(list_a: RocList, list_b: RocList, alignment: usize, element_width: usize) callconv(.C) RocList {
|
||||
if (list_a.isEmpty()) {
|
||||
return list_b;
|
||||
} else if (list_b.isEmpty()) {
|
||||
return list_a;
|
||||
} else if (!list_a.isEmpty() and list_a.isUnique()) {
|
||||
const total_length: usize = list_a.len() + list_b.len();
|
||||
|
||||
if (list_a.bytes) |source| {
|
||||
const new_source = utils.unsafeReallocate(
|
||||
source,
|
||||
std.heap.c_allocator,
|
||||
alignment,
|
||||
list_a.len(),
|
||||
total_length,
|
||||
element_width,
|
||||
);
|
||||
|
||||
if (list_b.bytes) |source_b| {
|
||||
@memcpy(new_source + list_a.len() * element_width, source_b, list_b.len() * element_width);
|
||||
}
|
||||
|
||||
return RocList{ .bytes = new_source, .length = total_length };
|
||||
}
|
||||
}
|
||||
const total_length: usize = list_a.len() + list_b.len();
|
||||
|
||||
const output = RocList.allocate(std.heap.c_allocator, alignment, total_length, element_width);
|
||||
|
||||
if (output.bytes) |target| {
|
||||
if (list_a.bytes) |source| {
|
||||
@memcpy(target, source, list_a.len() * element_width);
|
||||
}
|
||||
if (list_b.bytes) |source| {
|
||||
@memcpy(target + list_a.len() * element_width, source, list_b.len() * element_width);
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ comptime {
|
|||
exportListFn(list.listRange, "range");
|
||||
exportListFn(list.listReverse, "reverse");
|
||||
exportListFn(list.listSortWith, "sort_with");
|
||||
exportListFn(list.listConcat, "concat");
|
||||
}
|
||||
|
||||
// Dict Module
|
||||
|
|
|
@ -81,3 +81,4 @@ pub const LIST_JOIN: &str = "roc_builtins.list.join";
|
|||
pub const LIST_RANGE: &str = "roc_builtins.list.range";
|
||||
pub const LIST_REVERSE: &str = "roc_builtins.list.reverse";
|
||||
pub const LIST_SORT_WITH: &str = "roc_builtins.list.sort_with";
|
||||
pub const LIST_CONCAT: &str = "roc_builtins.list.concat";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue