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

v test-fmt: reformat some skipped files, comment on the remaining ones

This commit is contained in:
Delyan Angelov
2020-10-15 00:39:09 +03:00
parent e36f11750b
commit 3795aaab5c
19 changed files with 262 additions and 347 deletions

View File

@@ -5,15 +5,16 @@ module arrays
// - idx_min / idx_max - return the index of the first minumum / maximum
// - shuffle - randomize array items order in place (allowing exit after n items)
// - merge - combine two sorted arrays and maintain sorted order
import rand
// min returns the minimum
[direct_array_access]
pub fn min<T>(a []T) T {
if a.len==0 { panic('.min called on an empty array') }
if a.len == 0 {
panic('.min called on an empty array')
}
mut val := a[0]
for i in 0..a.len {
for i in 0 .. a.len {
if a[i] < val {
val = a[i]
}
@@ -24,9 +25,11 @@ pub fn min<T>(a []T) T {
// max returns the maximum
[direct_array_access]
pub fn max<T>(a []T) T {
if a.len==0 { panic('.max called on an empty array') }
if a.len == 0 {
panic('.max called on an empty array')
}
mut val := a[0]
for i in 0..a.len {
for i in 0 .. a.len {
if a[i] > val {
val = a[i]
}
@@ -37,10 +40,12 @@ pub fn max<T>(a []T) T {
// idx_min returns the index of the first minimum
[direct_array_access]
pub fn idx_min<T>(a []T) int {
if a.len==0 { panic('.idxmin called on an empty array') }
if a.len == 0 {
panic('.idxmin called on an empty array')
}
mut idx := 0
mut val := a[0]
for i in 0..a.len {
for i in 0 .. a.len {
if a[i] < val {
val = a[i]
idx = i
@@ -52,10 +57,12 @@ pub fn idx_min<T>(a []T) int {
// idx_max returns the index of the first maximum
[direct_array_access]
pub fn idx_max<T>(a []T) int {
if a.len==0 { panic('.idxmax called on an empty array') }
if a.len == 0 {
panic('.idxmax called on an empty array')
}
mut idx := 0
mut val := a[0]
for i in 0..a.len {
for i in 0 .. a.len {
if a[i] > val {
val = a[i]
idx = i
@@ -67,10 +74,12 @@ pub fn idx_max<T>(a []T) int {
// shuffle randomizes the first n items of an array in place (all if n=0)
[direct_array_access]
pub fn shuffle<T>(mut a []T, n int) {
if n < 0 || n > a.len { panic("shuffle's argument 'n' must be in range [0,a.len]") }
cnt := if n==0 { a.len-1 } else { n }
for i in 0..cnt {
x := rand.int_in_range(i,a.len)
if n < 0 || n > a.len {
panic("shuffle's argument 'n' must be in range [0,a.len]")
}
cnt := if n == 0 { a.len - 1 } else { n }
for i in 0 .. cnt {
x := rand.int_in_range(i, a.len)
// swap
a_i := a[i]
a[i] = a[x]
@@ -78,17 +87,15 @@ pub fn shuffle<T>(mut a []T, n int) {
}
}
// merge two sorted arrays (ascending) and maintain sorted order
[direct_array_access]
pub fn merge<T>(a []T, b []T) []T {
mut m := []T{len:a.len + b.len}
pub fn merge<T>(a, b []T) []T {
mut m := []T{len: a.len + b.len}
mut ia := 0
mut ib := 0
mut j := 0
// TODO efficient approach to merge_desc where: a[ia] >= b[ib]
for ia<a.len && ib<b.len {
for ia < a.len && ib < b.len {
if a[ia] <= b[ib] {
m[j] = a[ia]
ia++
@@ -98,21 +105,17 @@ pub fn merge<T>(a []T, b []T) []T {
}
j++
}
// a leftovers
for ia < a.len {
m[j] = a[ia]
ia++
j++
}
// b leftovers
for ib < b.len {
m[j] = b[ib]
ib++
j++
}
return m
}