mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
map: rename methods (part 2) (#9698)
This commit is contained in:
parent
b2c16ced57
commit
491fe49af6
@ -327,10 +327,6 @@ fn map_free_nop(_ voidptr) {
|
||||
}
|
||||
|
||||
fn new_map(key_bytes int, value_bytes int, hash_fn MapHashFn, key_eq_fn MapEqFn, clone_fn MapCloneFn, free_fn MapFreeFn) map {
|
||||
return new_map_2(key_bytes, value_bytes, hash_fn, key_eq_fn, clone_fn, free_fn)
|
||||
}
|
||||
|
||||
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)
|
||||
@ -353,18 +349,13 @@ fn new_map_2(key_bytes int, value_bytes int, hash_fn MapHashFn, key_eq_fn MapEqF
|
||||
}
|
||||
|
||||
fn new_map_init(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 {
|
||||
return new_map_init_2(hash_fn, key_eq_fn, clone_fn, free_fn, n, key_bytes, 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)
|
||||
mut out := new_map(key_bytes, value_bytes, hash_fn, key_eq_fn, clone_fn, free_fn)
|
||||
// TODO pre-allocate n slots
|
||||
mut pkey := &byte(keys)
|
||||
mut pval := &byte(values)
|
||||
for _ in 0 .. n {
|
||||
unsafe {
|
||||
out.set_1(pkey, pval)
|
||||
out.set(pkey, pval)
|
||||
pkey = pkey + key_bytes
|
||||
pval = pval + value_bytes
|
||||
}
|
||||
@ -445,14 +436,10 @@ fn (mut m map) ensure_extra_metas(probe_count u32) {
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut m map) set(key voidptr, 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.
|
||||
fn (mut m map) set_1(key voidptr, value voidptr) {
|
||||
fn (mut m map) set(key voidptr, value voidptr) {
|
||||
load_factor := f32(m.len << 1) / f32(m.even_index)
|
||||
if load_factor > max_load_factor {
|
||||
m.expand()
|
||||
@ -546,14 +533,10 @@ fn (mut m map) cached_rehash(old_cap u32) {
|
||||
unsafe { free(old_metas) }
|
||||
}
|
||||
|
||||
fn (mut m map) get_and_set(key voidptr, 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.
|
||||
fn (mut m map) get_and_set_1(key voidptr, zero voidptr) voidptr {
|
||||
fn (mut m map) get_and_set(key voidptr, zero voidptr) voidptr {
|
||||
for {
|
||||
mut index, mut meta := m.key_to_index(key)
|
||||
for {
|
||||
@ -572,20 +555,16 @@ fn (mut m map) get_and_set_1(key voidptr, zero voidptr) voidptr {
|
||||
}
|
||||
}
|
||||
// Key not found, insert key with zero-value
|
||||
m.set_1(key, zero)
|
||||
m.set(key, zero)
|
||||
}
|
||||
assert false
|
||||
return voidptr(0)
|
||||
}
|
||||
|
||||
fn (m &map) get(key voidptr, 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.
|
||||
fn (m &map) get_1(key voidptr, zero voidptr) voidptr {
|
||||
fn (m &map) get(key voidptr, zero voidptr) voidptr {
|
||||
mut index, mut meta := m.key_to_index(key)
|
||||
for {
|
||||
if meta == unsafe { m.metas[index] } {
|
||||
@ -605,15 +584,11 @@ fn (m &map) get_1(key voidptr, zero voidptr) voidptr {
|
||||
return zero
|
||||
}
|
||||
|
||||
fn (m &map) get_check(key voidptr) voidptr {
|
||||
return m.get_1_check(key)
|
||||
}
|
||||
|
||||
// If `key` matches the key of an element in the container,
|
||||
// the method returns a reference to its mapped value.
|
||||
// If not, a zero pointer is returned.
|
||||
// This is used in `x := m['key'] or { ... }`
|
||||
fn (m &map) get_1_check(key voidptr) voidptr {
|
||||
fn (m &map) get_check(key voidptr) voidptr {
|
||||
mut index, mut meta := m.key_to_index(key)
|
||||
for {
|
||||
if meta == unsafe { m.metas[index] } {
|
||||
@ -633,12 +608,8 @@ fn (m &map) get_1_check(key voidptr) voidptr {
|
||||
return 0
|
||||
}
|
||||
|
||||
fn (m &map) exists(key voidptr) bool {
|
||||
return m.exists_1(key)
|
||||
}
|
||||
|
||||
// Checks whether a particular key exists in the map.
|
||||
fn (m &map) exists_1(key voidptr) bool {
|
||||
fn (m &map) exists(key voidptr) bool {
|
||||
mut index, mut meta := m.key_to_index(key)
|
||||
for {
|
||||
if meta == unsafe { m.metas[index] } {
|
||||
@ -708,12 +679,8 @@ pub fn (mut m map) delete(key voidptr) {
|
||||
}
|
||||
}
|
||||
|
||||
fn (m &map) keys() array {
|
||||
return m.keys_1()
|
||||
}
|
||||
|
||||
// Returns all keys in the map.
|
||||
fn (m &map) keys_1() array {
|
||||
fn (m &map) keys() array {
|
||||
mut keys := __new_array(m.len, 0, m.key_bytes)
|
||||
mut item := unsafe { &byte(keys.data) }
|
||||
if m.key_values.deletes == 0 {
|
||||
|
@ -10,9 +10,9 @@ vlib/v/checker/tests/import_symbol_fn_private_err.vv:3:3: error: function `time.
|
||||
| ~~~~~~~~~~~~
|
||||
4 | }
|
||||
5 |
|
||||
vlib/v/checker/tests/import_symbol_fn_private_err.vv:7:20: error: method `map[string]int.exists_1` is private
|
||||
vlib/v/checker/tests/import_symbol_fn_private_err.vv:7:20: error: method `map[string]int.exists` is private
|
||||
5 |
|
||||
6 | fn method() {
|
||||
7 | _ = map{'h':2}.exists_1('h')
|
||||
| ~~~~~~~~~~~~~
|
||||
7 | _ = map{'h':2}.exists('h')
|
||||
| ~~~~~~~~~~~
|
||||
8 | }
|
||||
|
@ -4,5 +4,5 @@ fn main() {
|
||||
}
|
||||
|
||||
fn method() {
|
||||
_ = map{'h':2}.exists_1('h')
|
||||
_ = map{'h':2}.exists('h')
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user