1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

run vfmt on array.v

This commit is contained in:
Alexander Medvednikov
2019-12-18 20:07:32 +03:00
parent 9e11de4a8c
commit 1cef83aea4
5 changed files with 46 additions and 43 deletions

View File

@ -1,15 +1,14 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module builtin
import strings
struct array {
pub:
// Using a void pointer allows to implement arrays without generics and without generating
// extra code for every type.
// Using a void pointer allows to implement arrays without generics and without generating
// extra code for every type.
data voidptr
len int
cap int
@ -18,8 +17,8 @@ pub:
// Private function, used by V (`nums := []int`)
fn new_array(mylen, cap, elm_size int) array {
cap_ := if cap == 0 { 1 } else { cap }
arr := array {
cap_ := if cap == 0 {1}else {cap}
arr := array{
len: mylen
cap: cap
element_size: elm_size
@ -28,17 +27,15 @@ fn new_array(mylen, cap, elm_size int) array {
return arr
}
// TODO
pub fn make(len, cap, elm_size int) array {
return new_array(len, cap, elm_size)
}
// Private function, used by V (`nums := [1, 2, 3]`)
fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
cap_ := if cap == 0 { 1 } else { cap }
arr := array {
cap_ := if cap == 0 {1}else {cap}
arr := array{
len: len
cap: cap
element_size: elm_size
@ -51,7 +48,7 @@ fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
// 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 {
arr := array {
arr := array{
len: len
cap: cap
element_size: elm_size
@ -63,8 +60,10 @@ fn new_array_from_c_array_no_alloc(len, cap, elm_size int, c_array voidptr) arra
// Private function. Doubles array capacity if needed
fn (a mut array) ensure_cap(required int) {
if required > a.cap {
mut cap := if a.cap == 0 { 2 } else { a.cap * 2 }
for required > cap { cap *= 2 }
mut cap := if a.cap == 0 {2}else {a.cap * 2}
for required > cap {
cap *= 2
}
if a.cap == 0 {
a.data = calloc(cap * a.element_size)
}
@ -80,7 +79,7 @@ fn array_repeat_old(val voidptr, nr_repeats, elm_size int) array {
if nr_repeats < 0 {
panic('[0; len]: `len` is negative (len == $nr_repeats)')
}
arr := array {
arr := array{
len: nr_repeats
cap: nr_repeats
element_size: elm_size
@ -102,7 +101,7 @@ pub fn (a array) repeat(nr_repeats int) array {
if size == 0 {
size = a.element_size
}
arr := array {
arr := array{
len: nr_repeats * a.len
cap: nr_repeats * a.len
element_size: a.element_size
@ -122,8 +121,8 @@ pub fn (a mut array) sort_with_compare(compare voidptr) {
// TODO array.insert is broken
// Cannot pass literal or primitive type as it cannot be cast to voidptr.
// In the current state only that would work:
// i := 3
// a.insert(0, &i)
// i := 3
// a.insert(0, &i)
// ----------------------------
pub fn (a mut array) insert(i int, val voidptr) {
if i < 0 || i > a.len {
@ -205,7 +204,7 @@ fn (a array) right(n int) array {
// used internally for [2..4]
fn (a array) slice2(start, _end int, end_max bool) array {
end := if end_max { a.len } else { _end }
end := if end_max {a.len}else {_end}
return a.slice(start, end)
}
@ -225,7 +224,7 @@ fn (a array) slice(start, _end int) array {
panic('array.slice: slice bounds out of range ($start < 0)')
}
l := end - start
res := array {
res := array{
element_size: a.element_size
data: a.data + start * a.element_size
len: l
@ -246,7 +245,7 @@ fn (a array) slice_clone(start, _end int) array {
panic('array.slice: slice bounds out of range ($start < 0)')
}
l := end - start
res := array {
res := array{
element_size: a.element_size
data: a.data + start * a.element_size
len: l
@ -280,14 +279,14 @@ pub fn (a mut array) push_many(val voidptr, size int) {
// array.reverse returns a new array with the elements of
// the original array in reverse order.
pub fn (a array) reverse() array {
arr := array {
arr := array{
len: a.len
cap: a.cap
element_size: a.element_size
data: calloc(a.cap * a.element_size)
}
for i := 0; i < a.len; i++ {
C.memcpy(arr.data + i * arr.element_size, &a[a.len-1-i], arr.element_size)
C.memcpy(arr.data + i * arr.element_size, &a[a.len - 1 - i], arr.element_size)
}
return arr
}
@ -298,7 +297,7 @@ pub fn (a array) clone() array {
if size == 0 {
size++
}
arr := array {
arr := array{
len: a.len
cap: a.cap
element_size: a.element_size
@ -308,12 +307,12 @@ pub fn (a array) clone() array {
return arr
}
//pub fn (a []int) free() {
// pub fn (a []int) free() {
[unsafe_fn]
pub fn (a array) free() {
//if a.is_slice {
//return
//}
// if a.is_slice {
// return
// }
C.free(a.data)
}
@ -344,7 +343,8 @@ pub fn (a []bool) str() string {
val := a[i]
if val {
sb.write('true')
} else {
}
else {
sb.write('false')
}
if i < a.len - 1 {
@ -358,9 +358,9 @@ pub fn (a []bool) str() string {
// []byte.hex returns a string with the hexadecimal representation
// of the byte elements of the array
pub fn (b []byte) hex() string {
mut hex := malloc(b.len*2+1)
mut hex := malloc(b.len * 2 + 1)
mut ptr := &hex[0]
for i := 0; i < b.len ; i++ {
for i := 0; i < b.len; i++ {
ptr += C.sprintf(charptr(ptr), '%02x', b[i])
}
return string(hex)
@ -372,8 +372,8 @@ pub fn (b []byte) hex() string {
// TODO: implement for all types
pub fn copy(dst, src []byte) int {
if dst.len > 0 && src.len > 0 {
min := if dst.len < src.len { dst.len } else { src.len }
C.memcpy(dst.data, src.left(min).data, dst.element_size*min)
min := if dst.len < src.len {dst.len}else {src.len}
C.memcpy(dst.data, src.left(min).data, dst.element_size * min)
return min
}
return 0
@ -442,11 +442,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(accum, curr int)int, accum_start int) int {
mut _accum := 0
_accum = accum_start
for i := 0; i < a.len; i++ {
_accum = iter(_accum, a[i])
_accum = iter(_accum, a[i])
}
return _accum
}