21 lines
742 B
JavaScript
21 lines
742 B
JavaScript
// Tiny wrapper around function-red-black-tree that overrides the [inspect.custom] symbol so when we print it (during debugging) we just see the (key=>value)-pairs instead of the tree structure.
|
|
|
|
import createRBTree from "functional-red-black-tree";
|
|
import { inspect } from "node:util";
|
|
|
|
function defaultPrintf(depth, options, inspect) {
|
|
const entries = [];
|
|
this.tree.forEach((key, val) => { entries.push(`${inspect(key)} => ${inspect(val)}`); });
|
|
return `RBTree(${this.tree.length}) {${entries.join(', ')}}`;
|
|
}
|
|
|
|
export class RBTreeWrapper {
|
|
constructor(tree, printf = defaultPrintf) {
|
|
this.tree = tree;
|
|
this[inspect.custom] = printf;
|
|
}
|
|
|
|
static new(compareFn) {
|
|
return new RBTreeWrapper(createRBTree(compareFn))
|
|
}
|
|
}
|