splay.esm.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /**
  2. * splaytree v3.1.1
  3. * Fast Splay tree for Node and browser
  4. *
  5. * @author Alexander Milevski <info@w8r.name>
  6. * @license MIT
  7. * @preserve
  8. */
  9. /*! *****************************************************************************
  10. Copyright (c) Microsoft Corporation. All rights reserved.
  11. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  12. this file except in compliance with the License. You may obtain a copy of the
  13. License at http://www.apache.org/licenses/LICENSE-2.0
  14. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  16. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  17. MERCHANTABLITY OR NON-INFRINGEMENT.
  18. See the Apache Version 2.0 License for specific language governing permissions
  19. and limitations under the License.
  20. ***************************************************************************** */
  21. function __generator(thisArg, body) {
  22. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  23. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  24. function verb(n) { return function (v) { return step([n, v]); }; }
  25. function step(op) {
  26. if (f) throw new TypeError("Generator is already executing.");
  27. while (_) try {
  28. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  29. if (y = 0, t) op = [op[0] & 2, t.value];
  30. switch (op[0]) {
  31. case 0: case 1: t = op; break;
  32. case 4: _.label++; return { value: op[1], done: false };
  33. case 5: _.label++; y = op[1]; op = [0]; continue;
  34. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  35. default:
  36. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  37. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  38. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  39. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  40. if (t[2]) _.ops.pop();
  41. _.trys.pop(); continue;
  42. }
  43. op = body.call(thisArg, _);
  44. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  45. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  46. }
  47. }
  48. var Node = /** @class */ (function () {
  49. function Node(key, data) {
  50. this.next = null;
  51. this.key = key;
  52. this.data = data;
  53. this.left = null;
  54. this.right = null;
  55. }
  56. return Node;
  57. }());
  58. /* follows "An implementation of top-down splaying"
  59. * by D. Sleator <sleator@cs.cmu.edu> March 1992
  60. */
  61. function DEFAULT_COMPARE(a, b) {
  62. return a > b ? 1 : a < b ? -1 : 0;
  63. }
  64. /**
  65. * Simple top down splay, not requiring i to be in the tree t.
  66. */
  67. function splay(i, t, comparator) {
  68. var N = new Node(null, null);
  69. var l = N;
  70. var r = N;
  71. while (true) {
  72. var cmp = comparator(i, t.key);
  73. //if (i < t.key) {
  74. if (cmp < 0) {
  75. if (t.left === null)
  76. break;
  77. //if (i < t.left.key) {
  78. if (comparator(i, t.left.key) < 0) {
  79. var y = t.left; /* rotate right */
  80. t.left = y.right;
  81. y.right = t;
  82. t = y;
  83. if (t.left === null)
  84. break;
  85. }
  86. r.left = t; /* link right */
  87. r = t;
  88. t = t.left;
  89. //} else if (i > t.key) {
  90. }
  91. else if (cmp > 0) {
  92. if (t.right === null)
  93. break;
  94. //if (i > t.right.key) {
  95. if (comparator(i, t.right.key) > 0) {
  96. var y = t.right; /* rotate left */
  97. t.right = y.left;
  98. y.left = t;
  99. t = y;
  100. if (t.right === null)
  101. break;
  102. }
  103. l.right = t; /* link left */
  104. l = t;
  105. t = t.right;
  106. }
  107. else
  108. break;
  109. }
  110. /* assemble */
  111. l.right = t.left;
  112. r.left = t.right;
  113. t.left = N.right;
  114. t.right = N.left;
  115. return t;
  116. }
  117. function insert(i, data, t, comparator) {
  118. var node = new Node(i, data);
  119. if (t === null) {
  120. node.left = node.right = null;
  121. return node;
  122. }
  123. t = splay(i, t, comparator);
  124. var cmp = comparator(i, t.key);
  125. if (cmp < 0) {
  126. node.left = t.left;
  127. node.right = t;
  128. t.left = null;
  129. }
  130. else if (cmp >= 0) {
  131. node.right = t.right;
  132. node.left = t;
  133. t.right = null;
  134. }
  135. return node;
  136. }
  137. function split(key, v, comparator) {
  138. var left = null;
  139. var right = null;
  140. if (v) {
  141. v = splay(key, v, comparator);
  142. var cmp = comparator(v.key, key);
  143. if (cmp === 0) {
  144. left = v.left;
  145. right = v.right;
  146. }
  147. else if (cmp < 0) {
  148. right = v.right;
  149. v.right = null;
  150. left = v;
  151. }
  152. else {
  153. left = v.left;
  154. v.left = null;
  155. right = v;
  156. }
  157. }
  158. return { left: left, right: right };
  159. }
  160. function merge(left, right, comparator) {
  161. if (right === null)
  162. return left;
  163. if (left === null)
  164. return right;
  165. right = splay(left.key, right, comparator);
  166. right.left = left;
  167. return right;
  168. }
  169. /**
  170. * Prints level of the tree
  171. */
  172. function printRow(root, prefix, isTail, out, printNode) {
  173. if (root) {
  174. out("" + prefix + (isTail ? '└── ' : '├── ') + printNode(root) + "\n");
  175. var indent = prefix + (isTail ? ' ' : '│ ');
  176. if (root.left)
  177. printRow(root.left, indent, false, out, printNode);
  178. if (root.right)
  179. printRow(root.right, indent, true, out, printNode);
  180. }
  181. }
  182. var Tree = /** @class */ (function () {
  183. function Tree(comparator) {
  184. if (comparator === void 0) { comparator = DEFAULT_COMPARE; }
  185. this._root = null;
  186. this._size = 0;
  187. this._comparator = comparator;
  188. }
  189. /**
  190. * Inserts a key, allows duplicates
  191. */
  192. Tree.prototype.insert = function (key, data) {
  193. this._size++;
  194. return this._root = insert(key, data, this._root, this._comparator);
  195. };
  196. /**
  197. * Adds a key, if it is not present in the tree
  198. */
  199. Tree.prototype.add = function (key, data) {
  200. var node = new Node(key, data);
  201. if (this._root === null) {
  202. node.left = node.right = null;
  203. this._size++;
  204. this._root = node;
  205. }
  206. var comparator = this._comparator;
  207. var t = splay(key, this._root, comparator);
  208. var cmp = comparator(key, t.key);
  209. if (cmp === 0)
  210. this._root = t;
  211. else {
  212. if (cmp < 0) {
  213. node.left = t.left;
  214. node.right = t;
  215. t.left = null;
  216. }
  217. else if (cmp > 0) {
  218. node.right = t.right;
  219. node.left = t;
  220. t.right = null;
  221. }
  222. this._size++;
  223. this._root = node;
  224. }
  225. return this._root;
  226. };
  227. /**
  228. * @param {Key} key
  229. * @return {Node|null}
  230. */
  231. Tree.prototype.remove = function (key) {
  232. this._root = this._remove(key, this._root, this._comparator);
  233. };
  234. /**
  235. * Deletes i from the tree if it's there
  236. */
  237. Tree.prototype._remove = function (i, t, comparator) {
  238. var x;
  239. if (t === null)
  240. return null;
  241. t = splay(i, t, comparator);
  242. var cmp = comparator(i, t.key);
  243. if (cmp === 0) { /* found it */
  244. if (t.left === null) {
  245. x = t.right;
  246. }
  247. else {
  248. x = splay(i, t.left, comparator);
  249. x.right = t.right;
  250. }
  251. this._size--;
  252. return x;
  253. }
  254. return t; /* It wasn't there */
  255. };
  256. /**
  257. * Removes and returns the node with smallest key
  258. */
  259. Tree.prototype.pop = function () {
  260. var node = this._root;
  261. if (node) {
  262. while (node.left)
  263. node = node.left;
  264. this._root = splay(node.key, this._root, this._comparator);
  265. this._root = this._remove(node.key, this._root, this._comparator);
  266. return { key: node.key, data: node.data };
  267. }
  268. return null;
  269. };
  270. /**
  271. * Find without splaying
  272. */
  273. Tree.prototype.findStatic = function (key) {
  274. var current = this._root;
  275. var compare = this._comparator;
  276. while (current) {
  277. var cmp = compare(key, current.key);
  278. if (cmp === 0)
  279. return current;
  280. else if (cmp < 0)
  281. current = current.left;
  282. else
  283. current = current.right;
  284. }
  285. return null;
  286. };
  287. Tree.prototype.find = function (key) {
  288. if (this._root) {
  289. this._root = splay(key, this._root, this._comparator);
  290. if (this._comparator(key, this._root.key) !== 0)
  291. return null;
  292. }
  293. return this._root;
  294. };
  295. Tree.prototype.contains = function (key) {
  296. var current = this._root;
  297. var compare = this._comparator;
  298. while (current) {
  299. var cmp = compare(key, current.key);
  300. if (cmp === 0)
  301. return true;
  302. else if (cmp < 0)
  303. current = current.left;
  304. else
  305. current = current.right;
  306. }
  307. return false;
  308. };
  309. Tree.prototype.forEach = function (visitor, ctx) {
  310. var current = this._root;
  311. var Q = []; /* Initialize stack s */
  312. var done = false;
  313. while (!done) {
  314. if (current !== null) {
  315. Q.push(current);
  316. current = current.left;
  317. }
  318. else {
  319. if (Q.length !== 0) {
  320. current = Q.pop();
  321. visitor.call(ctx, current);
  322. current = current.right;
  323. }
  324. else
  325. done = true;
  326. }
  327. }
  328. return this;
  329. };
  330. /**
  331. * Walk key range from `low` to `high`. Stops if `fn` returns a value.
  332. */
  333. Tree.prototype.range = function (low, high, fn, ctx) {
  334. var Q = [];
  335. var compare = this._comparator;
  336. var node = this._root;
  337. var cmp;
  338. while (Q.length !== 0 || node) {
  339. if (node) {
  340. Q.push(node);
  341. node = node.left;
  342. }
  343. else {
  344. node = Q.pop();
  345. cmp = compare(node.key, high);
  346. if (cmp > 0) {
  347. break;
  348. }
  349. else if (compare(node.key, low) >= 0) {
  350. if (fn.call(ctx, node))
  351. return this; // stop if smth is returned
  352. }
  353. node = node.right;
  354. }
  355. }
  356. return this;
  357. };
  358. /**
  359. * Returns array of keys
  360. */
  361. Tree.prototype.keys = function () {
  362. var keys = [];
  363. this.forEach(function (_a) {
  364. var key = _a.key;
  365. return keys.push(key);
  366. });
  367. return keys;
  368. };
  369. /**
  370. * Returns array of all the data in the nodes
  371. */
  372. Tree.prototype.values = function () {
  373. var values = [];
  374. this.forEach(function (_a) {
  375. var data = _a.data;
  376. return values.push(data);
  377. });
  378. return values;
  379. };
  380. Tree.prototype.min = function () {
  381. if (this._root)
  382. return this.minNode(this._root).key;
  383. return null;
  384. };
  385. Tree.prototype.max = function () {
  386. if (this._root)
  387. return this.maxNode(this._root).key;
  388. return null;
  389. };
  390. Tree.prototype.minNode = function (t) {
  391. if (t === void 0) { t = this._root; }
  392. if (t)
  393. while (t.left)
  394. t = t.left;
  395. return t;
  396. };
  397. Tree.prototype.maxNode = function (t) {
  398. if (t === void 0) { t = this._root; }
  399. if (t)
  400. while (t.right)
  401. t = t.right;
  402. return t;
  403. };
  404. /**
  405. * Returns node at given index
  406. */
  407. Tree.prototype.at = function (index) {
  408. var current = this._root;
  409. var done = false;
  410. var i = 0;
  411. var Q = [];
  412. while (!done) {
  413. if (current) {
  414. Q.push(current);
  415. current = current.left;
  416. }
  417. else {
  418. if (Q.length > 0) {
  419. current = Q.pop();
  420. if (i === index)
  421. return current;
  422. i++;
  423. current = current.right;
  424. }
  425. else
  426. done = true;
  427. }
  428. }
  429. return null;
  430. };
  431. Tree.prototype.next = function (d) {
  432. var root = this._root;
  433. var successor = null;
  434. if (d.right) {
  435. successor = d.right;
  436. while (successor.left)
  437. successor = successor.left;
  438. return successor;
  439. }
  440. var comparator = this._comparator;
  441. while (root) {
  442. var cmp = comparator(d.key, root.key);
  443. if (cmp === 0)
  444. break;
  445. else if (cmp < 0) {
  446. successor = root;
  447. root = root.left;
  448. }
  449. else
  450. root = root.right;
  451. }
  452. return successor;
  453. };
  454. Tree.prototype.prev = function (d) {
  455. var root = this._root;
  456. var predecessor = null;
  457. if (d.left !== null) {
  458. predecessor = d.left;
  459. while (predecessor.right)
  460. predecessor = predecessor.right;
  461. return predecessor;
  462. }
  463. var comparator = this._comparator;
  464. while (root) {
  465. var cmp = comparator(d.key, root.key);
  466. if (cmp === 0)
  467. break;
  468. else if (cmp < 0)
  469. root = root.left;
  470. else {
  471. predecessor = root;
  472. root = root.right;
  473. }
  474. }
  475. return predecessor;
  476. };
  477. Tree.prototype.clear = function () {
  478. this._root = null;
  479. this._size = 0;
  480. return this;
  481. };
  482. Tree.prototype.toList = function () {
  483. return toList(this._root);
  484. };
  485. /**
  486. * Bulk-load items. Both array have to be same size
  487. */
  488. Tree.prototype.load = function (keys, values, presort) {
  489. if (values === void 0) { values = []; }
  490. if (presort === void 0) { presort = false; }
  491. var size = keys.length;
  492. var comparator = this._comparator;
  493. // sort if needed
  494. if (presort)
  495. sort(keys, values, 0, size - 1, comparator);
  496. if (this._root === null) { // empty tree
  497. this._root = loadRecursive(keys, values, 0, size);
  498. this._size = size;
  499. }
  500. else { // that re-builds the whole tree from two in-order traversals
  501. var mergedList = mergeLists(this.toList(), createList(keys, values), comparator);
  502. size = this._size + size;
  503. this._root = sortedListToBST({ head: mergedList }, 0, size);
  504. }
  505. return this;
  506. };
  507. Tree.prototype.isEmpty = function () { return this._root === null; };
  508. Object.defineProperty(Tree.prototype, "size", {
  509. get: function () { return this._size; },
  510. enumerable: true,
  511. configurable: true
  512. });
  513. Object.defineProperty(Tree.prototype, "root", {
  514. get: function () { return this._root; },
  515. enumerable: true,
  516. configurable: true
  517. });
  518. Tree.prototype.toString = function (printNode) {
  519. if (printNode === void 0) { printNode = function (n) { return String(n.key); }; }
  520. var out = [];
  521. printRow(this._root, '', true, function (v) { return out.push(v); }, printNode);
  522. return out.join('');
  523. };
  524. Tree.prototype.update = function (key, newKey, newData) {
  525. var comparator = this._comparator;
  526. var _a = split(key, this._root, comparator), left = _a.left, right = _a.right;
  527. if (comparator(key, newKey) < 0) {
  528. right = insert(newKey, newData, right, comparator);
  529. }
  530. else {
  531. left = insert(newKey, newData, left, comparator);
  532. }
  533. this._root = merge(left, right, comparator);
  534. };
  535. Tree.prototype.split = function (key) {
  536. return split(key, this._root, this._comparator);
  537. };
  538. Tree.prototype[Symbol.iterator] = function () {
  539. var n;
  540. return __generator(this, function (_a) {
  541. switch (_a.label) {
  542. case 0:
  543. n = this.minNode();
  544. _a.label = 1;
  545. case 1:
  546. if (!n) return [3 /*break*/, 3];
  547. return [4 /*yield*/, n];
  548. case 2:
  549. _a.sent();
  550. n = this.next(n);
  551. return [3 /*break*/, 1];
  552. case 3: return [2 /*return*/];
  553. }
  554. });
  555. };
  556. return Tree;
  557. }());
  558. function loadRecursive(keys, values, start, end) {
  559. var size = end - start;
  560. if (size > 0) {
  561. var middle = start + Math.floor(size / 2);
  562. var key = keys[middle];
  563. var data = values[middle];
  564. var node = new Node(key, data);
  565. node.left = loadRecursive(keys, values, start, middle);
  566. node.right = loadRecursive(keys, values, middle + 1, end);
  567. return node;
  568. }
  569. return null;
  570. }
  571. function createList(keys, values) {
  572. var head = new Node(null, null);
  573. var p = head;
  574. for (var i = 0; i < keys.length; i++) {
  575. p = p.next = new Node(keys[i], values[i]);
  576. }
  577. p.next = null;
  578. return head.next;
  579. }
  580. function toList(root) {
  581. var current = root;
  582. var Q = [];
  583. var done = false;
  584. var head = new Node(null, null);
  585. var p = head;
  586. while (!done) {
  587. if (current) {
  588. Q.push(current);
  589. current = current.left;
  590. }
  591. else {
  592. if (Q.length > 0) {
  593. current = p = p.next = Q.pop();
  594. current = current.right;
  595. }
  596. else
  597. done = true;
  598. }
  599. }
  600. p.next = null; // that'll work even if the tree was empty
  601. return head.next;
  602. }
  603. function sortedListToBST(list, start, end) {
  604. var size = end - start;
  605. if (size > 0) {
  606. var middle = start + Math.floor(size / 2);
  607. var left = sortedListToBST(list, start, middle);
  608. var root = list.head;
  609. root.left = left;
  610. list.head = list.head.next;
  611. root.right = sortedListToBST(list, middle + 1, end);
  612. return root;
  613. }
  614. return null;
  615. }
  616. function mergeLists(l1, l2, compare) {
  617. var head = new Node(null, null); // dummy
  618. var p = head;
  619. var p1 = l1;
  620. var p2 = l2;
  621. while (p1 !== null && p2 !== null) {
  622. if (compare(p1.key, p2.key) < 0) {
  623. p.next = p1;
  624. p1 = p1.next;
  625. }
  626. else {
  627. p.next = p2;
  628. p2 = p2.next;
  629. }
  630. p = p.next;
  631. }
  632. if (p1 !== null) {
  633. p.next = p1;
  634. }
  635. else if (p2 !== null) {
  636. p.next = p2;
  637. }
  638. return head.next;
  639. }
  640. function sort(keys, values, left, right, compare) {
  641. if (left >= right)
  642. return;
  643. var pivot = keys[(left + right) >> 1];
  644. var i = left - 1;
  645. var j = right + 1;
  646. while (true) {
  647. do
  648. i++;
  649. while (compare(keys[i], pivot) < 0);
  650. do
  651. j--;
  652. while (compare(keys[j], pivot) > 0);
  653. if (i >= j)
  654. break;
  655. var tmp = keys[i];
  656. keys[i] = keys[j];
  657. keys[j] = tmp;
  658. tmp = values[i];
  659. values[i] = values[j];
  660. values[j] = tmp;
  661. }
  662. sort(keys, values, left, j, compare);
  663. sort(keys, values, j + 1, right, compare);
  664. }
  665. export default Tree;
  666. //# sourceMappingURL=splay.esm.js.map