aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/arrays
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/vlib/arrays
downloadcli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.gz
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.bz2
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/vlib/arrays')
-rw-r--r--v_windows/v/vlib/arrays/arrays.v126
-rw-r--r--v_windows/v/vlib/arrays/arrays_test.v87
2 files changed, 213 insertions, 0 deletions
diff --git a/v_windows/v/vlib/arrays/arrays.v b/v_windows/v/vlib/arrays/arrays.v
new file mode 100644
index 0000000..26636d1
--- /dev/null
+++ b/v_windows/v/vlib/arrays/arrays.v
@@ -0,0 +1,126 @@
+module arrays
+
+// Common arrays functions:
+// - min / max - return the value of the minumum / maximum
+// - idx_min / idx_max - return the index of the first minumum / maximum
+// - merge - combine two sorted arrays and maintain sorted order
+
+// min returns the minimum
+pub fn min<T>(a []T) T {
+ if a.len == 0 {
+ panic('.min called on an empty array')
+ }
+ mut val := a[0]
+ for e in a {
+ if e < val {
+ val = e
+ }
+ }
+ return val
+}
+
+// max returns the maximum
+pub fn max<T>(a []T) T {
+ if a.len == 0 {
+ panic('.max called on an empty array')
+ }
+ mut val := a[0]
+ for e in a {
+ if e > val {
+ val = e
+ }
+ }
+ return val
+}
+
+// idx_min returns the index of the first minimum
+pub fn idx_min<T>(a []T) int {
+ if a.len == 0 {
+ panic('.idx_min called on an empty array')
+ }
+ mut idx := 0
+ mut val := a[0]
+ for i, e in a {
+ if e < val {
+ val = e
+ idx = i
+ }
+ }
+ return idx
+}
+
+// idx_max returns the index of the first maximum
+pub fn idx_max<T>(a []T) int {
+ if a.len == 0 {
+ panic('.idx_max called on an empty array')
+ }
+ mut idx := 0
+ mut val := a[0]
+ for i, e in a {
+ if e > val {
+ val = e
+ idx = i
+ }
+ }
+ return idx
+}
+
+// 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}
+ 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 {
+ if a[ia] <= b[ib] {
+ m[j] = a[ia]
+ ia++
+ } else {
+ m[j] = b[ib]
+ ib++
+ }
+ 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
+}
+
+// group n arrays into a single array of arrays with n elements
+pub fn group<T>(lists ...[]T) [][]T {
+ mut length := if lists.len > 0 { lists[0].len } else { 0 }
+ // calculate length of output by finding shortest input array
+ for ndx in 1 .. lists.len {
+ if lists[ndx].len < length {
+ length = lists[ndx].len
+ }
+ }
+
+ if length > 0 {
+ mut arr := [][]T{cap: length}
+ // append all combined arrays into the resultant array
+ for ndx in 0 .. length {
+ mut zipped := []T{cap: lists.len}
+ // combine each list item for the ndx position into one array
+ for list_ndx in 0 .. lists.len {
+ zipped << lists[list_ndx][ndx]
+ }
+ arr << zipped
+ }
+ return arr
+ }
+
+ return [][]T{}
+}
diff --git a/v_windows/v/vlib/arrays/arrays_test.v b/v_windows/v/vlib/arrays/arrays_test.v
new file mode 100644
index 0000000..54042b1
--- /dev/null
+++ b/v_windows/v/vlib/arrays/arrays_test.v
@@ -0,0 +1,87 @@
+module arrays
+
+fn test_min() {
+ a := [8, 2, 6, 4]
+ assert min<int>(a) == 2
+ assert min<int>(a[2..]) == 4
+ b := [f32(5.1), 3.1, 1.1, 9.1]
+ assert min<f32>(b) == f32(1.1)
+ assert min<f32>(b[..2]) == f32(3.1)
+ c := [byte(4), 9, 3, 1]
+ assert min<byte>(c) == byte(1)
+ assert min<byte>(c[..3]) == byte(3)
+}
+
+fn test_max() {
+ a := [8, 2, 6, 4]
+ assert max<int>(a) == 8
+ assert max<int>(a[1..]) == 6
+ b := [f32(5.1), 3.1, 1.1, 9.1]
+ assert max<f32>(b) == f32(9.1)
+ assert max<f32>(b[..3]) == f32(5.1)
+ c := [byte(4), 9, 3, 1]
+ assert max<byte>(c) == byte(9)
+ assert max<byte>(c[2..]) == byte(3)
+}
+
+fn test_idx_min() {
+ a := [8, 2, 6, 4]
+ assert idx_min<int>(a) == 1
+ b := [f32(5.1), 3.1, 1.1, 9.1]
+ assert idx_min<f32>(b) == 2
+ c := [byte(4), 9, 3, 1]
+ assert idx_min<byte>(c) == 3
+}
+
+fn test_idx_max() {
+ a := [8, 2, 6, 4]
+ assert idx_max<int>(a) == 0
+ b := [f32(5.1), 3.1, 1.1, 9.1]
+ assert idx_max<f32>(b) == 3
+ c := [byte(4), 9, 3, 1]
+ assert idx_max<byte>(c) == 1
+}
+
+fn test_merge() {
+ a := [1, 3, 5, 5, 7]
+ b := [2, 4, 4, 5, 6, 8]
+ c := []int{}
+ d := []int{}
+ assert merge<int>(a, b) == [1, 2, 3, 4, 4, 5, 5, 5, 6, 7, 8]
+ assert merge<int>(c, d) == []
+ assert merge<int>(a, c) == a
+ 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'
+ }
+}
+
+fn test_group() {
+ x := [4, 5, 6]
+ y := [2, 1, 3]
+
+ z := group<int>(x, y)
+ assert z == [[4, 2], [5, 1], [6, 3]]
+ x2 := [8, 9]
+ z2 := group<int>(x2, y)
+ assert z2 == [[8, 2], [9, 1]]
+ assert group<int>(x, []int{}) == [][]int{}
+}