map: fixed deletion of non-existent keys
parent
5da3c49497
commit
e8d34002e1
|
@ -207,8 +207,10 @@ pub fn (n mut mapnode) delete(key string, element_size int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (m mut map) delete(key string) {
|
pub fn (m mut map) delete(key string) {
|
||||||
m.root.delete(key, m.element_size)
|
if m.exists(key) {
|
||||||
m.size--
|
m.root.delete(key, m.element_size)
|
||||||
|
m.size--
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (m map) exists(key string) bool {
|
fn (m map) exists(key string) bool {
|
||||||
|
|
|
@ -38,6 +38,8 @@ fn test_map() {
|
||||||
assert keys[1] == 'hello'
|
assert keys[1] == 'hello'
|
||||||
m.delete('hi')
|
m.delete('hi')
|
||||||
assert m.size == 1
|
assert m.size == 1
|
||||||
|
m.delete('aloha')
|
||||||
|
assert m.size == 1
|
||||||
|
|
||||||
assert m['hi'] == 0
|
assert m['hi'] == 0
|
||||||
assert m.keys().len == 1
|
assert m.keys().len == 1
|
||||||
|
|
Loading…
Reference in New Issue