Skip to content

Commit 7c255f0

Browse files
authored
builtin, cgen: fix array of map init with default value (#12885)
1 parent 50d988e commit 7c255f0

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

vlib/builtin/array.v

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array) a
7272
return arr
7373
}
7474

75+
fn __new_array_with_map_default(mylen int, cap int, elm_size int, val map) array {
76+
cap_ := if cap < mylen { mylen } else { cap }
77+
mut arr := array{
78+
element_size: elm_size
79+
data: unsafe { malloc(cap_ * elm_size) }
80+
len: mylen
81+
cap: cap_
82+
}
83+
for i in 0 .. arr.len {
84+
val_clone := unsafe { val.clone() }
85+
unsafe { arr.set_unsafe(i, &val_clone) }
86+
}
87+
return arr
88+
}
89+
7590
// Private function, used by V (`nums := [1, 2, 3]`)
7691
fn new_array_from_c_array(len int, cap int, elm_size int, c_array voidptr) array {
7792
cap_ := if cap < len { len } else { cap }

vlib/v/gen/c/array.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
106106
noscan := g.check_noscan(elem_type.typ)
107107
if node.exprs.len == 0 {
108108
is_default_array := elem_type.unaliased_sym.kind == .array && node.has_default
109+
is_default_map := elem_type.unaliased_sym.kind == .map && node.has_default
109110
if node.has_it { // []int{len: 6, init: it * it} when variable it is used in init expression
110111
g.inside_lambda = true
111112
tmp := g.new_tmp_var()
@@ -118,6 +119,8 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
118119
g.write('$ret_typ $tmp =')
119120
if is_default_array {
120121
g.write('__new_array_with_array_default${noscan}(')
122+
} else if is_default_map {
123+
g.write('__new_array_with_map_default${noscan}(')
121124
} else {
122125
g.write('__new_array_with_default${noscan}(')
123126
}
@@ -182,6 +185,8 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
182185
}
183186
if is_default_array {
184187
g.write('__new_array_with_array_default${noscan}(')
188+
} else if is_default_map {
189+
g.write('__new_array_with_map_default${noscan}(')
185190
} else {
186191
g.write('__new_array_with_default${noscan}(')
187192
}
@@ -202,7 +207,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
202207
} else {
203208
g.write('sizeof($elem_styp), ')
204209
}
205-
if is_default_array {
210+
if is_default_array || is_default_map {
206211
g.write('($elem_styp[]){')
207212
g.expr(node.default_expr)
208213
g.write('}[0])')
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
fn init_a(n_rows int) []map[int]int {
2+
mut tally := []map[int]int{}
3+
for _ in 0 .. n_rows {
4+
tally << map[int]int{}
5+
}
6+
return tally
7+
}
8+
9+
fn init_b(n_rows int) []map[int]int {
10+
mut tally := []map[int]int{len: n_rows, init: map[int]int{}}
11+
return tally
12+
}
13+
14+
pub fn tallys_in_array(indexs []int, values [][]int, init fn (int) []map[int]int) []map[int]int {
15+
mut tally := init(indexs.len)
16+
for row in 0 .. values.len {
17+
for i, index in indexs {
18+
tally[i][values[row][index]]++
19+
}
20+
}
21+
return tally
22+
}
23+
24+
fn test_array_of_map_with_default() {
25+
indexs := [0, 1]
26+
values := [[1, 201], [1, 3], [1, 201], [1, 3]]
27+
28+
out1 := tallys_in_array(indexs, values, init_a)
29+
println(out1)
30+
out2 := tallys_in_array(indexs, values, init_b)
31+
println(out2)
32+
33+
mut maps := []map[int]int{}
34+
maps << map[int]int{}
35+
maps << map[int]int{}
36+
maps[0][1] = 4
37+
maps[1][3] = 2
38+
maps[1][201] = 2
39+
40+
assert out1 == maps
41+
assert out2 == maps
42+
}

0 commit comments

Comments
 (0)