mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
map: cleanup (#7621)
This commit is contained in:
@@ -308,11 +308,29 @@ fn map_free_string(pkey voidptr) {
|
||||
fn map_free_nop(_ voidptr) {
|
||||
}
|
||||
|
||||
// bootstrap
|
||||
fn new_map_1(value_bytes int) map {
|
||||
return new_map(int(sizeof(string)), value_bytes)
|
||||
fn new_map_2(key_bytes int, value_bytes int, hash_fn MapHashFn, key_eq_fn MapEqFn, clone_fn MapCloneFn, free_fn MapFreeFn) map {
|
||||
metasize := int(sizeof(u32) * (init_capicity + extra_metas_inc))
|
||||
// for now assume anything bigger than a pointer is a string
|
||||
has_string_keys := key_bytes > sizeof(voidptr)
|
||||
return map{
|
||||
key_bytes: key_bytes
|
||||
value_bytes: value_bytes
|
||||
even_index: init_even_index
|
||||
cached_hashbits: max_cached_hashbits
|
||||
shift: init_log_capicity
|
||||
key_values: new_dense_array(key_bytes, value_bytes)
|
||||
metas: &u32(vcalloc(metasize))
|
||||
extra_metas: extra_metas_inc
|
||||
len: 0
|
||||
has_string_keys: has_string_keys
|
||||
hash_fn: hash_fn
|
||||
key_eq_fn: key_eq_fn
|
||||
clone_fn: clone_fn
|
||||
free_fn: free_fn
|
||||
}
|
||||
}
|
||||
|
||||
// delete this
|
||||
fn new_map(key_bytes int, value_bytes int) map {
|
||||
metasize := int(sizeof(u32) * (init_capicity + extra_metas_inc))
|
||||
// for now assume anything bigger than a pointer is a string
|
||||
@@ -372,10 +390,22 @@ fn new_map(key_bytes int, value_bytes int) map {
|
||||
}
|
||||
}
|
||||
|
||||
fn new_map_init(n int, value_bytes int, keys &string, values voidptr) map {
|
||||
return new_map_init_1(n, int(sizeof(string)), value_bytes, keys, values)
|
||||
fn new_map_init_2(hash_fn MapHashFn, key_eq_fn MapEqFn, clone_fn MapCloneFn, free_fn MapFreeFn, n int, key_bytes int, value_bytes int, keys voidptr, values voidptr) map {
|
||||
mut out := new_map_2(key_bytes, value_bytes, hash_fn, key_eq_fn, clone_fn, free_fn)
|
||||
// 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
|
||||
}
|
||||
|
||||
// delete this
|
||||
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
|
||||
@@ -454,11 +484,6 @@ fn (mut m map) ensure_extra_metas(probe_count u32) {
|
||||
}
|
||||
}
|
||||
|
||||
// bootstrap
|
||||
fn (mut m map) set(key string, value voidptr) {
|
||||
m.set_1(&key, value)
|
||||
}
|
||||
|
||||
// Insert new element to the map. The element is inserted if its key is
|
||||
// not equivalent to the key of any other element already in the container.
|
||||
// If the key already exists, its value is changed to the value of the new element.
|
||||
@@ -554,10 +579,6 @@ fn (mut m map) cached_rehash(old_cap u32) {
|
||||
unsafe { free(old_metas) }
|
||||
}
|
||||
|
||||
fn (mut m map) get_and_set(key string, zero voidptr) voidptr {
|
||||
return m.get_and_set_1(&key, zero)
|
||||
}
|
||||
|
||||
// This method is used for assignment operators. If the argument-key
|
||||
// does not exist in the map, it's added to the map along with the zero/default value.
|
||||
// If the key exists, its respective value is returned.
|
||||
@@ -585,10 +606,6 @@ fn (mut m map) get_and_set_1(key voidptr, zero voidptr) voidptr {
|
||||
return voidptr(0)
|
||||
}
|
||||
|
||||
fn (m map) get(key string, zero voidptr) voidptr {
|
||||
return m.get_1(&key, zero)
|
||||
}
|
||||
|
||||
// If `key` matches the key of an element in the container,
|
||||
// the method returns a reference to its mapped value.
|
||||
// If not, a zero/default value is returned.
|
||||
@@ -611,6 +628,7 @@ fn (m &map) get_1(key voidptr, zero voidptr) voidptr {
|
||||
return zero
|
||||
}
|
||||
|
||||
// delete this
|
||||
fn (m map) exists(key string) bool {
|
||||
return m.exists_1(&key)
|
||||
}
|
||||
@@ -646,6 +664,7 @@ fn (mut d DenseArray) delete(i int) {
|
||||
}
|
||||
}
|
||||
|
||||
// delete this
|
||||
pub fn (mut m map) delete(key string) {
|
||||
m.delete_1(&key)
|
||||
}
|
||||
@@ -690,6 +709,7 @@ pub fn (mut m map) delete_1(key voidptr) {
|
||||
}
|
||||
|
||||
// bootstrap
|
||||
// delete this
|
||||
pub fn (m &map) keys() []string {
|
||||
mut keys := []string{len: m.len}
|
||||
mut item := unsafe { byteptr(keys.data) }
|
||||
|
||||
Reference in New Issue
Block a user