crossfilter2
Version:
Fast multidimensional filtering for coordinated views.
41 lines (31 loc) • 995 B
JavaScript
import identity from './identity';
import xFilterHeap from './heap';
function heapselect_by(f) {
var heap = xFilterHeap.by(f);
// Returns a new array containing the top k elements in the array a[lo:hi].
// The returned array is not sorted, but maintains the heap property. If k is
// greater than hi - lo, then fewer than k elements will be returned. The
// order of elements in a is unchanged by this operation.
function heapselect(a, lo, hi, k) {
var queue = new Array(k = Math.min(hi - lo, k)),
min,
i,
d;
for (i = 0; i < k; ++i) queue[i] = a[lo++];
heap(queue, 0, k);
if (lo < hi) {
min = f(queue[0]);
do {
if (f(d = a[lo]) > min) {
queue[0] = d;
min = f(heap(queue, 0, k)[0]);
}
} while (++lo < hi);
}
return queue;
}
return heapselect;
}
const h = heapselect_by(identity);
h.by = heapselect_by; // assign the raw function to the export as well
export default h;