d3-binarytree.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // https://github.com/vasturiano/d3-binarytree v0.2.0 Copyright 2021 Vasco Asturiano
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
  6. }(this, (function (exports) { 'use strict';
  7. function tree_add(d) {
  8. var x = +this._x.call(null, d);
  9. return add(this.cover(x), x, d);
  10. }
  11. function add(tree, x, d) {
  12. if (isNaN(x)) return tree; // ignore invalid points
  13. var parent,
  14. node = tree._root,
  15. leaf = {data: d},
  16. x0 = tree._x0,
  17. x1 = tree._x1,
  18. xm,
  19. xp,
  20. right,
  21. i,
  22. j;
  23. // If the tree is empty, initialize the root as a leaf.
  24. if (!node) return tree._root = leaf, tree;
  25. // Find the existing leaf for the new point, or add it.
  26. while (node.length) {
  27. if (right = x >= (xm = (x0 + x1) / 2)) x0 = xm; else x1 = xm;
  28. if (parent = node, !(node = node[i = +right])) return parent[i] = leaf, tree;
  29. }
  30. // Is the new point is exactly coincident with the existing point?
  31. xp = +tree._x.call(null, node.data);
  32. if (x === xp) return leaf.next = node, parent ? parent[i] = leaf : tree._root = leaf, tree;
  33. // Otherwise, split the leaf node until the old and new point are separated.
  34. do {
  35. parent = parent ? parent[i] = new Array(2) : tree._root = new Array(2);
  36. if (right = x >= (xm = (x0 + x1) / 2)) x0 = xm; else x1 = xm;
  37. } while ((i = +right) === (j = +(xp >= xm)));
  38. return parent[j] = node, parent[i] = leaf, tree;
  39. }
  40. function addAll(data) {
  41. var i, n = data.length,
  42. x,
  43. xz = new Array(n),
  44. x0 = Infinity,
  45. x1 = -Infinity;
  46. // Compute the points and their extent.
  47. for (i = 0; i < n; ++i) {
  48. if (isNaN(x = +this._x.call(null, data[i]))) continue;
  49. xz[i] = x;
  50. if (x < x0) x0 = x;
  51. if (x > x1) x1 = x;
  52. }
  53. // If there were no (valid) points, abort.
  54. if (x0 > x1) return this;
  55. // Expand the tree to cover the new points.
  56. this.cover(x0).cover(x1);
  57. // Add the new points.
  58. for (i = 0; i < n; ++i) {
  59. add(this, xz[i], data[i]);
  60. }
  61. return this;
  62. }
  63. function tree_cover(x) {
  64. if (isNaN(x = +x)) return this; // ignore invalid points
  65. var x0 = this._x0,
  66. x1 = this._x1;
  67. // If the binarytree has no extent, initialize them.
  68. // Integer extent are necessary so that if we later double the extent,
  69. // the existing half boundaries don’t change due to floating point error!
  70. if (isNaN(x0)) {
  71. x1 = (x0 = Math.floor(x)) + 1;
  72. }
  73. // Otherwise, double repeatedly to cover.
  74. else {
  75. var z = x1 - x0 || 1,
  76. node = this._root,
  77. parent,
  78. i;
  79. while (x0 > x || x >= x1) {
  80. i = +(x < x0);
  81. parent = new Array(2), parent[i] = node, node = parent, z *= 2;
  82. switch (i) {
  83. case 0: x1 = x0 + z; break;
  84. case 1: x0 = x1 - z; break;
  85. }
  86. }
  87. if (this._root && this._root.length) this._root = node;
  88. }
  89. this._x0 = x0;
  90. this._x1 = x1;
  91. return this;
  92. }
  93. function tree_data() {
  94. var data = [];
  95. this.visit(function(node) {
  96. if (!node.length) do data.push(node.data); while (node = node.next)
  97. });
  98. return data;
  99. }
  100. function tree_extent(_) {
  101. return arguments.length
  102. ? this.cover(+_[0][0]).cover(+_[1][0])
  103. : isNaN(this._x0) ? undefined : [[this._x0], [this._x1]];
  104. }
  105. function Half(node, x0, x1) {
  106. this.node = node;
  107. this.x0 = x0;
  108. this.x1 = x1;
  109. }
  110. function tree_find(x, radius) {
  111. var data,
  112. x0 = this._x0,
  113. x1,
  114. x2,
  115. x3 = this._x1,
  116. halves = [],
  117. node = this._root,
  118. q,
  119. i;
  120. if (node) halves.push(new Half(node, x0, x3));
  121. if (radius == null) radius = Infinity;
  122. else {
  123. x0 = x - radius;
  124. x3 = x + radius;
  125. }
  126. while (q = halves.pop()) {
  127. // Stop searching if this half can’t contain a closer node.
  128. if (!(node = q.node)
  129. || (x1 = q.x0) > x3
  130. || (x2 = q.x1) < x0) continue;
  131. // Bisect the current half.
  132. if (node.length) {
  133. var xm = (x1 + x2) / 2;
  134. halves.push(
  135. new Half(node[1], xm, x2),
  136. new Half(node[0], x1, xm)
  137. );
  138. // Visit the closest half first.
  139. if (i = +(x >= xm)) {
  140. q = halves[halves.length - 1];
  141. halves[halves.length - 1] = halves[halves.length - 1 - i];
  142. halves[halves.length - 1 - i] = q;
  143. }
  144. }
  145. // Visit this point. (Visiting coincident points isn’t necessary!)
  146. else {
  147. var d = Math.abs(x - +this._x.call(null, node.data));
  148. if (d < radius) {
  149. radius = d;
  150. x0 = x - d;
  151. x3 = x + d;
  152. data = node.data;
  153. }
  154. }
  155. }
  156. return data;
  157. }
  158. function tree_remove(d) {
  159. if (isNaN(x = +this._x.call(null, d))) return this; // ignore invalid points
  160. var parent,
  161. node = this._root,
  162. retainer,
  163. previous,
  164. next,
  165. x0 = this._x0,
  166. x1 = this._x1,
  167. x,
  168. xm,
  169. right,
  170. i,
  171. j;
  172. // If the tree is empty, initialize the root as a leaf.
  173. if (!node) return this;
  174. // Find the leaf node for the point.
  175. // While descending, also retain the deepest parent with a non-removed sibling.
  176. if (node.length) while (true) {
  177. if (right = x >= (xm = (x0 + x1) / 2)) x0 = xm; else x1 = xm;
  178. if (!(parent = node, node = node[i = +right])) return this;
  179. if (!node.length) break;
  180. if (parent[(i + 1) & 1]) retainer = parent, j = i;
  181. }
  182. // Find the point to remove.
  183. while (node.data !== d) if (!(previous = node, node = node.next)) return this;
  184. if (next = node.next) delete node.next;
  185. // If there are multiple coincident points, remove just the point.
  186. if (previous) return (next ? previous.next = next : delete previous.next), this;
  187. // If this is the root point, remove it.
  188. if (!parent) return this._root = next, this;
  189. // Remove this leaf.
  190. next ? parent[i] = next : delete parent[i];
  191. // If the parent now contains exactly one leaf, collapse superfluous parents.
  192. if ((node = parent[0] || parent[1])
  193. && node === (parent[1] || parent[0])
  194. && !node.length) {
  195. if (retainer) retainer[j] = node;
  196. else this._root = node;
  197. }
  198. return this;
  199. }
  200. function removeAll(data) {
  201. for (var i = 0, n = data.length; i < n; ++i) this.remove(data[i]);
  202. return this;
  203. }
  204. function tree_root() {
  205. return this._root;
  206. }
  207. function tree_size() {
  208. var size = 0;
  209. this.visit(function(node) {
  210. if (!node.length) do ++size; while (node = node.next)
  211. });
  212. return size;
  213. }
  214. function tree_visit(callback) {
  215. var halves = [], q, node = this._root, child, x0, x1;
  216. if (node) halves.push(new Half(node, this._x0, this._x1));
  217. while (q = halves.pop()) {
  218. if (!callback(node = q.node, x0 = q.x0, x1 = q.x1) && node.length) {
  219. var xm = (x0 + x1) / 2;
  220. if (child = node[1]) halves.push(new Half(child, xm, x1));
  221. if (child = node[0]) halves.push(new Half(child, x0, xm));
  222. }
  223. }
  224. return this;
  225. }
  226. function tree_visitAfter(callback) {
  227. var halves = [], next = [], q;
  228. if (this._root) halves.push(new Half(this._root, this._x0, this._x1));
  229. while (q = halves.pop()) {
  230. var node = q.node;
  231. if (node.length) {
  232. var child, x0 = q.x0, x1 = q.x1, xm = (x0 + x1) / 2;
  233. if (child = node[0]) halves.push(new Half(child, x0, xm));
  234. if (child = node[1]) halves.push(new Half(child, xm, x1));
  235. }
  236. next.push(q);
  237. }
  238. while (q = next.pop()) {
  239. callback(q.node, q.x0, q.x1);
  240. }
  241. return this;
  242. }
  243. function defaultX(d) {
  244. return d[0];
  245. }
  246. function tree_x(_) {
  247. return arguments.length ? (this._x = _, this) : this._x;
  248. }
  249. function binarytree(nodes, x) {
  250. var tree = new Binarytree(x == null ? defaultX : x, NaN, NaN);
  251. return nodes == null ? tree : tree.addAll(nodes);
  252. }
  253. function Binarytree(x, x0, x1) {
  254. this._x = x;
  255. this._x0 = x0;
  256. this._x1 = x1;
  257. this._root = undefined;
  258. }
  259. function leaf_copy(leaf) {
  260. var copy = {data: leaf.data}, next = copy;
  261. while (leaf = leaf.next) next = next.next = {data: leaf.data};
  262. return copy;
  263. }
  264. var treeProto = binarytree.prototype = Binarytree.prototype;
  265. treeProto.copy = function() {
  266. var copy = new Binarytree(this._x, this._x0, this._x1),
  267. node = this._root,
  268. nodes,
  269. child;
  270. if (!node) return copy;
  271. if (!node.length) return copy._root = leaf_copy(node), copy;
  272. nodes = [{source: node, target: copy._root = new Array(2)}];
  273. while (node = nodes.pop()) {
  274. for (var i = 0; i < 2; ++i) {
  275. if (child = node.source[i]) {
  276. if (child.length) nodes.push({source: child, target: node.target[i] = new Array(2)});
  277. else node.target[i] = leaf_copy(child);
  278. }
  279. }
  280. }
  281. return copy;
  282. };
  283. treeProto.add = tree_add;
  284. treeProto.addAll = addAll;
  285. treeProto.cover = tree_cover;
  286. treeProto.data = tree_data;
  287. treeProto.extent = tree_extent;
  288. treeProto.find = tree_find;
  289. treeProto.remove = tree_remove;
  290. treeProto.removeAll = removeAll;
  291. treeProto.root = tree_root;
  292. treeProto.size = tree_size;
  293. treeProto.visit = tree_visit;
  294. treeProto.visitAfter = tree_visitAfter;
  295. treeProto.x = tree_x;
  296. exports.binarytree = binarytree;
  297. Object.defineProperty(exports, '__esModule', { value: true });
  298. })));