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

arrays: use for/in instead of unsafe [direct_array_access] (#8857)

This commit is contained in:
Nick Treleaven 2021-02-20 13:27:36 +00:00 committed by GitHub
parent 38d1eac7f5
commit 2be852e461
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 41 deletions

View File

@ -6,46 +6,43 @@ module arrays
// - merge - combine two sorted arrays and maintain sorted order // - merge - combine two sorted arrays and maintain sorted order
// min returns the minimum // min returns the minimum
[direct_array_access]
pub fn min<T>(a []T) T { pub fn min<T>(a []T) T {
if a.len == 0 { if a.len == 0 {
panic('.min called on an empty array') panic('.min called on an empty array')
} }
mut val := a[0] mut val := a[0]
for i in 0 .. a.len { for e in a {
if a[i] < val { if e < val {
val = a[i] val = e
} }
} }
return val return val
} }
// max returns the maximum // max returns the maximum
[direct_array_access]
pub fn max<T>(a []T) T { pub fn max<T>(a []T) T {
if a.len == 0 { if a.len == 0 {
panic('.max called on an empty array') panic('.max called on an empty array')
} }
mut val := a[0] mut val := a[0]
for i in 0 .. a.len { for e in a {
if a[i] > val { if e > val {
val = a[i] val = e
} }
} }
return val return val
} }
// idx_min returns the index of the first minimum // idx_min returns the index of the first minimum
[direct_array_access]
pub fn idx_min<T>(a []T) int { pub fn idx_min<T>(a []T) int {
if a.len == 0 { if a.len == 0 {
panic('.idxmin called on an empty array') panic('.idx_min called on an empty array')
} }
mut idx := 0 mut idx := 0
mut val := a[0] mut val := a[0]
for i in 0 .. a.len { for i, e in a {
if a[i] < val { if e < val {
val = a[i] val = e
idx = i idx = i
} }
} }
@ -53,16 +50,15 @@ pub fn idx_min<T>(a []T) int {
} }
// idx_max returns the index of the first maximum // idx_max returns the index of the first maximum
[direct_array_access]
pub fn idx_max<T>(a []T) int { pub fn idx_max<T>(a []T) int {
if a.len == 0 { if a.len == 0 {
panic('.idxmax called on an empty array') panic('.idx_max called on an empty array')
} }
mut idx := 0 mut idx := 0
mut val := a[0] mut val := a[0]
for i in 0 .. a.len { for i, e in a {
if a[i] > val { if e > val {
val = a[i] val = e
idx = i idx = i
} }
} }

View File

@ -1,7 +1,5 @@
module arrays module arrays
import rand
fn test_min() { fn test_min() {
a := [8, 2, 6, 4] a := [8, 2, 6, 4]
assert min<int>(a) == 2 assert min<int>(a) == 2
@ -54,24 +52,3 @@ fn test_merge() {
assert merge<int>(a, c) == a assert merge<int>(a, c) == a
assert merge<int>(d, b) == b assert merge<int>(d, b) == b
} }
fn test_fixed_array_assignment() {
mut a := [2]int{}
a[0] = 111
a[1] = 222
b := a
assert b[0] == a[0]
assert b[1] == a[1]
mut c := [2]int{}
c = a
assert c[0] == a[0]
assert c[1] == a[1]
d := [3]int{init: 333}
for val in d {
assert val == 333
}
e := [3]string{init: 'vlang'}
for val in e {
assert val == 'vlang'
}
}

View File

@ -18,6 +18,27 @@ fn test_fixed_array_can_be_assigned() {
assert v[1] == 3.0 assert v[1] == 3.0
} }
fn test_fixed_array_assignment() {
mut a := [2]int{}
a[0] = 111
a[1] = 222
b := a
assert b[0] == a[0]
assert b[1] == a[1]
mut c := [2]int{}
c = a
assert c[0] == a[0]
assert c[1] == a[1]
d := [3]int{init: 333}
for val in d {
assert val == 333
}
e := [3]string{init: 'vlang'}
for val in e {
assert val == 'vlang'
}
}
fn test_fixed_array_can_be_used_in_declaration() { fn test_fixed_array_can_be_used_in_declaration() {
x := 2.32 x := 2.32
v := [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]! v := [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]!
@ -99,3 +120,4 @@ fn test_for_in_fixed_array() {
arr := [1,2,3]! arr := [1,2,3]!
calc_size(arr) calc_size(arr)
} }