Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map: add 3 DenseArray methods for bootstrapping #7113

Merged
merged 1 commit into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions vlib/builtin/map.v
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ fn new_dense_array(value_bytes int) DenseArray {
}
}

[inline]
fn (d &DenseArray) key(i int) voidptr {
return unsafe {voidptr(d.keys + i)}
}

// for cgen
[inline]
fn (d &DenseArray) value(i int) voidptr {
return unsafe {voidptr(d.values + i * d.value_bytes)}
}

[inline]
fn (d &DenseArray) has_index(i int) bool {
pkey := unsafe {d.keys + i}
return pkey.str != 0
}

// Push element to array and return index
// The growth-factor is roughly 1.125 `(x + (x >> 3))`
[inline]
Expand Down
10 changes: 3 additions & 7 deletions vlib/v/gen/auto_str_methods.v
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,18 @@ fn (mut g Gen) gen_str_for_map(info table.Map, styp string, str_fn_name string)
if !val_sym.has_method('str') {
g.gen_str_for_type(info.value_type)
}
zero := g.type_default(info.value_type)
g.type_definitions.writeln('static string ${str_fn_name}($styp m); // auto')
g.auto_str_funcs.writeln('static string ${str_fn_name}($styp m) { return indent_${str_fn_name}(m, 0);}')
g.type_definitions.writeln('static string indent_${str_fn_name}($styp m, int indent_count); // auto')
g.auto_str_funcs.writeln('static string indent_${str_fn_name}($styp m, int indent_count) { /* gen_str_for_map */')
g.auto_str_funcs.writeln('\tstrings__Builder sb = strings__new_builder(m.key_values.len*10);')
g.auto_str_funcs.writeln('\tstrings__Builder_write(&sb, _SLIT("{"));')
g.auto_str_funcs.writeln('\tfor (unsigned int i = 0; i < m.key_values.len; ++i) {')
g.auto_str_funcs.writeln('\t\tif (m.key_values.keys[i].str == 0) { continue; }')
g.auto_str_funcs.writeln('\t\tstring key = (*(string*)DenseArray_get(m.key_values, i));')
g.auto_str_funcs.writeln('\t\tif (!DenseArray_has_index(&m.key_values, i)) { continue; }')
g.auto_str_funcs.writeln('\t\tstring key = *(string*)DenseArray_key(&m.key_values, i);')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\'%.*s\\000\'", 2, key));')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _SLIT(": "));')
g.auto_str_funcs.write('\t$val_styp it = (*($val_styp*)map_get(')
g.auto_str_funcs.write('m, (*(string*)DenseArray_get(m.key_values, i))')
g.auto_str_funcs.write(', ')
g.auto_str_funcs.writeln(' &($val_styp[]) { $zero }));')
g.auto_str_funcs.writeln('\t\t$val_styp it = *($val_styp*)DenseArray_value(&m.key_values, i);')
if val_sym.kind == .string {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\'%.*s\\000\'", 2, it));')
} else if val_sym.kind == .struct_ && !val_sym.has_method('str') {
Expand Down
13 changes: 6 additions & 7 deletions vlib/v/gen/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -1221,28 +1221,27 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
g.expr(it.cond)
g.writeln(';')
g.writeln('for (int $idx = 0; $idx < (int)${atmp}.key_values.len; ++$idx) {')
g.writeln('\tif (${atmp}.key_values.keys[$idx].str == 0) {continue;}')
g.writeln('\tif (!DenseArray_has_index(&${atmp}.key_values, $idx)) {continue;}')
if it.key_var != '_' {
key_styp := g.typ(it.key_type)
key := c_name(it.key_var)
g.writeln('\t$key_styp $key = /*key*/ *($key_styp*)DenseArray_key(&${atmp}.key_values, $idx);')
// TODO: analyze whether it.key_type has a .clone() method and call .clone() for all types:
if it.key_type == table.string_type {
g.writeln('\t$key_styp $key = /*key*/ string_clone(${atmp}.key_values.keys[$idx]);')
} else {
g.writeln('\t$key_styp $key = /*key*/ ${atmp}.key_values.keys[$idx];')
g.writeln('\t$key = string_clone($key);')
}
}
if it.val_var != '_' {
val_sym := g.table.get_type_symbol(it.val_type)
valstr := '(void*)(${atmp}.key_values.values + $idx * (u32)(${atmp}.value_bytes))'
if val_sym.kind == .function {
g.write('\t')
g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(it.val_var))
g.writeln(' = (*(voidptr*)$valstr);')
g.write(' = (*(voidptr*)')
} else {
val_styp := g.typ(it.val_type)
g.writeln('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)$valstr);')
g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)')
}
g.writeln('DenseArray_value(&${atmp}.key_values, $idx));')
}
g.stmts(it.stmts)
if it.key_type == table.string_type && !g.is_builtin_mod {
Expand Down