pull/1213/head
Joe Conigliaro 2019-07-18 02:17:07 +10:00 committed by Alexander Medvednikov
parent 251e32948e
commit 09fb000e67
2 changed files with 19 additions and 0 deletions

View File

@ -189,6 +189,19 @@ pub fn (arr mut array) _push_many(val voidptr, size int) {
arr.len += size arr.len += size
} }
pub fn (a array) reverse() array {
arr := array {
len: a.len
cap: a.cap
element_size: a.element_size
data: malloc(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)
}
return arr
}
pub fn (a []int) str() string { pub fn (a []int) str() string {
mut res := '[' mut res := '['
for i := 0; i < a.len; i++ { for i := 0; i < a.len; i++ {

View File

@ -148,4 +148,10 @@ fn test_push_many() {
assert a[5] == 6 assert a[5] == 6
} }
fn test_reverse() {
mut a := [1, 2, 3, 4]
mut b := ['test', 'array', 'reverse']
assert a.reverse() == [4, 3, 2, 1]
assert b.reverse() == ['reverse', 'array', 'test']
}