mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +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 {
|
pub fn listJoin(list_of_lists: RocList, alignment: usize, element_width: usize) callconv(.C) RocList {
|
||||||
var total_length: usize = 0;
|
var total_length: usize = 0;
|
||||||
|
|
||||||
const size = list_of_lists.len();
|
|
||||||
const slice_of_lists = @ptrCast([*]RocList, @alignCast(@alignOf(RocList), list_of_lists.bytes));
|
const slice_of_lists = @ptrCast([*]RocList, @alignCast(@alignOf(RocList), list_of_lists.bytes));
|
||||||
|
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
|
@ -873,3 +872,44 @@ pub fn listJoin(list_of_lists: RocList, alignment: usize, element_width: usize)
|
||||||
|
|
||||||
return output;
|
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.listRange, "range");
|
||||||
exportListFn(list.listReverse, "reverse");
|
exportListFn(list.listReverse, "reverse");
|
||||||
exportListFn(list.listSortWith, "sort_with");
|
exportListFn(list.listSortWith, "sort_with");
|
||||||
|
exportListFn(list.listConcat, "concat");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dict Module
|
// 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_RANGE: &str = "roc_builtins.list.range";
|
||||||
pub const LIST_REVERSE: &str = "roc_builtins.list.reverse";
|
pub const LIST_REVERSE: &str = "roc_builtins.list.reverse";
|
||||||
pub const LIST_SORT_WITH: &str = "roc_builtins.list.sort_with";
|
pub const LIST_SORT_WITH: &str = "roc_builtins.list.sort_with";
|
||||||
|
pub const LIST_CONCAT: &str = "roc_builtins.list.concat";
|
||||||
|
|
|
@ -1028,196 +1028,30 @@ pub fn list_map3<'a, 'ctx, 'env>(
|
||||||
/// List.concat : List elem, List elem -> List elem
|
/// List.concat : List elem, List elem -> List elem
|
||||||
pub fn list_concat<'a, 'ctx, 'env>(
|
pub fn list_concat<'a, 'ctx, 'env>(
|
||||||
env: &Env<'a, 'ctx, 'env>,
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
inplace: InPlace,
|
_inplace: InPlace,
|
||||||
parent: FunctionValue<'ctx>,
|
_parent: FunctionValue<'ctx>,
|
||||||
first_list: BasicValueEnum<'ctx>,
|
first_list: BasicValueEnum<'ctx>,
|
||||||
second_list: BasicValueEnum<'ctx>,
|
second_list: BasicValueEnum<'ctx>,
|
||||||
list_layout: &Layout<'a>,
|
list_layout: &Layout<'a>,
|
||||||
) -> BasicValueEnum<'ctx> {
|
) -> BasicValueEnum<'ctx> {
|
||||||
let builder = env.builder;
|
|
||||||
let ctx = env.context;
|
|
||||||
|
|
||||||
let second_list_wrapper = second_list.into_struct_value();
|
|
||||||
|
|
||||||
let second_list_len = list_len(builder, second_list_wrapper);
|
|
||||||
|
|
||||||
// We only match on the first lists layout
|
|
||||||
// because the first and second input lists
|
|
||||||
// necessarily have the same layout
|
|
||||||
match list_layout {
|
match list_layout {
|
||||||
Layout::Builtin(Builtin::EmptyList) => empty_list(env),
|
Layout::Builtin(Builtin::EmptyList) => {
|
||||||
Layout::Builtin(Builtin::List(_, elem_layout)) => {
|
// If the input list is empty, or if it is a list of empty lists
|
||||||
let first_list_wrapper = first_list.into_struct_value();
|
// then simply return an empty list
|
||||||
|
empty_list(env)
|
||||||
let first_list_len = list_len(builder, first_list_wrapper);
|
|
||||||
|
|
||||||
// first_list_len > 0
|
|
||||||
// We do this check to avoid allocating memory. If the first input
|
|
||||||
// list is empty, then we can just return the second list cloned
|
|
||||||
let first_list_length_comparison = list_is_not_empty(env, first_list_len);
|
|
||||||
|
|
||||||
let if_first_list_is_empty = || {
|
|
||||||
// second_list_len > 0
|
|
||||||
// We do this check to avoid allocating memory. If the second input
|
|
||||||
// list is empty, then we can just return an empty list
|
|
||||||
let second_list_length_comparison = list_is_not_empty(env, second_list_len);
|
|
||||||
|
|
||||||
let build_second_list_then = || {
|
|
||||||
let elem_type = basic_type_from_layout(env, elem_layout);
|
|
||||||
let ptr_type = get_ptr_type(&elem_type, AddressSpace::Generic);
|
|
||||||
|
|
||||||
let (new_wrapper, _) = clone_nonempty_list(
|
|
||||||
env,
|
|
||||||
inplace,
|
|
||||||
second_list_len,
|
|
||||||
load_list_ptr(builder, second_list_wrapper, ptr_type),
|
|
||||||
elem_layout,
|
|
||||||
);
|
|
||||||
|
|
||||||
BasicValueEnum::StructValue(new_wrapper)
|
|
||||||
};
|
|
||||||
|
|
||||||
let build_second_list_else = || empty_list(env);
|
|
||||||
|
|
||||||
build_basic_phi2(
|
|
||||||
env,
|
|
||||||
parent,
|
|
||||||
second_list_length_comparison,
|
|
||||||
build_second_list_then,
|
|
||||||
build_second_list_else,
|
|
||||||
BasicTypeEnum::StructType(super::convert::zig_list_type(env)),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let if_first_list_is_not_empty = || {
|
|
||||||
let elem_type = basic_type_from_layout(env, elem_layout);
|
|
||||||
let ptr_type = get_ptr_type(&elem_type, AddressSpace::Generic);
|
|
||||||
|
|
||||||
let if_second_list_is_empty = || {
|
|
||||||
let (new_wrapper, _) = clone_nonempty_list(
|
|
||||||
env,
|
|
||||||
inplace,
|
|
||||||
first_list_len,
|
|
||||||
load_list_ptr(builder, first_list_wrapper, ptr_type),
|
|
||||||
elem_layout,
|
|
||||||
);
|
|
||||||
|
|
||||||
BasicValueEnum::StructValue(new_wrapper)
|
|
||||||
};
|
|
||||||
|
|
||||||
// second_list_len > 0
|
|
||||||
// We do this check to avoid allocating memory. If the second input
|
|
||||||
// list is empty, then we can just return the first list cloned
|
|
||||||
let second_list_length_comparison = list_is_not_empty(env, second_list_len);
|
|
||||||
|
|
||||||
let if_second_list_is_not_empty = || {
|
|
||||||
let combined_list_len =
|
|
||||||
builder.build_int_add(first_list_len, second_list_len, "add_list_lengths");
|
|
||||||
|
|
||||||
let combined_list_ptr =
|
|
||||||
allocate_list(env, inplace, elem_layout, combined_list_len);
|
|
||||||
|
|
||||||
let first_list_ptr = load_list_ptr(builder, first_list_wrapper, ptr_type);
|
|
||||||
|
|
||||||
// FIRST LOOP
|
|
||||||
// TODO when the element type supports it, replace FIRST_LOOP with a memcpy!
|
|
||||||
let first_loop = |first_index, first_list_elem| {
|
|
||||||
// The pointer to the element in the combined list
|
|
||||||
let combined_list_elem_ptr = unsafe {
|
|
||||||
builder.build_in_bounds_gep(
|
|
||||||
combined_list_ptr,
|
|
||||||
&[first_index],
|
|
||||||
"load_index_combined_list",
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Mutate the new array in-place to change the element.
|
|
||||||
builder.build_store(combined_list_elem_ptr, first_list_elem);
|
|
||||||
};
|
|
||||||
|
|
||||||
let index_name = "#index";
|
|
||||||
|
|
||||||
let index_alloca = incrementing_elem_loop(
|
|
||||||
builder,
|
|
||||||
ctx,
|
|
||||||
parent,
|
|
||||||
first_list_ptr,
|
|
||||||
first_list_len,
|
|
||||||
index_name,
|
|
||||||
first_loop,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Reset the index variable to 0
|
|
||||||
builder.build_store(index_alloca, ctx.i64_type().const_int(0, false));
|
|
||||||
|
|
||||||
let second_list_ptr = load_list_ptr(builder, second_list_wrapper, ptr_type);
|
|
||||||
|
|
||||||
// SECOND LOOP
|
|
||||||
// TODO when the element type supports it, replace SECOND_LOOP with a memcpy!
|
|
||||||
let second_loop = |second_index, second_list_elem| {
|
|
||||||
// The pointer to the element in the combined list.
|
|
||||||
// Note that the pointer does not start at the index
|
|
||||||
// 0, it starts at the index of first_list_len. In that
|
|
||||||
// sense it is "offset".
|
|
||||||
let offset_combined_list_elem_ptr = unsafe {
|
|
||||||
builder.build_in_bounds_gep(
|
|
||||||
combined_list_ptr,
|
|
||||||
&[first_list_len],
|
|
||||||
"elem",
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
// The pointer to the element from the second list
|
|
||||||
// in the combined list
|
|
||||||
let combined_list_elem_ptr = unsafe {
|
|
||||||
builder.build_in_bounds_gep(
|
|
||||||
offset_combined_list_elem_ptr,
|
|
||||||
&[second_index],
|
|
||||||
"load_index_combined_list",
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Mutate the new array in-place to change the element.
|
|
||||||
builder.build_store(combined_list_elem_ptr, second_list_elem);
|
|
||||||
};
|
|
||||||
|
|
||||||
incrementing_elem_loop(
|
|
||||||
builder,
|
|
||||||
ctx,
|
|
||||||
parent,
|
|
||||||
second_list_ptr,
|
|
||||||
second_list_len,
|
|
||||||
index_name,
|
|
||||||
second_loop,
|
|
||||||
);
|
|
||||||
|
|
||||||
store_list(env, combined_list_ptr, combined_list_len)
|
|
||||||
};
|
|
||||||
|
|
||||||
build_basic_phi2(
|
|
||||||
env,
|
|
||||||
parent,
|
|
||||||
second_list_length_comparison,
|
|
||||||
if_second_list_is_not_empty,
|
|
||||||
if_second_list_is_empty,
|
|
||||||
BasicTypeEnum::StructType(super::convert::zig_list_type(env)),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
build_basic_phi2(
|
|
||||||
env,
|
|
||||||
parent,
|
|
||||||
first_list_length_comparison,
|
|
||||||
if_first_list_is_not_empty,
|
|
||||||
if_first_list_is_empty,
|
|
||||||
BasicTypeEnum::StructType(super::convert::zig_list_type(env)),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
Layout::Builtin(Builtin::List(_, elem_layout)) => call_bitcode_fn_returns_list(
|
||||||
|
env,
|
||||||
|
&[
|
||||||
|
pass_list_as_i128(env, first_list),
|
||||||
|
pass_list_as_i128(env, second_list),
|
||||||
|
alignment_intvalue(env, elem_layout),
|
||||||
|
layout_width(env, elem_layout),
|
||||||
|
],
|
||||||
|
&bitcode::LIST_CONCAT,
|
||||||
|
),
|
||||||
_ => {
|
_ => {
|
||||||
unreachable!(
|
unreachable!("Invalid List layout for List.concat {:?}", list_layout);
|
||||||
"Invalid List layout for first list in List.concat : {:?}",
|
|
||||||
list_layout
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1428,15 +1262,6 @@ pub fn empty_list<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> BasicValueEnum<'
|
||||||
BasicValueEnum::StructValue(struct_type.const_zero())
|
BasicValueEnum::StructValue(struct_type.const_zero())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_is_not_empty<'ctx>(env: &Env<'_, 'ctx, '_>, len: IntValue<'ctx>) -> IntValue<'ctx> {
|
|
||||||
env.builder.build_int_compare(
|
|
||||||
IntPredicate::UGT,
|
|
||||||
len,
|
|
||||||
env.ptr_int().const_zero(),
|
|
||||||
"list_len_is_nonzero",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load_list<'ctx>(
|
pub fn load_list<'ctx>(
|
||||||
builder: &Builder<'ctx>,
|
builder: &Builder<'ctx>,
|
||||||
wrapper_struct: StructValue<'ctx>,
|
wrapper_struct: StructValue<'ctx>,
|
||||||
|
|
|
@ -642,7 +642,7 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
|
||||||
ListSet => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),
|
ListSet => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),
|
||||||
ListSetInPlace => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),
|
ListSetInPlace => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),
|
||||||
ListGetUnsafe => arena.alloc_slice_copy(&[borrowed, irrelevant]),
|
ListGetUnsafe => arena.alloc_slice_copy(&[borrowed, irrelevant]),
|
||||||
ListConcat => arena.alloc_slice_copy(&[borrowed, borrowed]),
|
ListConcat => arena.alloc_slice_copy(&[owned, owned]),
|
||||||
StrConcat => arena.alloc_slice_copy(&[owned, borrowed]),
|
StrConcat => arena.alloc_slice_copy(&[owned, borrowed]),
|
||||||
StrSplit => arena.alloc_slice_copy(&[borrowed, borrowed]),
|
StrSplit => arena.alloc_slice_copy(&[borrowed, borrowed]),
|
||||||
ListSingle => arena.alloc_slice_copy(&[irrelevant]),
|
ListSingle => arena.alloc_slice_copy(&[irrelevant]),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue