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

array: more eq functions

This commit is contained in:
Charles WANG 2020-01-22 01:49:30 +08:00 committed by Alexander Medvednikov
parent dd5751df0c
commit 136c469ef7
2 changed files with 137 additions and 84 deletions

View File

@ -242,7 +242,6 @@ pub fn (a array) clone() array {
return arr return arr
} }
fn (a array) slice_clone(start, _end int) array { fn (a array) slice_clone(start, _end int) array {
mut end := _end mut end := _end
if start > end { if start > end {
@ -304,13 +303,11 @@ pub fn (a array) reverse() array {
data: calloc(a.cap * a.element_size) data: calloc(a.cap * a.element_size)
} }
for i := 0; i < a.len; i++ { for i := 0; i < a.len; i++ {
C.memcpy(arr.data + i * arr.element_size, C.memcpy(arr.data + i * arr.element_size, &a[a.len - 1 - i], arr.element_size)
&a[a.len - 1 - i], arr.element_size)
} }
return arr return arr
} }
// pub fn (a []int) free() { // pub fn (a []int) free() {
[unsafe_fn] [unsafe_fn]
pub fn (a array) free() { pub fn (a array) free() {
@ -489,3 +486,44 @@ pub fn (a []f32) eq(a2 []f32) bool {
return array_eq(a, a2) return array_eq(a, a2)
} }
// compare_i64 for []f64 sort_with_compare()
// sort []i64 with quicksort
// usage :
// mut x := [i64(100),10,70,28,92]
// x.sort_with_compare(compare_i64)
// println(x) // Sorted i64 Array
// output:
// [10, 28, 70, 92, 100]
pub fn compare_i64(a, b &i64) int {
if *a < *b {
return -1
}
if *a > *b {
return 1
}
return 0
}
// compare_f64 for []f64 sort_with_compare()
// ref. compare_i64(...)
pub fn compare_f64(a, b &f64) int {
if *a < *b {
return -1
}
if *a > *b {
return 1
}
return 0
}
// compare_f32 for []f32 sort_with_compare()
// ref. compare_i64(...)
pub fn compare_f32(a, b &f32) int {
if *a < *b {
return -1
}
if *a > *b {
return 1
}
return 0
}

View File

@ -30,7 +30,6 @@ fn test_ints() {
assert a.len == 5 assert a.len == 5
assert a[4] == 4 assert a[4] == 4
assert a.last() == 4 assert a.last() == 4
s := a.str() s := a.str()
assert s == '[1, 5, 2, 3, 4]' assert s == '[1, 5, 2, 3, 4]'
assert a[1] == 5 assert a[1] == 5
@ -41,15 +40,12 @@ fn test_deleting() {
mut a := [1, 5, 2, 3, 4] mut a := [1, 5, 2, 3, 4]
assert a.len == 5 assert a.len == 5
assert a.str() == '[1, 5, 2, 3, 4]' assert a.str() == '[1, 5, 2, 3, 4]'
a.delete(0) a.delete(0)
assert a.str() == '[5, 2, 3, 4]' assert a.str() == '[5, 2, 3, 4]'
assert a.len == 4 assert a.len == 4
a.delete(1) a.delete(1)
assert a.str() == '[5, 3, 4]' assert a.str() == '[5, 3, 4]'
assert a.len == 3 assert a.len == 3
a.delete(a.len - 1) a.delete(a.len - 1)
assert a.str() == '[5, 3]' assert a.str() == '[5, 3]'
assert a.len == 2 assert a.len == 2
@ -107,36 +103,33 @@ fn test_push() {
// i := 3 // i := 3
// a.insert(0, &i) // a.insert(0, &i)
// ---------------------------- // ----------------------------
/*
fn test_insert() { fn test_insert() {
// mut a := [1, 2] mut a := [1, 2]
// a.insert(0, 3) a.insert(0, 3)
// println(a) println(a)
} }
*/
// fn test_insert() { // fn test_insert() {
// mut a := [1, 2] // mut a := [1, 2]
// a.insert(0, 3) // a.insert(0, 3)
// assert a[0] == 3 // assert a[0] == 3
// assert a[2] == 2 // assert a[2] == 2
// assert a.len == 3 // assert a.len == 3
// a.insert(1, 4) // a.insert(1, 4)
// assert a[1] == 4 // assert a[1] == 4
// assert a[2] == 1 // assert a[2] == 1
// assert a.len == 4 // assert a.len == 4
// a.insert(4, 5) // a.insert(4, 5)
// assert a[4] == 5 // assert a[4] == 5
// assert a[3] == 2 // assert a[3] == 2
// assert a.len == 5 // assert a.len == 5
// mut b := []f64 // mut b := []f64
// assert b.len == 0 // assert b.len == 0
// b.insert(0, f64(1.1)) // b.insert(0, f64(1.1))
// assert b.len == 1 // assert b.len == 1
// assert b[0] == f64(1.1) // assert b[0] == f64(1.1)
// } // }
// TODO array.prepend is broken // TODO array.prepend is broken
// It depends on array.insert // It depends on array.insert
// ----------------------------- // -----------------------------
@ -146,21 +139,19 @@ fn test_insert() {
// a.prepend(1) // a.prepend(1)
// assert a.len == 1 // assert a.len == 1
// assert a[0] == 1 // assert a[0] == 1
// mut b := []f64 // mut b := []f64
// assert b.len == 0 // assert b.len == 0
// b.prepend(f64(1.1)) // b.prepend(f64(1.1))
// assert b.len == 1 // assert b.len == 1
// assert b[0] == f64(1.1) // assert b[0] == f64(1.1)
// } // }
fn test_strings() { fn test_strings() {
a := ['a', 'b', 'c'] a := ['a', 'b', 'c']
assert a.str() == '["a", "b", "c"]' assert a.str() == '["a", "b", "c"]'
} }
/*
fn test_compare_ints() { fn test_compare_ints() {
/*
assert compare_ints(1, 2) == -1 assert compare_ints(1, 2) == -1
assert compare_ints(2, 1) == 1 assert compare_ints(2, 1) == 1
assert compare_ints(0, 0) == 0 assert compare_ints(0, 0) == 0
@ -170,8 +161,9 @@ fn test_compare_ints() {
assert compare_ints(a, b) == -1 assert compare_ints(a, b) == -1
assert compare_ints(b, a) == 1 assert compare_ints(b, a) == 1
assert compare_ints(a, a) == 0 assert compare_ints(a, a) == 0
*/
} }
*/
fn test_repeat() { fn test_repeat() {
{ {
@ -181,16 +173,21 @@ fn test_repeat() {
} }
{ {
a := [1.1].repeat(10) a := [1.1].repeat(10)
// FIXME: assert aa[0] == 1.1 will fail, need fix assert a[0] == 1.1
assert a[0] == f32(1.1) assert a[5] == 1.1
assert a[5] == f32(1.1) assert a[9] == 1.1
assert a[9] == f32(1.1)
} }
{ {
a := [f32(1.1)].repeat(10) a := [i64(-123)].repeat(10)
assert a[0] == f32(1.1) assert a[0] == -123
assert a[5] == f32(1.1) assert a[5] == -123
assert a[9] == f32(1.1) assert a[9] == -123
}
{
a := [u64(123)].repeat(10)
assert a[0] == 123
assert a[5] == 123
assert a[9] == 123
} }
{ {
a := [f64(1.1)].repeat(10) a := [f64(1.1)].repeat(10)
@ -264,10 +261,10 @@ fn test_reverse() {
c := a.reverse() c := a.reverse()
d := b.reverse() d := b.reverse()
for i, _ in c { for i, _ in c {
assert c[i] == a[a.len-i-1] assert c[i] == a[a.len - i - 1]
} }
for i, _ in d { for i, _ in d {
assert d[i] == b[b.len-i-1] assert d[i] == b[b.len - i - 1]
} }
} }
@ -291,12 +288,12 @@ fn test_fixed() {
assert nums2[N - 1] == 0 assert nums2[N - 1] == 0
} }
fn modify (numbers mut []int) { fn modify(numbers mut []int) {
numbers[0] = 777 numbers[0] = 777
} }
fn test_mut_slice() { fn test_mut_slice() {
mut n := [1,2,3] mut n := [1, 2, 3]
modify(mut n[..2]) modify(mut n[..2])
assert n[0] == 777 assert n[0] == 777
modify(mut n[2..]) modify(mut n[2..])
@ -340,12 +337,12 @@ pub fn (t Test) str() string {
} }
fn test_struct_print() { fn test_struct_print() {
mut a := Test { mut a := Test{
a: 'Test', a: 'Test'
b: [] b: []
} }
b := Test2 { b := Test2{
one: 1, one: 1
two: 2 two: 2
} }
a.b << b a.b << b
@ -370,19 +367,16 @@ fn test_find_index() {
assert a.index('v') == 0 assert a.index('v') == 0
assert a.index('is') == 1 assert a.index('is') == 1
assert a.index('gre') == -1 assert a.index('gre') == -1
// int // int
b := [1, 2, 3, 4] b := [1, 2, 3, 4]
assert b.index(1) == 0 assert b.index(1) == 0
assert b.index(4) == 3 assert b.index(4) == 3
assert b.index(5) == -1 assert b.index(5) == -1
// byte // byte
c := [0x22, 0x33, 0x55] c := [0x22, 0x33, 0x55]
assert c.index(0x22) == 0 assert c.index(0x22) == 0
assert c.index(0x55) == 2 assert c.index(0x55) == 2
assert c.index(0x99) == -1 assert c.index(0x99) == -1
// char // char
d := [`a`, `b`, `c`] d := [`a`, `b`, `c`]
assert d.index(`b`) == 1 assert d.index(`b`) == 1
@ -391,19 +385,19 @@ fn test_find_index() {
} }
fn test_multi() { fn test_multi() {
a := [[1,2,3],[4,5,6]] a := [[1, 2, 3], [4, 5, 6]]
assert a.len == 2 assert a.len == 2
assert a[0].len == 3 assert a[0].len == 3
assert a[0][0] == 1 assert a[0][0] == 1
assert a[0][2] == 3 assert a[0][2] == 3
assert a[1][2] == 6 assert a[1][2] == 6
// TODO // TODO
//b := [ [[1,2,3],[4,5,6]], [[1,2]] ] // b := [ [[1,2,3],[4,5,6]], [[1,2]] ]
//assert b[0][0][0] == 1 // assert b[0][0][0] == 1
} }
fn test_in() { fn test_in() {
a := [1,2,3] a := [1, 2, 3]
assert 1 in a assert 1 in a
assert 2 in a assert 2 in a
assert 3 in a assert 3 in a
@ -427,7 +421,6 @@ fn test_reduce() {
assert b == 15 assert b == 15
assert c == 20 assert c == 20
assert d == 14 assert d == 14
e := [1, 2, 3] e := [1, 2, 3]
f := e.reduce(sub, 0) f := e.reduce(sub, 0)
g := e.reduce(sub, -1) g := e.reduce(sub, -1)
@ -465,7 +458,6 @@ fn test_map() {
assert bools[0] == true assert bools[0] == true
assert bools[1] == false assert bools[1] == false
assert bools[2] == false assert bools[2] == false
} }
fn test_array_str() { fn test_array_str() {
@ -477,8 +469,8 @@ fn test_array_str() {
} }
fn test_eq() { fn test_eq() {
assert [5,6,7].eq([6,7]) == false assert [5, 6, 7].eq([6, 7]) == false
assert [`a`,`b`].eq([`a`,`b`]) == true assert [`a`, `b`].eq([`a`, `b`]) == true
} }
fn test_sort() { fn test_sort() {
@ -498,7 +490,29 @@ fn test_sort() {
assert nums[4] == 108 assert nums[4] == 108
} }
fn test_f32_sort() {
mut f := [50.0, 15, 1, 79, 38, 0, 27]
f.sort_with_compare(compare_f32)
assert f[0] == 0.0
assert f[1] == 1.0
assert f[6] == 79.0
}
fn test_f64_sort() {
mut f := [f64(50.0), 15, 1, 79, 38, 0, 27]
f.sort_with_compare(compare_f64)
assert f[0] == 0.0
assert f[1] == 1.0
assert f[6] == 79.0
}
fn test_i64_sort() {
mut f := [i64(50), 15, 1, 79, 38, 0, 27]
f.sort_with_compare(compare_i64)
assert f[0] == 0
assert f[1] == 1
assert f[6] == 79
}
/* /*
fn test_for_last() { fn test_for_last() {
@ -516,8 +530,9 @@ fn test_for_last() {
} }
*/ */
struct Foo { struct Foo {
mut: mut:
bar []int bar []int
} }