mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
map: encapsulate key clone operation on voidptr (#7327)
This commit is contained in:
parent
665e6cc957
commit
7c8fa62cc2
@ -139,6 +139,14 @@ fn (d &DenseArray) has_index(i int) bool {
|
|||||||
return d.deletes == 0 || unsafe {d.all_deleted[i]} == 0
|
return d.deletes == 0 || unsafe {d.all_deleted[i]} == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[inline]
|
||||||
|
fn (d &DenseArray) clone_key(dest voidptr, pkey voidptr) {
|
||||||
|
unsafe {
|
||||||
|
s := (*&string(pkey)).clone()
|
||||||
|
C.memcpy(dest, &s, d.key_bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Push element to array and return index
|
// Push element to array and return index
|
||||||
// The growth-factor is roughly 1.125 `(x + (x >> 3))`
|
// The growth-factor is roughly 1.125 `(x + (x >> 3))`
|
||||||
[inline]
|
[inline]
|
||||||
@ -159,7 +167,7 @@ fn (mut d DenseArray) push(key voidptr, value voidptr) int {
|
|||||||
d.all_deleted[push_index] = 0
|
d.all_deleted[push_index] = 0
|
||||||
}
|
}
|
||||||
ptr := d.key(push_index)
|
ptr := d.key(push_index)
|
||||||
C.memcpy(ptr, key, d.key_bytes)
|
d.clone_key(ptr, key)
|
||||||
C.memcpy(byteptr(ptr) + d.key_bytes, value, d.value_bytes)
|
C.memcpy(byteptr(ptr) + d.key_bytes, value, d.value_bytes)
|
||||||
}
|
}
|
||||||
d.len++
|
d.len++
|
||||||
@ -316,8 +324,7 @@ fn (mut m map) ensure_extra_metas(probe_count u32) {
|
|||||||
// 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(k string, value voidptr) {
|
fn (mut m map) set(key string, value voidptr) {
|
||||||
key := k.clone()
|
|
||||||
load_factor := f32(m.len << 1) / f32(m.cap)
|
load_factor := f32(m.len << 1) / f32(m.cap)
|
||||||
if load_factor > max_load_factor {
|
if load_factor > max_load_factor {
|
||||||
m.expand()
|
m.expand()
|
||||||
@ -519,12 +526,14 @@ pub fn (mut m map) delete(key string) {
|
|||||||
// Returns all keys in the map.
|
// Returns all keys in the map.
|
||||||
pub fn (m &map) keys() []string {
|
pub fn (m &map) keys() []string {
|
||||||
mut keys := []string{len: m.len}
|
mut keys := []string{len: m.len}
|
||||||
mut j := 0
|
mut item := unsafe {byteptr(keys.data)}
|
||||||
if m.key_values.deletes == 0 {
|
if m.key_values.deletes == 0 {
|
||||||
for i := 0; i < m.key_values.len; i++ {
|
for i := 0; i < m.key_values.len; i++ {
|
||||||
pkey := unsafe {&string(m.key_values.key(i))}
|
unsafe {
|
||||||
keys[j] = pkey.clone()
|
pkey := m.key_values.key(i)
|
||||||
j++
|
m.key_values.clone_key(item, pkey)
|
||||||
|
item += m.key_bytes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
@ -532,15 +541,17 @@ pub fn (m &map) keys() []string {
|
|||||||
if !m.key_values.has_index(i) {
|
if !m.key_values.has_index(i) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
pkey := unsafe {&string(m.key_values.key(i))}
|
unsafe {
|
||||||
keys[j] = pkey.clone()
|
pkey := m.key_values.key(i)
|
||||||
j++
|
m.key_values.clone_key(item, pkey)
|
||||||
|
item += m.key_bytes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe]
|
[unsafe]
|
||||||
pub fn (d DenseArray) clone() DenseArray {
|
pub fn (d &DenseArray) clone() DenseArray {
|
||||||
res := DenseArray{
|
res := DenseArray{
|
||||||
key_bytes: d.key_bytes
|
key_bytes: d.key_bytes
|
||||||
value_bytes: d.value_bytes
|
value_bytes: d.value_bytes
|
||||||
|
Loading…
Reference in New Issue
Block a user