Skip to content

Commit

Permalink
add maxX/maxY defaults to add for simpler point indexing, close #42
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Jan 29, 2024
1 parent 1b0b90b commit 2485486
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ other types may be faster in certain cases (e.g. `Int32Array` when your data is
- `ArrayBufferType`: the array buffer type used to store data (`ArrayBuffer` by default);
you may prefer `SharedArrayBuffer` if you want to share the index between threads (multiple `Worker`, `SharedWorker` or `ServiceWorker`).

#### `index.add(minX, minY, maxX, maxY)`
#### `index.add(minX, minY[, maxX, maxY])`

Adds a given rectangle to the index. Returns a zero-based, incremental number that represents the newly added rectangle.
If not provided, `maxX` and `maxY` default to `minX` and `minY` (essentially adding a point).

#### `index.finish()`

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default class Flatbush {
* @param {number} maxY
* @returns {number} A zero-based, incremental number that represents the newly added rectangle.
*/
add(minX, minY, maxX, maxY) {
add(minX, minY, maxX = minX, maxY = minY) {
const index = this._pos >> 2;
const boxes = this._boxes;
this._indices[index] = index;
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ test('reconstructs an index from a Uint8Array', () => {
assert.notEqual(index.byteOffset, index2.byteOffset);
});

test('defaults to adding a point when not providing maxX/maxY', () => {
const index = new Flatbush(1);
index.add(10, 10);
index.finish();
assert.deepEqual(index.search(0, 0, 20, 20), [0]);
});

test('throws an error if added less items than the index size', () => {
assert.throws(() => {
const index = new Flatbush(data.length / 4);
Expand Down

0 comments on commit 2485486

Please sign in to comment.