map.delete(key)

pull/1389/head
Alexander Medvednikov 2019-07-30 21:27:31 +02:00
parent ad8b105b2c
commit 17e8c1d628
2 changed files with 30 additions and 2 deletions

View File

@ -169,6 +169,32 @@ fn (m map) get(key string, out voidptr) bool {
return m.root.find(key, out, m.element_size) return m.root.find(key, out, m.element_size)
} }
pub fn (n mut Node) delete(key string, element_size int) {
if n.key == key {
C.memset(n.val, 0, element_size)
return
}
else if n.key > key {
if isnil(n.left) {
return
} else {
n.left.delete(key, element_size)
}
}
else {
if isnil(n.right) {
return
} else {
n.right.delete(key, element_size)
}
}
}
pub fn (m mut map) delete(key string) {
m.root.delete(key, m.element_size)
m.size--
}
pub fn (m map) exists(key string) bool { pub fn (m map) exists(key string) bool {
panic('map.exists(key) was removed from the language. Use `key in map` instead.') panic('map.exists(key) was removed from the language. Use `key in map` instead.')
return false return false

View File

@ -36,13 +36,15 @@ fn test_map() {
assert keys.len == 2 assert keys.len == 2
assert keys[0] == 'hi' assert keys[0] == 'hi'
assert keys[1] == 'hello' assert keys[1] == 'hello'
m.delete('hi')
assert m.size == 1
assert m['hi'] == 0
//assert m.keys().len == 1
//// ////
mut users := map[string]User{} mut users := map[string]User{}
users['1'] = User{'Peter'} users['1'] = User{'Peter'}
peter := users['1'] peter := users['1']
assert peter.name == 'Peter' assert peter.name == 'Peter'
println(peter.name)
mut a := A{ mut a := A{
m: map[string]int{} m: map[string]int{}
users: map[string]User{} users: map[string]User{}