array: clear()

pull/3581/head
prime31 2020-01-27 13:31:48 -08:00 committed by Alexander Medvednikov
parent 8986633624
commit 08d3401092
2 changed files with 25 additions and 0 deletions

View File

@ -143,6 +143,11 @@ pub fn (a mut array) delete(i int) {
a.len--
}
// clears the array without deallocating the allocated data
pub fn (a mut array) clear() {
a.len = 0
}
// Private function. Used to implement array[] operator
fn (a array) get(i int) voidptr {
if i < 0 || i >= a.len {

View File

@ -570,3 +570,23 @@ fn test_for() {
}
assert sum == 6
}
fn test_clear() {
mut arr := [1,2,3]
assert arr.len == 3
arr.clear()
assert arr.len == 0
arr << 3
arr << 2
arr << 1
arr << 0
assert arr.len == 4
assert arr[0] == 3
assert arr[1] == 2
assert arr[2] == 1
assert arr[3] == 0
arr.clear()
assert arr.len == 0
}