splay
Version:
Immutable splay trees.
42 lines (35 loc) • 837 B
JavaScript
var splay = require('../../index')
// get
// set
// read(from, to) w/ early terminate
module.exports = function () {
return new Dict()
}
function Dict (tree) {
this._tree = tree || splay(byKey)
}
Dict.prototype.get = function (key) {
var root = this._tree = this._tree.access({ key: key })
if (root.value.key === key) {
return this._tree.value
}
}
Dict.prototype.set = function (key, value) {
this._tree = this._tree.uInsert({key: key, value: value})
return new Dict(this._tree)
}
Dict.prototype.range = function (gte, lt, opts) {
var res = []
var root = this._tree
root = root.access({ key: gte })
while (!root.isEmpty()) {
res.push(root.value)
root = root._higher()
}
return res
}
function byKey (a, b) {
console.log(a, b)
if (a.key === b.key) return 0
return a.key > b.key ? 1 : -1
}