map: fix delete (part 1) (#9579)

pull/9585/head
ka-weihe 2021-04-04 01:11:47 +02:00 committed by GitHub
parent 1a324679b9
commit 337e447cfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 2 deletions

View File

@ -561,11 +561,28 @@ fn test_int_keys() {
m[5] += 24 m[5] += 24
m[5]++ m[5]++
assert m[5] == 25 assert m[5] == 25
m2 := map{ mut m2 := map{
3: 9 3: 9
4: 16 4: 16
5: 25 5: 25
} }
four := 4
m2.delete_1(3)
m2.delete_1(four)
m2.delete_1(5)
assert m2.len == 0
assert m2[3] == 0
assert m2[4] == 0
assert m2[5] == 0
assert m2.keys() == []
m2 = map{
3: 9
4: 16
5: 25
}
assert m2.len == 3 assert m2.len == 3
// clone // clone
mc := m.clone() mc := m.clone()
@ -580,6 +597,13 @@ fn test_int_keys() {
all << v all << v
} }
assert all == [3, 9, 4, 16, 5, 25] assert all == [3, 9, 4, 16, 5, 25]
mut m3 := map{
1: 'one'
2: 'two'
}
assert m3[1] == 'one'
m3.delete_1(1)
} }
fn test_voidptr_keys() { fn test_voidptr_keys() {

View File

@ -1461,7 +1461,7 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) ast.Type {
// FIXME: Argument count != 1 will break these // FIXME: Argument count != 1 will break these
if left_type_sym.kind == .array && method_name in checker.array_builtin_methods { if left_type_sym.kind == .array && method_name in checker.array_builtin_methods {
return c.call_array_builtin_method(mut call_expr, left_type, left_type_sym) return c.call_array_builtin_method(mut call_expr, left_type, left_type_sym)
} else if left_type_sym.kind == .map && method_name in ['clone', 'keys', 'move'] { } else if left_type_sym.kind == .map && method_name in ['clone', 'keys', 'move', 'delete_1'] {
return c.call_map_builtin_method(mut call_expr, left_type, left_type_sym) return c.call_map_builtin_method(mut call_expr, left_type, left_type_sym)
} else if left_type_sym.kind == .array && method_name in ['insert', 'prepend'] { } else if left_type_sym.kind == .array && method_name in ['insert', 'prepend'] {
info := left_type_sym.info as ast.Array info := left_type_sym.info as ast.Array

View File

@ -550,6 +550,18 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
else {} else {}
} }
} }
if left_sym.kind == .map && node.name == 'delete_1' {
left_info := left_sym.info as ast.Map
elem_type_str := g.typ(left_info.key_type)
g.write('map_delete_1(&')
g.expr(node.left)
g.write(', &($elem_type_str[]){')
g.expr(node.args[0].expr)
g.write('})')
return
}
if left_sym.kind == .sum_type && node.name == 'type_name' { if left_sym.kind == .sum_type && node.name == 'type_name' {
g.write('tos3( /* $left_sym.name */ v_typeof_sumtype_${typ_sym.cname}( (') g.write('tos3( /* $left_sym.name */ v_typeof_sumtype_${typ_sym.cname}( (')
g.expr(node.left) g.expr(node.left)