@tanishiking/aho-corasick
Version:
TypeScript implementation of the Aho-Corasick algorithm for efficient string matching
37 lines (36 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntervalTree = void 0;
var interval_node_1 = require("./interval-node");
var IntervalTree = /** @class */ (function () {
function IntervalTree(intervals) {
this.rootNode = new interval_node_1.IntervalNode(intervals);
}
IntervalTree.prototype.removeOverlaps = function (intervals) {
var _this = this;
var removedIntervals = [];
// sort by size descending so that intervals that has larger size should survive over that smaller ones.
intervals
.sort(function (a, b) { return b.size() - a.size(); })
.forEach(function (current) {
if (!_this.containsInterval(removedIntervals, current)) {
var overlaps = _this.rootNode.findOverlaps(current);
overlaps.forEach(function (overlap) {
if (!_this.containsInterval(removedIntervals, overlap))
removedIntervals.push(overlap);
});
}
});
// sort by position ascend
return intervals
.sort(function (a, b) { return a.start - b.start; })
.filter(function (interval) {
return !_this.containsInterval(removedIntervals, interval);
});
};
IntervalTree.prototype.containsInterval = function (intervals, interval) {
return intervals.some(function (current) { return current.equals(interval); });
};
return IntervalTree;
}());
exports.IntervalTree = IntervalTree;