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

map: fixed deletion of non-existent keys

This commit is contained in:
Amal Shaji 2019-10-30 22:49:59 +05:30 committed by Alexander Medvednikov
parent 5da3c49497
commit e8d34002e1
2 changed files with 6 additions and 2 deletions

View File

@ -207,9 +207,11 @@ pub fn (n mut mapnode) delete(key string, element_size int) {
}
pub fn (m mut map) delete(key string) {
if m.exists(key) {
m.root.delete(key, m.element_size)
m.size--
}
}
fn (m map) exists(key string) bool {
return !isnil(m.root) && m.root.find2(key, m.element_size)

View File

@ -38,6 +38,8 @@ fn test_map() {
assert keys[1] == 'hello'
m.delete('hi')
assert m.size == 1
m.delete('aloha')
assert m.size == 1
assert m['hi'] == 0
assert m.keys().len == 1