map: rename methods (#9657)

pull/9661/head
ka-weihe 2021-04-10 04:00:29 +02:00 committed by GitHub
parent 6c1a43415e
commit 5273214ec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 49 deletions

View File

@ -324,6 +324,10 @@ fn map_free_string(pkey voidptr) {
fn map_free_nop(_ voidptr) {
}
fn new_map(key_bytes int, value_bytes int, hash_fn MapHashFn, key_eq_fn MapEqFn, clone_fn MapCloneFn, free_fn MapFreeFn) map {
return new_map_2(key_bytes, value_bytes, hash_fn, key_eq_fn, clone_fn, free_fn)
}
fn new_map_2(key_bytes int, value_bytes int, hash_fn MapHashFn, key_eq_fn MapEqFn, clone_fn MapCloneFn, free_fn MapFreeFn) map {
metasize := int(sizeof(u32) * (init_capicity + extra_metas_inc))
// for now assume anything bigger than a pointer is a string
@ -346,6 +350,11 @@ fn new_map_2(key_bytes int, value_bytes int, hash_fn MapHashFn, key_eq_fn MapEqF
}
}
fn new_map_init(hash_fn MapHashFn, key_eq_fn MapEqFn, clone_fn MapCloneFn, free_fn MapFreeFn, n int, key_bytes int, value_bytes int, keys voidptr, values voidptr) map {
return new_map_init_2(hash_fn, key_eq_fn, clone_fn, free_fn, n, key_bytes, value_bytes,
keys, values)
}
fn new_map_init_2(hash_fn MapHashFn, key_eq_fn MapEqFn, clone_fn MapCloneFn, free_fn MapFreeFn, n int, key_bytes int, value_bytes int, keys voidptr, values voidptr) map {
mut out := new_map_2(key_bytes, value_bytes, hash_fn, key_eq_fn, clone_fn, free_fn)
// TODO pre-allocate n slots
@ -434,6 +443,10 @@ fn (mut m map) ensure_extra_metas(probe_count u32) {
}
}
fn (mut m map) set(key voidptr, value voidptr) {
m.set_1(key, value)
}
// Insert new element to the map. The element is inserted if its key is
// not equivalent to the key of any other element already in the container.
// If the key already exists, its value is changed to the value of the new element.
@ -531,6 +544,10 @@ fn (mut m map) cached_rehash(old_cap u32) {
unsafe { free(old_metas) }
}
fn (mut m map) get_and_set(key voidptr, zero voidptr) voidptr {
return m.get_and_set_1(key, zero)
}
// This method is used for assignment operators. If the argument-key
// does not exist in the map, it's added to the map along with the zero/default value.
// If the key exists, its respective value is returned.
@ -559,6 +576,10 @@ fn (mut m map) get_and_set_1(key voidptr, zero voidptr) voidptr {
return voidptr(0)
}
fn (m &map) get(key voidptr, zero voidptr) voidptr {
return m.get_1(key, zero)
}
// If `key` matches the key of an element in the container,
// the method returns a reference to its mapped value.
// If not, a zero/default value is returned.
@ -582,6 +603,10 @@ fn (m &map) get_1(key voidptr, zero voidptr) voidptr {
return zero
}
fn (m &map) get_check(key voidptr) voidptr {
return m.get_1_check(key)
}
// If `key` matches the key of an element in the container,
// the method returns a reference to its mapped value.
// If not, a zero pointer is returned.
@ -606,6 +631,10 @@ fn (m &map) get_1_check(key voidptr) voidptr {
return 0
}
fn (m &map) exists(key voidptr) bool {
return m.exists_1(key)
}
// Checks whether a particular key exists in the map.
fn (m &map) exists_1(key voidptr) bool {
mut index, mut meta := m.key_to_index(key)
@ -677,22 +706,8 @@ pub fn (mut m map) delete(key voidptr) {
}
}
// bootstrap
// delete this
pub fn (m &map) keys() []string {
mut keys := []string{len: m.len}
mut item := unsafe { &byte(keys.data) }
for i := 0; i < m.key_values.len; i++ {
if !m.key_values.has_index(i) {
continue
}
unsafe {
pkey := m.key_values.key(i)
m.clone_fn(item, pkey)
item = item + m.key_bytes
}
}
return keys
fn (m &map) keys() array {
return m.keys_1()
}
// Returns all keys in the map.

View File

@ -277,7 +277,7 @@ fn (mut g Gen) gen_map_equality_fn(left ast.Type) string {
fn_builder.writeln('\tfor (int i = 0; i < a.key_values.len; ++i) {')
fn_builder.writeln('\t\tif (!DenseArray_has_index(&a.key_values, i)) continue;')
fn_builder.writeln('\t\tvoidptr k = DenseArray_key(&a.key_values, i);')
fn_builder.writeln('\t\tif (!map_exists_1(&b, k)) return false;')
fn_builder.writeln('\t\tif (!map_exists(&b, k)) return false;')
kind := g.table.type_kind(value_typ)
if kind == .function {
value_sym := g.table.get_type_symbol(value_typ)
@ -292,44 +292,44 @@ fn (mut g Gen) gen_map_equality_fn(left ast.Type) string {
fn_builder.write_string(', ')
}
}
fn_builder.writeln(') = *(voidptr*)map_get_1(&a, k, &(voidptr[]){ 0 });')
fn_builder.writeln(') = *(voidptr*)map_get(&a, k, &(voidptr[]){ 0 });')
} else {
fn_builder.writeln('\t\t$ptr_value_typ v = *($ptr_value_typ*)map_get_1(&a, k, &($ptr_value_typ[]){ 0 });')
fn_builder.writeln('\t\t$ptr_value_typ v = *($ptr_value_typ*)map_get(&a, k, &($ptr_value_typ[]){ 0 });')
}
match kind {
.string {
fn_builder.writeln('\t\tif (!fast_string_eq(*(string*)map_get_1(&b, k, &(string[]){_SLIT("")}), v)) {')
fn_builder.writeln('\t\tif (!fast_string_eq(*(string*)map_get(&b, k, &(string[]){_SLIT("")}), v)) {')
}
.sum_type {
eq_fn := g.gen_sumtype_equality_fn(value_typ)
fn_builder.writeln('\t\tif (!${eq_fn}_sumtype_eq(*($ptr_value_typ*)map_get_1(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
fn_builder.writeln('\t\tif (!${eq_fn}_sumtype_eq(*($ptr_value_typ*)map_get(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
}
.struct_ {
eq_fn := g.gen_struct_equality_fn(value_typ)
fn_builder.writeln('\t\tif (!${eq_fn}_struct_eq(*($ptr_value_typ*)map_get_1(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
fn_builder.writeln('\t\tif (!${eq_fn}_struct_eq(*($ptr_value_typ*)map_get(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
}
.array {
eq_fn := g.gen_array_equality_fn(value_typ)
fn_builder.writeln('\t\tif (!${eq_fn}_arr_eq(*($ptr_value_typ*)map_get_1(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
fn_builder.writeln('\t\tif (!${eq_fn}_arr_eq(*($ptr_value_typ*)map_get(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
}
.array_fixed {
eq_fn := g.gen_fixed_array_equality_fn(value_typ)
fn_builder.writeln('\t\tif (!${eq_fn}_arr_eq(*($ptr_value_typ*)map_get_1(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
fn_builder.writeln('\t\tif (!${eq_fn}_arr_eq(*($ptr_value_typ*)map_get(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
}
.map {
eq_fn := g.gen_map_equality_fn(value_typ)
fn_builder.writeln('\t\tif (!${eq_fn}_map_eq(*($ptr_value_typ*)map_get_1(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
fn_builder.writeln('\t\tif (!${eq_fn}_map_eq(*($ptr_value_typ*)map_get(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
}
.alias {
eq_fn := g.gen_alias_equality_fn(value_typ)
fn_builder.writeln('\t\tif (!${eq_fn}_alias_eq(*($ptr_value_typ*)map_get_1(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
fn_builder.writeln('\t\tif (!${eq_fn}_alias_eq(*($ptr_value_typ*)map_get(&b, k, &($ptr_value_typ[]){ 0 }), v)) {')
}
.function {
fn_builder.writeln('\t\tif (*(voidptr*)map_get_1(&b, k, &(voidptr[]){ 0 }) != v) {')
fn_builder.writeln('\t\tif (*(voidptr*)map_get(&b, k, &(voidptr[]){ 0 }) != v) {')
}
else {
println(kind)
fn_builder.writeln('\t\tif (*($ptr_value_typ*)map_get_1(&b, k, &($ptr_value_typ[]){ 0 }) != v) {')
fn_builder.writeln('\t\tif (*($ptr_value_typ*)map_get(&b, k, &($ptr_value_typ[]){ 0 }) != v) {')
}
}
fn_builder.writeln('\t\t\treturn false;')

View File

@ -2183,9 +2183,9 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
left_type := assign_stmt.left_types[i]
left_sym := g.table.get_type_symbol(left_type)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_$left.pos.pos')
g.write(' = *(voidptr*)map_get_1(')
g.write(' = *(voidptr*)map_get(')
} else {
g.write('$styp _var_$left.pos.pos = *($styp*)map_get_1(')
g.write('$styp _var_$left.pos.pos = *($styp*)map_get(')
}
if !left.left_type.is_ptr() {
g.write('ADDR(map, ')
@ -4118,9 +4118,9 @@ fn (mut g Gen) map_init(node ast.MapInit) {
}
if size > 0 {
if value_typ.kind == .function {
g.write('new_map_init_2($hash_fn, $key_eq_fn, $clone_fn, $free_fn, $size, sizeof($key_typ_str), sizeof(voidptr), _MOV(($key_typ_str[$size]){')
g.write('new_map_init($hash_fn, $key_eq_fn, $clone_fn, $free_fn, $size, sizeof($key_typ_str), sizeof(voidptr), _MOV(($key_typ_str[$size]){')
} else {
g.write('new_map_init_2($hash_fn, $key_eq_fn, $clone_fn, $free_fn, $size, sizeof($key_typ_str), sizeof($value_typ_str), _MOV(($key_typ_str[$size]){')
g.write('new_map_init($hash_fn, $key_eq_fn, $clone_fn, $free_fn, $size, sizeof($key_typ_str), sizeof($value_typ_str), _MOV(($key_typ_str[$size]){')
}
for expr in node.keys {
g.expr(expr)
@ -4140,7 +4140,7 @@ fn (mut g Gen) map_init(node ast.MapInit) {
}
g.write('}))')
} else {
g.write('new_map_2(sizeof($key_typ_str), sizeof($value_typ_str), $hash_fn, $key_eq_fn, $clone_fn, $free_fn)')
g.write('new_map(sizeof($key_typ_str), sizeof($value_typ_str), $hash_fn, $key_eq_fn, $clone_fn, $free_fn)')
}
if g.is_shared {
g.write('}, sizeof($shared_styp))')
@ -5751,7 +5751,7 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
info := sym.map_info()
key_typ := g.table.get_type_symbol(info.key_type)
hash_fn, key_eq_fn, clone_fn, free_fn := g.map_fn_ptrs(key_typ)
return 'new_map_2(sizeof(${g.typ(info.key_type)}), sizeof(${g.typ(info.value_type)}), $hash_fn, $key_eq_fn, $clone_fn, $free_fn)'
return 'new_map(sizeof(${g.typ(info.key_type)}), sizeof(${g.typ(info.value_type)}), $hash_fn, $key_eq_fn, $clone_fn, $free_fn)'
}
// User struct defined in another module.
// if typ.contains('__') {

View File

@ -303,7 +303,7 @@ static void* g_live_info = NULL;
// copy something to the heap
#define HEAP(type, expr) ((type*)memdup((void*)&((type[]){expr}[0]), sizeof(type)))
#define _PUSH_MANY(arr, val, tmp, tmp_typ) {tmp_typ tmp = (val); array_push_many(arr, tmp.data, tmp.len);}
#define _IN_MAP(val, m) map_exists_1(m, val)
#define _IN_MAP(val, m) map_exists(m, val)
// unsigned/signed comparisons
static inline bool _us32_gt(uint32_t a, int32_t b) { return a > INT32_MAX || (int32_t)a > b; }

View File

@ -630,7 +630,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
}
} else if left_sym.kind == .map {
if node.name == 'keys' {
name = 'map_keys_1'
name = 'map_keys'
}
}
if g.pref.obfuscate && g.cur_mod.name == 'main' && name.starts_with('main__')

View File

@ -302,12 +302,12 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
if g.is_assign_lhs && !g.is_arraymap_set && !get_and_set_types {
if g.assign_op == .assign || info.value_type == ast.string_type {
g.is_arraymap_set = true
g.write('map_set_1(')
g.write('map_set(')
} else {
if node.is_setter {
g.write('(*(($elem_type_str*)map_get_and_set_1(')
g.write('(*(($elem_type_str*)map_get_and_set(')
} else {
g.write('(*(($elem_type_str*)map_get_1(')
g.write('(*(($elem_type_str*)map_get(')
}
}
if !left_is_ptr || node.left_type.has_flag(.shared_f) {
@ -344,9 +344,9 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
|| (g.is_assign_lhs && !g.is_arraymap_set && get_and_set_types) {
zero := g.type_default(info.value_type)
if node.is_setter {
g.write('(*($elem_type_str*)map_get_and_set_1(')
g.write('(*($elem_type_str*)map_get_and_set(')
} else {
g.write('(*($elem_type_str*)map_get_1(')
g.write('(*($elem_type_str*)map_get(')
}
if !left_is_ptr {
g.write('&')
@ -368,18 +368,18 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
tmp_opt := if gen_or { g.new_tmp_var() } else { '' }
tmp_opt_ptr := if gen_or { g.new_tmp_var() } else { '' }
if gen_or {
g.write('$elem_type_str* $tmp_opt_ptr = ($elem_type_str*)/*ee elem_ptr_typ */(map_get_1_check(')
g.write('$elem_type_str* $tmp_opt_ptr = ($elem_type_str*)/*ee elem_ptr_typ */(map_get_check(')
} else {
if g.is_fn_index_call {
if elem_typ.info is ast.FnType {
g.write('((')
g.write_fn_ptr_decl(&elem_typ.info, '')
g.write(')(*(voidptr*)map_get_1(')
g.write(')(*(voidptr*)map_get(')
}
} else if elem_typ.kind == .function {
g.write('(*(voidptr*)map_get_1(')
g.write('(*(voidptr*)map_get(')
} else {
g.write('(*($elem_type_str*)map_get_1(')
g.write('(*($elem_type_str*)map_get(')
}
}
if !left_is_ptr || node.left_type.has_flag(.shared_f) {

View File

@ -270,13 +270,13 @@ fn (mut g Gen) decode_map(key_type ast.Type, value_type ast.Type) string {
if(!cJSON_IsObject(root) && !cJSON_IsNull(root)) {
return (Option_Map_${styp}_$styp_v){ .state = 2, .err = v_error( string_add(_SLIT("Json element is not an object: "), tos2((byteptr)cJSON_PrintUnformatted(root))) )};
}
res = new_map_2(sizeof($styp), sizeof($styp_v), $hash_fn, $key_eq_fn, $clone_fn, $free_fn);
res = new_map(sizeof($styp), sizeof($styp_v), $hash_fn, $key_eq_fn, $clone_fn, $free_fn);
cJSON *jsval = NULL;
cJSON_ArrayForEach(jsval, root)
{
$s
string key = tos2((byteptr)jsval->string);
map_set_1(&res, &key, &val);
map_set(&res, &key, &val);
}
'
}
@ -299,7 +299,7 @@ fn (mut g Gen) encode_map(key_type ast.Type, value_type ast.Type) string {
Array_$styp $keys_tmp = map_keys(&val);
for (int i = 0; i < ${keys_tmp}.len; ++i) {
$key
cJSON_AddItemToObject(o, (char*) key.str, $fn_name_v ( *($styp_v*) map_get_1(&val, &key, &($styp_v[]) { $zero } ) ) );
cJSON_AddItemToObject(o, (char*) key.str, $fn_name_v ( *($styp_v*) map_get(&val, &key, &($styp_v[]) { $zero } ) ) );
}
array_free(&$keys_tmp);
'

View File

@ -178,7 +178,7 @@ pub fn mark_used(mut table ast.Table, pref &pref.Preferences, ast_files []ast.Fi
if mfn.is_method {
method_receiver_typename = table.type_to_str(mfn.receiver.typ)
}
if k in ['new_map_2', 'new_map_init_2', 'map_hash_string']
if k in ['new_map', 'new_map_init', 'map_hash_string']
|| method_receiver_typename == '&map' || method_receiver_typename == '&DenseArray'
|| k.starts_with('map_') {
walker.fn_decl(mut mfn)