mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-13 09:49:11 +03:00
Merge pull request #1584 from rtfeldman/fix-list-prepend
move List.prepend to zig
This commit is contained in:
commit
b031eb0e54
@ -733,6 +733,30 @@ pub fn listAppend(list: RocList, alignment: u32, element: Opaque, element_width:
|
||||
return output;
|
||||
}
|
||||
|
||||
pub fn listPrepend(list: RocList, alignment: u32, element: Opaque, element_width: usize) callconv(.C) RocList {
|
||||
const old_length = list.len();
|
||||
var output = list.reallocate(alignment, old_length + 1, element_width);
|
||||
|
||||
// can't use one memcpy here because source and target overlap
|
||||
if (output.bytes) |target| {
|
||||
var i: usize = old_length;
|
||||
|
||||
while (i > 0) {
|
||||
i -= 1;
|
||||
|
||||
// move the ith element to the (i + 1)th position
|
||||
@memcpy(target + (i + 1) * element_width, target + i * element_width, element_width);
|
||||
}
|
||||
|
||||
// finally copy in the new first element
|
||||
if (element) |source| {
|
||||
@memcpy(target, source, element_width);
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
pub fn listSwap(
|
||||
list: RocList,
|
||||
alignment: u32,
|
||||
|
@ -33,6 +33,7 @@ comptime {
|
||||
exportListFn(list.listContains, "contains");
|
||||
exportListFn(list.listRepeat, "repeat");
|
||||
exportListFn(list.listAppend, "append");
|
||||
exportListFn(list.listPrepend, "prepend");
|
||||
exportListFn(list.listSingle, "single");
|
||||
exportListFn(list.listJoin, "join");
|
||||
exportListFn(list.listRange, "range");
|
||||
|
@ -57,6 +57,7 @@ pub const LIST_WALK_BACKWARDS: &str = "roc_builtins.list.walk_backwards";
|
||||
pub const LIST_CONTAINS: &str = "roc_builtins.list.contains";
|
||||
pub const LIST_REPEAT: &str = "roc_builtins.list.repeat";
|
||||
pub const LIST_APPEND: &str = "roc_builtins.list.append";
|
||||
pub const LIST_PREPEND: &str = "roc_builtins.list.prepend";
|
||||
pub const LIST_DROP: &str = "roc_builtins.list.drop";
|
||||
pub const LIST_SWAP: &str = "roc_builtins.list.swap";
|
||||
pub const LIST_SINGLE: &str = "roc_builtins.list.single";
|
||||
|
@ -121,69 +121,6 @@ pub fn list_repeat<'a, 'ctx, 'env>(
|
||||
)
|
||||
}
|
||||
|
||||
/// List.prepend : List elem, elem -> List elem
|
||||
pub fn list_prepend<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
original_wrapper: StructValue<'ctx>,
|
||||
elem: BasicValueEnum<'ctx>,
|
||||
elem_layout: &Layout<'a>,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
let builder = env.builder;
|
||||
|
||||
// Load the usize length from the wrapper.
|
||||
let len = list_len(builder, original_wrapper);
|
||||
let elem_type = basic_type_from_layout(env, elem_layout);
|
||||
let ptr_type = elem_type.ptr_type(AddressSpace::Generic);
|
||||
let list_ptr = load_list_ptr(builder, original_wrapper, ptr_type);
|
||||
|
||||
// The output list length, which is the old list length + 1
|
||||
let new_list_len = env.builder.build_int_add(
|
||||
env.ptr_int().const_int(1_u64, false),
|
||||
len,
|
||||
"new_list_length",
|
||||
);
|
||||
|
||||
// Allocate space for the new array that we'll copy into.
|
||||
let clone_ptr = allocate_list(env, elem_layout, new_list_len);
|
||||
|
||||
builder.build_store(clone_ptr, elem);
|
||||
|
||||
let index_1_ptr = unsafe {
|
||||
builder.build_in_bounds_gep(
|
||||
clone_ptr,
|
||||
&[env.ptr_int().const_int(1_u64, false)],
|
||||
"load_index",
|
||||
)
|
||||
};
|
||||
|
||||
// Calculate the number of bytes we'll need to allocate.
|
||||
let elem_bytes = env
|
||||
.ptr_int()
|
||||
.const_int(elem_layout.stack_size(env.ptr_bytes) as u64, false);
|
||||
|
||||
// This is the size of the list coming in, before we have added an element
|
||||
// to the beginning.
|
||||
let list_size = env
|
||||
.builder
|
||||
.build_int_mul(elem_bytes, len, "mul_old_len_by_elem_bytes");
|
||||
|
||||
let ptr_bytes = env.ptr_bytes;
|
||||
|
||||
if elem_layout.safe_to_memcpy() {
|
||||
// Copy the bytes from the original array into the new
|
||||
// one we just allocated
|
||||
//
|
||||
// TODO how do we decide when to do the small memcpy vs the normal one?
|
||||
builder
|
||||
.build_memcpy(index_1_ptr, ptr_bytes, list_ptr, ptr_bytes, list_size)
|
||||
.unwrap();
|
||||
} else {
|
||||
panic!("TODO Cranelift currently only knows how to clone list elements that are Copy.");
|
||||
}
|
||||
|
||||
store_list(env, clone_ptr, new_list_len)
|
||||
}
|
||||
|
||||
/// List.join : List (List elem) -> List elem
|
||||
pub fn list_join<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
@ -299,6 +236,25 @@ pub fn list_append<'a, 'ctx, 'env>(
|
||||
)
|
||||
}
|
||||
|
||||
/// List.prepend : List elem, elem -> List elem
|
||||
pub fn list_prepend<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
original_wrapper: StructValue<'ctx>,
|
||||
element: BasicValueEnum<'ctx>,
|
||||
element_layout: &Layout<'a>,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
call_bitcode_fn_returns_list(
|
||||
env,
|
||||
&[
|
||||
pass_list_as_i128(env, original_wrapper.into()),
|
||||
env.alignment_intvalue(element_layout),
|
||||
pass_element_as_opaque(env, element),
|
||||
layout_width(env, element_layout),
|
||||
],
|
||||
bitcode::LIST_PREPEND,
|
||||
)
|
||||
}
|
||||
|
||||
/// List.swap : List elem, Nat, Nat -> List elem
|
||||
pub fn list_swap<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
|
@ -276,6 +276,20 @@ fn list_prepend() {
|
||||
RocList::from_slice(&[6, 4]),
|
||||
RocList<i64>
|
||||
);
|
||||
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
init : List Str
|
||||
init =
|
||||
["foo"]
|
||||
|
||||
List.prepend init "bar"
|
||||
"#
|
||||
),
|
||||
RocList::from_slice(&[RocStr::from_slice(b"bar"), RocStr::from_slice(b"foo"),]),
|
||||
RocList<RocStr>
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user