From 2147d8785b8587be24e414f880897e963e72008c Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Mon, 21 Dec 2020 05:03:59 +0000 Subject: [PATCH] map: add functions new_map/new_map_init_1 with key_bytes parameter (#7424) --- vlib/builtin/map.v | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index b36a3f257a..f9a174dde0 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -227,9 +227,13 @@ pub mut: len int } +// bootstrap 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)) - key_bytes := int(sizeof(string)) return map{ key_bytes: key_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 { - mut out := new_map_1(value_bytes) - for i in 0 .. n { - unsafe {out.set(keys[i], byteptr(values) + i * value_bytes)} + return new_map_init_1(n, int(sizeof(string)), value_bytes, keys, values) +} + +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 }