rbush-rs 1.0.0 → 1.0.2

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,19 @@
1
1
  {
2
2
  "name": "rbush-rs",
3
- "version": "1.0.0",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "1.0.2",
4
+ "description": "High-performance RBush port in WebAssembly (Rust)",
5
+ "main": "rbush.js",
6
+ "module": "rbush.js",
7
+ "type": "module",
8
+ "files": [
9
+ "rbush.js",
10
+ "pkg/"
11
+ ],
6
12
  "scripts": {
7
- "test": "jest"
13
+ "build": "wasm-pack build --target bundler",
14
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
8
15
  },
9
- "repository": {
10
- "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"
19
- },
20
- "homepage": "https://github.com/ppmpreetham/rbush-rs#readme",
21
16
  "devDependencies": {
22
- "jest": "^30.2.0",
23
- "rbush": "^3.0.1"
17
+ "jest": "^30.2.0"
24
18
  }
25
- }
19
+ }
package/rbush.js ADDED
@@ -0,0 +1,42 @@
1
+ import { RBush as WasmRBush } from './pkg/rbush_rs.js';
2
+
3
+ export default class RBush {
4
+ constructor(maxEntries = 9) {
5
+ this._tree = new WasmRBush(maxEntries);
6
+ }
7
+
8
+ toBBox(item) { return item; }
9
+
10
+ insert(item) {
11
+ const b = this.toBBox(item);
12
+ this._tree.insert({
13
+ ...item,
14
+ minX: b.minX, minY: b.minY, maxX: b.maxX, maxY: b.maxY
15
+ });
16
+ return this;
17
+ }
18
+
19
+ load(data) {
20
+ const normalized = data.map(item => {
21
+ const b = this.toBBox(item);
22
+ return { ...item, minX: b.minX, minY: b.minY, maxX: b.maxX, maxY: b.maxY };
23
+ });
24
+ this._tree.load(normalized);
25
+ return this;
26
+ }
27
+
28
+ remove(item) {
29
+ const b = this.toBBox(item);
30
+ this._tree.remove({ ...item, minX: b.minX, minY: b.minY, maxX: b.maxX, maxY: b.maxY });
31
+ return this;
32
+ }
33
+
34
+ search(bbox) { return this._tree.search(bbox); }
35
+ collides(bbox) { return this._tree.collides(bbox); }
36
+ all() { return this._tree.all(); }
37
+ clear() { this._tree.clear(); return this; }
38
+ toJSON() { return this._tree.toJSON(); }
39
+ fromJSON(data) { this._tree.fromJSON(data); return this; }
40
+
41
+ destroy() { this._tree.free(); }
42
+ }
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
- }