Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tnrich opened this issue Nov 20, 2015 · 0 comments
Open

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

tnrich opened this issue Nov 20, 2015 · 0 comments

Comments

@tnrich
Copy link

tnrich commented Nov 20, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant