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

builtin: add a map.clear() method (#14373)

This commit is contained in:
CC 2022-05-12 00:43:01 -06:00 committed by GitHub
parent 49382f1f43
commit 901b8f0c24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -294,6 +294,14 @@ pub fn (mut m map) move() map {
return r
}
// clear clears the map without deallocating the allocated data.
// It does it by setting the map length to `0`
// Example: a.clear() // `a.len` and `a.key_values.len` is now 0
pub fn (mut m map) clear() {
m.len = 0
m.key_values.len = 0
}
[inline]
fn (m &map) key_to_index(pkey voidptr) (u32, u32) {
hash := m.hash_fn(pkey)

View File

@ -245,6 +245,14 @@ fn test_mut_arg() {
assert a == 10
}
fn test_clear() {
mut m := map[string]int{}
m['one'] = 1
m['two'] = 2
m.clear()
assert m.len == 0
}
fn test_delete() {
mut m := map[string]int{}
m['one'] = 1