1
0
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:
ka-weihe 2021-04-12 16:22:02 +02:00 committed by GitHub
parent b2c16ced57
commit 491fe49af6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 46 deletions

View File

@ -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 { 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)) metasize := int(sizeof(u32) * (init_capicity + extra_metas_inc))
// for now assume anything bigger than a pointer is a string // for now assume anything bigger than a pointer is a string
has_string_keys := key_bytes > sizeof(voidptr) 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 { 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, mut out := new_map(key_bytes, value_bytes, hash_fn, key_eq_fn, clone_fn, free_fn)
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 // TODO pre-allocate n slots
mut pkey := &byte(keys) mut pkey := &byte(keys)
mut pval := &byte(values) mut pval := &byte(values)
for _ in 0 .. n { for _ in 0 .. n {
unsafe { unsafe {
out.set_1(pkey, pval) out.set(pkey, pval)
pkey = pkey + key_bytes pkey = pkey + key_bytes
pval = pval + value_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 // 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. // 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. // 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) load_factor := f32(m.len << 1) / f32(m.even_index)
if load_factor > max_load_factor { if load_factor > max_load_factor {
m.expand() m.expand()
@ -546,14 +533,10 @@ fn (mut m map) cached_rehash(old_cap u32) {
unsafe { free(old_metas) } 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 // 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. // 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. // 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 { for {
mut index, mut meta := m.key_to_index(key) mut index, mut meta := m.key_to_index(key)
for { 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 // Key not found, insert key with zero-value
m.set_1(key, zero) m.set(key, zero)
} }
assert false assert false
return voidptr(0) 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, // If `key` matches the key of an element in the container,
// the method returns a reference to its mapped value. // the method returns a reference to its mapped value.
// If not, a zero/default value is returned. // 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) mut index, mut meta := m.key_to_index(key)
for { for {
if meta == unsafe { m.metas[index] } { if meta == unsafe { m.metas[index] } {
@ -605,15 +584,11 @@ fn (m &map) get_1(key voidptr, zero voidptr) voidptr {
return zero 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, // If `key` matches the key of an element in the container,
// the method returns a reference to its mapped value. // the method returns a reference to its mapped value.
// If not, a zero pointer is returned. // If not, a zero pointer is returned.
// This is used in `x := m['key'] or { ... }` // 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) mut index, mut meta := m.key_to_index(key)
for { for {
if meta == unsafe { m.metas[index] } { if meta == unsafe { m.metas[index] } {
@ -633,12 +608,8 @@ fn (m &map) get_1_check(key voidptr) voidptr {
return 0 return 0
} }
fn (m &map) exists(key voidptr) bool {
return m.exists_1(key)
}
// Checks whether a particular key exists in the map. // 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) mut index, mut meta := m.key_to_index(key)
for { for {
if meta == unsafe { m.metas[index] } { 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. // 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 keys := __new_array(m.len, 0, m.key_bytes)
mut item := unsafe { &byte(keys.data) } mut item := unsafe { &byte(keys.data) }
if m.key_values.deletes == 0 { if m.key_values.deletes == 0 {

View File

@ -10,9 +10,9 @@ vlib/v/checker/tests/import_symbol_fn_private_err.vv:3:3: error: function `time.
| ~~~~~~~~~~~~ | ~~~~~~~~~~~~
4 | } 4 | }
5 | 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 | 5 |
6 | fn method() { 6 | fn method() {
7 | _ = map{'h':2}.exists_1('h') 7 | _ = map{'h':2}.exists('h')
| ~~~~~~~~~~~~~ | ~~~~~~~~~~~
8 | } 8 | }

View File

@ -4,5 +4,5 @@ fn main() {
} }
fn method() { fn method() {
_ = map{'h':2}.exists_1('h') _ = map{'h':2}.exists('h')
} }