diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index e6f500aab3..8b41284542 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -561,11 +561,28 @@ fn test_int_keys() { m[5] += 24 m[5]++ assert m[5] == 25 - m2 := map{ + mut m2 := map{ 3: 9 4: 16 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 // clone mc := m.clone() @@ -580,6 +597,13 @@ fn test_int_keys() { all << v } 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() { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8806741a05..bfd0c9f128 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 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) - } 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) } else if left_type_sym.kind == .array && method_name in ['insert', 'prepend'] { info := left_type_sym.info as ast.Array diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 73125609a6..745cba3d67 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -550,6 +550,18 @@ fn (mut g Gen) method_call(node ast.CallExpr) { 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' { g.write('tos3( /* $left_sym.name */ v_typeof_sumtype_${typ_sym.cname}( (') g.expr(node.left)