interval-tree2 icon indicating copy to clipboard operation
interval-tree2 copied to clipboard

Allow an optional object to be passed as the 4th param

Open tnrich opened this issue 9 years ago • 0 comments

I monkey patched the lib to allow an optional 4th param to be set on the intervals. This helps in my case because I'm interested in the objects that "own" the intervals and think it would be useful to be able to directly access them after a query.

It seems to work fine, but I'm wondering if there is any performance hit from doing this.

Here's the changed code (in js):

IntervalTree.prototype.add = function(start, end, id, object) {
    var interval;
    if (this.intervalsById[id] != null) {
      throw new Error('id ' + id + ' is already registered.');
    }
    if (id == null) {
      while (this.intervalsById[this.idCandidate] != null) {
        this.idCandidate++;
      }
      id = this.idCandidate;
    }
    Util.assertNumber(start, '1st argument of IntervalTree#add()');
    Util.assertNumber(end, '2nd argument of IntervalTree#add()');
    if (start >= end) {
      Util.assertOrder(start, end, 'start', 'end');
    }
    interval = new Interval(start, end, id, object);
    this.pointTree.insert(new Point(interval.start, id));
    this.pointTree.insert(new Point(interval.end, id));
    this.intervalsById[id] = interval;
    return this.insert(interval, this.root);
  };

and

  function Interval(start, end, id, object) {
    this.start = start;
    this.end = end;
    this.id = id;
    this.object = object;
  }

Thanks for your input and the excellent library @shinout

tnrich avatar Nov 20 '15 08:11 tnrich