Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support adding fewer than numItems via auto-trim() #44

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add auto-trim to finish()
  • Loading branch information
leeoniya committed Jan 14, 2023
commit 76b2944fb1b4cbf2ec73be054544d7041a49f4d8
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,29 @@ export default class Flatbush {
return index;
}

trim() {
const {_boxes, _indices, _pos, minX, minY, maxX, maxY, nodeSize, ArrayType} = this;
const numItems = _pos >> 2;

this.init(numItems, nodeSize, ArrayType);

this._boxes.set(_boxes.slice(0, this._boxes.length));
this._indices.set(_indices.slice(0, this._indices.length));

this._pos = _pos;
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
}

finish() {
if (this._pos >> 2 !== this.numItems) {
const numAdded = this._pos >> 2;

if (numAdded < this.numItems) {
this.trim();

} else if (numAdded > this.numItems) {
throw new Error(`Added ${this._pos >> 2} items when expected ${this.numItems}.`);
}
const boxes = this._boxes;
Expand Down