pretty print enhancements + comparison of generic functions

This commit is contained in:
Joeri Exelmans 2025-05-08 22:59:01 +02:00
parent bbac7858ae
commit 9e1f679dba
15 changed files with 93 additions and 45 deletions

View file

@ -3,19 +3,19 @@
import createRBTree from "functional-red-black-tree";
import { inspect } from "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) {
constructor(tree, printf = defaultPrintf) {
this.tree = tree;
this[inspect.custom] = printf;
}
static new(compareFn) {
return new RBTreeWrapper(createRBTree(compareFn))
}
// pretty print to console
[inspect.custom](depth, options, inspect) {
const entries = [];
this.tree.forEach((key, val) => { entries.push(`${inspect(key)} => ${inspect(val)}`); });
return `RBTree(${this.tree.length}) {${entries.join(', ')}}`;
}
}