2019-03-26 17:50:22 +01:00
|
|
|
class Node {
|
|
|
|
|
constructor(value) {
|
|
|
|
|
this.children = new Map()
|
|
|
|
|
this.value = value
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-25 17:36:41 +01:00
|
|
|
|
2019-03-26 17:50:22 +01:00
|
|
|
function del(node, i, keys) {
|
|
|
|
|
if (i === keys.length) {
|
|
|
|
|
if (node instanceof Node) {
|
|
|
|
|
node.value = undefined
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (!(node instanceof Node)) {
|
|
|
|
|
return node
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
|
|
|
|
const key = keys[i]
|
2019-03-26 17:50:22 +01:00
|
|
|
const { children } = node
|
|
|
|
|
const child = children.get(key)
|
2019-03-25 17:36:41 +01:00
|
|
|
if (child === undefined) {
|
2019-03-26 17:50:22 +01:00
|
|
|
return node
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
2019-03-26 17:50:22 +01:00
|
|
|
const newChild = del(child, i + 1, keys)
|
|
|
|
|
if (newChild === undefined) {
|
|
|
|
|
if (children.size === 1) {
|
|
|
|
|
return node.value
|
|
|
|
|
}
|
|
|
|
|
children.delete(key)
|
|
|
|
|
} else if (newChild !== child) {
|
|
|
|
|
children.set(key, newChild)
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
2019-03-26 17:50:22 +01:00
|
|
|
return node
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-26 17:50:22 +01:00
|
|
|
function get(node, i, keys) {
|
|
|
|
|
return i === keys.length
|
|
|
|
|
? node instanceof Node
|
|
|
|
|
? node.value
|
|
|
|
|
: node
|
|
|
|
|
: node instanceof Node
|
|
|
|
|
? get(node.children.get(keys[i]), i + 1, keys)
|
|
|
|
|
: undefined
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-26 17:50:22 +01:00
|
|
|
function set(node, i, keys, value) {
|
|
|
|
|
if (i === keys.length) {
|
|
|
|
|
if (node instanceof Node) {
|
|
|
|
|
node.value = value
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
return value
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
|
|
|
|
const key = keys[i]
|
2019-03-26 17:50:22 +01:00
|
|
|
if (!(node instanceof Node)) {
|
|
|
|
|
node = new Node(node)
|
|
|
|
|
node.children.set(key, set(undefined, i + 1, keys, value))
|
|
|
|
|
} else {
|
|
|
|
|
const { children } = node
|
|
|
|
|
const child = children.get(key)
|
|
|
|
|
const newChild = set(child, i + 1, keys, value)
|
|
|
|
|
if (newChild !== child) {
|
|
|
|
|
children.set(key, newChild)
|
|
|
|
|
}
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
2019-03-26 17:50:22 +01:00
|
|
|
return node
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class MultiKeyMap {
|
|
|
|
|
constructor() {
|
2019-03-26 17:50:22 +01:00
|
|
|
// each node is either a value or a Node if it contains children
|
|
|
|
|
this._root = undefined
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete(keys) {
|
2019-03-26 17:50:22 +01:00
|
|
|
this._root = del(this._root, 0, keys)
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get(keys) {
|
2019-03-26 17:50:22 +01:00
|
|
|
return get(this._root, 0, keys)
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set(keys, value) {
|
2019-03-26 17:50:22 +01:00
|
|
|
this._root = set(this._root, 0, keys, value)
|
2019-03-25 17:36:41 +01:00
|
|
|
}
|
|
|
|
|
}
|