Skip to content

Commit

Permalink
Add non-string key function new_map_init_1
Browse files Browse the repository at this point in the history
  • Loading branch information
ntrel committed Dec 20, 2020
1 parent 2a02d1f commit c44aede
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions vlib/builtin/map.v
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,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)
return new_map_init_1<string>(n, value_bytes, keys, values)
}

fn new_map_init_1<T>(n int, value_bytes int, keys voidptr, values voidptr) map {
mut out := new_map<T>(value_bytes)
// TODO pre-allocate n slots
for i in 0 .. n {
unsafe {out.set(keys[i], byteptr(values) + i * value_bytes)}
mut pkey := byteptr(keys)
mut pval := byteptr(values)
for _ in 0 .. n {
unsafe {
out.set_1(pkey, pval)
pkey += out.key_bytes
pval += value_bytes
}
}
return out
}
Expand Down

0 comments on commit c44aede

Please sign in to comment.