Skip to content

Commit 1eb0932

Browse files
committed
Simplify binary search.
1 parent 48a40ec commit 1eb0932

3 files changed

Lines changed: 7 additions & 25 deletions

File tree

d3.layout.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -818,8 +818,7 @@ d3.layout.histogram = function() {
818818

819819
// Count the number of samples per bin.
820820
for (var i = 0; i < x.length; i++) {
821-
var j = d3_layout_histogramSearchIndex(ticks, x[i]) - 1,
822-
bin = bins[Math.max(0, Math.min(bins.length - 1, j))];
821+
var bin = bins[d3_layout_histogramSearch(ticks, x[i])];
823822
bin.y++;
824823
bin.push(data[i]);
825824
}
@@ -854,23 +853,15 @@ d3.layout.histogram = function() {
854853
};
855854

856855
// Performs a binary search on a sorted array.
857-
// Returns the index of the value if found, otherwise -(insertion point) - 1.
858-
// The insertion point is the index at which value should be inserted into the
859-
// array for the array to remain sorted.
860856
function d3_layout_histogramSearch(array, value) {
861-
var low = 0, high = array.length - 1;
857+
var low = 1, high = array.length - 2;
862858
while (low <= high) {
863859
var mid = (low + high) >> 1, midValue = array[mid];
864860
if (midValue < value) low = mid + 1;
865861
else if (midValue > value) high = mid - 1;
866862
else return mid;
867863
}
868-
return -low - 1;
869-
}
870-
871-
function d3_layout_histogramSearchIndex(array, value) {
872-
var i = d3_layout_histogramSearch(array, value);
873-
return (i < 0) ? (-i - 1) : i;
864+
return low - 1;
874865
}
875866

876867
function d3_layout_histogramTicks(x) {

0 commit comments

Comments
 (0)