智慧水务管理系统 - 精河县供水工程综合管理平台

rbush.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.RBush = factory());
  5. })(this, (function () { 'use strict';
  6. /**
  7. * Rearranges items so that all items in the [left, k] are the smallest.
  8. * The k-th element will have the (k - left + 1)-th smallest value in [left, right].
  9. *
  10. * @template T
  11. * @param {T[]} arr the array to partially sort (in place)
  12. * @param {number} k middle index for partial sorting (as defined above)
  13. * @param {number} [left=0] left index of the range to sort
  14. * @param {number} [right=arr.length-1] right index
  15. * @param {(a: T, b: T) => number} [compare = (a, b) => a - b] compare function
  16. */
  17. function quickselect(arr, k, left = 0, right = arr.length - 1, compare = defaultCompare) {
  18. while (right > left) {
  19. if (right - left > 600) {
  20. const n = right - left + 1;
  21. const m = k - left + 1;
  22. const z = Math.log(n);
  23. const s = 0.5 * Math.exp(2 * z / 3);
  24. const sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1);
  25. const newLeft = Math.max(left, Math.floor(k - m * s / n + sd));
  26. const newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd));
  27. quickselect(arr, k, newLeft, newRight, compare);
  28. }
  29. const t = arr[k];
  30. let i = left;
  31. /** @type {number} */
  32. let j = right;
  33. swap(arr, left, k);
  34. if (compare(arr[right], t) > 0) swap(arr, left, right);
  35. while (i < j) {
  36. swap(arr, i, j);
  37. i++;
  38. j--;
  39. while (compare(arr[i], t) < 0) i++;
  40. while (compare(arr[j], t) > 0) j--;
  41. }
  42. if (compare(arr[left], t) === 0) swap(arr, left, j);
  43. else {
  44. j++;
  45. swap(arr, j, right);
  46. }
  47. if (j <= k) left = j + 1;
  48. if (k <= j) right = j - 1;
  49. }
  50. }
  51. /**
  52. * @template T
  53. * @param {T[]} arr
  54. * @param {number} i
  55. * @param {number} j
  56. */
  57. function swap(arr, i, j) {
  58. const tmp = arr[i];
  59. arr[i] = arr[j];
  60. arr[j] = tmp;
  61. }
  62. /**
  63. * @template T
  64. * @param {T} a
  65. * @param {T} b
  66. * @returns {number}
  67. */
  68. function defaultCompare(a, b) {
  69. return a < b ? -1 : a > b ? 1 : 0;
  70. }
  71. class RBush {
  72. constructor(maxEntries = 9) {
  73. // max entries in a node is 9 by default; min node fill is 40% for best performance
  74. this._maxEntries = Math.max(4, maxEntries);
  75. this._minEntries = Math.max(2, Math.ceil(this._maxEntries * 0.4));
  76. this.clear();
  77. }
  78. all() {
  79. return this._all(this.data, []);
  80. }
  81. search(bbox) {
  82. let node = this.data;
  83. const result = [];
  84. if (!intersects(bbox, node)) return result;
  85. const toBBox = this.toBBox;
  86. const nodesToSearch = [];
  87. while (node) {
  88. for (let i = 0; i < node.children.length; i++) {
  89. const child = node.children[i];
  90. const childBBox = node.leaf ? toBBox(child) : child;
  91. if (intersects(bbox, childBBox)) {
  92. if (node.leaf) result.push(child);
  93. else if (contains(bbox, childBBox)) this._all(child, result);
  94. else nodesToSearch.push(child);
  95. }
  96. }
  97. node = nodesToSearch.pop();
  98. }
  99. return result;
  100. }
  101. collides(bbox) {
  102. let node = this.data;
  103. if (!intersects(bbox, node)) return false;
  104. const nodesToSearch = [];
  105. while (node) {
  106. for (let i = 0; i < node.children.length; i++) {
  107. const child = node.children[i];
  108. const childBBox = node.leaf ? this.toBBox(child) : child;
  109. if (intersects(bbox, childBBox)) {
  110. if (node.leaf || contains(bbox, childBBox)) return true;
  111. nodesToSearch.push(child);
  112. }
  113. }
  114. node = nodesToSearch.pop();
  115. }
  116. return false;
  117. }
  118. load(data) {
  119. if (!(data && data.length)) return this;
  120. if (data.length < this._minEntries) {
  121. for (let i = 0; i < data.length; i++) {
  122. this.insert(data[i]);
  123. }
  124. return this;
  125. }
  126. // recursively build the tree with the given data from scratch using OMT algorithm
  127. let node = this._build(data.slice(), 0, data.length - 1, 0);
  128. if (!this.data.children.length) {
  129. // save as is if tree is empty
  130. this.data = node;
  131. } else if (this.data.height === node.height) {
  132. // split root if trees have the same height
  133. this._splitRoot(this.data, node);
  134. } else {
  135. if (this.data.height < node.height) {
  136. // swap trees if inserted one is bigger
  137. const tmpNode = this.data;
  138. this.data = node;
  139. node = tmpNode;
  140. }
  141. // insert the small tree into the large tree at appropriate level
  142. this._insert(node, this.data.height - node.height - 1, true);
  143. }
  144. return this;
  145. }
  146. insert(item) {
  147. if (item) this._insert(item, this.data.height - 1);
  148. return this;
  149. }
  150. clear() {
  151. this.data = createNode([]);
  152. return this;
  153. }
  154. remove(item, equalsFn) {
  155. if (!item) return this;
  156. let node = this.data;
  157. const bbox = this.toBBox(item);
  158. const path = [];
  159. const indexes = [];
  160. let i, parent, goingUp;
  161. // depth-first iterative tree traversal
  162. while (node || path.length) {
  163. if (!node) { // go up
  164. node = path.pop();
  165. parent = path[path.length - 1];
  166. i = indexes.pop();
  167. goingUp = true;
  168. }
  169. if (node.leaf) { // check current node
  170. const index = findItem(item, node.children, equalsFn);
  171. if (index !== -1) {
  172. // item found, remove the item and condense tree upwards
  173. node.children.splice(index, 1);
  174. path.push(node);
  175. this._condense(path);
  176. return this;
  177. }
  178. }
  179. if (!goingUp && !node.leaf && contains(node, bbox)) { // go down
  180. path.push(node);
  181. indexes.push(i);
  182. i = 0;
  183. parent = node;
  184. node = node.children[0];
  185. } else if (parent) { // go right
  186. i++;
  187. node = parent.children[i];
  188. goingUp = false;
  189. } else node = null; // nothing found
  190. }
  191. return this;
  192. }
  193. toBBox(item) { return item; }
  194. compareMinX(a, b) { return a.minX - b.minX; }
  195. compareMinY(a, b) { return a.minY - b.minY; }
  196. toJSON() { return this.data; }
  197. fromJSON(data) {
  198. this.data = data;
  199. return this;
  200. }
  201. _all(node, result) {
  202. const nodesToSearch = [];
  203. while (node) {
  204. if (node.leaf) result.push(...node.children);
  205. else nodesToSearch.push(...node.children);
  206. node = nodesToSearch.pop();
  207. }
  208. return result;
  209. }
  210. _build(items, left, right, height) {
  211. const N = right - left + 1;
  212. let M = this._maxEntries;
  213. let node;
  214. if (N <= M) {
  215. // reached leaf level; return leaf
  216. node = createNode(items.slice(left, right + 1));
  217. calcBBox(node, this.toBBox);
  218. return node;
  219. }
  220. if (!height) {
  221. // target height of the bulk-loaded tree
  222. height = Math.ceil(Math.log(N) / Math.log(M));
  223. // target number of root entries to maximize storage utilization
  224. M = Math.ceil(N / Math.pow(M, height - 1));
  225. }
  226. node = createNode([]);
  227. node.leaf = false;
  228. node.height = height;
  229. // split the items into M mostly square tiles
  230. const N2 = Math.ceil(N / M);
  231. const N1 = N2 * Math.ceil(Math.sqrt(M));
  232. multiSelect(items, left, right, N1, this.compareMinX);
  233. for (let i = left; i <= right; i += N1) {
  234. const right2 = Math.min(i + N1 - 1, right);
  235. multiSelect(items, i, right2, N2, this.compareMinY);
  236. for (let j = i; j <= right2; j += N2) {
  237. const right3 = Math.min(j + N2 - 1, right2);
  238. // pack each entry recursively
  239. node.children.push(this._build(items, j, right3, height - 1));
  240. }
  241. }
  242. calcBBox(node, this.toBBox);
  243. return node;
  244. }
  245. _chooseSubtree(bbox, node, level, path) {
  246. while (true) {
  247. path.push(node);
  248. if (node.leaf || path.length - 1 === level) break;
  249. let minArea = Infinity;
  250. let minEnlargement = Infinity;
  251. let targetNode;
  252. for (let i = 0; i < node.children.length; i++) {
  253. const child = node.children[i];
  254. const area = bboxArea(child);
  255. const enlargement = enlargedArea(bbox, child) - area;
  256. // choose entry with the least area enlargement
  257. if (enlargement < minEnlargement) {
  258. minEnlargement = enlargement;
  259. minArea = area < minArea ? area : minArea;
  260. targetNode = child;
  261. } else if (enlargement === minEnlargement) {
  262. // otherwise choose one with the smallest area
  263. if (area < minArea) {
  264. minArea = area;
  265. targetNode = child;
  266. }
  267. }
  268. }
  269. node = targetNode || node.children[0];
  270. }
  271. return node;
  272. }
  273. _insert(item, level, isNode) {
  274. const bbox = isNode ? item : this.toBBox(item);
  275. const insertPath = [];
  276. // find the best node for accommodating the item, saving all nodes along the path too
  277. const node = this._chooseSubtree(bbox, this.data, level, insertPath);
  278. // put the item into the node
  279. node.children.push(item);
  280. extend(node, bbox);
  281. // split on node overflow; propagate upwards if necessary
  282. while (level >= 0) {
  283. if (insertPath[level].children.length > this._maxEntries) {
  284. this._split(insertPath, level);
  285. level--;
  286. } else break;
  287. }
  288. // adjust bboxes along the insertion path
  289. this._adjustParentBBoxes(bbox, insertPath, level);
  290. }
  291. // split overflowed node into two
  292. _split(insertPath, level) {
  293. const node = insertPath[level];
  294. const M = node.children.length;
  295. const m = this._minEntries;
  296. this._chooseSplitAxis(node, m, M);
  297. const splitIndex = this._chooseSplitIndex(node, m, M);
  298. const newNode = createNode(node.children.splice(splitIndex, node.children.length - splitIndex));
  299. newNode.height = node.height;
  300. newNode.leaf = node.leaf;
  301. calcBBox(node, this.toBBox);
  302. calcBBox(newNode, this.toBBox);
  303. if (level) insertPath[level - 1].children.push(newNode);
  304. else this._splitRoot(node, newNode);
  305. }
  306. _splitRoot(node, newNode) {
  307. // split root node
  308. this.data = createNode([node, newNode]);
  309. this.data.height = node.height + 1;
  310. this.data.leaf = false;
  311. calcBBox(this.data, this.toBBox);
  312. }
  313. _chooseSplitIndex(node, m, M) {
  314. let index;
  315. let minOverlap = Infinity;
  316. let minArea = Infinity;
  317. for (let i = m; i <= M - m; i++) {
  318. const bbox1 = distBBox(node, 0, i, this.toBBox);
  319. const bbox2 = distBBox(node, i, M, this.toBBox);
  320. const overlap = intersectionArea(bbox1, bbox2);
  321. const area = bboxArea(bbox1) + bboxArea(bbox2);
  322. // choose distribution with minimum overlap
  323. if (overlap < minOverlap) {
  324. minOverlap = overlap;
  325. index = i;
  326. minArea = area < minArea ? area : minArea;
  327. } else if (overlap === minOverlap) {
  328. // otherwise choose distribution with minimum area
  329. if (area < minArea) {
  330. minArea = area;
  331. index = i;
  332. }
  333. }
  334. }
  335. return index || M - m;
  336. }
  337. // sorts node children by the best axis for split
  338. _chooseSplitAxis(node, m, M) {
  339. const compareMinX = node.leaf ? this.compareMinX : compareNodeMinX;
  340. const compareMinY = node.leaf ? this.compareMinY : compareNodeMinY;
  341. const xMargin = this._allDistMargin(node, m, M, compareMinX);
  342. const yMargin = this._allDistMargin(node, m, M, compareMinY);
  343. // if total distributions margin value is minimal for x, sort by minX,
  344. // otherwise it's already sorted by minY
  345. if (xMargin < yMargin) node.children.sort(compareMinX);
  346. }
  347. // total margin of all possible split distributions where each node is at least m full
  348. _allDistMargin(node, m, M, compare) {
  349. node.children.sort(compare);
  350. const toBBox = this.toBBox;
  351. const leftBBox = distBBox(node, 0, m, toBBox);
  352. const rightBBox = distBBox(node, M - m, M, toBBox);
  353. let margin = bboxMargin(leftBBox) + bboxMargin(rightBBox);
  354. for (let i = m; i < M - m; i++) {
  355. const child = node.children[i];
  356. extend(leftBBox, node.leaf ? toBBox(child) : child);
  357. margin += bboxMargin(leftBBox);
  358. }
  359. for (let i = M - m - 1; i >= m; i--) {
  360. const child = node.children[i];
  361. extend(rightBBox, node.leaf ? toBBox(child) : child);
  362. margin += bboxMargin(rightBBox);
  363. }
  364. return margin;
  365. }
  366. _adjustParentBBoxes(bbox, path, level) {
  367. // adjust bboxes along the given tree path
  368. for (let i = level; i >= 0; i--) {
  369. extend(path[i], bbox);
  370. }
  371. }
  372. _condense(path) {
  373. // go through the path, removing empty nodes and updating bboxes
  374. for (let i = path.length - 1, siblings; i >= 0; i--) {
  375. if (path[i].children.length === 0) {
  376. if (i > 0) {
  377. siblings = path[i - 1].children;
  378. siblings.splice(siblings.indexOf(path[i]), 1);
  379. } else this.clear();
  380. } else calcBBox(path[i], this.toBBox);
  381. }
  382. }
  383. }
  384. function findItem(item, items, equalsFn) {
  385. if (!equalsFn) return items.indexOf(item);
  386. for (let i = 0; i < items.length; i++) {
  387. if (equalsFn(item, items[i])) return i;
  388. }
  389. return -1;
  390. }
  391. // calculate node's bbox from bboxes of its children
  392. function calcBBox(node, toBBox) {
  393. distBBox(node, 0, node.children.length, toBBox, node);
  394. }
  395. // min bounding rectangle of node children from k to p-1
  396. function distBBox(node, k, p, toBBox, destNode) {
  397. if (!destNode) destNode = createNode(null);
  398. destNode.minX = Infinity;
  399. destNode.minY = Infinity;
  400. destNode.maxX = -Infinity;
  401. destNode.maxY = -Infinity;
  402. for (let i = k; i < p; i++) {
  403. const child = node.children[i];
  404. extend(destNode, node.leaf ? toBBox(child) : child);
  405. }
  406. return destNode;
  407. }
  408. function extend(a, b) {
  409. a.minX = Math.min(a.minX, b.minX);
  410. a.minY = Math.min(a.minY, b.minY);
  411. a.maxX = Math.max(a.maxX, b.maxX);
  412. a.maxY = Math.max(a.maxY, b.maxY);
  413. return a;
  414. }
  415. function compareNodeMinX(a, b) { return a.minX - b.minX; }
  416. function compareNodeMinY(a, b) { return a.minY - b.minY; }
  417. function bboxArea(a) { return (a.maxX - a.minX) * (a.maxY - a.minY); }
  418. function bboxMargin(a) { return (a.maxX - a.minX) + (a.maxY - a.minY); }
  419. function enlargedArea(a, b) {
  420. return (Math.max(b.maxX, a.maxX) - Math.min(b.minX, a.minX)) *
  421. (Math.max(b.maxY, a.maxY) - Math.min(b.minY, a.minY));
  422. }
  423. function intersectionArea(a, b) {
  424. const minX = Math.max(a.minX, b.minX);
  425. const minY = Math.max(a.minY, b.minY);
  426. const maxX = Math.min(a.maxX, b.maxX);
  427. const maxY = Math.min(a.maxY, b.maxY);
  428. return Math.max(0, maxX - minX) *
  429. Math.max(0, maxY - minY);
  430. }
  431. function contains(a, b) {
  432. return a.minX <= b.minX &&
  433. a.minY <= b.minY &&
  434. b.maxX <= a.maxX &&
  435. b.maxY <= a.maxY;
  436. }
  437. function intersects(a, b) {
  438. return b.minX <= a.maxX &&
  439. b.minY <= a.maxY &&
  440. b.maxX >= a.minX &&
  441. b.maxY >= a.minY;
  442. }
  443. function createNode(children) {
  444. return {
  445. children,
  446. height: 1,
  447. leaf: true,
  448. minX: Infinity,
  449. minY: Infinity,
  450. maxX: -Infinity,
  451. maxY: -Infinity
  452. };
  453. }
  454. // sort an array so that items come in groups of n unsorted items, with groups sorted between each other;
  455. // combines selection algorithm with binary divide & conquer approach
  456. function multiSelect(arr, left, right, n, compare) {
  457. const stack = [left, right];
  458. while (stack.length) {
  459. right = stack.pop();
  460. left = stack.pop();
  461. if (right - left <= n) continue;
  462. const mid = left + Math.ceil((right - left) / n / 2) * n;
  463. quickselect(arr, mid, left, right, compare);
  464. stack.push(left, mid, mid, right);
  465. }
  466. }
  467. return RBush;
  468. }));