Skip to content

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

@tnrich

Description

@tnrich

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions