1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

Revert "map: fix map.clone, refactor, fix map_get_1, map_set_1 non-string keys (#7538)"

This reverts commit bcdf3ca0cf.
This commit is contained in:
Alexander Medvednikov 2020-12-27 12:04:08 +01:00
parent 0d43ff2453
commit 06e9951224
2 changed files with 22 additions and 33 deletions

View File

@ -139,10 +139,18 @@ fn (d &DenseArray) has_index(i int) bool {
return d.deletes == 0 || unsafe { d.all_deleted[i] } == 0
}
// Make space to append an element to array and return index
[inline]
fn (d &DenseArray) clone_key(dest voidptr, pkey voidptr) {
unsafe {
s := (*&string(pkey)).clone()
C.memcpy(dest, &s, d.key_bytes)
}
}
// Push element to array and return index
// The growth-factor is roughly 1.125 `(x + (x >> 3))`
[inline]
fn (mut d DenseArray) push() int {
fn (mut d DenseArray) push(key voidptr, value voidptr) int {
if d.cap == d.len {
d.cap += d.cap >> 3
unsafe {
@ -158,6 +166,9 @@ fn (mut d DenseArray) push() int {
if d.deletes != 0 {
d.all_deleted[push_index] = 0
}
ptr := d.key(push_index)
d.clone_key(ptr, key)
C.memcpy(byteptr(ptr) + d.key_bytes, value, d.value_bytes)
}
d.len++
return push_index
@ -270,14 +281,6 @@ fn (m &map) key_to_index(pkey voidptr) (u32, u32) {
return u32(index), u32(meta)
}
[inline]
fn (m &map) clone_key(dest voidptr, pkey voidptr) {
unsafe {
s := (*&string(pkey)).clone()
C.memcpy(dest, &s, m.key_bytes)
}
}
fn (m &map) free_key(pkey voidptr) {
(*&string(pkey)).free()
}
@ -366,12 +369,7 @@ fn (mut m map) set_1(key voidptr, value voidptr) {
index += 2
meta += probe_inc
}
kv_index := m.key_values.push()
unsafe {
pkey := m.key_values.key(kv_index)
m.clone_key(pkey, key)
C.memcpy(byteptr(pkey) + m.key_bytes, value, m.value_bytes)
}
kv_index := m.key_values.push(key, value)
m.meta_greater(index, meta, u32(kv_index))
m.len++
}
@ -582,7 +580,7 @@ pub fn (m &map) keys() []string {
}
unsafe {
pkey := m.key_values.key(i)
m.clone_key(item, pkey)
m.key_values.clone_key(item, pkey)
item += m.key_bytes
}
}
@ -597,7 +595,7 @@ pub fn (m &map) keys_1() array {
for i := 0; i < m.key_values.len; i++ {
unsafe {
pkey := m.key_values.key(i)
m.clone_key(item, pkey)
m.key_values.clone_key(item, pkey)
item += m.key_bytes
}
}
@ -609,14 +607,13 @@ pub fn (m &map) keys_1() array {
}
unsafe {
pkey := m.key_values.key(i)
m.clone_key(item, pkey)
m.key_values.clone_key(item, pkey)
item += m.key_bytes
}
}
return keys
}
// warning: only copies keys, does not clone
[unsafe]
pub fn (d &DenseArray) clone() DenseArray {
res := DenseArray{
@ -635,6 +632,7 @@ pub fn (d &DenseArray) clone() DenseArray {
}
res.data = memdup(d.data, d.cap * d.slot_bytes)
}
// FIXME clone each key
return res
}
@ -653,13 +651,6 @@ pub fn (m &map) clone() map {
len: m.len
}
unsafe { C.memcpy(res.metas, m.metas, metasize) }
// clone keys
for i in 0 .. m.key_values.len {
if !m.key_values.has_index(i) {
continue
}
m.clone_key(res.key_values.key(i), m.key_values.key(i))
}
return res
}

View File

@ -1726,7 +1726,6 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
g.writeln(');')
} else if sym.kind == .map {
info := sym.info as table.Map
skeytyp := g.typ(info.key_type)
styp := g.typ(info.value_type)
zero := g.type_default(info.value_type)
val_typ := g.table.get_type_symbol(info.value_type)
@ -1745,7 +1744,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} else {
g.expr(left.left)
}
g.write(', &($skeytyp[]){')
g.write(', &(string[]){')
g.expr(left.index)
g.write('}')
if val_typ.kind == .function {
@ -3885,7 +3884,6 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
}
} else if sym.kind == .map {
info := sym.info as table.Map
key_type_str := g.typ(info.key_type)
elem_type_str := g.typ(info.value_type)
elem_typ := g.table.get_type_symbol(info.value_type)
get_and_set_types := elem_typ.kind in [.struct_, .map]
@ -3907,7 +3905,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.write('.val')
}
}
g.write(', &($key_type_str[]){')
g.write(', &(string[]){')
g.expr(node.index)
g.write('}')
if elem_typ.kind == .function {
@ -3927,7 +3925,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.write('&')
}
g.expr(node.left)
g.write(', &($key_type_str[]){')
g.write(', &(string[]){')
g.expr(node.index)
g.write('}, &($elem_type_str[]){ $zero }))')
} else {
@ -3957,7 +3955,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.write('.val')
}
}
g.write('), &($key_type_str[]){')
g.write('), &(string[]){')
g.expr(node.index)
g.write('}')
if g.is_fn_index_call {