2019-12-01 11:27:36 +03:00
|
|
|
module builtin
|
|
|
|
|
|
|
|
pub struct array {
|
|
|
|
pub:
|
2021-03-24 21:39:59 +03:00
|
|
|
data voidptr
|
|
|
|
len int
|
|
|
|
cap int
|
2019-12-01 11:27:36 +03:00
|
|
|
element_size int
|
|
|
|
}
|
|
|
|
|
|
|
|
// for now off the stack
|
2019-12-31 21:42:16 +03:00
|
|
|
fn new_array_from_c_array(len int, cap int, elm_size int, c_array voidptr) array {
|
2021-03-24 21:39:59 +03:00
|
|
|
arr := array{
|
2019-12-01 11:27:36 +03:00
|
|
|
len: len
|
|
|
|
cap: cap
|
|
|
|
element_size: elm_size
|
|
|
|
data: c_array
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private function. Used to implement array[] operator
|
|
|
|
fn (a array) get(i int) voidptr {
|
|
|
|
if i < 0 || i >= a.len {
|
|
|
|
panic('array.get: index out of range') // FIXME: (i == $i, a.len == $a.len)')
|
|
|
|
}
|
|
|
|
return a.data + i * a.element_size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private function. Used to implement assigment to the array element.
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut a array) set(i int, val voidptr) {
|
2019-12-01 11:27:36 +03:00
|
|
|
if i < 0 || i >= a.len {
|
2021-03-24 21:39:59 +03:00
|
|
|
panic('array.set: index out of range') // FIXME: (i == $i, a.len == $a.len)')
|
2019-12-01 11:27:36 +03:00
|
|
|
}
|
|
|
|
mem_copy(a.data + a.element_size * i, val, a.element_size)
|
|
|
|
}
|
2019-12-08 13:44:52 +03:00
|
|
|
|
|
|
|
// array.repeat returns new array with the given array elements
|
|
|
|
// repeated `nr_repeat` times
|
|
|
|
pub fn (a array) repeat(nr_repeats int) array {
|
|
|
|
assert nr_repeats >= 0
|
|
|
|
|
2021-03-24 21:39:59 +03:00
|
|
|
arr := array{
|
2019-12-08 13:44:52 +03:00
|
|
|
len: nr_repeats * a.len
|
|
|
|
cap: nr_repeats * a.len
|
|
|
|
element_size: a.element_size
|
|
|
|
data: malloc(nr_repeats * a.len * a.element_size)
|
|
|
|
}
|
2021-03-24 21:39:59 +03:00
|
|
|
for i in 0 .. nr_repeats {
|
2019-12-08 13:44:52 +03:00
|
|
|
mem_copy(arr.data + i * a.len * a.element_size, a.data, a.len * a.element_size)
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|