1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/builtin/map.v

445 lines
10 KiB
V
Raw Normal View History

2020-02-03 07:00:36 +03:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-06-23 05:21:30 +03:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2020-01-24 22:13:59 +03:00
2019-06-22 21:20:28 +03:00
module builtin
2019-08-29 01:52:32 +03:00
import strings
2019-08-05 05:34:12 +03:00
2020-02-04 14:50:58 +03:00
// B-trees are balanced search trees with all leaves at
// the same level. B-trees are generally faster than
// binary search trees due to the better locality of
2020-01-24 22:13:59 +03:00
// reference, since multiple keys are stored in one node.
// The number for `degree` has been picked through vigor-
2020-02-04 14:50:58 +03:00
// ous benchmarking but can be changed to any number > 1.
2020-01-24 22:13:59 +03:00
// `degree` determines the size of each node.
const (
degree = 6
mid_index = degree - 1
max_size = 2 * degree - 1
children_bytes = sizeof(voidptr) * (max_size + 1)
)
2019-10-24 12:47:21 +03:00
pub struct map {
2020-01-24 22:13:59 +03:00
value_bytes int
mut:
root &mapnode
pub mut:
size int
2019-06-22 21:20:28 +03:00
}
2019-08-29 01:52:32 +03:00
struct mapnode {
2020-01-24 22:13:59 +03:00
mut:
2020-02-04 14:50:58 +03:00
keys [11]string // TODO: Should use `max_size`
2020-01-24 22:13:59 +03:00
values [11]voidptr // TODO: Should use `max_size`
children &voidptr
size int
2019-06-22 21:20:28 +03:00
}
2020-01-24 22:13:59 +03:00
fn new_map(n, value_bytes int) map { // TODO: Remove `n`
return map {
value_bytes: value_bytes
root: new_node()
size: 0
2019-06-22 21:20:28 +03:00
}
}
2020-01-24 22:13:59 +03:00
fn new_map_init(n, value_bytes int, keys &string, values voidptr) map {
mut out := new_map(n, value_bytes)
for i in 0 .. n {
out.set(keys[i], values + i * value_bytes)
2019-08-03 10:44:08 +03:00
}
2020-01-24 22:13:59 +03:00
return out
2019-08-29 01:52:32 +03:00
}
2019-08-03 10:44:08 +03:00
2020-01-24 22:13:59 +03:00
// The tree is initialized with an empty node as root to
// avoid having to check whether the root is null for
// each insertion.
fn new_node() &mapnode {
return &mapnode {
children: 0
size: 0
}
2019-06-22 21:20:28 +03:00
}
2020-01-24 22:13:59 +03:00
// This implementation does proactive insertion, meaning
2020-02-04 14:50:58 +03:00
// that splits are done top-down and not bottom-up.
2020-01-24 22:13:59 +03:00
fn (m mut map) set(key string, value voidptr) {
mut node := m.root
mut child_index := 0
mut parent := &mapnode(0)
for {
if node.size == max_size {
if isnil(parent) {
parent = new_node()
m.root = parent
}
parent.split_child(child_index, mut node)
if key == parent.keys[child_index] {
C.memcpy(parent.values[child_index], value, m.value_bytes)
return
}
node = if key < parent.keys[child_index] {
&mapnode(parent.children[child_index])
} else {
&mapnode(parent.children[child_index + 1])
}
2019-12-19 23:52:45 +03:00
}
2020-01-24 22:13:59 +03:00
mut i := 0
for i < node.size && key > node.keys[i] { i++ }
if i != node.size && key == node.keys[i] {
C.memcpy(node.values[i], value, m.value_bytes)
return
}
if isnil(node.children) {
mut j := node.size - 1
for j >= 0 && key < node.keys[j] {
node.keys[j + 1] = node.keys[j]
node.values[j + 1] = node.values[j]
j--
}
node.keys[j + 1] = key
node.values[j + 1] = malloc(m.value_bytes)
C.memcpy(node.values[j + 1], value, m.value_bytes)
node.size++
m.size++
2020-01-24 22:13:59 +03:00
return
}
2020-01-24 22:13:59 +03:00
parent = node
child_index = i
node = &mapnode(node.children[child_index])
}
}
fn (n mut mapnode) split_child(child_index int, y mut mapnode) {
mut z := new_node()
z.size = mid_index
y.size = mid_index
for j := mid_index - 1; j >= 0; j-- {
z.keys[j] = y.keys[j + degree]
z.values[j] = y.values[j + degree]
}
if !isnil(y.children) {
z.children = &voidptr(malloc(children_bytes))
2020-02-04 19:44:39 +03:00
for jj := degree - 1; jj >= 0; jj-- {
z.children[jj] = y.children[jj + degree]
2019-08-29 01:52:32 +03:00
}
}
2020-01-24 22:13:59 +03:00
if isnil(n.children) {
n.children = &voidptr(malloc(children_bytes))
2019-12-19 23:52:45 +03:00
}
2020-01-24 22:13:59 +03:00
n.children[n.size + 1] = n.children[n.size]
for j := n.size; j > child_index; j-- {
n.keys[j] = n.keys[j - 1]
n.values[j] = n.values[j - 1]
n.children[j] = n.children[j - 1]
2019-08-29 01:52:32 +03:00
}
2020-01-24 22:13:59 +03:00
n.keys[child_index] = y.keys[mid_index]
n.values[child_index] = y.values[mid_index]
n.children[child_index] = voidptr(y)
n.children[child_index + 1] = voidptr(z)
n.size++
2019-08-29 01:52:32 +03:00
}
2020-01-24 22:13:59 +03:00
fn (m map) get(key string, out voidptr) bool {
mut node := m.root
for {
mut i := node.size - 1
for i >= 0 && key < node.keys[i] { i-- }
if i != -1 && key == node.keys[i] {
C.memcpy(out, node.values[i], m.value_bytes)
return true
2019-12-19 23:52:45 +03:00
}
2020-01-24 22:13:59 +03:00
if isnil(node.children) {
break
2019-08-29 01:52:32 +03:00
}
2020-01-24 22:13:59 +03:00
node = &mapnode(node.children[i + 1])
2019-08-29 01:52:32 +03:00
}
2020-01-24 22:13:59 +03:00
return false
}
fn (m map) exists(key string) bool {
if isnil(m.root) { // TODO: find out why root can be nil
return false
}
mut node := m.root
for {
mut i := node.size - 1
for i >= 0 && key < node.keys[i] { i-- }
if i != -1 && key == node.keys[i] {
return true
2019-12-19 23:52:45 +03:00
}
2020-01-24 22:13:59 +03:00
if isnil(node.children) {
break
2019-08-29 01:52:32 +03:00
}
2020-01-24 22:13:59 +03:00
node = &mapnode(node.children[i + 1])
}
2020-01-24 22:13:59 +03:00
return false
2019-08-29 01:52:32 +03:00
}
2019-07-14 12:01:32 +03:00
2020-02-04 14:50:58 +03:00
fn (n mapnode) find_key(k string) int {
2020-01-24 22:13:59 +03:00
mut idx := 0
for idx < n.size && n.keys[idx] < k {
idx++
}
2020-01-24 22:13:59 +03:00
return idx
}
fn (n mut mapnode) remove_key(k string) bool {
idx := n.find_key(k)
if idx < n.size && n.keys[idx] == k {
if isnil(n.children) {
n.remove_from_leaf(idx)
} else {
n.remove_from_non_leaf(idx)
2019-08-29 01:52:32 +03:00
}
2020-01-24 22:13:59 +03:00
return true
} else {
if isnil(n.children) {
return false
2019-12-30 08:57:56 +03:00
}
2020-01-24 22:13:59 +03:00
flag := if idx == n.size {true} else {false}
if (&mapnode(n.children[idx])).size < degree {
n.fill(idx)
}
if flag && idx > n.size {
return (&mapnode(n.children[idx - 1])).remove_key(k)
} else {
return (&mapnode(n.children[idx])).remove_key(k)
2019-12-30 08:57:56 +03:00
}
2019-06-22 21:20:28 +03:00
}
}
2019-12-19 23:52:45 +03:00
2020-01-24 22:13:59 +03:00
fn (n mut mapnode) remove_from_leaf(idx int) {
for i := idx + 1; i < n.size; i++ {
n.keys[i - 1] = n.keys[i]
n.values[i - 1] = n.values[i]
}
2020-01-24 22:13:59 +03:00
n.size--
2019-08-29 01:52:32 +03:00
}
2019-06-22 21:20:28 +03:00
2020-01-24 22:13:59 +03:00
fn (n mut mapnode) remove_from_non_leaf(idx int) {
k := n.keys[idx]
if &mapnode(n.children[idx]).size >= degree {
mut current := &mapnode(n.children[idx])
for !isnil(current.children) {
current = &mapnode(current.children[current.size])
}
predecessor := current.keys[current.size - 1]
n.keys[idx] = predecessor
n.values[idx] = current.values[current.size - 1]
(&mapnode(n.children[idx])).remove_key(predecessor)
} else if &mapnode(n.children[idx + 1]).size >= degree {
mut current := &mapnode(n.children[idx + 1])
for !isnil(current.children) {
current = &mapnode(current.children[0])
}
successor := current.keys[0]
n.keys[idx] = successor
n.values[idx] = current.values[0]
(&mapnode(n.children[idx + 1])).remove_key(successor)
} else {
n.merge(idx)
(&mapnode(n.children[idx])).remove_key(k)
2019-08-29 01:52:32 +03:00
}
2019-06-22 21:20:28 +03:00
}
2019-12-30 08:57:56 +03:00
2020-01-24 22:13:59 +03:00
fn (n mut mapnode) fill(idx int) {
if idx != 0 && &mapnode(n.children[idx - 1]).size >= degree {
n.borrow_from_prev(idx)
} else if idx != n.size && &mapnode(n.children[idx + 1]).size >= degree {
n.borrow_from_next(idx)
} else if idx != n.size {
n.merge(idx)
} else {
n.merge(idx - 1)
2019-12-30 08:57:56 +03:00
}
2020-01-24 22:13:59 +03:00
}
fn (n mut mapnode) borrow_from_prev(idx int) {
mut child := &mapnode(n.children[idx])
mut sibling := &mapnode(n.children[idx - 1])
for i := child.size - 1; i >= 0; i-- {
2020-02-04 14:50:58 +03:00
child.keys[i + 1] = child.keys[i]
child.values[i + 1] = child.values[i]
2020-01-24 22:13:59 +03:00
}
2020-02-04 14:50:58 +03:00
if !isnil(child.children) {
2020-01-24 22:13:59 +03:00
for i := child.size; i >= 0; i-- {
2020-02-04 14:50:58 +03:00
child.children[i + 1] = child.children[i]
2020-01-24 22:13:59 +03:00
}
2019-08-29 01:52:32 +03:00
}
2020-02-04 14:50:58 +03:00
child.keys[0] = n.keys[idx - 1]
child.values[0] = n.values[idx - 1]
2020-01-24 22:13:59 +03:00
if !isnil(child.children) {
child.children[0] = sibling.children[sibling.size]
2019-08-29 01:52:32 +03:00
}
2020-01-24 22:13:59 +03:00
n.keys[idx - 1] = sibling.keys[sibling.size - 1]
n.values[idx - 1] = sibling.values[sibling.size - 1]
2020-02-04 14:50:58 +03:00
child.size++
sibling.size--
2019-08-29 01:52:32 +03:00
}
2020-01-24 22:13:59 +03:00
fn (n mut mapnode) borrow_from_next(idx int) {
mut child := &mapnode(n.children[idx])
mut sibling := &mapnode(n.children[idx + 1])
child.keys[child.size] = n.keys[idx]
child.values[child.size] = n.values[idx]
if !isnil(child.children) {
child.children[child.size + 1] = sibling.children[0]
}
n.keys[idx] = sibling.keys[0]
n.values[idx] = sibling.values[0]
for i := 1; i < sibling.size; i++ {
sibling.keys[i - 1] = sibling.keys[i]
sibling.values[i - 1] = sibling.values[i]
}
if !isnil(sibling.children) {
for i := 1; i <= sibling.size; i++ {
sibling.children[i - 1] = sibling.children[i]
}
}
2020-01-24 22:13:59 +03:00
child.size++
sibling.size--
2019-08-29 01:52:32 +03:00
}
2019-07-30 22:27:31 +03:00
2020-01-24 22:13:59 +03:00
fn (n mut mapnode) merge(idx int) {
mut child := &mapnode(n.children[idx])
sibling := &mapnode(n.children[idx + 1])
child.keys[mid_index] = n.keys[idx]
child.values[mid_index] = n.values[idx]
for i := 0; i < sibling.size; i++ {
child.keys[i + degree] = sibling.keys[i]
child.values[i + degree] = sibling.values[i]
}
if !isnil(child.children) {
for i := 0; i <= sibling.size; i++ {
child.children[i + degree] = sibling.children[i]
}
}
for i := idx + 1; i < n.size; i++ {
n.keys[i - 1] = n.keys[i]
n.values[i - 1] = n.values[i]
}
for i := idx + 2; i <= n.size; i++ {
n.children[i - 1] = n.children[i]
}
2020-01-24 22:13:59 +03:00
child.size += sibling.size + 1
n.size--
// free(sibling)
}
2020-01-24 22:13:59 +03:00
pub fn (m mut map) delete(key string) {
if m.root.size == 0 {
return
}
2020-01-24 22:13:59 +03:00
removed := m.root.remove_key(key)
if removed {
m.size--
}
2020-02-04 14:50:58 +03:00
2020-01-24 22:13:59 +03:00
if m.root.size == 0 {
// tmp := t.root
if isnil(m.root.children) {
return
2020-01-24 22:13:59 +03:00
} else {
m.root = &mapnode(m.root.children[0])
2019-12-30 08:57:56 +03:00
}
2020-01-24 22:13:59 +03:00
// free(tmp)
}
2020-01-24 22:13:59 +03:00
}
// Insert all keys of the subtree into array `keys`
2020-02-04 14:50:58 +03:00
// starting at `at`. Keys are inserted in order.
2020-01-24 22:13:59 +03:00
fn (n mapnode) subkeys(keys mut []string, at int) int {
mut position := at
if !isnil(n.children) {
// Traverse children and insert
// keys inbetween children
for i in 0..n.size {
child := &mapnode(n.children[i])
position += child.subkeys(mut keys, position)
keys[position] = n.keys[i]
position++
}
2020-01-24 22:13:59 +03:00
// Insert the keys of the last child
child := &mapnode(n.children[n.size])
position += child.subkeys(mut keys, position)
} else {
// If leaf, insert keys
for i in 0..n.size {
keys[position + i] = n.keys[i]
2019-12-30 08:57:56 +03:00
}
2020-01-24 22:13:59 +03:00
position += n.size
2019-12-30 08:57:56 +03:00
}
2020-01-24 22:13:59 +03:00
// Return # of added keys
return position - at
2019-12-30 08:57:56 +03:00
}
2020-01-24 22:13:59 +03:00
pub fn (m &map) keys() []string {
mut keys := [''].repeat(m.size)
if isnil(m.root) || m.root.size == 0 {
return keys
2019-12-30 08:57:56 +03:00
}
2020-01-24 22:13:59 +03:00
m.root.subkeys(mut keys, 0)
return keys
}
2020-01-24 22:13:59 +03:00
fn (n mut mapnode) free() {
mut i := 0
if isnil(n.children) {
i = 0
for i < n.size {
i++
}
} else {
i = 0
for i < n.size {
&mapnode(n.children[i]).free()
i++
}
&mapnode(n.children[i]).free()
}
// free(n)
}
pub fn (m mut map) free() {
if isnil(m.root) {
return
}
m.root.free()
2019-07-23 23:57:06 +03:00
}
pub fn (m map) print() {
2019-06-22 21:20:28 +03:00
println('<<<<<<<<')
2020-01-24 22:13:59 +03:00
//for i := 0; i < m.entries.len; i++ {
// entry := m.entries[i]
// println('$entry.key => $entry.val')
//}
2019-06-24 23:37:53 +03:00
/*
2020-01-24 22:13:59 +03:00
for i := 0; i < m.cap * m.value_bytes; i++ {
2019-06-22 21:20:28 +03:00
b := m.table[i]
print('$i: ')
C.printf('%02x', b)
println('')
}
*/
println('>>>>>>>>>>')
}
pub fn (m map_string) str() string {
2019-07-14 12:01:32 +03:00
if m.size == 0 {
2019-06-22 21:20:28 +03:00
return '{}'
}
mut sb := strings.new_builder(50)
2019-08-29 01:52:32 +03:00
sb.writeln('{')
2020-01-24 22:13:59 +03:00
for key, val in m {
2019-08-29 01:52:32 +03:00
sb.writeln(' "$key" => "$val"')
2019-08-05 05:34:12 +03:00
}
2019-08-29 01:52:32 +03:00
sb.writeln('}')
return sb.str()
}