rbush-rs 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,25 +1,24 @@
1
1
  {
2
2
  "name": "rbush-rs",
3
- "version": "1.0.0",
4
- "description": "",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "jest"
8
- },
3
+ "type": "module",
4
+ "collaborators": [
5
+ "Preetham <ppmpreetham1@gmail.com>"
6
+ ],
7
+ "description": "High-performance RBush port in WebAssembly",
8
+ "version": "1.0.1",
9
+ "license": "MIT",
9
10
  "repository": {
10
11
  "type": "git",
11
- "url": "git+https://github.com/ppmpreetham/rbush-rs.git"
12
- },
13
- "keywords": [],
14
- "author": "",
15
- "license": "ISC",
16
- "type": "commonjs",
17
- "bugs": {
18
- "url": "https://github.com/ppmpreetham/rbush-rs/issues"
12
+ "url": "https://github.com/ppmpreetham/rbush-rs"
19
13
  },
20
- "homepage": "https://github.com/ppmpreetham/rbush-rs#readme",
21
- "devDependencies": {
22
- "jest": "^30.2.0",
23
- "rbush": "^3.0.1"
24
- }
25
- }
14
+ "files": [
15
+ "rbush_rs_bg.wasm",
16
+ "rbush_rs.js",
17
+ "rbush_rs.d.ts"
18
+ ],
19
+ "main": "rbush_rs.js",
20
+ "types": "rbush_rs.d.ts",
21
+ "sideEffects": [
22
+ "./snippets/*"
23
+ ]
24
+ }
package/rbush_rs.d.ts ADDED
@@ -0,0 +1,59 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export class RBush {
5
+ free(): void;
6
+ [Symbol.dispose](): void;
7
+ all(): Array<any>;
8
+ clear(): void;
9
+ collides(bbox_js: any): boolean;
10
+ insert(item: any): void;
11
+ load(data: Array<any>): void;
12
+ loadHybrid(fast_coords: Float64Array, items: Array<any>): void;
13
+ constructor(max_entries?: number | null);
14
+ remove(item: any): void;
15
+ search(bbox_js: any): Array<any>;
16
+ }
17
+
18
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
19
+
20
+ export interface InitOutput {
21
+ readonly memory: WebAssembly.Memory;
22
+ readonly __wbg_rbush_free: (a: number, b: number) => void;
23
+ readonly rbush_all: (a: number) => any;
24
+ readonly rbush_clear: (a: number) => void;
25
+ readonly rbush_collides: (a: number, b: any) => number;
26
+ readonly rbush_insert: (a: number, b: any) => void;
27
+ readonly rbush_load: (a: number, b: any) => void;
28
+ readonly rbush_loadHybrid: (a: number, b: number, c: number, d: any) => void;
29
+ readonly rbush_new: (a: number) => number;
30
+ readonly rbush_remove: (a: number, b: any) => void;
31
+ readonly rbush_search: (a: number, b: any) => any;
32
+ readonly __wbindgen_exn_store: (a: number) => void;
33
+ readonly __externref_table_alloc: () => number;
34
+ readonly __wbindgen_externrefs: WebAssembly.Table;
35
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
36
+ readonly __wbindgen_start: () => void;
37
+ }
38
+
39
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
40
+
41
+ /**
42
+ * Instantiates the given `module`, which can either be bytes or
43
+ * a precompiled `WebAssembly.Module`.
44
+ *
45
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
46
+ *
47
+ * @returns {InitOutput}
48
+ */
49
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
50
+
51
+ /**
52
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
53
+ * for everything else, calls `WebAssembly.instantiate` directly.
54
+ *
55
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
56
+ *
57
+ * @returns {Promise<InitOutput>}
58
+ */
59
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
package/rbush_rs.js ADDED
@@ -0,0 +1,311 @@
1
+ /* @ts-self-types="./rbush_rs.d.ts" */
2
+
3
+ export class RBush {
4
+ __destroy_into_raw() {
5
+ const ptr = this.__wbg_ptr;
6
+ this.__wbg_ptr = 0;
7
+ RBushFinalization.unregister(this);
8
+ return ptr;
9
+ }
10
+ free() {
11
+ const ptr = this.__destroy_into_raw();
12
+ wasm.__wbg_rbush_free(ptr, 0);
13
+ }
14
+ /**
15
+ * @returns {Array<any>}
16
+ */
17
+ all() {
18
+ const ret = wasm.rbush_all(this.__wbg_ptr);
19
+ return ret;
20
+ }
21
+ clear() {
22
+ wasm.rbush_clear(this.__wbg_ptr);
23
+ }
24
+ /**
25
+ * @param {any} bbox_js
26
+ * @returns {boolean}
27
+ */
28
+ collides(bbox_js) {
29
+ const ret = wasm.rbush_collides(this.__wbg_ptr, bbox_js);
30
+ return ret !== 0;
31
+ }
32
+ /**
33
+ * @param {any} item
34
+ */
35
+ insert(item) {
36
+ wasm.rbush_insert(this.__wbg_ptr, item);
37
+ }
38
+ /**
39
+ * @param {Array<any>} data
40
+ */
41
+ load(data) {
42
+ wasm.rbush_load(this.__wbg_ptr, data);
43
+ }
44
+ /**
45
+ * @param {Float64Array} fast_coords
46
+ * @param {Array<any>} items
47
+ */
48
+ loadHybrid(fast_coords, items) {
49
+ const ptr0 = passArrayF64ToWasm0(fast_coords, wasm.__wbindgen_malloc);
50
+ const len0 = WASM_VECTOR_LEN;
51
+ wasm.rbush_loadHybrid(this.__wbg_ptr, ptr0, len0, items);
52
+ }
53
+ /**
54
+ * @param {number | null} [max_entries]
55
+ */
56
+ constructor(max_entries) {
57
+ const ret = wasm.rbush_new(isLikeNone(max_entries) ? 0x100000001 : (max_entries) >>> 0);
58
+ this.__wbg_ptr = ret >>> 0;
59
+ RBushFinalization.register(this, this.__wbg_ptr, this);
60
+ return this;
61
+ }
62
+ /**
63
+ * @param {any} item
64
+ */
65
+ remove(item) {
66
+ wasm.rbush_remove(this.__wbg_ptr, item);
67
+ }
68
+ /**
69
+ * @param {any} bbox_js
70
+ * @returns {Array<any>}
71
+ */
72
+ search(bbox_js) {
73
+ const ret = wasm.rbush_search(this.__wbg_ptr, bbox_js);
74
+ return ret;
75
+ }
76
+ }
77
+ if (Symbol.dispose) RBush.prototype[Symbol.dispose] = RBush.prototype.free;
78
+
79
+ function __wbg_get_imports() {
80
+ const import0 = {
81
+ __proto__: null,
82
+ __wbg___wbindgen_is_null_ac34f5003991759a: function(arg0) {
83
+ const ret = arg0 === null;
84
+ return ret;
85
+ },
86
+ __wbg___wbindgen_is_undefined_9e4d92534c42d778: function(arg0) {
87
+ const ret = arg0 === undefined;
88
+ return ret;
89
+ },
90
+ __wbg___wbindgen_jsval_eq_11888390b0186270: function(arg0, arg1) {
91
+ const ret = arg0 === arg1;
92
+ return ret;
93
+ },
94
+ __wbg___wbindgen_number_get_8ff4255516ccad3e: function(arg0, arg1) {
95
+ const obj = arg1;
96
+ const ret = typeof(obj) === 'number' ? obj : undefined;
97
+ getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
98
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
99
+ },
100
+ __wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) {
101
+ throw new Error(getStringFromWasm0(arg0, arg1));
102
+ },
103
+ __wbg_get_9b94d73e6221f75c: function(arg0, arg1) {
104
+ const ret = arg0[arg1 >>> 0];
105
+ return ret;
106
+ },
107
+ __wbg_get_b3ed3ad4be2bc8ac: function() { return handleError(function (arg0, arg1) {
108
+ const ret = Reflect.get(arg0, arg1);
109
+ return ret;
110
+ }, arguments); },
111
+ __wbg_length_35a7bace40f36eac: function(arg0) {
112
+ const ret = arg0.length;
113
+ return ret;
114
+ },
115
+ __wbg_new_3eb36ae241fe6f44: function() {
116
+ const ret = new Array();
117
+ return ret;
118
+ },
119
+ __wbg_push_8ffdcb2063340ba5: function(arg0, arg1) {
120
+ const ret = arg0.push(arg1);
121
+ return ret;
122
+ },
123
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
124
+ // Cast intrinsic for `Ref(String) -> Externref`.
125
+ const ret = getStringFromWasm0(arg0, arg1);
126
+ return ret;
127
+ },
128
+ __wbindgen_init_externref_table: function() {
129
+ const table = wasm.__wbindgen_externrefs;
130
+ const offset = table.grow(4);
131
+ table.set(0, undefined);
132
+ table.set(offset + 0, undefined);
133
+ table.set(offset + 1, null);
134
+ table.set(offset + 2, true);
135
+ table.set(offset + 3, false);
136
+ },
137
+ };
138
+ return {
139
+ __proto__: null,
140
+ "./rbush_rs_bg.js": import0,
141
+ };
142
+ }
143
+
144
+ const RBushFinalization = (typeof FinalizationRegistry === 'undefined')
145
+ ? { register: () => {}, unregister: () => {} }
146
+ : new FinalizationRegistry(ptr => wasm.__wbg_rbush_free(ptr >>> 0, 1));
147
+
148
+ function addToExternrefTable0(obj) {
149
+ const idx = wasm.__externref_table_alloc();
150
+ wasm.__wbindgen_externrefs.set(idx, obj);
151
+ return idx;
152
+ }
153
+
154
+ let cachedDataViewMemory0 = null;
155
+ function getDataViewMemory0() {
156
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
157
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
158
+ }
159
+ return cachedDataViewMemory0;
160
+ }
161
+
162
+ let cachedFloat64ArrayMemory0 = null;
163
+ function getFloat64ArrayMemory0() {
164
+ if (cachedFloat64ArrayMemory0 === null || cachedFloat64ArrayMemory0.byteLength === 0) {
165
+ cachedFloat64ArrayMemory0 = new Float64Array(wasm.memory.buffer);
166
+ }
167
+ return cachedFloat64ArrayMemory0;
168
+ }
169
+
170
+ function getStringFromWasm0(ptr, len) {
171
+ ptr = ptr >>> 0;
172
+ return decodeText(ptr, len);
173
+ }
174
+
175
+ let cachedUint8ArrayMemory0 = null;
176
+ function getUint8ArrayMemory0() {
177
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
178
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
179
+ }
180
+ return cachedUint8ArrayMemory0;
181
+ }
182
+
183
+ function handleError(f, args) {
184
+ try {
185
+ return f.apply(this, args);
186
+ } catch (e) {
187
+ const idx = addToExternrefTable0(e);
188
+ wasm.__wbindgen_exn_store(idx);
189
+ }
190
+ }
191
+
192
+ function isLikeNone(x) {
193
+ return x === undefined || x === null;
194
+ }
195
+
196
+ function passArrayF64ToWasm0(arg, malloc) {
197
+ const ptr = malloc(arg.length * 8, 8) >>> 0;
198
+ getFloat64ArrayMemory0().set(arg, ptr / 8);
199
+ WASM_VECTOR_LEN = arg.length;
200
+ return ptr;
201
+ }
202
+
203
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
204
+ cachedTextDecoder.decode();
205
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
206
+ let numBytesDecoded = 0;
207
+ function decodeText(ptr, len) {
208
+ numBytesDecoded += len;
209
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
210
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
211
+ cachedTextDecoder.decode();
212
+ numBytesDecoded = len;
213
+ }
214
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
215
+ }
216
+
217
+ let WASM_VECTOR_LEN = 0;
218
+
219
+ let wasmModule, wasm;
220
+ function __wbg_finalize_init(instance, module) {
221
+ wasm = instance.exports;
222
+ wasmModule = module;
223
+ cachedDataViewMemory0 = null;
224
+ cachedFloat64ArrayMemory0 = null;
225
+ cachedUint8ArrayMemory0 = null;
226
+ wasm.__wbindgen_start();
227
+ return wasm;
228
+ }
229
+
230
+ async function __wbg_load(module, imports) {
231
+ if (typeof Response === 'function' && module instanceof Response) {
232
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
233
+ try {
234
+ return await WebAssembly.instantiateStreaming(module, imports);
235
+ } catch (e) {
236
+ const validResponse = module.ok && expectedResponseType(module.type);
237
+
238
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
239
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
240
+
241
+ } else { throw e; }
242
+ }
243
+ }
244
+
245
+ const bytes = await module.arrayBuffer();
246
+ return await WebAssembly.instantiate(bytes, imports);
247
+ } else {
248
+ const instance = await WebAssembly.instantiate(module, imports);
249
+
250
+ if (instance instanceof WebAssembly.Instance) {
251
+ return { instance, module };
252
+ } else {
253
+ return instance;
254
+ }
255
+ }
256
+
257
+ function expectedResponseType(type) {
258
+ switch (type) {
259
+ case 'basic': case 'cors': case 'default': return true;
260
+ }
261
+ return false;
262
+ }
263
+ }
264
+
265
+ function initSync(module) {
266
+ if (wasm !== undefined) return wasm;
267
+
268
+
269
+ if (module !== undefined) {
270
+ if (Object.getPrototypeOf(module) === Object.prototype) {
271
+ ({module} = module)
272
+ } else {
273
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
274
+ }
275
+ }
276
+
277
+ const imports = __wbg_get_imports();
278
+ if (!(module instanceof WebAssembly.Module)) {
279
+ module = new WebAssembly.Module(module);
280
+ }
281
+ const instance = new WebAssembly.Instance(module, imports);
282
+ return __wbg_finalize_init(instance, module);
283
+ }
284
+
285
+ async function __wbg_init(module_or_path) {
286
+ if (wasm !== undefined) return wasm;
287
+
288
+
289
+ if (module_or_path !== undefined) {
290
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
291
+ ({module_or_path} = module_or_path)
292
+ } else {
293
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
294
+ }
295
+ }
296
+
297
+ if (module_or_path === undefined) {
298
+ module_or_path = new URL('rbush_rs_bg.wasm', import.meta.url);
299
+ }
300
+ const imports = __wbg_get_imports();
301
+
302
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
303
+ module_or_path = fetch(module_or_path);
304
+ }
305
+
306
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
307
+
308
+ return __wbg_finalize_init(instance, module);
309
+ }
310
+
311
+ export { initSync, __wbg_init as default };
Binary file
package/Cargo.lock DELETED
@@ -1,125 +0,0 @@
1
- # This file is automatically @generated by Cargo.
2
- # It is not intended for manual editing.
3
- version = 4
4
-
5
- [[package]]
6
- name = "bumpalo"
7
- version = "3.19.1"
8
- source = "registry+https://github.com/rust-lang/crates.io-index"
9
- checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510"
10
-
11
- [[package]]
12
- name = "cfg-if"
13
- version = "1.0.4"
14
- source = "registry+https://github.com/rust-lang/crates.io-index"
15
- checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
16
-
17
- [[package]]
18
- name = "js-sys"
19
- version = "0.3.85"
20
- source = "registry+https://github.com/rust-lang/crates.io-index"
21
- checksum = "8c942ebf8e95485ca0d52d97da7c5a2c387d0e7f0ba4c35e93bfcaee045955b3"
22
- dependencies = [
23
- "once_cell",
24
- "wasm-bindgen",
25
- ]
26
-
27
- [[package]]
28
- name = "once_cell"
29
- version = "1.21.3"
30
- source = "registry+https://github.com/rust-lang/crates.io-index"
31
- checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
32
-
33
- [[package]]
34
- name = "proc-macro2"
35
- version = "1.0.105"
36
- source = "registry+https://github.com/rust-lang/crates.io-index"
37
- checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7"
38
- dependencies = [
39
- "unicode-ident",
40
- ]
41
-
42
- [[package]]
43
- name = "quote"
44
- version = "1.0.43"
45
- source = "registry+https://github.com/rust-lang/crates.io-index"
46
- checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a"
47
- dependencies = [
48
- "proc-macro2",
49
- ]
50
-
51
- [[package]]
52
- name = "rbush-rs"
53
- version = "0.1.0"
54
- dependencies = [
55
- "js-sys",
56
- "wasm-bindgen",
57
- ]
58
-
59
- [[package]]
60
- name = "rustversion"
61
- version = "1.0.22"
62
- source = "registry+https://github.com/rust-lang/crates.io-index"
63
- checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
64
-
65
- [[package]]
66
- name = "syn"
67
- version = "2.0.114"
68
- source = "registry+https://github.com/rust-lang/crates.io-index"
69
- checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a"
70
- dependencies = [
71
- "proc-macro2",
72
- "quote",
73
- "unicode-ident",
74
- ]
75
-
76
- [[package]]
77
- name = "unicode-ident"
78
- version = "1.0.22"
79
- source = "registry+https://github.com/rust-lang/crates.io-index"
80
- checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
81
-
82
- [[package]]
83
- name = "wasm-bindgen"
84
- version = "0.2.108"
85
- source = "registry+https://github.com/rust-lang/crates.io-index"
86
- checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566"
87
- dependencies = [
88
- "cfg-if",
89
- "once_cell",
90
- "rustversion",
91
- "wasm-bindgen-macro",
92
- "wasm-bindgen-shared",
93
- ]
94
-
95
- [[package]]
96
- name = "wasm-bindgen-macro"
97
- version = "0.2.108"
98
- source = "registry+https://github.com/rust-lang/crates.io-index"
99
- checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608"
100
- dependencies = [
101
- "quote",
102
- "wasm-bindgen-macro-support",
103
- ]
104
-
105
- [[package]]
106
- name = "wasm-bindgen-macro-support"
107
- version = "0.2.108"
108
- source = "registry+https://github.com/rust-lang/crates.io-index"
109
- checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55"
110
- dependencies = [
111
- "bumpalo",
112
- "proc-macro2",
113
- "quote",
114
- "syn",
115
- "wasm-bindgen-shared",
116
- ]
117
-
118
- [[package]]
119
- name = "wasm-bindgen-shared"
120
- version = "0.2.108"
121
- source = "registry+https://github.com/rust-lang/crates.io-index"
122
- checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12"
123
- dependencies = [
124
- "unicode-ident",
125
- ]
package/Cargo.toml DELETED
@@ -1,15 +0,0 @@
1
- [package]
2
- name = "rbush-rs"
3
- version = "0.1.0"
4
- authors = ["Preetham <ppmpreetham1@gmail.com>"]
5
- description = "High-performance RBush port in WebAssembly"
6
- repository = "https://github.com/ppmpreetham/rbush-rs"
7
- license = "MIT"
8
- edition = "2024"
9
-
10
- [lib]
11
- crate-type = ["cdylib"]
12
-
13
- [dependencies]
14
- js-sys = "0.3.85"
15
- wasm-bindgen = "0.2.108"
package/rbush.test.js DELETED
@@ -1,168 +0,0 @@
1
- const RBushJS = require("rbush")
2
- const { RBush: RBushWasm } = require("./pkg/rbush_rs")
3
-
4
- describe("RBush Comprehensive Benchmark", () => {
5
- function generateData(count) {
6
- const data = []
7
- const flatData = new Float64Array(count * 4)
8
- for (let i = 0; i < count; i++) {
9
- const minX = Math.random() * 1000
10
- const minY = Math.random() * 1000
11
- const maxX = minX + Math.random() * 20
12
- const maxY = minY + Math.random() * 20
13
-
14
- data.push({ minX, minY, maxX, maxY, id: i })
15
-
16
- flatData[i * 4] = minX
17
- flatData[i * 4 + 1] = minY
18
- flatData[i * 4 + 2] = maxX
19
- flatData[i * 4 + 3] = maxY
20
- }
21
- return { data, flatData }
22
- }
23
-
24
- const DATA_SIZE = 10000
25
- const { data: testData, flatData: testDataFlat } = generateData(DATA_SIZE)
26
- const searchBox = { minX: 400, minY: 400, maxX: 600, maxY: 600 }
27
- const itemsToRemove = testData.slice(0, 1000)
28
-
29
- function benchmark(name, fn, iterations = 100) {
30
- const start = performance.now()
31
- for (let i = 0; i < iterations; i++) {
32
- fn()
33
- }
34
- const end = performance.now()
35
- const avg = (end - start) / iterations
36
- console.log(`${name.padEnd(30)}: ${avg.toFixed(3)} ms`)
37
- }
38
-
39
- console.log(`\nBENCHMARK RESULTS (N=${DATA_SIZE})`)
40
-
41
- test("Benchmark: Bulk Load", () => {
42
- console.log("\n Bulk Load ")
43
- benchmark(
44
- "JS RBush",
45
- () => {
46
- const tree = new RBushJS(9)
47
- tree.load(testData)
48
- },
49
- 10
50
- )
51
-
52
- benchmark(
53
- "WASM RBush (Hybrid)",
54
- () => {
55
- const tree = new RBushWasm(9)
56
- tree.loadHybrid(testDataFlat, testData)
57
- },
58
- 10
59
- )
60
- })
61
-
62
- test("Benchmark: Insert (One by One)", () => {
63
- console.log("\n Insert (1000 items) ")
64
-
65
- const smallSet = testData.slice(0, 1000)
66
-
67
- benchmark(
68
- "JS RBush",
69
- () => {
70
- const tree = new RBushJS(9)
71
- for (const item of smallSet) tree.insert(item)
72
- },
73
- 20
74
- )
75
-
76
- benchmark(
77
- "WASM RBush",
78
- () => {
79
- const tree = new RBushWasm(9)
80
- for (const item of smallSet) tree.insert(item)
81
- },
82
- 20
83
- )
84
- })
85
-
86
- test("Benchmark: Search", () => {
87
- console.log("\n Search ")
88
- const jsTree = new RBushJS(9)
89
- jsTree.load(testData)
90
-
91
- const wasmTree = new RBushWasm(9)
92
- wasmTree.loadHybrid(testDataFlat, testData)
93
-
94
- benchmark("JS RBush", () => {
95
- jsTree.search(searchBox)
96
- })
97
-
98
- benchmark("WASM RBush", () => {
99
- wasmTree.search(searchBox)
100
- })
101
- })
102
-
103
- test("Benchmark: Collides", () => {
104
- console.log("\n Collides ")
105
- const jsTree = new RBushJS(9)
106
- jsTree.load(testData)
107
-
108
- const wasmTree = new RBushWasm(9)
109
- wasmTree.loadHybrid(testDataFlat, testData)
110
-
111
- benchmark("JS RBush", () => {
112
- jsTree.collides(searchBox)
113
- })
114
-
115
- benchmark("WASM RBush", () => {
116
- wasmTree.collides(searchBox)
117
- })
118
- })
119
-
120
- test("Benchmark: Remove", () => {
121
- console.log("\n Remove (1000 items) ")
122
-
123
- benchmark(
124
- "JS RBush",
125
- () => {
126
- const tree = new RBushJS(9)
127
- tree.load(testData)
128
- for (const item of itemsToRemove) tree.remove(item)
129
- },
130
- 5
131
- )
132
-
133
- benchmark(
134
- "WASM RBush",
135
- () => {
136
- const tree = new RBushWasm(9)
137
- tree.loadHybrid(testDataFlat, testData)
138
- for (const item of itemsToRemove) tree.remove(item)
139
- },
140
- 5
141
- )
142
- })
143
-
144
- test("Benchmark: Clear", () => {
145
- console.log("\n Clear ")
146
- const jsTree = new RBushJS(9)
147
- jsTree.load(testData)
148
-
149
- const wasmTree = new RBushWasm(9)
150
- wasmTree.loadHybrid(testDataFlat, testData)
151
-
152
- benchmark(
153
- "JS RBush",
154
- () => {
155
- jsTree.clear()
156
- },
157
- 1000
158
- )
159
-
160
- benchmark(
161
- "WASM RBush",
162
- () => {
163
- wasmTree.clear()
164
- },
165
- 1000
166
- )
167
- })
168
- })
package/src/lib.rs DELETED
@@ -1,667 +0,0 @@
1
- use js_sys::{Array, Reflect};
2
- use wasm_bindgen::prelude::*;
3
-
4
-
5
-
6
- #[derive(Clone, Copy, Debug, PartialEq)]
7
- struct Rect {
8
- min_x: f64,
9
- min_y: f64,
10
- max_x: f64,
11
- max_y: f64,
12
- }
13
-
14
- impl Rect {
15
- fn new_empty() -> Self {
16
- Rect {
17
- min_x: f64::INFINITY,
18
- min_y: f64::INFINITY,
19
- max_x: f64::NEG_INFINITY,
20
- max_y: f64::NEG_INFINITY,
21
- }
22
- }
23
-
24
- fn from_js(item: &JsValue) -> Self {
25
- fn get_coord(val: &JsValue, prop: &str) -> f64 {
26
- match Reflect::get(val, &JsValue::from_str(prop)) {
27
- Ok(v) => v.as_f64().unwrap_or(0.0),
28
- Err(_) => 0.0,
29
- }
30
- }
31
- Rect {
32
- min_x: get_coord(item, "minX"),
33
- min_y: get_coord(item, "minY"),
34
- max_x: get_coord(item, "maxX"),
35
- max_y: get_coord(item, "maxY"),
36
- }
37
- }
38
-
39
- fn from_flat(data: &[f64]) -> Self {
40
- Rect {
41
- min_x: data[0],
42
- min_y: data[1],
43
- max_x: data[2],
44
- max_y: data[3],
45
- }
46
- }
47
-
48
- fn area(&self) -> f64 {
49
- (self.max_x - self.min_x) * (self.max_y - self.min_y)
50
- }
51
-
52
- fn margin(&self) -> f64 {
53
- (self.max_x - self.min_x) + (self.max_y - self.min_y)
54
- }
55
-
56
- fn enlarged_area(&self, other: &Rect) -> f64 {
57
- (f64::max(other.max_x, self.max_x) - f64::min(other.min_x, self.min_x))
58
- * (f64::max(other.max_y, self.max_y) - f64::min(other.min_y, self.min_y))
59
- }
60
-
61
- fn intersection_area(&self, other: &Rect) -> f64 {
62
- let min_x = f64::max(self.min_x, other.min_x);
63
- let min_y = f64::max(self.min_y, other.min_y);
64
- let max_x = f64::min(self.max_x, other.max_x);
65
- let max_y = f64::min(self.max_y, other.max_y);
66
-
67
- f64::max(0.0, max_x - min_x) * f64::max(0.0, max_y - min_y)
68
- }
69
-
70
- fn contains(&self, other: &Rect) -> bool {
71
- self.min_x <= other.min_x
72
- && self.min_y <= other.min_y
73
- && other.max_x <= self.max_x
74
- && other.max_y <= self.max_y
75
- }
76
-
77
- fn intersects(&self, other: &Rect) -> bool {
78
- other.min_x <= self.max_x
79
- && other.min_y <= self.max_y
80
- && other.max_x >= self.min_x
81
- && other.max_y >= self.min_y
82
- }
83
-
84
- fn extend(&mut self, other: &Rect) {
85
- self.min_x = f64::min(self.min_x, other.min_x);
86
- self.min_y = f64::min(self.min_y, other.min_y);
87
- self.max_x = f64::max(self.max_x, other.max_x);
88
- self.max_y = f64::max(self.max_y, other.max_y);
89
- }
90
- }
91
-
92
-
93
-
94
- #[derive(Clone)]
95
- struct Entry {
96
- bbox: Rect,
97
- data: JsValue,
98
- is_leaf: bool,
99
- height: usize,
100
- children: Vec<Entry>,
101
- }
102
-
103
- impl Entry {
104
- fn new_leaf(item: JsValue) -> Self {
105
- let bbox = Rect::from_js(&item);
106
- Entry {
107
- bbox,
108
- data: item,
109
- is_leaf: true,
110
- height: 1,
111
- children: vec![],
112
- }
113
- }
114
-
115
- fn new_node(children: Vec<Entry>) -> Self {
116
- let mut node = Entry {
117
- bbox: Rect::new_empty(),
118
- data: JsValue::NULL,
119
- is_leaf: false,
120
- height: 1,
121
- children,
122
- };
123
- node.calc_bbox();
124
- node
125
- }
126
-
127
- fn calc_bbox(&mut self) {
128
- let mut dist_bbox = Rect::new_empty();
129
- for child in &self.children {
130
- dist_bbox.extend(&child.bbox);
131
- }
132
- self.bbox = dist_bbox;
133
- }
134
- }
135
-
136
-
137
-
138
- #[wasm_bindgen]
139
- pub struct RBush {
140
- root: Entry,
141
- max_entries: usize,
142
- min_entries: usize,
143
- }
144
-
145
- #[wasm_bindgen]
146
- impl RBush {
147
- #[wasm_bindgen(constructor)]
148
- pub fn new(max_entries: Option<usize>) -> RBush {
149
- let m = max_entries.unwrap_or(9).max(4);
150
- let min = (m as f64 * 0.4).ceil().max(2.0) as usize;
151
-
152
- RBush {
153
- root: Entry::new_node(vec![]),
154
- max_entries: m,
155
- min_entries: min,
156
- }
157
- }
158
-
159
- pub fn clear(&mut self) {
160
- self.root = Entry::new_node(vec![]);
161
- }
162
-
163
- pub fn all(&self) -> Array {
164
- let result = Array::new();
165
- self._all(&self.root, &result);
166
- result
167
- }
168
-
169
- pub fn search(&self, bbox_js: &JsValue) -> Array {
170
- let bbox = Rect::from_js(bbox_js);
171
- let result = Array::new();
172
- let mut stack = vec![&self.root];
173
-
174
- while let Some(node) = stack.pop() {
175
- if !bbox.intersects(&node.bbox) {
176
- continue;
177
- }
178
-
179
- for child in &node.children {
180
- if bbox.intersects(&child.bbox) {
181
- if child.is_leaf {
182
-
183
- if !child.data.is_null() && !child.data.is_undefined() {
184
- result.push(&child.data);
185
- }
186
- } else if bbox.contains(&child.bbox) {
187
- self._all(child, &result);
188
- } else {
189
- stack.push(child);
190
- }
191
- }
192
- }
193
- }
194
- result
195
- }
196
-
197
- pub fn collides(&self, bbox_js: &JsValue) -> bool {
198
- let bbox = Rect::from_js(bbox_js);
199
- let mut stack = vec![&self.root];
200
-
201
- while let Some(node) = stack.pop() {
202
- if !bbox.intersects(&node.bbox) {
203
- continue;
204
- }
205
-
206
- for child in &node.children {
207
- if bbox.intersects(&child.bbox) {
208
- if child.is_leaf || bbox.contains(&child.bbox) {
209
- return true;
210
- }
211
- stack.push(child);
212
- }
213
- }
214
- }
215
- false
216
- }
217
-
218
- #[wasm_bindgen(js_name = insert)]
219
- pub fn insert(&mut self, item: JsValue) {
220
-
221
- if item.is_null() || item.is_undefined() {
222
- return;
223
- }
224
-
225
- let entry = Entry::new_leaf(item);
226
- self.insert_entry(entry);
227
- }
228
-
229
-
230
- pub fn load(&mut self, data: &Array) {
231
- if data.length() == 0 {
232
- return;
233
- }
234
-
235
- let items: Vec<Entry> = (0..data.length())
236
- .filter_map(|i| {
237
- let val = data.get(i);
238
- if val.is_null() || val.is_undefined() {
239
- return None;
240
- }
241
- Some(Entry::new_leaf(val))
242
- })
243
- .collect();
244
-
245
- if !items.is_empty() {
246
- self.bulk_load(items);
247
- }
248
- }
249
-
250
-
251
- #[wasm_bindgen(js_name = loadHybrid)]
252
- pub fn load_hybrid(&mut self, fast_coords: &[f64], items: &Array) {
253
- if fast_coords.is_empty() {
254
- return;
255
- }
256
-
257
- let count = fast_coords.len() / 4;
258
- let mut entries = Vec::with_capacity(count);
259
-
260
- for i in 0..count {
261
- let item_data = items.get(i as u32);
262
-
263
-
264
- if item_data.is_null() || item_data.is_undefined() {
265
- continue;
266
- }
267
-
268
- let start = i * 4;
269
- let bbox = Rect::from_flat(&fast_coords[start..start + 4]);
270
-
271
- entries.push(Entry {
272
- bbox,
273
- data: item_data,
274
- is_leaf: true,
275
- height: 1,
276
- children: vec![],
277
- });
278
- }
279
-
280
- if !entries.is_empty() {
281
- self.bulk_load(entries);
282
- }
283
- }
284
-
285
- pub fn remove(&mut self, item: JsValue) {
286
- let bbox = Rect::from_js(&item);
287
- let mut items_to_reinsert = Vec::new();
288
-
289
- RBush::remove_from_node(
290
- &mut self.root,
291
- &item,
292
- &bbox,
293
- self.min_entries,
294
- &mut items_to_reinsert,
295
- );
296
-
297
- for item in items_to_reinsert {
298
- self.insert_entry(item);
299
- }
300
-
301
- if !self.root.is_leaf && self.root.children.len() == 1 {
302
- self.root = self.root.children.pop().unwrap();
303
- }
304
- }
305
-
306
-
307
-
308
- fn remove_from_node(
309
- node: &mut Entry,
310
- item: &JsValue,
311
- bbox: &Rect,
312
- min_entries: usize,
313
- reinsert: &mut Vec<Entry>,
314
- ) -> bool {
315
- if node.is_leaf {
316
- let mut index = None;
317
- for (i, child) in node.children.iter().enumerate() {
318
- if &child.data == item {
319
- index = Some(i);
320
- break;
321
- }
322
- }
323
-
324
- if let Some(idx) = index {
325
- node.children.remove(idx);
326
- node.calc_bbox();
327
- return true;
328
- }
329
- return false;
330
- }
331
-
332
- let mut removed = false;
333
- let mut removal_index = None;
334
-
335
- for (i, child) in node.children.iter_mut().enumerate() {
336
- if child.bbox.contains(bbox) {
337
- if RBush::remove_from_node(child, item, bbox, min_entries, reinsert) {
338
- removed = true;
339
- if child.children.len() < min_entries {
340
- removal_index = Some(i);
341
- } else {
342
- child.calc_bbox();
343
- }
344
- break;
345
- }
346
- }
347
- }
348
-
349
- if let Some(idx) = removal_index {
350
- let underflowed_child = node.children.remove(idx);
351
- RBush::collect_items(&underflowed_child, reinsert);
352
- node.calc_bbox();
353
- } else if removed {
354
- node.calc_bbox();
355
- }
356
-
357
- removed
358
- }
359
-
360
- fn collect_items(node: &Entry, acc: &mut Vec<Entry>) {
361
- if node.is_leaf {
362
- for child in &node.children {
363
- acc.push(child.clone());
364
- }
365
- } else {
366
- for child in &node.children {
367
- RBush::collect_items(child, acc);
368
- }
369
- }
370
- }
371
-
372
- fn bulk_load(&mut self, mut items: Vec<Entry>) {
373
- if items.len() < self.min_entries {
374
- for item in items {
375
- self.insert_entry(item);
376
- }
377
- return;
378
- }
379
-
380
- let len = items.len();
381
- let node = self._build(&mut items, 0, len - 1, 0);
382
-
383
- if self.root.children.is_empty() {
384
- self.root = node;
385
- } else if self.root.height == node.height {
386
- self._split_root(node);
387
- } else {
388
- if self.root.height < node.height {
389
- let tmp = self.root.clone();
390
- self.root = node;
391
- let level = self.root.height - tmp.height - 1;
392
- self._insert_at_level(tmp, level);
393
- } else {
394
- let level = self.root.height - node.height - 1;
395
- self._insert_at_level(node, level);
396
- }
397
- }
398
- }
399
-
400
- fn insert_entry(&mut self, item: Entry) {
401
- let level = self.root.height - 1;
402
- self._insert_at_level(item, level);
403
- }
404
-
405
- fn _insert_at_level(&mut self, item: Entry, level: usize) {
406
- let split = RBush::insert_recursive(
407
- &mut self.root,
408
- item,
409
- level,
410
- self.max_entries,
411
- self.min_entries,
412
- );
413
- if let Some(new_node) = split {
414
- self._split_root(new_node);
415
- }
416
- }
417
-
418
- fn _split_root(&mut self, new_node: Entry) {
419
- let old_root_children = std::mem::take(&mut self.root.children);
420
- let mut old_root = Entry::new_node(old_root_children);
421
- old_root.height = self.root.height;
422
- old_root.calc_bbox();
423
-
424
- self.root.height += 1;
425
- self.root.is_leaf = false;
426
- self.root.children = vec![old_root, new_node];
427
- self.root.calc_bbox();
428
- }
429
-
430
- fn _all(&self, node: &Entry, result: &Array) {
431
- let mut stack = vec![node];
432
- while let Some(n) = stack.pop() {
433
- for child in &n.children {
434
- if child.is_leaf {
435
- if !child.data.is_null() && !child.data.is_undefined() {
436
- result.push(&child.data);
437
- }
438
- } else {
439
- stack.push(child);
440
- }
441
- }
442
- }
443
- }
444
-
445
- fn _build(&self, items: &mut [Entry], left: usize, right: usize, height: usize) -> Entry {
446
- let n = right - left + 1;
447
- let mut m = self.max_entries;
448
-
449
- if n <= m {
450
- let children = items[left..=right].to_vec();
451
- return Entry::new_node(children);
452
- }
453
-
454
- let mut target_height = height;
455
- if target_height == 0 {
456
- target_height = (n as f64).log(m as f64).ceil() as usize;
457
- m = (n as f64 / (m as f64).powi((target_height - 1) as i32)).ceil() as usize;
458
- }
459
-
460
- let mut node = Entry::new_node(vec![]);
461
- node.height = target_height;
462
-
463
- let n2 = (n as f64 / m as f64).ceil() as usize;
464
- let n1 = n2 * (m as f64).sqrt().ceil() as usize;
465
-
466
- RBush::multi_select(items, left, right, n1, true);
467
-
468
- let mut children = vec![];
469
- let mut i = left;
470
- while i <= right {
471
- let right2 = std::cmp::min(i + n1 - 1, right);
472
-
473
- RBush::multi_select(items, i, right2, n2, false);
474
-
475
- let mut j = i;
476
- while j <= right2 {
477
- let right3 = std::cmp::min(j + n2 - 1, right2);
478
- children.push(self._build(items, j, right3, target_height - 1));
479
- j += n2;
480
- }
481
- i += n1;
482
- }
483
-
484
- node.children = children;
485
- node.calc_bbox();
486
- node
487
- }
488
-
489
- fn multi_select(arr: &mut [Entry], left: usize, right: usize, n: usize, compare_x: bool) {
490
- let mut stack = vec![(left, right)];
491
-
492
- while let Some((l, r)) = stack.pop() {
493
- if r - l <= n {
494
- continue;
495
- }
496
-
497
- let mid = l + ((r - l) as f64 / n as f64 / 2.0).ceil() as usize * n;
498
- let target_idx = mid - l;
499
- let slice = &mut arr[l..=r];
500
-
501
- if compare_x {
502
- slice.select_nth_unstable_by(target_idx, |a, b| {
503
- a.bbox.min_x.partial_cmp(&b.bbox.min_x).unwrap()
504
- });
505
- } else {
506
- slice.select_nth_unstable_by(target_idx, |a, b| {
507
- a.bbox.min_y.partial_cmp(&b.bbox.min_y).unwrap()
508
- });
509
- }
510
-
511
- stack.push((l, mid));
512
- stack.push((mid, r));
513
- }
514
- }
515
-
516
- fn insert_recursive(
517
- node: &mut Entry,
518
- item: Entry,
519
- target_level: usize,
520
- max_entries: usize,
521
- min_entries: usize,
522
- ) -> Option<Entry> {
523
- node.bbox.extend(&item.bbox);
524
-
525
- if node.height - 1 == target_level {
526
- node.children.push(item);
527
- if node.children.len() > max_entries {
528
- return Some(RBush::split(node, min_entries));
529
- }
530
- return None;
531
- }
532
-
533
- let best_index = RBush::choose_subtree(node, &item.bbox);
534
-
535
- let split_node = RBush::insert_recursive(
536
- &mut node.children[best_index],
537
- item,
538
- target_level,
539
- max_entries,
540
- min_entries,
541
- );
542
-
543
- if let Some(new_child) = split_node {
544
- node.children.push(new_child);
545
- if node.children.len() > max_entries {
546
- return Some(RBush::split(node, min_entries));
547
- }
548
- }
549
-
550
- None
551
- }
552
-
553
- fn choose_subtree(node: &Entry, bbox: &Rect) -> usize {
554
- let mut best_index = 0;
555
- let mut min_enlargement = f64::INFINITY;
556
- let mut min_area = f64::INFINITY;
557
-
558
- for (i, child) in node.children.iter().enumerate() {
559
- let area = child.bbox.area();
560
- let enlargement = bbox.enlarged_area(&child.bbox) - area;
561
-
562
- if enlargement < min_enlargement {
563
- min_enlargement = enlargement;
564
- min_area = if area < min_area { area } else { min_area };
565
- best_index = i;
566
- } else if enlargement == min_enlargement {
567
- if area < min_area {
568
- min_area = area;
569
- best_index = i;
570
- }
571
- }
572
- }
573
- best_index
574
- }
575
-
576
- fn split(node: &mut Entry, min_entries: usize) -> Entry {
577
- let count = node.children.len();
578
-
579
- RBush::choose_split_axis(node, min_entries, count);
580
- let split_index = RBush::choose_split_index(node, min_entries, count);
581
-
582
- let new_children = node.children.split_off(split_index);
583
- let mut new_node = Entry::new_node(new_children);
584
- new_node.height = node.height;
585
-
586
- node.calc_bbox();
587
- new_node.calc_bbox();
588
-
589
- new_node
590
- }
591
-
592
- fn choose_split_axis(node: &mut Entry, m: usize, count: usize) {
593
- let x_margin = RBush::all_dist_margin(node, m, count, true);
594
- let y_margin = RBush::all_dist_margin(node, m, count, false);
595
-
596
- if x_margin < y_margin {
597
- node.children
598
- .sort_by(|a, b| a.bbox.min_x.partial_cmp(&b.bbox.min_x).unwrap());
599
- }
600
- }
601
-
602
- fn all_dist_margin(node: &mut Entry, m: usize, count: usize, compare_x: bool) -> f64 {
603
- if compare_x {
604
- node.children
605
- .sort_by(|a, b| a.bbox.min_x.partial_cmp(&b.bbox.min_x).unwrap());
606
- } else {
607
- node.children
608
- .sort_by(|a, b| a.bbox.min_y.partial_cmp(&b.bbox.min_y).unwrap());
609
- }
610
-
611
- let mut left_bbox = Rect::new_empty();
612
- let mut right_bbox = Rect::new_empty();
613
-
614
- for i in 0..m {
615
- left_bbox.extend(&node.children[i].bbox);
616
- }
617
- for i in (count - m)..count {
618
- right_bbox.extend(&node.children[i].bbox);
619
- }
620
-
621
- let mut margin = left_bbox.margin() + right_bbox.margin();
622
-
623
- for i in m..(count - m) {
624
- left_bbox.extend(&node.children[i].bbox);
625
- margin += left_bbox.margin();
626
- }
627
- for i in ((m)..(count - m)).rev() {
628
- right_bbox.extend(&node.children[i].bbox);
629
- margin += right_bbox.margin();
630
- }
631
-
632
- margin
633
- }
634
-
635
- fn choose_split_index(node: &Entry, m: usize, count: usize) -> usize {
636
- let mut min_overlap = f64::INFINITY;
637
- let mut min_area = f64::INFINITY;
638
- let mut index = count - m;
639
-
640
- for i in m..=(count - m) {
641
- let mut bbox1 = Rect::new_empty();
642
- let mut bbox2 = Rect::new_empty();
643
-
644
- for c in &node.children[0..i] {
645
- bbox1.extend(&c.bbox);
646
- }
647
- for c in &node.children[i..count] {
648
- bbox2.extend(&c.bbox);
649
- }
650
-
651
- let overlap = bbox1.intersection_area(&bbox2);
652
- let area = bbox1.area() + bbox2.area();
653
-
654
- if overlap < min_overlap {
655
- min_overlap = overlap;
656
- index = i;
657
- min_area = if area < min_area { area } else { min_area };
658
- } else if overlap == min_overlap {
659
- if area < min_area {
660
- min_area = area;
661
- index = i;
662
- }
663
- }
664
- }
665
- index
666
- }
667
- }