2019-06-23 05:21:30 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
module builtin
|
|
|
|
|
|
|
|
struct map {
|
|
|
|
element_size int
|
2019-07-14 12:01:32 +03:00
|
|
|
root *Node
|
|
|
|
_keys []string // used by `keys()` TODO remove this from map struct,
|
|
|
|
key_i int // store in a separate var
|
|
|
|
pub:
|
|
|
|
size int
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:01:32 +03:00
|
|
|
struct Node {
|
|
|
|
left *Node
|
|
|
|
right *Node
|
2019-06-22 21:20:28 +03:00
|
|
|
key string
|
|
|
|
val voidptr
|
|
|
|
}
|
|
|
|
|
2019-06-30 23:44:15 +03:00
|
|
|
fn new_map(cap, elm_size int) map {
|
2019-06-22 21:20:28 +03:00
|
|
|
res := map {
|
|
|
|
element_size: elm_size
|
2019-07-14 12:01:32 +03:00
|
|
|
root: 0
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-07-14 12:01:32 +03:00
|
|
|
fn new_node(key string, val voidptr, element_size int) *Node {
|
|
|
|
new_e := &Node {
|
2019-06-22 21:20:28 +03:00
|
|
|
key: key
|
2019-07-14 12:01:32 +03:00
|
|
|
val: malloc(element_size)
|
|
|
|
left: 0
|
|
|
|
right: 0
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-14 12:01:32 +03:00
|
|
|
C.memcpy(new_e.val, val, element_size)
|
2019-06-22 21:20:28 +03:00
|
|
|
return new_e
|
|
|
|
}
|
|
|
|
|
2019-07-14 12:01:32 +03:00
|
|
|
fn (m mut map) insert(n mut Node, key string, val voidptr) {
|
|
|
|
if n.key == key {
|
|
|
|
C.memcpy(n.val, val, m.element_size)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n.key > key {
|
|
|
|
if isnil(n.left) {
|
|
|
|
n.left = new_node(key, val, m.element_size)
|
|
|
|
m.size++
|
|
|
|
} else {
|
|
|
|
m.insert(mut n.left, key, val)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if isnil(n.right) {
|
|
|
|
n.right = new_node(key, val, m.element_size)
|
|
|
|
m.size++
|
|
|
|
} else {
|
|
|
|
m.insert(mut n.right, key, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (n & Node) find(key string, out voidptr, element_size int) bool{
|
|
|
|
if n.key == key {
|
|
|
|
C.memcpy(out, n.val, element_size)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
else if n.key > key {
|
|
|
|
if isnil(n.left) {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return n.left.find(key, out, element_size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if isnil(n.right) {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return n.right.find(key, out, element_size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// same as `find`, but doesn't return a value. Used by `exists`
|
|
|
|
fn (n & Node) find2(key string, element_size int) bool{
|
|
|
|
if n.key == key {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
else if n.key > key {
|
|
|
|
if isnil(n.left) {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return n.left.find2(key, element_size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if isnil(n.right) {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return n.right.find2(key, element_size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (m mut map) _set(key string, val voidptr) {
|
2019-07-14 12:01:32 +03:00
|
|
|
if isnil(m.root) {
|
|
|
|
m.root = new_node(key, val, m.element_size)
|
|
|
|
m.size++
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.insert(mut m.root, key, val)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:01:32 +03:00
|
|
|
/*
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (m map) bs(query string, start, end int, out voidptr) {
|
|
|
|
// println('bs "$query" $start -> $end')
|
|
|
|
mid := start + ((end - start) / 2)
|
|
|
|
if end - start == 0 {
|
|
|
|
last := m.entries[end]
|
|
|
|
C.memcpy(out, last.val, m.element_size)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if end - start == 1 {
|
|
|
|
first := m.entries[start]
|
|
|
|
C.memcpy(out, first.val, m.element_size)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if mid >= m.entries.len {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mid_msg := m.entries[mid]
|
|
|
|
// println('mid.key=$mid_msg.key')
|
|
|
|
if query < mid_msg.key {
|
|
|
|
m.bs(query, start, mid, out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.bs(query, mid, end, out)
|
|
|
|
}
|
2019-07-14 12:01:32 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2019-07-14 12:01:32 +03:00
|
|
|
fn (m mut map) preorder_keys(node &Node) {
|
|
|
|
m._keys[m.key_i] = node.key
|
|
|
|
m.key_i++
|
|
|
|
if !isnil(node.left) {
|
|
|
|
m.preorder_keys(node.left)
|
|
|
|
}
|
|
|
|
if !isnil(node.right) {
|
|
|
|
m.preorder_keys(node.right)
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2019-07-14 12:01:32 +03:00
|
|
|
pub fn (m mut map) keys() []string {
|
|
|
|
m._keys = [''; m.size]
|
|
|
|
m.key_i = 0
|
|
|
|
if isnil(m.root) {
|
|
|
|
return m._keys
|
|
|
|
}
|
|
|
|
m.preorder_keys(m.root)
|
|
|
|
return m._keys
|
2019-06-24 12:29:54 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (m map) get(key string, out voidptr) bool {
|
2019-07-14 12:01:32 +03:00
|
|
|
if isnil(m.root) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return m.root.find(key, out, m.element_size)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (m map) exists(key string) bool {
|
2019-07-14 12:01:32 +03:00
|
|
|
return !isnil(m.root) && m.root.find2(key, m.element_size)
|
2019-06-24 23:37:53 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (m map) print() {
|
2019-06-22 21:20:28 +03:00
|
|
|
println('<<<<<<<<')
|
2019-07-14 12:01:32 +03:00
|
|
|
//for i := 0; i < m.entries.len; i++ {
|
2019-06-22 21:20:28 +03:00
|
|
|
// entry := m.entries[i]
|
|
|
|
// println('$entry.key => $entry.val')
|
2019-07-14 12:01:32 +03:00
|
|
|
//}
|
2019-06-24 23:37:53 +03:00
|
|
|
/*
|
2019-06-22 21:20:28 +03:00
|
|
|
for i := 0; i < m.cap * m.element_size; i++ {
|
|
|
|
b := m.table[i]
|
|
|
|
print('$i: ')
|
|
|
|
C.printf('%02x', b)
|
|
|
|
println('')
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
println('>>>>>>>>>>')
|
|
|
|
}
|
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (m map) free() {
|
2019-06-22 21:20:28 +03:00
|
|
|
// C.free(m.table)
|
|
|
|
// C.free(m.keys_table)
|
|
|
|
}
|
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (m map_string) str() string {
|
2019-06-22 21:20:28 +03:00
|
|
|
// return 'not impl'
|
2019-07-14 12:01:32 +03:00
|
|
|
if m.size == 0 {
|
2019-06-22 21:20:28 +03:00
|
|
|
return '{}'
|
|
|
|
}
|
|
|
|
// TODO use bytes buffer
|
|
|
|
mut s := '{\n'
|
2019-07-14 12:01:32 +03:00
|
|
|
//for entry in m.entries {
|
|
|
|
//val := m[entry.key]
|
|
|
|
//s += ' "$entry.key" => "$val"\n'
|
|
|
|
//}
|
2019-06-22 21:20:28 +03:00
|
|
|
s += '}'
|
|
|
|
return s
|
|
|
|
}
|