@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
81 lines • 2.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TreeShiftTree = void 0;
const simpletree_1 = require("./simpletree");
class TreeShiftTree {
constructor(d, length) {
if (length <= 0) {
throw new Error("length must be > 0");
}
this.d = d;
this.v = 0;
this.trees = [];
for (let i = 0; i < length; i++) {
this.trees.push((0, simpletree_1.newSimpleTree)());
}
}
static newTreeShiftTree(d, length) {
return new TreeShiftTree(d, length);
}
shape(d, v) {
if (d !== this.d) {
throw new Error("dimension mismatch");
}
if (v >= this.trees.length) {
throw new Error("value out of range");
}
// Create a shallow copy with new value
const newTree = Object.create(TreeShiftTree.prototype);
Object.assign(newTree, this);
newTree.v = v;
return newTree;
}
get(s) {
return this.trees[this.v].get(s);
}
longestPrefix(s) {
return this.trees[this.v].longestPrefix(s);
}
insert(s, v) {
return this.trees[this.v].insert(s, v);
}
async walkPrefix(lockType, s, f) {
return await this.trees[this.v].walkPrefix(lockType, s, f);
}
delete(key) {
for (const tree of this.trees) {
tree.tree.delete(key);
}
}
async deletePrefix(prefix) {
let count = 0;
for (const tree of this.trees) {
count += await tree.tree.deletePrefix(prefix);
}
return count;
}
lock(writable) {
if (writable) {
// Lock all trees for writing
for (const tree of this.trees) {
tree.mu.lock();
}
return () => {
for (const tree of this.trees) {
tree.mu.unlock();
}
};
}
// Lock all trees for reading
for (const tree of this.trees) {
tree.mu.rLock();
}
return () => {
for (const tree of this.trees) {
tree.mu.rUnlock();
}
};
}
}
exports.TreeShiftTree = TreeShiftTree;
//# sourceMappingURL=treeshifttree.js.map