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

map: add functions new_map/new_map_init_1 with key_bytes parameter (#7424)

This commit is contained in:
Nick Treleaven 2020-12-21 05:03:59 +00:00 committed by GitHub
parent 25dd983d97
commit 2147d8785b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -227,9 +227,13 @@ pub mut:
len int len int
} }
// bootstrap
fn new_map_1(value_bytes int) map { fn new_map_1(value_bytes int) map {
return new_map(int(sizeof(string)), value_bytes)
}
fn new_map(key_bytes int, value_bytes int) map {
metasize := int(sizeof(u32) * (init_capicity + extra_metas_inc)) metasize := int(sizeof(u32) * (init_capicity + extra_metas_inc))
key_bytes := int(sizeof(string))
return map{ return map{
key_bytes: key_bytes key_bytes: key_bytes
value_bytes: value_bytes value_bytes: value_bytes
@ -244,9 +248,20 @@ fn new_map_1(value_bytes int) map {
} }
fn new_map_init(n int, value_bytes int, keys &string, values voidptr) map { fn new_map_init(n int, value_bytes int, keys &string, values voidptr) map {
mut out := new_map_1(value_bytes) return new_map_init_1(n, int(sizeof(string)), value_bytes, keys, values)
for i in 0 .. n { }
unsafe {out.set(keys[i], byteptr(values) + i * value_bytes)}
fn new_map_init_1(n int, key_bytes int, value_bytes int, keys voidptr, values voidptr) map {
mut out := new_map(key_bytes, value_bytes)
// TODO pre-allocate n slots
mut pkey := byteptr(keys)
mut pval := byteptr(values)
for _ in 0 .. n {
unsafe {
out.set_1(pkey, pval)
pkey += key_bytes
pval += value_bytes
}
} }
return out return out
} }