Skip to content

Commit

Permalink
Merge branch 'bisect-large' of git://github.com/jasondavies/d3 into 2…
Browse files Browse the repository at this point in the history
….9.7
  • Loading branch information
mbostock committed Jul 31, 2012
2 parents cb832c1 + 65a4645 commit ea48a4a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
4 changes: 2 additions & 2 deletions d3.v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
if (arguments.length < 3) lo = 0;
if (arguments.length < 4) hi = a.length;
while (lo < hi) {
var mid = lo + hi >> 1;
var mid = lo + hi >>> 1;
if (f.call(a, a[mid], mid) < x) lo = mid + 1; else hi = mid;
}
return lo;
Expand All @@ -243,7 +243,7 @@
if (arguments.length < 3) lo = 0;
if (arguments.length < 4) hi = a.length;
while (lo < hi) {
var mid = lo + hi >> 1;
var mid = lo + hi >>> 1;
if (x < f.call(a, a[mid], mid)) hi = mid; else lo = mid + 1;
}
return lo;
Expand Down
2 changes: 1 addition & 1 deletion d3.v2.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/core/bisect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ d3.bisector = function(f) {
if (arguments.length < 3) lo = 0;
if (arguments.length < 4) hi = a.length;
while (lo < hi) {
var mid = lo + hi >> 1;
var mid = lo + hi >>> 1;
if (f.call(a, a[mid], mid) < x) lo = mid + 1;
else hi = mid;
}
Expand All @@ -14,7 +14,7 @@ d3.bisector = function(f) {
if (arguments.length < 3) lo = 0;
if (arguments.length < 4) hi = a.length;
while (lo < hi) {
var mid = lo + hi >> 1;
var mid = lo + hi >>> 1;
if (x < f.call(a, a[mid], mid)) hi = mid;
else lo = mid + 1;
}
Expand Down
66 changes: 66 additions & 0 deletions test/core/bisect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var vows = require("vows"),

var suite = vows.describe("d3.bisect");

var i30 = 1 << 30;

suite.addBatch({
"bisectLeft": {
topic: function() {
Expand Down Expand Up @@ -48,6 +50,22 @@ suite.addBatch({
assert.equal(bisect(array, 4, 2, 3), 3);
assert.equal(bisect(array, 5, 2, 3), 3);
assert.equal(bisect(array, 6, 2, 3), 3);
},
"large arrays": function(bisect) {
var array = [],
i = i30;
array[i++] = 1;
array[i++] = 2;
array[i++] = 3;
array[i++] = 4;
array[i++] = 5;
assert.equal(bisect(array, 0, i - 5, i), i - 5);
assert.equal(bisect(array, 1, i - 5, i), i - 5);
assert.equal(bisect(array, 2, i - 5, i), i - 4);
assert.equal(bisect(array, 3, i - 5, i), i - 3);
assert.equal(bisect(array, 4, i - 5, i), i - 2);
assert.equal(bisect(array, 5, i - 5, i), i - 1);
assert.equal(bisect(array, 6, i - 5, i), i - 0);
}
}
});
Expand Down Expand Up @@ -95,6 +113,22 @@ suite.addBatch({
assert.equal(bisect(array, 4, 2, 3), 3);
assert.equal(bisect(array, 5, 2, 3), 3);
assert.equal(bisect(array, 6, 2, 3), 3);
},
"large arrays": function(bisect) {
var array = [],
i = i30;
array[i++] = 1;
array[i++] = 2;
array[i++] = 3;
array[i++] = 4;
array[i++] = 5;
assert.equal(bisect(array, 0, i - 5, i), i - 5);
assert.equal(bisect(array, 1, i - 5, i), i - 4);
assert.equal(bisect(array, 2, i - 5, i), i - 3);
assert.equal(bisect(array, 3, i - 5, i), i - 2);
assert.equal(bisect(array, 4, i - 5, i), i - 1);
assert.equal(bisect(array, 5, i - 5, i), i - 0);
assert.equal(bisect(array, 6, i - 5, i), i - 0);
}
}
});
Expand Down Expand Up @@ -146,6 +180,22 @@ suite.addBatch({
assert.equal(bisect(array, 4, 2, 3), 3);
assert.equal(bisect(array, 5, 2, 3), 3);
assert.equal(bisect(array, 6, 2, 3), 3);
},
"large arrays": function(bisect) {
var array = [],
i = i30;
array[i++] = {key: 1};
array[i++] = {key: 2};
array[i++] = {key: 3};
array[i++] = {key: 4};
array[i++] = {key: 5};
assert.equal(bisect(array, 0, i - 5, i), i - 5);
assert.equal(bisect(array, 1, i - 5, i), i - 5);
assert.equal(bisect(array, 2, i - 5, i), i - 4);
assert.equal(bisect(array, 3, i - 5, i), i - 3);
assert.equal(bisect(array, 4, i - 5, i), i - 2);
assert.equal(bisect(array, 5, i - 5, i), i - 1);
assert.equal(bisect(array, 6, i - 5, i), i - 0);
}
},
"right": {
Expand Down Expand Up @@ -190,6 +240,22 @@ suite.addBatch({
assert.equal(bisect(array, 4, 2, 3), 3);
assert.equal(bisect(array, 5, 2, 3), 3);
assert.equal(bisect(array, 6, 2, 3), 3);
},
"large arrays": function(bisect) {
var array = [],
i = i30;
array[i++] = {key: 1};
array[i++] = {key: 2};
array[i++] = {key: 3};
array[i++] = {key: 4};
array[i++] = {key: 5};
assert.equal(bisect(array, 0, i - 5, i), i - 5);
assert.equal(bisect(array, 1, i - 5, i), i - 4);
assert.equal(bisect(array, 2, i - 5, i), i - 3);
assert.equal(bisect(array, 3, i - 5, i), i - 2);
assert.equal(bisect(array, 4, i - 5, i), i - 1);
assert.equal(bisect(array, 5, i - 5, i), i - 0);
assert.equal(bisect(array, 6, i - 5, i), i - 0);
}
}
}
Expand Down

0 comments on commit ea48a4a

Please sign in to comment.