Skip to content

shinout/interval-tree2

Repository files navigation

Circle CI

interval-tree2

interval tree in JavaScript (source is written in CoffeeScript)

improves previous interval-tree

more maintenanceability with CoffeeScript OOP.

more robustness with mocha, CircleCI

API Documentation

latest API documentation Page (YUIDoc)

installation

$ npm install interval-tree2

usage

require

var IntervalTree = require('interval-tree2');

when using in web, use dist/interval-tree.js in this module.

<script src="dist/interval-tree.js"></scirpt>

create

var itree = new IntervalTree(300); // 300 : the center of the tree

add interval data

itree.add(22, 56,  'foo'); // 'foo' is the id of the interval data
itree.add(44, 199, 'bar'); // 'bar' is the id of the interval data
itree.add(1, 38); // id is automatically set when not given

search by point: get overlapped intervals from one point

var intervals = itree.search(103);

or

var intervals = itree.pointSearch(103);
intervals.forEach(function(interval) {
  console.log(interval.start); // overlapped interval start position
  console.log(interval.end);   // overlapped interval end position
  console.log(interval.id);    // id of the overlapped interval
});

search by range: get overlapped intervals from a range

var intervals2 = itree.search(103, 400);

or

var intervals2 = itree.rangeSearch(103, 400);
intervals2.forEach(function(interval) {
  console.log(interval.start); // overlapped interval start position
  console.log(interval.end);   // overlapped interval end position
  console.log(interval.id);    // id of the overlapped interval
});

remove an interval by id

itree.remove('foo');

About

interval tree in CoffeeScript, available in any JS runtime

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published