@univerjs/core v0.5.1 • Docs
Class: RBush<T>
Type Parameters
Type Parameter |
---|
T |
Constructors
new RBush()
new RBush<T>(maxEntries?): RBush<T>
Constructs an RBush
, a high-performance 2D spatial index for points and
rectangles. Based on an optimized R-tree data structure with
bulk-insertion support.
Parameters
Parameter | Type | Description |
---|---|---|
maxEntries ? | number | An optional argument to RBush defines the maximum number of entries in a tree node. 9 (used by default) is a reasonable choice for most applications. Higher value means faster insertion and slower search, and vice versa. |
Returns
RBush
<T
>
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:20
Methods
all()
all(): T[]
Returns all items contained in the tree.
Returns
T
[]
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:77
clear()
clear(): RBush<T>
Removes all items.
Returns
RBush
<T
>
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:61
collides()
collides(box): boolean
Returns true
if there are any items intersecting the given bounding
box, otherwise false
.
Parameters
Parameter | Type | Description |
---|---|---|
box | BBox | The bounding box in which to search. |
Returns
boolean
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:85
compareMinX()
compareMinX(a, b): number
Compares the minimum x coordinate of two items. Returns -1 if a
’s
x-coordinate is smaller, 1 if b
’s x coordinate is smaller, or 0 if
they’re equal.
By default, RBush
assumes the format of data points to be an object
with minX
, minY
, maxX
, and maxY
. However, you can specify a
custom item format by overriding toBBox()
, compareMinX()
, and
compareMinY()
.
Parameters
Parameter | Type | Description |
---|---|---|
a | T | The first item to compare. |
b | T | The second item to compare. |
Returns
number
Example
class MyRBush<T> extends RBush<T> {
toBBox([x, y]) { return { minX: x, minY: y, maxX: x, maxY: y }; }
compareMinX(a, b) { return a.x - b.x; }
compareMinY(a, b) { return a.y - b.y; }
}
const tree = new MyRBush<[number, number]>();
tree.insert([20, 50]); // accepts [x, y] points
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:130
compareMinY()
compareMinY(a, b): number
Compares the minimum y coordinate of two items. Returns -1 if a
’s
x-coordinate is smaller, 1 if b
’s x coordinate is smaller, or 0 if
they’re equal.
By default, RBush
assumes the format of data points to be an object
with minX
, minY
, maxX
, and maxY
. However, you can specify a
custom item format by overriding toBBox()
, compareMinX()
, and
compareMinY()
.
Parameters
Parameter | Type | Description |
---|---|---|
a | T | The first item to compare. |
b | T | The second item to compare. |
Returns
number
Example
class MyRBush<T> extends RBush<T> {
toBBox([x, y]) { return { minX: x, minY: y, maxX: x, maxY: y }; }
compareMinX(a, b) { return a.x - b.x; }
compareMinY(a, b) { return a.y - b.y; }
}
const tree = new MyRBush<[number, number]>();
tree.insert([20, 50]); // accepts [x, y] points
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:154
fromJSON()
fromJSON(data): RBush<T>
Imports previously exported data into the tree (i.e., data that was
emitted by toJSON()
).
Importing and exporting as JSON allows you to use RBush on both the server (using Node.js) and the browser combined, e.g. first indexing the data on the server and and then importing the resulting tree data on the client for searching.
Note that the maxEntries
option from the constructor must be the same
in both trees for export/import to work properly.
Parameters
Parameter | Type | Description |
---|---|---|
data | any | The previously exported JSON data. |
Returns
RBush
<T
>
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:183
insert()
insert(item): RBush<T>
Inserts an item. To insert many items at once, use load()
.
Parameters
Parameter | Type | Description |
---|---|---|
item | T | The item to insert. |
Returns
RBush
<T
>
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:27
load()
load(items): RBush<T>
Bulk-inserts the given items into the tree.
Bulk insertion is usually ~2-3 times faster than inserting items one by one. After bulk loading (bulk insertion into an empty tree), subsequent query performance is also ~20-30% better.
Note that when you do bulk insertion into an existing tree, it bulk-loads the given data into a separate tree and inserts the smaller tree into the larger tree. This means that bulk insertion works very well for clustered data (where items in one update are close to each other), but makes query performance worse if the data is scattered.
Parameters
Parameter | Type | Description |
---|---|---|
items | readonly T [] | The items to load. |
Returns
RBush
<T
>
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:44
remove()
remove(item, equals?): RBush<T>
Removes a previously inserted item, comparing by reference.
To remove all items, use clear()
.
Parameters
Parameter | Type | Description |
---|---|---|
item | T | The item to remove. |
equals ? | (a , b ) => boolean | A custom function that allows comparing by value instead. Useful when you have only a copy of the object you need removed (e.g. loaded from server). |
Returns
RBush
<T
>
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:56
search()
search(box): T[]
Returns an array of data items (points or rectangles) that the given bounding box intersects.
Note that the search method accepts a bounding box in {minX, minY, maxX, maxY}
format regardless of the data format.
Parameters
Parameter | Type | Description |
---|---|---|
box | BBox | The bounding box in which to search. |
Returns
T
[]
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:72
toBBox()
toBBox(item): BBox
Returns the bounding box for the provided item.
By default, RBush
assumes the format of data points to be an object
with minX
, minY
, maxX
, and maxY
. However, you can specify a
custom item format by overriding toBBox()
, compareMinX()
, and
compareMinY()
.
Parameters
Parameter | Type | Description |
---|---|---|
item | T | The item whose bounding box should be returned. |
Returns
Example
class MyRBush<T> extends RBush<T> {
toBBox([x, y]) { return { minX: x, minY: y, maxX: x, maxY: y }; }
compareMinX(a, b) { return a.x - b.x; }
compareMinY(a, b) { return a.y - b.y; }
}
const tree = new MyRBush<[number, number]>();
tree.insert([20, 50]); // accepts [x, y] points
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:106
toJSON()
toJSON(): any
Exports the tree’s contents as a JSON object.
Importing and exporting as JSON allows you to use RBush on both the server (using Node.js) and the browser combined, e.g. first indexing the data on the server and and then importing the resulting tree data on the client for searching.
Note that the maxEntries
option from the constructor must be the same
in both trees for export/import to work properly.
Returns
any
Defined in
packages/api/tmp/univer-pro/node_modules/.pnpm/@types+rbush@4.0.0/node_modules/@types/rbush/index.d.ts:167