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

pull/7448/head
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
1 changed files with 19 additions and 4 deletions

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
} }