Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,56 @@
* @param {number} nodeSize
*/
function sort(values, boxes, indices, left, right, nodeSize) {
if (Math.floor(left / nodeSize) >= Math.floor(right / nodeSize)) return;
const stack = [];
let stackPointer = 0;

stack.push(left);
stackPointer++;
stack.push(right);
stackPointer++;

while (stackPointer > 0) {
const r = stack.pop();

Check failure on line 361 in index.js

View workflow job for this annotation

GitHub Actions / test

'r' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
stackPointer--;
const l = stack.pop();
stackPointer--;

if (Math.floor(l / nodeSize) >= Math.floor(r / nodeSize)) continue;

Check failure on line 366 in index.js

View workflow job for this annotation

GitHub Actions / test

'l' is possibly 'undefined'.

const pivot = getPivot(values, l, r);

Check failure on line 368 in index.js

View workflow job for this annotation

GitHub Actions / test

Argument of type 'number | undefined' is not assignable to parameter of type 'number'.

let i = l - 1;

Check failure on line 370 in index.js

View workflow job for this annotation

GitHub Actions / test

'l' is possibly 'undefined'.
let j = r + 1;

Check failure on line 371 in index.js

View workflow job for this annotation

GitHub Actions / test

'j' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

while (true) {
do i++; while (values[i] < pivot);
do j--; while (values[j] > pivot);
if (i >= j) break;
swap(values, boxes, indices, i, j);
}

stack.push(l);
stackPointer++;
stack.push(j);
stackPointer++;
stack.push(j + 1);
stackPointer++;
stack.push(r);
stackPointer++;
}
}

/**
* Determine pivot value.
* @param {Uint32Array} values
* @param {number} l
* @param {number} r
*/
function getPivot(values, l, r) {
// apply median of three method
const start = values[left];
const mid = values[(left + right) >> 1];
const end = values[right];
const start = values[l];
const mid = values[(l + r) >> 1];
const end = values[r];

let pivot = end;

Expand All @@ -366,19 +410,7 @@
} else if (x === mid) {
pivot = Math.max(start, end);
}

let i = left - 1;
let j = right + 1;

while (true) {
do i++; while (values[i] < pivot);
do j--; while (values[j] > pivot);
if (i >= j) break;
swap(values, boxes, indices, i, j);
}

sort(values, boxes, indices, left, j, nodeSize);
sort(values, boxes, indices, j + 1, right, nodeSize);
return pivot;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,35 @@ test('quicksort should work with an inbalanced dataset', () => {
});
});

test('quicksort should work with duplicates', () => {
const n = 55000 + 5500 + 7700;
const index = new Flatbush(n);

let x = 0;

for (let p = 0; p < 55000; p++) {
index.add(x, 3.0, x, 3.0);
x++;
}

for (let p = 0; p < 5500; p++) {
index.add(x, 4.0, x, 4.0);
x++;
}

for (let p = 0; p < 7700; p++) {
index.add(x, 5.0, x, 5.0);
x++;
}

index.finish();

assert.doesNotThrow(() => {
index.search(0.5, -1, 6.5, 1);
});
});




function compare(a, b) { return a - b; }
Loading