mirror of
https://github.com/mourner/flatbush.git
synced 2025-12-08 17:36:26 +00:00
Fix stack overflow for some degenerate data cases (#65)
* fix pivot selection * add regression test * fix linting
This commit is contained in:
parent
3a6f0f0cc5
commit
1e87f4071d
17
index.js
17
index.js
@ -343,7 +343,22 @@ function upperBound(value, arr) {
|
||||
function sort(values, boxes, indices, left, right, nodeSize) {
|
||||
if (Math.floor(left / nodeSize) >= Math.floor(right / nodeSize)) return;
|
||||
|
||||
const pivot = values[(left + right) >> 1];
|
||||
// apply median of three method
|
||||
const start = values[left];
|
||||
const mid = values[(left + right) >> 1];
|
||||
const end = values[right];
|
||||
|
||||
let pivot = end;
|
||||
|
||||
const x = Math.max(start, mid);
|
||||
if (end > x) {
|
||||
pivot = x;
|
||||
} else if (x === start) {
|
||||
pivot = Math.max(mid, end);
|
||||
} else if (x === mid) {
|
||||
pivot = Math.max(start, end);
|
||||
}
|
||||
|
||||
let i = left - 1;
|
||||
let j = right + 1;
|
||||
|
||||
|
||||
28
test.js
28
test.js
@ -222,4 +222,32 @@ test('reconstructs an index from SharedArrayBuffer', () => {
|
||||
assert.deepEqual(index, index2);
|
||||
});
|
||||
|
||||
test('quicksort should work with an inbalanced dataset', () => {
|
||||
const n = 15000;
|
||||
const index = new Flatbush(2 * n);
|
||||
|
||||
function linspace(start, stop, num, endpoint = true) {
|
||||
const div = endpoint ? (num - 1) : num;
|
||||
const step = (stop - start) / div;
|
||||
return Array.from({length: num}, (_, i) => start + step * i);
|
||||
}
|
||||
|
||||
const items = linspace(0, 1000, n);
|
||||
const items2 = linspace(0, 1000, n);
|
||||
|
||||
for (const p of items) {
|
||||
index.add(p, 0, p, 0);
|
||||
}
|
||||
|
||||
for (const p of items2) {
|
||||
index.add(p, 0, p, 0);
|
||||
}
|
||||
|
||||
index.finish();
|
||||
|
||||
assert.doesNotThrow(() => {
|
||||
index.search(-100, -1, 15000, 1);
|
||||
});
|
||||
});
|
||||
|
||||
function compare(a, b) { return a - b; }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user