SortedList.prototype.bsearch = function()

in src/lib/sortedList.js [111:125]


SortedList.prototype.bsearch = function(val) {
  if (!this.length) return -1;
  var mpos,
      spos = 0,
      epos = this.length;
  while (epos - spos > 1) {
    mpos = Math.floor((spos + epos)/2);
    var mval = this[mpos];
    var comp = this._compare(val, mval);
    if (comp == 0) return mpos;
    if (comp > 0)  spos = mpos;
    else           epos = mpos;
  }
  return (spos == 0 && this._compare(this[0], val) > 0) ? -1 : spos;
};