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

builtin: fix m.len to 0, after calling map.clear() (#16720)

This commit is contained in:
yuyi 2022-12-23 03:36:33 +08:00 committed by GitHub
parent f9043c84a7
commit e01dac885c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View File

@ -299,6 +299,7 @@ pub fn (mut m map) move() map {
// Example: a.clear() // `a.len` and `a.key_values.len` is now 0 // Example: a.clear() // `a.len` and `a.key_values.len` is now 0
pub fn (mut m map) clear() { pub fn (mut m map) clear() {
m.len = 0 m.len = 0
m.even_index = 0
m.key_values.len = 0 m.key_values.len = 0
} }

View File

@ -993,3 +993,45 @@ fn test_alias_of_map_delete() {
assert m.len == 1 assert m.len == 1
assert m[22] == 222 assert m[22] == 222
} }
struct State {
mut:
state map[string]rune
}
struct Foo {
bar int
baz int
}
fn (mut st State) add(s string, r rune) {
st.state[s] = r
}
fn (mut st State) add2(foos []Foo) {
for f in foos {
st.state['${f.bar},${f.baz}'] = `z`
}
}
fn (mut st State) reset() {
st.state.clear()
}
fn test_map_clear() {
mut s := State{}
item1 := [Foo{
bar: 1
baz: 2
}, Foo{
bar: 3
baz: 4
}]
s.add2(item1)
assert s.state.len == 2
s.reset()
s.add2(item1)
assert s.state.len == 2
}