2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-23 04:21:30 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 20:20:28 +02:00
|
|
|
module builtin
|
|
|
|
|
2020-04-26 07:06:33 +02:00
|
|
|
import strings
|
2019-08-10 23:02:48 +02:00
|
|
|
|
2020-12-15 09:31:35 +01:00
|
|
|
// array is a struct used for denoting array types in V
|
2020-01-12 19:59:57 +01:00
|
|
|
pub struct array {
|
2019-07-21 12:43:47 +02:00
|
|
|
pub:
|
2020-12-15 09:31:35 +01:00
|
|
|
element_size int // size in bytes of one element in the array.
|
2020-04-27 22:53:26 +02:00
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
data voidptr
|
|
|
|
len int // length of the array.
|
|
|
|
cap int // capacity of the array.
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-15 09:31:35 +01:00
|
|
|
// array.data uses a void pointer, which allows implementing arrays without generics and without generating
|
|
|
|
// extra code for every type.
|
2020-01-02 20:09:15 +01:00
|
|
|
// Internal function, used by V (`nums := []int`)
|
2020-04-25 08:42:23 +02:00
|
|
|
fn __new_array(mylen int, cap int, elm_size int) array {
|
2020-05-07 22:41:41 +02:00
|
|
|
cap_ := if cap < mylen { mylen } else { cap }
|
2019-12-18 18:07:32 +01:00
|
|
|
arr := array{
|
2020-05-18 17:05:48 +02:00
|
|
|
element_size: elm_size
|
|
|
|
data: vcalloc(cap_ * elm_size)
|
2020-05-18 21:38:06 +02:00
|
|
|
len: mylen
|
|
|
|
cap: cap_
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
2020-04-15 20:12:06 +02:00
|
|
|
|
2020-05-16 15:21:37 +02:00
|
|
|
fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array {
|
|
|
|
cap_ := if cap < mylen { mylen } else { cap }
|
2020-07-03 18:10:10 +02:00
|
|
|
mut arr := array{
|
2020-05-18 17:05:48 +02:00
|
|
|
element_size: elm_size
|
|
|
|
data: vcalloc(cap_ * elm_size)
|
2020-05-18 21:38:06 +02:00
|
|
|
len: mylen
|
|
|
|
cap: cap_
|
2020-05-16 15:21:37 +02:00
|
|
|
}
|
|
|
|
if val != 0 {
|
2020-10-15 12:32:28 +02:00
|
|
|
for i in 0 .. arr.len {
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { arr.set_unsafe(i, val) }
|
2020-05-16 15:21:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2020-06-12 01:24:25 +02:00
|
|
|
fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array) array {
|
|
|
|
cap_ := if cap < mylen { mylen } else { cap }
|
2020-07-03 18:10:10 +02:00
|
|
|
mut arr := array{
|
2020-06-12 01:24:25 +02:00
|
|
|
element_size: elm_size
|
|
|
|
data: vcalloc(cap_ * elm_size)
|
|
|
|
len: mylen
|
|
|
|
cap: cap_
|
|
|
|
}
|
2020-10-15 12:32:28 +02:00
|
|
|
for i in 0 .. arr.len {
|
2020-06-12 01:24:25 +02:00
|
|
|
val_clone := val.clone()
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { arr.set_unsafe(i, &val_clone) }
|
2020-06-12 01:24:25 +02:00
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
// Private function, used by V (`nums := [1, 2, 3]`)
|
2020-10-15 12:32:28 +02:00
|
|
|
fn new_array_from_c_array(len int, cap int, elm_size int, c_array voidptr) array {
|
2020-05-07 22:41:41 +02:00
|
|
|
cap_ := if cap < len { len } else { cap }
|
2019-12-18 18:07:32 +01:00
|
|
|
arr := array{
|
2020-05-18 17:05:48 +02:00
|
|
|
element_size: elm_size
|
|
|
|
data: vcalloc(cap_ * elm_size)
|
2020-05-18 21:38:06 +02:00
|
|
|
len: len
|
|
|
|
cap: cap_
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// TODO Write all memory functions (like memcpy) in V
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { C.memcpy(arr.data, c_array, len * elm_size) }
|
2019-06-22 20:20:28 +02:00
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private function, used by V (`nums := [1, 2, 3] !`)
|
2020-10-15 12:32:28 +02:00
|
|
|
fn new_array_from_c_array_no_alloc(len int, cap int, elm_size int, c_array voidptr) array {
|
2019-12-18 18:07:32 +01:00
|
|
|
arr := array{
|
2020-05-18 17:05:48 +02:00
|
|
|
element_size: elm_size
|
|
|
|
data: c_array
|
2020-05-18 21:38:06 +02:00
|
|
|
len: len
|
|
|
|
cap: cap
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// Private function. Doubles array capacity if needed.
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut a array) ensure_cap(required int) {
|
2020-01-19 13:11:58 +01:00
|
|
|
if required <= a.cap {
|
|
|
|
return
|
2019-10-31 19:50:20 +01:00
|
|
|
}
|
2020-12-19 20:35:53 +01:00
|
|
|
mut cap := if a.cap > 0 { a.cap } else { 2 }
|
2020-01-19 13:11:58 +01:00
|
|
|
for required > cap {
|
|
|
|
cap *= 2
|
|
|
|
}
|
2020-12-19 20:35:53 +01:00
|
|
|
new_size := cap * a.element_size
|
|
|
|
mut new_data := byteptr(0)
|
|
|
|
if a.cap > 0 {
|
2021-03-14 13:54:35 +01:00
|
|
|
new_data = unsafe { realloc_data(a.data, a.cap * a.element_size, new_size) }
|
2020-10-15 12:32:28 +02:00
|
|
|
} else {
|
2020-12-19 20:35:53 +01:00
|
|
|
new_data = vcalloc(new_size)
|
2020-01-19 13:11:58 +01:00
|
|
|
}
|
2020-12-19 20:35:53 +01:00
|
|
|
a.data = new_data
|
2020-01-19 13:11:58 +01:00
|
|
|
a.cap = cap
|
2019-10-31 19:50:20 +01:00
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// repeat returns a new array with the given array elements repeated given times.
|
2020-02-29 15:25:49 +01:00
|
|
|
pub fn (a array) repeat(count int) array {
|
|
|
|
if count < 0 {
|
|
|
|
panic('array.repeat: count is negative: $count')
|
2019-10-31 19:50:20 +01:00
|
|
|
}
|
2020-02-29 15:25:49 +01:00
|
|
|
mut size := count * a.len * a.element_size
|
2019-12-16 20:22:04 +01:00
|
|
|
if size == 0 {
|
|
|
|
size = a.element_size
|
|
|
|
}
|
2019-12-18 18:07:32 +01:00
|
|
|
arr := array{
|
2020-05-18 17:05:48 +02:00
|
|
|
element_size: a.element_size
|
|
|
|
data: vcalloc(size)
|
2020-05-18 21:38:06 +02:00
|
|
|
len: count * a.len
|
|
|
|
cap: count * a.len
|
2019-09-14 22:48:30 +02:00
|
|
|
}
|
2020-10-30 13:11:07 +01:00
|
|
|
size_of_array := int(sizeof(array))
|
2020-10-15 12:32:28 +02:00
|
|
|
for i in 0 .. count {
|
2020-10-30 13:11:07 +01:00
|
|
|
if a.len > 0 && a.element_size == size_of_array {
|
2020-06-12 11:42:26 +02:00
|
|
|
ary := array{}
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { C.memcpy(&ary, a.data, size_of_array) }
|
2020-06-12 11:42:26 +02:00
|
|
|
ary_clone := ary.clone()
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { C.memcpy(arr.get_unsafe(i * a.len), &ary_clone, a.len * a.element_size) }
|
2020-06-12 11:42:26 +02:00
|
|
|
} else {
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { C.memcpy(arr.get_unsafe(i * a.len), byteptr(a.data), a.len * a.element_size) }
|
2020-06-12 11:42:26 +02:00
|
|
|
}
|
2019-09-14 22:48:30 +02:00
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2021-02-05 16:51:45 +01:00
|
|
|
// sort_with_compare sorts array in-place using given `compare` function as comparator.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut a array) sort_with_compare(compare voidptr) {
|
2020-09-22 05:28:29 +02:00
|
|
|
C.qsort(mut a.data, a.len, a.element_size, compare)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// insert inserts a value in the array at index `i`
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut a array) insert(i int, val voidptr) {
|
2020-10-15 12:32:28 +02:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 16:13:45 +01:00
|
|
|
if i < 0 || i > a.len {
|
|
|
|
panic('array.insert: index out of range (i == $i, a.len == $a.len)')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-10-31 19:50:20 +01:00
|
|
|
a.ensure_cap(a.len + 1)
|
2020-07-03 18:10:10 +02:00
|
|
|
unsafe {
|
|
|
|
C.memmove(a.get_unsafe(i + 1), a.get_unsafe(i), (a.len - i) * a.element_size)
|
|
|
|
a.set_unsafe(i, val)
|
|
|
|
}
|
2019-10-31 19:50:20 +01:00
|
|
|
a.len++
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// insert_many inserts many values into the array from index `i`.
|
2021-02-26 22:55:09 +01:00
|
|
|
[unsafe]
|
2020-06-18 12:08:11 +02:00
|
|
|
pub fn (mut a array) insert_many(i int, val voidptr, size int) {
|
2020-10-15 12:32:28 +02:00
|
|
|
$if !no_bounds_checking ? {
|
2020-06-18 12:08:11 +02:00
|
|
|
if i < 0 || i > a.len {
|
|
|
|
panic('array.insert_many: index out of range (i == $i, a.len == $a.len)')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.ensure_cap(a.len + size)
|
|
|
|
elem_size := a.element_size
|
2020-07-03 18:10:10 +02:00
|
|
|
unsafe {
|
|
|
|
iptr := a.get_unsafe(i)
|
|
|
|
C.memmove(a.get_unsafe(i + size), iptr, (a.len - i) * elem_size)
|
|
|
|
C.memcpy(iptr, val, size * elem_size)
|
|
|
|
}
|
2020-06-18 12:08:11 +02:00
|
|
|
a.len += size
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// prepend prepends one value to the array.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut a array) prepend(val voidptr) {
|
2019-06-22 20:20:28 +02:00
|
|
|
a.insert(0, val)
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// prepend_many prepends another array to this array.
|
2021-02-26 22:55:09 +01:00
|
|
|
[unsafe]
|
2020-06-18 12:08:11 +02:00
|
|
|
pub fn (mut a array) prepend_many(val voidptr, size int) {
|
2021-02-26 22:55:09 +01:00
|
|
|
unsafe { a.insert_many(0, val, size) }
|
2020-06-18 12:08:11 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// delete deletes array element at index `i`.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut a array) delete(i int) {
|
2020-10-15 12:32:28 +02:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 16:13:45 +01:00
|
|
|
if i < 0 || i >= a.len {
|
|
|
|
panic('array.delete: index out of range (i == $i, a.len == $a.len)')
|
|
|
|
}
|
2019-10-31 19:50:20 +01:00
|
|
|
}
|
2020-06-12 01:24:25 +02:00
|
|
|
// NB: if a is [12,34], a.len = 2, a.delete(0)
|
2020-05-24 10:10:41 +02:00
|
|
|
// should move (2-0-1) elements = 1 element (the 34) forward
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { C.memmove(a.get_unsafe(i), a.get_unsafe(i + 1), (a.len - i - 1) * a.element_size) }
|
2019-06-22 20:20:28 +02:00
|
|
|
a.len--
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// clear clears the array without deallocating the allocated data.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut a array) clear() {
|
2020-01-27 22:31:48 +01:00
|
|
|
a.len = 0
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// trim trims the array length to "index" without modifying the allocated data. If "index" is greater
|
|
|
|
// than len nothing will be changed.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut a array) trim(index int) {
|
2020-02-17 20:31:40 +01:00
|
|
|
if index < a.len {
|
|
|
|
a.len = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:10:10 +02:00
|
|
|
// we manually inline this for single operations for performance without -prod
|
2021-03-05 06:17:57 +01:00
|
|
|
[inline; unsafe]
|
2020-07-03 18:10:10 +02:00
|
|
|
fn (a array) get_unsafe(i int) voidptr {
|
|
|
|
unsafe {
|
|
|
|
return byteptr(a.data) + i * a.element_size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// Private function. Used to implement array[] operator.
|
2019-10-09 22:38:33 +02:00
|
|
|
fn (a array) get(i int) voidptr {
|
2020-10-15 12:32:28 +02:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 16:13:45 +01:00
|
|
|
if i < 0 || i >= a.len {
|
|
|
|
panic('array.get: index out of range (i == $i, a.len == $a.len)')
|
|
|
|
}
|
2019-10-09 22:38:33 +02:00
|
|
|
}
|
2020-07-03 18:10:10 +02:00
|
|
|
unsafe {
|
|
|
|
return byteptr(a.data) + i * a.element_size
|
|
|
|
}
|
2019-10-09 22:38:33 +02:00
|
|
|
}
|
|
|
|
|
2021-01-19 06:06:57 +01:00
|
|
|
// Private function. Used to implement x = a[i] or { ... }
|
|
|
|
fn (a array) get_with_check(i int) voidptr {
|
|
|
|
if i < 0 || i >= a.len {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
return byteptr(a.data) + i * a.element_size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// first returns the first element of the array.
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (a array) first() voidptr {
|
2020-10-15 12:32:28 +02:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 16:13:45 +01:00
|
|
|
if a.len == 0 {
|
|
|
|
panic('array.first: array is empty')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-04-02 15:31:44 +02:00
|
|
|
return a.data
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// last returns the last element of the array.
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (a array) last() voidptr {
|
2020-10-15 12:32:28 +02:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 16:13:45 +01:00
|
|
|
if a.len == 0 {
|
|
|
|
panic('array.last: array is empty')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-07-03 18:10:10 +02:00
|
|
|
unsafe {
|
|
|
|
return byteptr(a.data) + (a.len - 1) * a.element_size
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// pop returns the last element of the array, and removes it.
|
2020-07-14 18:55:44 +02:00
|
|
|
pub fn (mut a array) pop() voidptr {
|
|
|
|
// in a sense, this is the opposite of `a << x`
|
2020-10-15 12:32:28 +02:00
|
|
|
$if !no_bounds_checking ? {
|
2020-07-14 18:55:44 +02:00
|
|
|
if a.len == 0 {
|
|
|
|
panic('array.pop: array is empty')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_len := a.len - 1
|
2020-12-23 19:13:42 +01:00
|
|
|
last_elem := unsafe { byteptr(a.data) + (new_len) * a.element_size }
|
2020-07-14 18:55:44 +02:00
|
|
|
a.len = new_len
|
|
|
|
// NB: a.cap is not changed here *on purpose*, so that
|
|
|
|
// further << ops on that array will be more efficient.
|
2021-02-14 19:31:42 +01:00
|
|
|
return unsafe { memdup(last_elem, a.element_size) }
|
2020-07-14 18:55:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// delete_last efficiently deletes the last element of the array.
|
2020-10-23 23:04:22 +02:00
|
|
|
pub fn (mut a array) delete_last() {
|
|
|
|
// copy pasting code for performance
|
|
|
|
$if !no_bounds_checking ? {
|
|
|
|
if a.len == 0 {
|
|
|
|
panic('array.pop: array is empty')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.len--
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// slice returns an array using the same buffer as original array
|
2019-10-31 22:37:24 +01:00
|
|
|
// but starting from the `start` element and ending with the element before
|
|
|
|
// the `end` element of the original array with the length and capacity
|
2019-10-31 19:50:20 +01:00
|
|
|
// set to the number of the elements in the slice.
|
2020-10-15 12:32:28 +02:00
|
|
|
fn (a array) slice(start int, _end int) array {
|
2019-12-16 23:29:40 +01:00
|
|
|
mut end := _end
|
2020-10-15 12:32:28 +02:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 16:13:45 +01:00
|
|
|
if start > end {
|
|
|
|
panic('array.slice: invalid slice index ($start > $end)')
|
|
|
|
}
|
|
|
|
if end > a.len {
|
|
|
|
panic('array.slice: slice bounds out of range ($end >= $a.len)')
|
|
|
|
}
|
|
|
|
if start < 0 {
|
|
|
|
panic('array.slice: slice bounds out of range ($start < 0)')
|
|
|
|
}
|
2019-12-16 23:29:40 +01:00
|
|
|
}
|
2020-07-03 18:10:10 +02:00
|
|
|
mut data := byteptr(0)
|
|
|
|
unsafe {
|
|
|
|
data = byteptr(a.data) + start * a.element_size
|
|
|
|
}
|
2019-12-16 23:29:40 +01:00
|
|
|
l := end - start
|
2019-12-18 18:07:32 +01:00
|
|
|
res := array{
|
2019-12-16 23:29:40 +01:00
|
|
|
element_size: a.element_size
|
2020-07-03 18:10:10 +02:00
|
|
|
data: data
|
2019-12-16 23:29:40 +01:00
|
|
|
len: l
|
|
|
|
cap: l
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-01-08 10:19:12 +01:00
|
|
|
// used internally for [2..4]
|
2020-10-15 12:32:28 +02:00
|
|
|
fn (a array) slice2(start int, _end int, end_max bool) array {
|
2020-01-08 10:19:12 +01:00
|
|
|
end := if end_max { a.len } else { _end }
|
|
|
|
return a.slice(start, end)
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// clone_static returns an independent copy of a given array
|
2020-05-06 18:03:44 +02:00
|
|
|
// It should be used only in -autofree generated code.
|
|
|
|
fn (a array) clone_static() array {
|
|
|
|
return a.clone()
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// clone returns an independent copy of a given array.
|
2020-03-10 23:21:26 +01:00
|
|
|
pub fn (a &array) clone() array {
|
2020-01-08 10:19:12 +01:00
|
|
|
mut size := a.cap * a.element_size
|
|
|
|
if size == 0 {
|
|
|
|
size++
|
|
|
|
}
|
2020-07-03 18:10:10 +02:00
|
|
|
mut arr := array{
|
2020-05-18 17:05:48 +02:00
|
|
|
element_size: a.element_size
|
|
|
|
data: vcalloc(size)
|
2020-05-18 21:38:06 +02:00
|
|
|
len: a.len
|
|
|
|
cap: a.cap
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-06-19 13:32:55 +02:00
|
|
|
// Recursively clone-generated elements if array element is array type
|
2020-10-30 13:11:07 +01:00
|
|
|
size_of_array := int(sizeof(array))
|
|
|
|
if a.element_size == size_of_array {
|
2021-01-21 10:17:00 +01:00
|
|
|
mut is_elem_array := true
|
2020-10-15 12:32:28 +02:00
|
|
|
for i in 0 .. a.len {
|
2020-06-19 13:32:55 +02:00
|
|
|
ar := array{}
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { C.memcpy(&ar, a.get_unsafe(i), size_of_array) }
|
2021-01-21 10:17:00 +01:00
|
|
|
if ar.len > ar.cap || ar.cap <= 0 || ar.element_size <= 0 {
|
|
|
|
is_elem_array = false
|
|
|
|
break
|
|
|
|
}
|
2020-06-19 13:32:55 +02:00
|
|
|
ar_clone := ar.clone()
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { arr.set_unsafe(i, &ar_clone) }
|
2020-06-19 13:32:55 +02:00
|
|
|
}
|
2021-01-21 10:17:00 +01:00
|
|
|
if is_elem_array {
|
|
|
|
return arr
|
2020-12-14 06:34:47 +01:00
|
|
|
}
|
2020-06-19 13:32:55 +02:00
|
|
|
}
|
2021-01-21 10:17:00 +01:00
|
|
|
|
|
|
|
if !isnil(a.data) {
|
|
|
|
unsafe { C.memcpy(byteptr(arr.data), a.data, a.cap * a.element_size) }
|
|
|
|
}
|
2020-01-08 10:19:12 +01:00
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:32:28 +02:00
|
|
|
fn (a &array) slice_clone(start int, _end int) array {
|
2019-06-22 20:20:28 +02:00
|
|
|
mut end := _end
|
2020-10-15 12:32:28 +02:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 16:13:45 +01:00
|
|
|
if start > end {
|
|
|
|
panic('array.slice: invalid slice index ($start > $end)')
|
|
|
|
}
|
|
|
|
if end > a.len {
|
|
|
|
panic('array.slice: slice bounds out of range ($end >= $a.len)')
|
|
|
|
}
|
|
|
|
if start < 0 {
|
|
|
|
panic('array.slice: slice bounds out of range ($start < 0)')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-07-03 18:10:10 +02:00
|
|
|
mut data := byteptr(0)
|
|
|
|
unsafe {
|
|
|
|
data = byteptr(a.data) + start * a.element_size
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
l := end - start
|
2019-12-18 18:07:32 +01:00
|
|
|
res := array{
|
2019-10-31 19:50:20 +01:00
|
|
|
element_size: a.element_size
|
2020-07-03 18:10:10 +02:00
|
|
|
data: data
|
2019-06-22 20:20:28 +02:00
|
|
|
len: l
|
|
|
|
cap: l
|
|
|
|
}
|
2019-12-16 23:21:10 +01:00
|
|
|
return res.clone()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:10:10 +02:00
|
|
|
// we manually inline this for single operations for performance without -prod
|
2021-03-05 06:17:57 +01:00
|
|
|
[inline; unsafe]
|
2020-07-03 18:10:10 +02:00
|
|
|
fn (mut a array) set_unsafe(i int, val voidptr) {
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size) }
|
2020-07-03 18:10:10 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 19:50:20 +01:00
|
|
|
// Private function. Used to implement assigment to the array element.
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut a array) set(i int, val voidptr) {
|
2020-10-15 12:32:28 +02:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 16:13:45 +01:00
|
|
|
if i < 0 || i >= a.len {
|
|
|
|
panic('array.set: index out of range (i == $i, a.len == $a.len)')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size) }
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut a array) push(val voidptr) {
|
2019-10-31 19:50:20 +01:00
|
|
|
a.ensure_cap(a.len + 1)
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { C.memmove(byteptr(a.data) + a.element_size * a.len, val, a.element_size) }
|
2019-10-31 19:50:20 +01:00
|
|
|
a.len++
|
2019-10-09 22:38:33 +02:00
|
|
|
}
|
|
|
|
|
2021-02-05 16:51:45 +01:00
|
|
|
// push_many implements the functionality for pushing another array.
|
|
|
|
// `val` is array.data and user facing usage is `a << [1,2,3]`
|
2021-02-26 22:55:09 +01:00
|
|
|
[unsafe]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut a3 array) push_many(val voidptr, size int) {
|
2020-12-14 06:34:47 +01:00
|
|
|
if a3.data == val && !isnil(a3.data) {
|
2020-03-08 22:11:56 +01:00
|
|
|
// handle `arr << arr`
|
|
|
|
copy := a3.clone()
|
|
|
|
a3.ensure_cap(a3.len + size)
|
2020-07-03 18:10:10 +02:00
|
|
|
unsafe {
|
2020-10-15 12:32:28 +02:00
|
|
|
// C.memcpy(a.data, copy.data, copy.element_size * copy.len)
|
2020-07-03 18:10:10 +02:00
|
|
|
C.memcpy(a3.get_unsafe(a3.len), copy.data, a3.element_size * size)
|
|
|
|
}
|
2020-01-19 13:11:58 +01:00
|
|
|
} else {
|
2020-03-08 22:11:56 +01:00
|
|
|
a3.ensure_cap(a3.len + size)
|
2020-12-14 06:34:47 +01:00
|
|
|
if !isnil(a3.data) && !isnil(val) {
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { C.memcpy(a3.get_unsafe(a3.len), val, a3.element_size * size) }
|
2020-12-14 06:34:47 +01:00
|
|
|
}
|
2020-01-19 13:11:58 +01:00
|
|
|
}
|
2020-03-08 22:11:56 +01:00
|
|
|
a3.len += size
|
2019-10-09 22:38:33 +02:00
|
|
|
}
|
|
|
|
|
2020-12-15 09:31:35 +01:00
|
|
|
// reverse_in_place reverses existing array data, modifying original array.
|
2020-07-11 13:17:11 +02:00
|
|
|
pub fn (mut a array) reverse_in_place() {
|
|
|
|
if a.len < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
mut tmp_value := malloc(a.element_size)
|
2020-10-15 12:32:28 +02:00
|
|
|
for i in 0 .. a.len / 2 {
|
2020-07-11 13:17:11 +02:00
|
|
|
C.memcpy(tmp_value, byteptr(a.data) + i * a.element_size, a.element_size)
|
2021-01-23 09:33:22 +01:00
|
|
|
C.memcpy(byteptr(a.data) + i * a.element_size, byteptr(a.data) +
|
|
|
|
(a.len - 1 - i) * a.element_size, a.element_size)
|
2020-10-15 12:32:28 +02:00
|
|
|
C.memcpy(byteptr(a.data) + (a.len - 1 - i) * a.element_size, tmp_value, a.element_size)
|
2020-07-11 13:17:11 +02:00
|
|
|
}
|
|
|
|
free(tmp_value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// reverse returns a new array with the elements of the original array in reverse order.
|
2019-07-17 18:17:07 +02:00
|
|
|
pub fn (a array) reverse() array {
|
2020-02-29 20:44:02 +01:00
|
|
|
if a.len < 2 {
|
|
|
|
return a
|
|
|
|
}
|
2020-07-03 18:10:10 +02:00
|
|
|
mut arr := array{
|
2020-05-18 17:05:48 +02:00
|
|
|
element_size: a.element_size
|
|
|
|
data: vcalloc(a.cap * a.element_size)
|
2020-05-18 21:38:06 +02:00
|
|
|
len: a.len
|
|
|
|
cap: a.cap
|
2019-07-17 18:17:07 +02:00
|
|
|
}
|
2020-10-15 12:32:28 +02:00
|
|
|
for i in 0 .. a.len {
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { arr.set_unsafe(i, a.get_unsafe(a.len - 1 - i)) }
|
2019-07-17 18:17:07 +02:00
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:07:32 +01:00
|
|
|
// pub fn (a []int) free() {
|
2020-11-30 18:51:00 +01:00
|
|
|
// free frees all memory occupied by the array.
|
2020-08-09 11:22:11 +02:00
|
|
|
[unsafe]
|
2020-05-06 18:03:44 +02:00
|
|
|
pub fn (a &array) free() {
|
2020-07-11 13:22:16 +02:00
|
|
|
$if prealloc {
|
|
|
|
return
|
|
|
|
}
|
2019-12-18 18:07:32 +01:00
|
|
|
// if a.is_slice {
|
|
|
|
// return
|
|
|
|
// }
|
2021-03-20 14:16:36 +01:00
|
|
|
unsafe { free(a.data) }
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-03-18 20:10:42 +01:00
|
|
|
[unsafe]
|
|
|
|
pub fn (mut a []string) free() {
|
|
|
|
$if prealloc {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for s in a {
|
|
|
|
unsafe { s.free() }
|
|
|
|
}
|
2021-03-20 14:16:36 +01:00
|
|
|
unsafe { free(a.data) }
|
2021-03-18 20:10:42 +01:00
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// str returns a string representation of the array of strings
|
|
|
|
// => '["a", "b", "c"]'.
|
2019-06-30 13:06:46 +02:00
|
|
|
pub fn (a []string) str() string {
|
2019-08-31 01:35:05 +02:00
|
|
|
mut sb := strings.new_builder(a.len * 3)
|
2021-02-22 12:18:11 +01:00
|
|
|
sb.write_string('[')
|
2020-10-15 12:32:28 +02:00
|
|
|
for i in 0 .. a.len {
|
2019-06-22 20:20:28 +02:00
|
|
|
val := a[i]
|
2021-02-22 12:18:11 +01:00
|
|
|
sb.write_string("'")
|
|
|
|
sb.write_string(val)
|
|
|
|
sb.write_string("'")
|
2019-06-22 20:20:28 +02:00
|
|
|
if i < a.len - 1 {
|
2021-02-22 12:18:11 +01:00
|
|
|
sb.write_string(', ')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-22 12:18:11 +01:00
|
|
|
sb.write_string(']')
|
2019-08-31 01:35:05 +02:00
|
|
|
return sb.str()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// hex returns a string with the hexadecimal representation
|
|
|
|
// of the byte elements of the array.
|
2019-07-15 17:49:01 +02:00
|
|
|
pub fn (b []byte) hex() string {
|
2021-02-14 19:31:42 +01:00
|
|
|
mut hex := unsafe { malloc(b.len * 2 + 1) }
|
2020-03-11 00:38:11 +01:00
|
|
|
mut dst_i := 0
|
|
|
|
for i in b {
|
2020-03-18 16:47:37 +01:00
|
|
|
n0 := i >> 4
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
hex[dst_i++] = if n0 < 10 { n0 + `0` } else { n0 + byte(87) }
|
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
n1 := i & 0xF
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
hex[dst_i++] = if n1 < 10 { n1 + `0` } else { n1 + byte(87) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
hex[dst_i] = `\0`
|
2020-10-15 12:32:28 +02:00
|
|
|
return tos(hex, dst_i)
|
2020-03-11 00:38:11 +01:00
|
|
|
}
|
2019-07-15 17:49:01 +02:00
|
|
|
}
|
2019-07-28 17:19:59 +02:00
|
|
|
|
2019-10-31 19:50:20 +01:00
|
|
|
// copy copies the `src` byte array elements to the `dst` byte array.
|
|
|
|
// The number of the elements copied is the minimum of the length of both arrays.
|
|
|
|
// Returns the number of elements copied.
|
2019-07-28 17:19:59 +02:00
|
|
|
// TODO: implement for all types
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn copy(dst []byte, src []byte) int {
|
2021-03-02 17:14:42 +01:00
|
|
|
min := if dst.len < src.len { dst.len } else { src.len }
|
|
|
|
if min > 0 {
|
|
|
|
unsafe { C.memcpy(byteptr(dst.data), src.data, min) }
|
2019-07-28 17:19:59 +02:00
|
|
|
}
|
2021-03-02 17:14:42 +01:00
|
|
|
return min
|
2019-07-28 17:19:59 +02:00
|
|
|
}
|
2019-09-01 19:55:34 +02:00
|
|
|
|
2019-10-31 19:50:20 +01:00
|
|
|
// Private function. Comparator for int type.
|
2020-10-15 12:32:28 +02:00
|
|
|
fn compare_ints(a &int, b &int) int {
|
2019-11-18 22:27:27 +01:00
|
|
|
if *a < *b {
|
2019-09-01 19:55:34 +02:00
|
|
|
return -1
|
|
|
|
}
|
2019-11-18 22:27:27 +01:00
|
|
|
if *a > *b {
|
2019-09-01 19:55:34 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:32:28 +02:00
|
|
|
fn compare_ints_reverse(a &int, b &int) int {
|
2020-08-12 06:11:40 +02:00
|
|
|
if *a > *b {
|
2020-12-05 22:14:14 +01:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if *a < *b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_u64s(a &u64, b &u64) int {
|
|
|
|
if *a < *b {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if *a > *b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_u64s_reverse(a &u64, b &u64) int {
|
|
|
|
if *a > *b {
|
2020-08-12 06:11:40 +02:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if *a < *b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:32:28 +02:00
|
|
|
fn compare_floats(a &f64, b &f64) int {
|
2020-08-12 05:54:51 +02:00
|
|
|
if *a < *b {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if *a > *b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:32:28 +02:00
|
|
|
fn compare_floats_reverse(a &f64, b &f64) int {
|
2020-08-12 06:11:40 +02:00
|
|
|
if *a > *b {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if *a < *b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-02-15 16:54:07 +01:00
|
|
|
// sort sorts an array of int in place in ascending order.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut a []int) sort() {
|
2019-09-01 19:55:34 +02:00
|
|
|
a.sort_with_compare(compare_ints)
|
|
|
|
}
|
2019-10-04 22:07:19 +02:00
|
|
|
|
2020-12-15 09:31:35 +01:00
|
|
|
// index returns the first index at which a given element can be found in the array
|
|
|
|
// or -1 if the value is not found.
|
2019-10-04 22:07:19 +02:00
|
|
|
pub fn (a []string) index(v string) int {
|
2020-10-15 12:32:28 +02:00
|
|
|
for i in 0 .. a.len {
|
2019-10-04 22:07:19 +02:00
|
|
|
if a[i] == v {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// reduce executes a given reducer function on each element of the array,
|
2019-10-11 03:12:40 +02:00
|
|
|
// resulting in a single output value.
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn (a []int) reduce(iter fn (int, int) int, accum_start int) int {
|
2020-04-04 14:55:40 +02:00
|
|
|
mut accum_ := accum_start
|
2020-02-24 17:55:16 +01:00
|
|
|
for i in a {
|
2020-04-04 14:55:40 +02:00
|
|
|
accum_ = iter(accum_, i)
|
2019-10-11 03:12:40 +02:00
|
|
|
}
|
2020-04-04 14:55:40 +02:00
|
|
|
return accum_
|
2019-10-11 03:12:40 +02:00
|
|
|
}
|
2019-10-29 12:26:00 +01:00
|
|
|
|
2020-12-16 18:22:26 +01:00
|
|
|
// grow_cap grows the array's capacity by `amount` elements.
|
|
|
|
pub fn (mut a array) grow_cap(amount int) {
|
|
|
|
a.ensure_cap(a.cap + amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// grow_len ensures that an array has a.len + amount of length
|
2021-02-26 22:55:09 +01:00
|
|
|
[unsafe]
|
2020-12-16 18:22:26 +01:00
|
|
|
pub fn (mut a array) grow_len(amount int) {
|
2020-11-15 21:54:47 +01:00
|
|
|
a.ensure_cap(a.len + amount)
|
2020-12-16 18:22:26 +01:00
|
|
|
a.len += amount
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
|
2019-10-31 19:50:20 +01:00
|
|
|
// array_eq<T> checks if two arrays contain all the same elements in the same order.
|
2019-10-29 12:26:00 +01:00
|
|
|
// []int == []int (also for: i64, f32, f64, byte, string)
|
2020-02-18 18:13:34 +01:00
|
|
|
/*
|
2019-10-29 12:26:00 +01:00
|
|
|
fn array_eq<T>(a1, a2 []T) bool {
|
|
|
|
if a1.len != a2.len {
|
|
|
|
return false
|
|
|
|
}
|
2020-02-24 17:55:16 +01:00
|
|
|
for i in 0..a1.len {
|
2019-10-29 12:26:00 +01:00
|
|
|
if a1[i] != a2[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-10-31 11:08:01 +01:00
|
|
|
pub fn (a []int) eq(a2 []int) bool {
|
|
|
|
return array_eq(a, a2)
|
2019-10-29 12:26:00 +01:00
|
|
|
}
|
|
|
|
|
2019-10-31 11:08:01 +01:00
|
|
|
pub fn (a []i64) eq(a2 []i64) bool {
|
|
|
|
return array_eq(a, a2)
|
2019-10-29 12:26:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-31 11:08:01 +01:00
|
|
|
pub fn (a []byte) eq(a2 []byte) bool {
|
|
|
|
return array_eq(a, a2)
|
2019-10-29 12:26:00 +01:00
|
|
|
}
|
|
|
|
|
2019-10-31 11:08:01 +01:00
|
|
|
pub fn (a []f32) eq(a2 []f32) bool {
|
|
|
|
return array_eq(a, a2)
|
2019-10-29 12:26:00 +01:00
|
|
|
}
|
2020-02-18 18:13:34 +01:00
|
|
|
*/
|
2020-12-15 09:31:35 +01:00
|
|
|
// eq checks if the arrays have the same elements or not.
|
|
|
|
// TODO: make it work with all types.
|
2020-02-18 20:31:08 +01:00
|
|
|
pub fn (a1 []string) eq(a2 []string) bool {
|
2020-10-15 12:32:28 +02:00
|
|
|
// return array_eq(a, a2)
|
2020-02-18 20:31:08 +01:00
|
|
|
if a1.len != a2.len {
|
|
|
|
return false
|
|
|
|
}
|
2020-10-15 12:32:28 +02:00
|
|
|
for i in 0 .. a1.len {
|
2020-02-18 20:31:08 +01:00
|
|
|
if a1[i] != a2[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:04:47 +01:00
|
|
|
// compare_i64 for []i64 sort_with_compare()
|
2020-01-21 18:49:30 +01:00
|
|
|
// sort []i64 with quicksort
|
|
|
|
// usage :
|
|
|
|
// mut x := [i64(100),10,70,28,92]
|
|
|
|
// x.sort_with_compare(compare_i64)
|
|
|
|
// println(x) // Sorted i64 Array
|
|
|
|
// output:
|
|
|
|
// [10, 28, 70, 92, 100]
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn compare_i64(a &i64, b &i64) int {
|
2020-01-21 18:49:30 +01:00
|
|
|
if *a < *b {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if *a > *b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare_f64 for []f64 sort_with_compare()
|
|
|
|
// ref. compare_i64(...)
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn compare_f64(a &f64, b &f64) int {
|
2020-01-21 18:49:30 +01:00
|
|
|
if *a < *b {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if *a > *b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare_f32 for []f32 sort_with_compare()
|
|
|
|
// ref. compare_i64(...)
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn compare_f32(a &f32, b &f32) int {
|
2020-01-21 18:49:30 +01:00
|
|
|
if *a < *b {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if *a > *b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2020-03-04 20:28:42 +01:00
|
|
|
|
2020-11-30 18:51:00 +01:00
|
|
|
// pointers returns a new array, where each element
|
|
|
|
// is the address of the corresponding element in the array.
|
2021-02-26 22:55:09 +01:00
|
|
|
[unsafe]
|
2020-03-04 20:28:42 +01:00
|
|
|
pub fn (a array) pointers() []voidptr {
|
2020-04-26 09:17:13 +02:00
|
|
|
mut res := []voidptr{}
|
2020-10-15 12:32:28 +02:00
|
|
|
for i in 0 .. a.len {
|
2020-12-23 19:13:42 +01:00
|
|
|
unsafe { res << a.get_unsafe(i) }
|
2020-03-04 20:28:42 +01:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2020-11-27 17:18:46 +01:00
|
|
|
|
|
|
|
// voidptr.vbytes() - makes a V []byte structure from a C style memory buffer. NB: the data is reused, NOT copied!
|
|
|
|
[unsafe]
|
|
|
|
pub fn (data voidptr) vbytes(len int) []byte {
|
|
|
|
res := array{
|
|
|
|
element_size: 1
|
|
|
|
data: data
|
|
|
|
len: len
|
|
|
|
cap: len
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// byteptr.vbytes() - makes a V []byte structure from a C style memory buffer. NB: the data is reused, NOT copied!
|
|
|
|
[unsafe]
|
|
|
|
pub fn (data byteptr) vbytes(len int) []byte {
|
2020-12-23 19:13:42 +01:00
|
|
|
return unsafe { voidptr(data).vbytes(len) }
|
2020-11-27 17:18:46 +01:00
|
|
|
}
|