Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function fromSortedRanges(ranges: ReadonlyArray<RangeCov>): RangeTree | undefined {
let root: RangeTree | undefined;
const stack: RangeTree[] = [];
for (const range of ranges) {
const node: RangeTree = new RangeTree(range.startOffset, range.endOffset, range.count, [], 0, 0);
if (root === undefined) {
root = node;
stack.push(node);
continue;
}
let top: RangeTree;
while (true) {
top = stack[stack.length - 1];
// assert: `top !== undefined` (the ranges are sorted)
if (range.startOffset < top.end) {
break;
} else {
stack.pop();
}
}
top.children.push(node);
stack.push(node);
}
return root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.