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

View File

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