fixpoint algorithm: parameterize the Map to use
This commit is contained in:
parent
ea878975be
commit
47786ae792
3 changed files with 58 additions and 10 deletions
|
|
@ -29,3 +29,46 @@ export class RBTreeWrapper {
|
|||
return this.tree.keys.map(key => [key, this.tree.get(key)]);
|
||||
}
|
||||
}
|
||||
|
||||
// Can be used as drop-in replacement for Map
|
||||
// Why create a mutable adapter for a purely-functional data structure?
|
||||
// Because the builtin Map does not allow custom comparison functions and this one does.
|
||||
export class MutableRBTree {
|
||||
constructor(tree, printf = defaultPrintf) {
|
||||
this.tree = tree;
|
||||
this[inspect.custom] = printf;
|
||||
}
|
||||
|
||||
static new(cmp) {
|
||||
return new MutableRBTree(createRBTree(cmp));
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
this.tree = this.tree.remove(key).insert(key, value);
|
||||
}
|
||||
|
||||
delete(key) {
|
||||
this.tree = this.tree.remove(key);
|
||||
}
|
||||
|
||||
get(key) {
|
||||
return this.tree.get(key);
|
||||
}
|
||||
|
||||
has(key) {
|
||||
return this.tree.get(key) !== undefined;
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.tree = createRBTree(this.tree._compare);
|
||||
}
|
||||
|
||||
*[Symbol.iterator]() {
|
||||
const iter = this.tree.begin;
|
||||
while (iter !== undefined && iter.valid) {
|
||||
yield [iter.key, iter.value];
|
||||
iter.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue