|
|
|
@ -9,8 +9,8 @@ pub struct array {
|
|
|
|
|
pub:
|
|
|
|
|
element_size int
|
|
|
|
|
pub mut:
|
|
|
|
|
data voidptr// Using a void pointer allows to implement arrays without generics and without generating
|
|
|
|
|
// extra code for every type.
|
|
|
|
|
data voidptr // Using a void pointer allows to implement arrays without generics and without generating
|
|
|
|
|
// extra code for every type.
|
|
|
|
|
len int
|
|
|
|
|
cap int
|
|
|
|
|
}
|
|
|
|
@ -36,10 +36,8 @@ fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array
|
|
|
|
|
cap: cap_
|
|
|
|
|
}
|
|
|
|
|
if val != 0 {
|
|
|
|
|
for i in 0..arr.len {
|
|
|
|
|
unsafe {
|
|
|
|
|
arr.set_unsafe(i, val)
|
|
|
|
|
}
|
|
|
|
|
for i in 0 .. arr.len {
|
|
|
|
|
unsafe {arr.set_unsafe(i, val)}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return arr
|
|
|
|
@ -53,19 +51,16 @@ fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array) a
|
|
|
|
|
len: mylen
|
|
|
|
|
cap: cap_
|
|
|
|
|
}
|
|
|
|
|
for i in 0..arr.len {
|
|
|
|
|
for i in 0 .. arr.len {
|
|
|
|
|
val_clone := val.clone()
|
|
|
|
|
unsafe {
|
|
|
|
|
arr.set_unsafe(i, &val_clone)
|
|
|
|
|
}
|
|
|
|
|
unsafe {arr.set_unsafe(i, &val_clone)}
|
|
|
|
|
}
|
|
|
|
|
return arr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Private function, used by V (`nums := [1, 2, 3]`)
|
|
|
|
|
fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
|
|
|
|
|
fn new_array_from_c_array(len int, cap int, elm_size int, c_array voidptr) array {
|
|
|
|
|
cap_ := if cap < len { len } else { cap }
|
|
|
|
|
|
|
|
|
|
arr := array{
|
|
|
|
|
element_size: elm_size
|
|
|
|
|
data: vcalloc(cap_ * elm_size)
|
|
|
|
@ -73,14 +68,12 @@ fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
|
|
|
|
|
cap: cap_
|
|
|
|
|
}
|
|
|
|
|
// TODO Write all memory functions (like memcpy) in V
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memcpy(arr.data, c_array, len * elm_size)
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memcpy(arr.data, c_array, len * elm_size)}
|
|
|
|
|
return arr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Private function, used by V (`nums := [1, 2, 3] !`)
|
|
|
|
|
fn new_array_from_c_array_no_alloc(len, cap, elm_size int, c_array voidptr) array {
|
|
|
|
|
fn new_array_from_c_array_no_alloc(len int, cap int, elm_size int, c_array voidptr) array {
|
|
|
|
|
arr := array{
|
|
|
|
|
element_size: elm_size
|
|
|
|
|
data: c_array
|
|
|
|
@ -102,8 +95,7 @@ fn (mut a array) ensure_cap(required int) {
|
|
|
|
|
}
|
|
|
|
|
if a.cap == 0 {
|
|
|
|
|
a.data = vcalloc(cap * a.element_size)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
} else {
|
|
|
|
|
a.data = v_realloc(a.data, u32(cap * a.element_size))
|
|
|
|
|
}
|
|
|
|
|
a.cap = cap
|
|
|
|
@ -124,20 +116,14 @@ pub fn (a array) repeat(count int) array {
|
|
|
|
|
len: count * a.len
|
|
|
|
|
cap: count * a.len
|
|
|
|
|
}
|
|
|
|
|
for i in 0..count {
|
|
|
|
|
for i in 0 .. count {
|
|
|
|
|
if a.len > 0 && a.element_size == sizeof(array) {
|
|
|
|
|
ary := array{}
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memcpy(&ary, a.data, sizeof(array))
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memcpy(&ary, a.data, sizeof(array))}
|
|
|
|
|
ary_clone := ary.clone()
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memcpy(arr.get_unsafe(i * a.len), &ary_clone, a.len * a.element_size)
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memcpy(arr.get_unsafe(i * a.len), &ary_clone, a.len * a.element_size)}
|
|
|
|
|
} else {
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memcpy(arr.get_unsafe(i * a.len), byteptr(a.data), a.len * a.element_size)
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memcpy(arr.get_unsafe(i * a.len), byteptr(a.data), a.len * a.element_size)}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return arr
|
|
|
|
@ -150,7 +136,7 @@ pub fn (mut a array) sort_with_compare(compare voidptr) {
|
|
|
|
|
|
|
|
|
|
// array.insert
|
|
|
|
|
pub fn (mut a array) insert(i int, val voidptr) {
|
|
|
|
|
$if !no_bounds_checking? {
|
|
|
|
|
$if !no_bounds_checking ? {
|
|
|
|
|
if i < 0 || i > a.len {
|
|
|
|
|
panic('array.insert: index out of range (i == $i, a.len == $a.len)')
|
|
|
|
|
}
|
|
|
|
@ -165,7 +151,7 @@ pub fn (mut a array) insert(i int, val voidptr) {
|
|
|
|
|
|
|
|
|
|
// array.insert_many
|
|
|
|
|
pub fn (mut a array) insert_many(i int, val voidptr, size int) {
|
|
|
|
|
$if !no_bounds_checking? {
|
|
|
|
|
$if !no_bounds_checking ? {
|
|
|
|
|
if i < 0 || i > a.len {
|
|
|
|
|
panic('array.insert_many: index out of range (i == $i, a.len == $a.len)')
|
|
|
|
|
}
|
|
|
|
@ -192,16 +178,14 @@ pub fn (mut a array) prepend_many(val voidptr, size int) {
|
|
|
|
|
|
|
|
|
|
// array.delete deletes array element at the given index
|
|
|
|
|
pub fn (mut a array) delete(i int) {
|
|
|
|
|
$if !no_bounds_checking? {
|
|
|
|
|
$if !no_bounds_checking ? {
|
|
|
|
|
if i < 0 || i >= a.len {
|
|
|
|
|
panic('array.delete: index out of range (i == $i, a.len == $a.len)')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// NB: if a is [12,34], a.len = 2, a.delete(0)
|
|
|
|
|
// should move (2-0-1) elements = 1 element (the 34) forward
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memmove(a.get_unsafe(i), a.get_unsafe(i + 1), (a.len - i - 1) * a.element_size)
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memmove(a.get_unsafe(i), a.get_unsafe(i + 1), (a.len - i - 1) * a.element_size)}
|
|
|
|
|
a.len--
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -219,7 +203,8 @@ pub fn (mut a array) trim(index int) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we manually inline this for single operations for performance without -prod
|
|
|
|
|
[inline] [unsafe]
|
|
|
|
|
[inline]
|
|
|
|
|
[unsafe]
|
|
|
|
|
fn (a array) get_unsafe(i int) voidptr {
|
|
|
|
|
unsafe {
|
|
|
|
|
return byteptr(a.data) + i * a.element_size
|
|
|
|
@ -228,7 +213,7 @@ fn (a array) get_unsafe(i int) voidptr {
|
|
|
|
|
|
|
|
|
|
// Private function. Used to implement array[] operator
|
|
|
|
|
fn (a array) get(i int) voidptr {
|
|
|
|
|
$if !no_bounds_checking? {
|
|
|
|
|
$if !no_bounds_checking ? {
|
|
|
|
|
if i < 0 || i >= a.len {
|
|
|
|
|
panic('array.get: index out of range (i == $i, a.len == $a.len)')
|
|
|
|
|
}
|
|
|
|
@ -240,7 +225,7 @@ fn (a array) get(i int) voidptr {
|
|
|
|
|
|
|
|
|
|
// array.first returns the first element of the array
|
|
|
|
|
pub fn (a array) first() voidptr {
|
|
|
|
|
$if !no_bounds_checking? {
|
|
|
|
|
$if !no_bounds_checking ? {
|
|
|
|
|
if a.len == 0 {
|
|
|
|
|
panic('array.first: array is empty')
|
|
|
|
|
}
|
|
|
|
@ -250,7 +235,7 @@ pub fn (a array) first() voidptr {
|
|
|
|
|
|
|
|
|
|
// array.last returns the last element of the array
|
|
|
|
|
pub fn (a array) last() voidptr {
|
|
|
|
|
$if !no_bounds_checking? {
|
|
|
|
|
$if !no_bounds_checking ? {
|
|
|
|
|
if a.len == 0 {
|
|
|
|
|
panic('array.last: array is empty')
|
|
|
|
|
}
|
|
|
|
@ -263,13 +248,13 @@ pub fn (a array) last() voidptr {
|
|
|
|
|
// array.pop returns the last element of the array, and removes it
|
|
|
|
|
pub fn (mut a array) pop() voidptr {
|
|
|
|
|
// in a sense, this is the opposite of `a << x`
|
|
|
|
|
$if !no_bounds_checking? {
|
|
|
|
|
$if !no_bounds_checking ? {
|
|
|
|
|
if a.len == 0 {
|
|
|
|
|
panic('array.pop: array is empty')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
new_len := a.len - 1
|
|
|
|
|
last_elem := unsafe { byteptr(a.data) + (new_len) * a.element_size }
|
|
|
|
|
last_elem := unsafe {byteptr(a.data) + (new_len) * a.element_size}
|
|
|
|
|
a.len = new_len
|
|
|
|
|
// NB: a.cap is not changed here *on purpose*, so that
|
|
|
|
|
// further << ops on that array will be more efficient.
|
|
|
|
@ -280,9 +265,9 @@ pub fn (mut a array) pop() voidptr {
|
|
|
|
|
// but starting from the `start` element and ending with the element before
|
|
|
|
|
// the `end` element of the original array with the length and capacity
|
|
|
|
|
// set to the number of the elements in the slice.
|
|
|
|
|
fn (a array) slice(start, _end int) array {
|
|
|
|
|
fn (a array) slice(start int, _end int) array {
|
|
|
|
|
mut end := _end
|
|
|
|
|
$if !no_bounds_checking? {
|
|
|
|
|
$if !no_bounds_checking ? {
|
|
|
|
|
if start > end {
|
|
|
|
|
panic('array.slice: invalid slice index ($start > $end)')
|
|
|
|
|
}
|
|
|
|
@ -308,7 +293,7 @@ fn (a array) slice(start, _end int) array {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// used internally for [2..4]
|
|
|
|
|
fn (a array) slice2(start, _end int, end_max bool) array {
|
|
|
|
|
fn (a array) slice2(start int, _end int, end_max bool) array {
|
|
|
|
|
end := if end_max { a.len } else { _end }
|
|
|
|
|
return a.slice(start, end)
|
|
|
|
|
}
|
|
|
|
@ -333,27 +318,21 @@ pub fn (a &array) clone() array {
|
|
|
|
|
}
|
|
|
|
|
// Recursively clone-generated elements if array element is array type
|
|
|
|
|
if a.element_size == sizeof(array) {
|
|
|
|
|
for i in 0..a.len {
|
|
|
|
|
for i in 0 .. a.len {
|
|
|
|
|
ar := array{}
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memcpy(&ar, a.get_unsafe(i), sizeof(array))
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memcpy(&ar, a.get_unsafe(i), sizeof(array))}
|
|
|
|
|
ar_clone := ar.clone()
|
|
|
|
|
unsafe {
|
|
|
|
|
arr.set_unsafe(i, &ar_clone)
|
|
|
|
|
}
|
|
|
|
|
unsafe {arr.set_unsafe(i, &ar_clone)}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memcpy(byteptr(arr.data), a.data, a.cap * a.element_size)
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memcpy(byteptr(arr.data), a.data, a.cap * a.element_size)}
|
|
|
|
|
}
|
|
|
|
|
return arr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (a &array) slice_clone(start, _end int) array {
|
|
|
|
|
fn (a &array) slice_clone(start int, _end int) array {
|
|
|
|
|
mut end := _end
|
|
|
|
|
$if !no_bounds_checking? {
|
|
|
|
|
$if !no_bounds_checking ? {
|
|
|
|
|
if start > end {
|
|
|
|
|
panic('array.slice: invalid slice index ($start > $end)')
|
|
|
|
|
}
|
|
|
|
@ -379,30 +358,25 @@ fn (a &array) slice_clone(start, _end int) array {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we manually inline this for single operations for performance without -prod
|
|
|
|
|
[inline] [unsafe]
|
|
|
|
|
[inline]
|
|
|
|
|
[unsafe]
|
|
|
|
|
fn (mut a array) set_unsafe(i int, val voidptr) {
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size)
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Private function. Used to implement assigment to the array element.
|
|
|
|
|
fn (mut a array) set(i int, val voidptr) {
|
|
|
|
|
$if !no_bounds_checking? {
|
|
|
|
|
$if !no_bounds_checking ? {
|
|
|
|
|
if i < 0 || i >= a.len {
|
|
|
|
|
panic('array.set: index out of range (i == $i, a.len == $a.len)')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size)
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (mut a array) push(val voidptr) {
|
|
|
|
|
a.ensure_cap(a.len + 1)
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memcpy(byteptr(a.data) + a.element_size * a.len, val, a.element_size)
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memcpy(byteptr(a.data) + a.element_size * a.len, val, a.element_size)}
|
|
|
|
|
a.len++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -414,14 +388,12 @@ pub fn (mut a3 array) push_many(val voidptr, size int) {
|
|
|
|
|
copy := a3.clone()
|
|
|
|
|
a3.ensure_cap(a3.len + size)
|
|
|
|
|
unsafe {
|
|
|
|
|
//C.memcpy(a.data, copy.data, copy.element_size * copy.len)
|
|
|
|
|
// C.memcpy(a.data, copy.data, copy.element_size * copy.len)
|
|
|
|
|
C.memcpy(a3.get_unsafe(a3.len), copy.data, a3.element_size * size)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
a3.ensure_cap(a3.len + size)
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memcpy(a3.get_unsafe(a3.len), val, a3.element_size * size)
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memcpy(a3.get_unsafe(a3.len), val, a3.element_size * size)}
|
|
|
|
|
}
|
|
|
|
|
a3.len += size
|
|
|
|
|
}
|
|
|
|
@ -432,10 +404,11 @@ pub fn (mut a array) reverse_in_place() {
|
|
|
|
|
}
|
|
|
|
|
unsafe {
|
|
|
|
|
mut tmp_value := malloc(a.element_size)
|
|
|
|
|
for i in 0..a.len/2 {
|
|
|
|
|
for i in 0 .. a.len / 2 {
|
|
|
|
|
C.memcpy(tmp_value, byteptr(a.data) + i * a.element_size, a.element_size)
|
|
|
|
|
C.memcpy(byteptr(a.data) + i * a.element_size, byteptr(a.data) + (a.len-1-i) * a.element_size, a.element_size)
|
|
|
|
|
C.memcpy(byteptr(a.data) + (a.len-1-i) * a.element_size, tmp_value, a.element_size)
|
|
|
|
|
C.memcpy(byteptr(a.data) + i * a.element_size, byteptr(a.data) + (a.len - 1 - i) *
|
|
|
|
|
a.element_size, a.element_size)
|
|
|
|
|
C.memcpy(byteptr(a.data) + (a.len - 1 - i) * a.element_size, tmp_value, a.element_size)
|
|
|
|
|
}
|
|
|
|
|
free(tmp_value)
|
|
|
|
|
}
|
|
|
|
@ -453,10 +426,8 @@ pub fn (a array) reverse() array {
|
|
|
|
|
len: a.len
|
|
|
|
|
cap: a.cap
|
|
|
|
|
}
|
|
|
|
|
for i in 0..a.len {
|
|
|
|
|
unsafe {
|
|
|
|
|
arr.set_unsafe(i, a.get_unsafe(a.len - 1 - i))
|
|
|
|
|
}
|
|
|
|
|
for i in 0 .. a.len {
|
|
|
|
|
unsafe {arr.set_unsafe(i, a.get_unsafe(a.len - 1 - i))}
|
|
|
|
|
}
|
|
|
|
|
return arr
|
|
|
|
|
}
|
|
|
|
@ -478,7 +449,7 @@ pub fn (a &array) free() {
|
|
|
|
|
pub fn (a []string) str() string {
|
|
|
|
|
mut sb := strings.new_builder(a.len * 3)
|
|
|
|
|
sb.write('[')
|
|
|
|
|
for i in 0..a.len {
|
|
|
|
|
for i in 0 .. a.len {
|
|
|
|
|
val := a[i]
|
|
|
|
|
sb.write("\'")
|
|
|
|
|
sb.write(val)
|
|
|
|
@ -508,7 +479,7 @@ pub fn (b []byte) hex() string {
|
|
|
|
|
}
|
|
|
|
|
unsafe {
|
|
|
|
|
hex[dst_i] = `\0`
|
|
|
|
|
return tos(hex,dst_i)
|
|
|
|
|
return tos(hex, dst_i)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -516,20 +487,18 @@ pub fn (b []byte) hex() string {
|
|
|
|
|
// The number of the elements copied is the minimum of the length of both arrays.
|
|
|
|
|
// Returns the number of elements copied.
|
|
|
|
|
// TODO: implement for all types
|
|
|
|
|
pub fn copy(dst, src []byte) int {
|
|
|
|
|
pub fn copy(dst []byte, src []byte) int {
|
|
|
|
|
if dst.len > 0 && src.len > 0 {
|
|
|
|
|
mut min := 0
|
|
|
|
|
min = if dst.len < src.len { dst.len } else { src.len }
|
|
|
|
|
unsafe {
|
|
|
|
|
C.memcpy(byteptr(dst.data), src[..min].data, dst.element_size * min)
|
|
|
|
|
}
|
|
|
|
|
unsafe {C.memcpy(byteptr(dst.data), src[..min].data, dst.element_size * min)}
|
|
|
|
|
return min
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Private function. Comparator for int type.
|
|
|
|
|
fn compare_ints(a, b &int) int {
|
|
|
|
|
fn compare_ints(a &int, b &int) int {
|
|
|
|
|
if *a < *b {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
@ -539,7 +508,7 @@ fn compare_ints(a, b &int) int {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn compare_ints_reverse(a, b &int) int {
|
|
|
|
|
fn compare_ints_reverse(a &int, b &int) int {
|
|
|
|
|
if *a > *b {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
@ -549,7 +518,7 @@ fn compare_ints_reverse(a, b &int) int {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn compare_floats(a, b &f64) int {
|
|
|
|
|
fn compare_floats(a &f64, b &f64) int {
|
|
|
|
|
if *a < *b {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
@ -559,7 +528,7 @@ fn compare_floats(a, b &f64) int {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn compare_floats_reverse(a, b &f64) int {
|
|
|
|
|
fn compare_floats_reverse(a &f64, b &f64) int {
|
|
|
|
|
if *a > *b {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
@ -577,7 +546,7 @@ pub fn (mut a []int) sort() {
|
|
|
|
|
// []string.index returns the index of the first element equal to the given value,
|
|
|
|
|
// or -1 if the value is not found in the array.
|
|
|
|
|
pub fn (a []string) index(v string) int {
|
|
|
|
|
for i in 0..a.len {
|
|
|
|
|
for i in 0 .. a.len {
|
|
|
|
|
if a[i] == v {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
@ -588,7 +557,7 @@ pub fn (a []string) index(v string) int {
|
|
|
|
|
// []int.index returns the index of the first element equal to the given value,
|
|
|
|
|
// or -1 if the value is not found in the array.
|
|
|
|
|
pub fn (a []int) index(v int) int {
|
|
|
|
|
for i in 0..a.len {
|
|
|
|
|
for i in 0 .. a.len {
|
|
|
|
|
if a[i] == v {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
@ -599,7 +568,7 @@ pub fn (a []int) index(v int) int {
|
|
|
|
|
// []byte.index returns the index of the first element equal to the given value,
|
|
|
|
|
// or -1 if the value is not found in the array.
|
|
|
|
|
pub fn (a []byte) index(v byte) int {
|
|
|
|
|
for i in 0..a.len {
|
|
|
|
|
for i in 0 .. a.len {
|
|
|
|
|
if a[i] == v {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
@ -608,7 +577,7 @@ pub fn (a []byte) index(v byte) int {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn (a []rune) index(v rune) int {
|
|
|
|
|
for i in 0..a.len {
|
|
|
|
|
for i in 0 .. a.len {
|
|
|
|
|
if a[i] == v {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
@ -620,7 +589,7 @@ pub fn (a []rune) index(v rune) int {
|
|
|
|
|
// or -1 if the value is not found in the array.
|
|
|
|
|
// TODO is `char` type yet in the language?
|
|
|
|
|
pub fn (a []char) index(v char) int {
|
|
|
|
|
for i in 0..a.len {
|
|
|
|
|
for i in 0 .. a.len {
|
|
|
|
|
if a[i] == v {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
@ -630,12 +599,11 @@ pub fn (a []char) index(v char) int {
|
|
|
|
|
|
|
|
|
|
// []int.reduce executes a given reducer function on each element of the array,
|
|
|
|
|
// resulting in a single output value.
|
|
|
|
|
pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int {
|
|
|
|
|
pub fn (a []int) reduce(iter fn (int, int) int, accum_start int) int {
|
|
|
|
|
mut accum_ := accum_start
|
|
|
|
|
for i in a {
|
|
|
|
|
accum_ = iter(accum_, i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return accum_
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -671,13 +639,12 @@ pub fn (a []f32) eq(a2 []f32) bool {
|
|
|
|
|
return array_eq(a, a2)
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
pub fn (a1 []string) eq(a2 []string) bool {
|
|
|
|
|
//return array_eq(a, a2)
|
|
|
|
|
// return array_eq(a, a2)
|
|
|
|
|
if a1.len != a2.len {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for i in 0..a1.len {
|
|
|
|
|
for i in 0 .. a1.len {
|
|
|
|
|
if a1[i] != a2[i] {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
@ -693,7 +660,7 @@ pub fn (a1 []string) eq(a2 []string) bool {
|
|
|
|
|
// println(x) // Sorted i64 Array
|
|
|
|
|
// output:
|
|
|
|
|
// [10, 28, 70, 92, 100]
|
|
|
|
|
pub fn compare_i64(a, b &i64) int {
|
|
|
|
|
pub fn compare_i64(a &i64, b &i64) int {
|
|
|
|
|
if *a < *b {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
@ -705,7 +672,7 @@ pub fn compare_i64(a, b &i64) int {
|
|
|
|
|
|
|
|
|
|
// compare_f64 for []f64 sort_with_compare()
|
|
|
|
|
// ref. compare_i64(...)
|
|
|
|
|
pub fn compare_f64(a, b &f64) int {
|
|
|
|
|
pub fn compare_f64(a &f64, b &f64) int {
|
|
|
|
|
if *a < *b {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
@ -717,7 +684,7 @@ pub fn compare_f64(a, b &f64) int {
|
|
|
|
|
|
|
|
|
|
// compare_f32 for []f32 sort_with_compare()
|
|
|
|
|
// ref. compare_i64(...)
|
|
|
|
|
pub fn compare_f32(a, b &f32) int {
|
|
|
|
|
pub fn compare_f32(a &f32, b &f32) int {
|
|
|
|
|
if *a < *b {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
@ -731,10 +698,8 @@ pub fn compare_f32(a, b &f32) int {
|
|
|
|
|
// is the address of the corresponding element in a.
|
|
|
|
|
pub fn (a array) pointers() []voidptr {
|
|
|
|
|
mut res := []voidptr{}
|
|
|
|
|
for i in 0..a.len {
|
|
|
|
|
unsafe {
|
|
|
|
|
res << a.get_unsafe(i)
|
|
|
|
|
}
|
|
|
|
|
for i in 0 .. a.len {
|
|
|
|
|
unsafe {res << a.get_unsafe(i)}
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|