Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ However, we advise the reader to take a look at the [`graphology`](https://graph

Don't find the data structure you need? Maybe we can work it out [together](https://github.com/Yomguithereal/mnemonist/issues).

## Open interval bounds

`StaticIntervalTree` queries include both endpoints by default. Pass
`openBegin: true` or `openEnd: true` to exclude an endpoint for a query:

```js
var StaticIntervalTree = require('mnemonist/static-interval-tree');
var tree = new StaticIntervalTree([[0, 10], [10, 20]]);

tree.intervalsContainingPoint(10, {openEnd: true});
// [[10, 20]]

tree.intervalsOverlappingInterval([10, 15], {openEnd: true});
// [[10, 20]]
```

The flags interpret all indexed intervals, and also the query interval for
overlap queries, with the same bounds. An interval whose endpoints are equal
is empty when either bound is open. Flags apply only to the current query;
the index retains closed bounds for conservative subtree pruning, so a tree
can serve queries with different endpoint conventions without rebuilding.

## Contribution

Contributions are obviously welcome. Be sure to lint the code & add relevant unit tests.
Expand Down
9 changes: 7 additions & 2 deletions static-interval-tree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
type StaticIntervalTreeGetter<T> = (item: T) => number;
type StaticIntervalTreeGettersTuple<T> = [StaticIntervalTreeGetter<T>, StaticIntervalTreeGetter<T>];

export type StaticIntervalTreeQueryOptions = {
openBegin?: boolean;
openEnd?: boolean;
};

export default class StaticIntervalTree<T> {

// Members
Expand All @@ -15,8 +20,8 @@ export default class StaticIntervalTree<T> {
constructor(intervals: Array<T>, getters?: StaticIntervalTreeGettersTuple<T>);

// Methods
intervalsContainingPoint(point: number): Array<T>;
intervalsOverlappingInterval(interval: T): Array<T>;
intervalsContainingPoint(point: number, options?: StaticIntervalTreeQueryOptions): Array<T>;
intervalsOverlappingInterval(interval: T, options?: StaticIntervalTreeQueryOptions): Array<T>;
inspect(): any;

// Statics
Expand Down
28 changes: 20 additions & 8 deletions static-interval-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
* then proceeds building the augmented balanced BST bottom-up from the
* sorted list.
*
* Note that this implementation considers every given intervals as closed for
* simplicity's sake.
* The index uses closed bounds; queries may interpret either bound as open.
*
* For more information: https://en.wikipedia.org/wiki/Interval_tree
*/
Expand Down Expand Up @@ -217,12 +216,16 @@ function StaticIntervalTree(intervals, getters) {
/**
* Method returning a list of intervals containing the given point.
*
* @param {any} point - Target point.
* @param {any} point - Target point.
* @param {object} options - Optional openBegin/openEnd flags (default false).
* @return {array}
*/
StaticIntervalTree.prototype.intervalsContainingPoint = function(point) {
StaticIntervalTree.prototype.intervalsContainingPoint = function(point, options) {
var matches = [];

var openBegin = options && options.openBegin,
openEnd = options && options.openEnd;

var stack = this.stack;

stack.clear();
Expand Down Expand Up @@ -262,7 +265,8 @@ StaticIntervalTree.prototype.intervalsContainingPoint = function(point) {
end = this.endGetter ? this.endGetter(interval) : interval[1];

// Checking current node
if (point >= start && point <= end)
if ((openBegin ? point > start : point >= start) &&
(openEnd ? point < end : point <= end))
matches.push(interval);

// If the point is to the left of the start of the current interval,
Expand All @@ -283,15 +287,22 @@ StaticIntervalTree.prototype.intervalsContainingPoint = function(point) {
/**
* Method returning a list of intervals overlapping the given interval.
*
* @param {any} interval - Target interval.
* @param {any} interval - Target interval.
* @param {object} options - Open bounds for both stored and query intervals.
* @return {array}
*/
StaticIntervalTree.prototype.intervalsOverlappingInterval = function(interval) {
StaticIntervalTree.prototype.intervalsOverlappingInterval = function(interval, options) {
var intervalStart = this.startGetter ? this.startGetter(interval) : interval[0],
intervalEnd = this.endGetter ? this.endGetter(interval) : interval[1];

var matches = [];

var open = options && (options.openBegin || options.openEnd);

// An interval with equal endpoints is empty if either bound is open.
if (open && intervalStart >= intervalEnd)
return matches;

var stack = this.stack;

stack.clear();
Expand Down Expand Up @@ -331,7 +342,8 @@ StaticIntervalTree.prototype.intervalsOverlappingInterval = function(interval) {
end = this.endGetter ? this.endGetter(currentInterval) : currentInterval[1];

// Checking current node
if (intervalEnd >= start && intervalStart <= end)
if (open ? start < end && intervalEnd > start && intervalStart < end :
intervalEnd >= start && intervalStart <= end)
matches.push(currentInterval);

// If the end is to the left of the start of the current interval,
Expand Down
54 changes: 54 additions & 0 deletions test/static-interval-tree-open.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var assert = require('assert'),
StaticIntervalTree = require('../static-interval-tree.js');

describe('StaticIntervalTree open bounds', function() {
[false, true].forEach(function(openBegin) {
[false, true].forEach(function(openEnd) {
var options = {openBegin: openBegin, openEnd: openEnd};

it('should query ' + (openBegin ? '(' : '[') + 'start,end' + (openEnd ? ')' : ']'), function() {
var intervals = [[0, 0], [0, 1], [1, 1], [1, 2], [0, 3], [2, 3]],
tree = new StaticIntervalTree(intervals),
samples = [0, 0.5, 1, 1.5, 2, 2.5, 3];

// Expand intervals into a finite set of boundary and interior points.
// With integer endpoints, these samples distinguish all intersections.
function members(interval) {
var first = samples.indexOf(interval[0]),
last = samples.indexOf(interval[1]);

return samples.slice(first + (openBegin ? 1 : 0), last + (openEnd ? 0 : 1));
}

var sets = intervals.map(members);

samples.forEach(function(point) {
var expected = intervals.filter(function(interval, i) {
return sets[i].indexOf(point) !== -1;
});
assert.deepStrictEqual(tree.intervalsContainingPoint(point, options).sort(), expected.sort());
});

intervals.forEach(function(query, j) {
var expected = intervals.filter(function(interval, i) {
return sets[i].some(function(point) {
return sets[j].indexOf(point) !== -1;
});
});
assert.deepStrictEqual(tree.intervalsOverlappingInterval(query, options).sort(), expected.sort());
});
});
});
});

it('should retain inclusive defaults and support getters', function() {
var interval = {from: 1, to: 2},
tree = StaticIntervalTree.from([interval], [function(i) { return i.from; }, function(i) { return i.to; }]);

assert.deepStrictEqual(tree.intervalsContainingPoint(2), [interval]);
assert.deepStrictEqual(tree.intervalsContainingPoint(2, {openEnd: true}), []);
assert.deepStrictEqual(tree.intervalsOverlappingInterval({from: 2, to: 3}, {openEnd: true}), []);
assert.deepStrictEqual(tree.intervalsOverlappingInterval({from: 1.5, to: 3}, {openEnd: true}), [interval]);
assert.deepStrictEqual(tree.intervalsContainingPoint(2, {}), [interval]);
});
});