splaytree-ts.umd.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. // splaytree-ts v1.0.2 Copyright (c) 2022, Luiz Felipe Machado Barboza
  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["splaytree-ts"] = global["splaytree-ts"] || {}));
  6. })(this, (function (exports) { 'use strict';
  7. var version = "1.0.2";
  8. class SplayTreeNode {
  9. key;
  10. left = null;
  11. right = null;
  12. constructor(key) {
  13. this.key = key;
  14. }
  15. }
  16. class SplayTreeSetNode extends SplayTreeNode {
  17. constructor(key) {
  18. super(key);
  19. }
  20. }
  21. class SplayTreeMapNode extends SplayTreeNode {
  22. value;
  23. constructor(key, value) {
  24. super(key);
  25. this.value = value;
  26. }
  27. replaceValue(value) {
  28. const node = new SplayTreeMapNode(this.key, value);
  29. node.left = this.left;
  30. node.right = this.right;
  31. return node;
  32. }
  33. }
  34. class SplayTree {
  35. size = 0;
  36. modificationCount = 0;
  37. splayCount = 0;
  38. splay(key) {
  39. const root = this.root;
  40. if (root == null) {
  41. this.compare(key, key);
  42. return -1;
  43. }
  44. let right = null;
  45. let newTreeRight = null;
  46. let left = null;
  47. let newTreeLeft = null;
  48. let current = root;
  49. const compare = this.compare;
  50. let comp;
  51. while (true) {
  52. comp = compare(current.key, key);
  53. if (comp > 0) {
  54. let currentLeft = current.left;
  55. if (currentLeft == null)
  56. break;
  57. comp = compare(currentLeft.key, key);
  58. if (comp > 0) {
  59. current.left = currentLeft.right;
  60. currentLeft.right = current;
  61. current = currentLeft;
  62. currentLeft = current.left;
  63. if (currentLeft == null)
  64. break;
  65. }
  66. if (right == null) {
  67. newTreeRight = current;
  68. }
  69. else {
  70. right.left = current;
  71. }
  72. right = current;
  73. current = currentLeft;
  74. }
  75. else if (comp < 0) {
  76. let currentRight = current.right;
  77. if (currentRight == null)
  78. break;
  79. comp = compare(currentRight.key, key);
  80. if (comp < 0) {
  81. current.right = currentRight.left;
  82. currentRight.left = current;
  83. current = currentRight;
  84. currentRight = current.right;
  85. if (currentRight == null)
  86. break;
  87. }
  88. if (left == null) {
  89. newTreeLeft = current;
  90. }
  91. else {
  92. left.right = current;
  93. }
  94. left = current;
  95. current = currentRight;
  96. }
  97. else {
  98. break;
  99. }
  100. }
  101. if (left != null) {
  102. left.right = current.left;
  103. current.left = newTreeLeft;
  104. }
  105. if (right != null) {
  106. right.left = current.right;
  107. current.right = newTreeRight;
  108. }
  109. if (this.root !== current) {
  110. this.root = current;
  111. this.splayCount++;
  112. }
  113. return comp;
  114. }
  115. splayMin(node) {
  116. let current = node;
  117. let nextLeft = current.left;
  118. while (nextLeft != null) {
  119. const left = nextLeft;
  120. current.left = left.right;
  121. left.right = current;
  122. current = left;
  123. nextLeft = current.left;
  124. }
  125. return current;
  126. }
  127. splayMax(node) {
  128. let current = node;
  129. let nextRight = current.right;
  130. while (nextRight != null) {
  131. const right = nextRight;
  132. current.right = right.left;
  133. right.left = current;
  134. current = right;
  135. nextRight = current.right;
  136. }
  137. return current;
  138. }
  139. _delete(key) {
  140. if (this.root == null)
  141. return null;
  142. const comp = this.splay(key);
  143. if (comp != 0)
  144. return null;
  145. let root = this.root;
  146. const result = root;
  147. const left = root.left;
  148. this.size--;
  149. if (left == null) {
  150. this.root = root.right;
  151. }
  152. else {
  153. const right = root.right;
  154. root = this.splayMax(left);
  155. root.right = right;
  156. this.root = root;
  157. }
  158. this.modificationCount++;
  159. return result;
  160. }
  161. addNewRoot(node, comp) {
  162. this.size++;
  163. this.modificationCount++;
  164. const root = this.root;
  165. if (root == null) {
  166. this.root = node;
  167. return;
  168. }
  169. if (comp < 0) {
  170. node.left = root;
  171. node.right = root.right;
  172. root.right = null;
  173. }
  174. else {
  175. node.right = root;
  176. node.left = root.left;
  177. root.left = null;
  178. }
  179. this.root = node;
  180. }
  181. _first() {
  182. const root = this.root;
  183. if (root == null)
  184. return null;
  185. this.root = this.splayMin(root);
  186. return this.root;
  187. }
  188. _last() {
  189. const root = this.root;
  190. if (root == null)
  191. return null;
  192. this.root = this.splayMax(root);
  193. return this.root;
  194. }
  195. clear() {
  196. this.root = null;
  197. this.size = 0;
  198. this.modificationCount++;
  199. }
  200. has(key) {
  201. return this.validKey(key) && this.splay(key) == 0;
  202. }
  203. defaultCompare() {
  204. return (a, b) => a < b ? -1 : a > b ? 1 : 0;
  205. }
  206. wrap() {
  207. return {
  208. getRoot: () => { return this.root; },
  209. setRoot: (root) => { this.root = root; },
  210. getSize: () => { return this.size; },
  211. getModificationCount: () => { return this.modificationCount; },
  212. getSplayCount: () => { return this.splayCount; },
  213. setSplayCount: (count) => { this.splayCount = count; },
  214. splay: (key) => { return this.splay(key); },
  215. has: (key) => { return this.has(key); },
  216. };
  217. }
  218. }
  219. class SplayTreeMap extends SplayTree {
  220. root = null;
  221. compare;
  222. validKey;
  223. constructor(compare, isValidKey) {
  224. super();
  225. this.compare = compare ?? this.defaultCompare();
  226. this.validKey = isValidKey ?? ((a) => a != null && a != undefined);
  227. }
  228. delete(key) {
  229. if (!this.validKey(key))
  230. return false;
  231. return this._delete(key) != null;
  232. }
  233. forEach(f) {
  234. const nodes = new SplayTreeMapEntryIterableIterator(this.wrap());
  235. let result;
  236. while (result = nodes.next(), !result.done) {
  237. f(result.value[1], result.value[0], this);
  238. }
  239. }
  240. get(key) {
  241. if (!this.validKey(key))
  242. return undefined;
  243. if (this.root != null) {
  244. const comp = this.splay(key);
  245. if (comp == 0) {
  246. return this.root.value;
  247. }
  248. }
  249. return undefined;
  250. }
  251. hasValue(value) {
  252. const initialSplayCount = this.splayCount;
  253. const visit = (node) => {
  254. while (node != null) {
  255. if (node.value == value)
  256. return true;
  257. if (initialSplayCount != this.splayCount) {
  258. throw "Concurrent modification during iteration.";
  259. }
  260. if (node.right != null && visit(node.right)) {
  261. return true;
  262. }
  263. node = node.left;
  264. }
  265. return false;
  266. };
  267. return visit(this.root);
  268. }
  269. set(key, value) {
  270. const comp = this.splay(key);
  271. if (comp == 0) {
  272. this.root = this.root.replaceValue(value);
  273. this.splayCount += 1;
  274. return this;
  275. }
  276. this.addNewRoot(new SplayTreeMapNode(key, value), comp);
  277. return this;
  278. }
  279. setAll(other) {
  280. other.forEach((value, key) => {
  281. this.set(key, value);
  282. });
  283. }
  284. setIfAbsent(key, ifAbsent) {
  285. let comp = this.splay(key);
  286. if (comp == 0) {
  287. return this.root.value;
  288. }
  289. const modificationCount = this.modificationCount;
  290. const splayCount = this.splayCount;
  291. const value = ifAbsent();
  292. if (modificationCount != this.modificationCount) {
  293. throw "Concurrent modification during iteration.";
  294. }
  295. if (splayCount != this.splayCount) {
  296. comp = this.splay(key);
  297. }
  298. this.addNewRoot(new SplayTreeMapNode(key, value), comp);
  299. return value;
  300. }
  301. isEmpty() {
  302. return this.root == null;
  303. }
  304. isNotEmpty() {
  305. return !this.isEmpty();
  306. }
  307. firstKey() {
  308. if (this.root == null)
  309. return null;
  310. return this._first().key;
  311. }
  312. lastKey() {
  313. if (this.root == null)
  314. return null;
  315. return this._last().key;
  316. }
  317. lastKeyBefore(key) {
  318. if (key == null)
  319. throw "Invalid arguments(s)";
  320. if (this.root == null)
  321. return null;
  322. const comp = this.splay(key);
  323. if (comp < 0)
  324. return this.root.key;
  325. let node = this.root.left;
  326. if (node == null)
  327. return null;
  328. let nodeRight = node.right;
  329. while (nodeRight != null) {
  330. node = nodeRight;
  331. nodeRight = node.right;
  332. }
  333. return node.key;
  334. }
  335. firstKeyAfter(key) {
  336. if (key == null)
  337. throw "Invalid arguments(s)";
  338. if (this.root == null)
  339. return null;
  340. const comp = this.splay(key);
  341. if (comp > 0)
  342. return this.root.key;
  343. let node = this.root.right;
  344. if (node == null)
  345. return null;
  346. let nodeLeft = node.left;
  347. while (nodeLeft != null) {
  348. node = nodeLeft;
  349. nodeLeft = node.left;
  350. }
  351. return node.key;
  352. }
  353. update(key, update, ifAbsent) {
  354. let comp = this.splay(key);
  355. if (comp == 0) {
  356. const modificationCount = this.modificationCount;
  357. const splayCount = this.splayCount;
  358. const newValue = update(this.root.value);
  359. if (modificationCount != this.modificationCount) {
  360. throw "Concurrent modification during iteration.";
  361. }
  362. if (splayCount != this.splayCount) {
  363. this.splay(key);
  364. }
  365. this.root = this.root.replaceValue(newValue);
  366. this.splayCount += 1;
  367. return newValue;
  368. }
  369. if (ifAbsent != null) {
  370. const modificationCount = this.modificationCount;
  371. const splayCount = this.splayCount;
  372. const newValue = ifAbsent();
  373. if (modificationCount != this.modificationCount) {
  374. throw "Concurrent modification during iteration.";
  375. }
  376. if (splayCount != this.splayCount) {
  377. comp = this.splay(key);
  378. }
  379. this.addNewRoot(new SplayTreeMapNode(key, newValue), comp);
  380. return newValue;
  381. }
  382. throw "Invalid argument (key): Key not in map.";
  383. }
  384. updateAll(update) {
  385. const root = this.root;
  386. if (root == null)
  387. return;
  388. const iterator = new SplayTreeMapEntryIterableIterator(this.wrap());
  389. let node;
  390. while (node = iterator.next(), !node.done) {
  391. const newValue = update(...node.value);
  392. iterator.replaceValue(newValue);
  393. }
  394. }
  395. keys() {
  396. return new SplayTreeKeyIterableIterator(this.wrap());
  397. }
  398. values() {
  399. return new SplayTreeValueIterableIterator(this.wrap());
  400. }
  401. entries() {
  402. return this[Symbol.iterator]();
  403. }
  404. [Symbol.iterator]() {
  405. return new SplayTreeMapEntryIterableIterator(this.wrap());
  406. }
  407. [Symbol.toStringTag] = '[object Map]';
  408. }
  409. class SplayTreeSet extends SplayTree {
  410. root = null;
  411. compare;
  412. validKey;
  413. constructor(compare, isValidKey) {
  414. super();
  415. this.compare = compare ?? this.defaultCompare();
  416. this.validKey = isValidKey ?? ((v) => v != null && v != undefined);
  417. }
  418. delete(element) {
  419. if (!this.validKey(element))
  420. return false;
  421. return this._delete(element) != null;
  422. }
  423. deleteAll(elements) {
  424. for (const element of elements) {
  425. this.delete(element);
  426. }
  427. }
  428. forEach(f) {
  429. const nodes = this[Symbol.iterator]();
  430. let result;
  431. while (result = nodes.next(), !result.done) {
  432. f(result.value, result.value, this);
  433. }
  434. }
  435. add(element) {
  436. const compare = this.splay(element);
  437. if (compare != 0)
  438. this.addNewRoot(new SplayTreeSetNode(element), compare);
  439. return this;
  440. }
  441. addAndReturn(element) {
  442. const compare = this.splay(element);
  443. if (compare != 0)
  444. this.addNewRoot(new SplayTreeSetNode(element), compare);
  445. return this.root.key;
  446. }
  447. addAll(elements) {
  448. for (const element of elements) {
  449. this.add(element);
  450. }
  451. }
  452. isEmpty() {
  453. return this.root == null;
  454. }
  455. isNotEmpty() {
  456. return this.root != null;
  457. }
  458. single() {
  459. if (this.size == 0)
  460. throw "Bad state: No element";
  461. if (this.size > 1)
  462. throw "Bad state: Too many element";
  463. return this.root.key;
  464. }
  465. first() {
  466. if (this.size == 0)
  467. throw "Bad state: No element";
  468. return this._first().key;
  469. }
  470. last() {
  471. if (this.size == 0)
  472. throw "Bad state: No element";
  473. return this._last().key;
  474. }
  475. lastBefore(element) {
  476. if (element == null)
  477. throw "Invalid arguments(s)";
  478. if (this.root == null)
  479. return null;
  480. const comp = this.splay(element);
  481. if (comp < 0)
  482. return this.root.key;
  483. let node = this.root.left;
  484. if (node == null)
  485. return null;
  486. let nodeRight = node.right;
  487. while (nodeRight != null) {
  488. node = nodeRight;
  489. nodeRight = node.right;
  490. }
  491. return node.key;
  492. }
  493. firstAfter(element) {
  494. if (element == null)
  495. throw "Invalid arguments(s)";
  496. if (this.root == null)
  497. return null;
  498. const comp = this.splay(element);
  499. if (comp > 0)
  500. return this.root.key;
  501. let node = this.root.right;
  502. if (node == null)
  503. return null;
  504. let nodeLeft = node.left;
  505. while (nodeLeft != null) {
  506. node = nodeLeft;
  507. nodeLeft = node.left;
  508. }
  509. return node.key;
  510. }
  511. retainAll(elements) {
  512. const retainSet = new SplayTreeSet(this.compare, this.validKey);
  513. const modificationCount = this.modificationCount;
  514. for (const object of elements) {
  515. if (modificationCount != this.modificationCount) {
  516. throw "Concurrent modification during iteration.";
  517. }
  518. if (this.validKey(object) && this.splay(object) == 0) {
  519. retainSet.add(this.root.key);
  520. }
  521. }
  522. if (retainSet.size != this.size) {
  523. this.root = retainSet.root;
  524. this.size = retainSet.size;
  525. this.modificationCount++;
  526. }
  527. }
  528. lookup(object) {
  529. if (!this.validKey(object))
  530. return null;
  531. const comp = this.splay(object);
  532. if (comp != 0)
  533. return null;
  534. return this.root.key;
  535. }
  536. intersection(other) {
  537. const result = new SplayTreeSet(this.compare, this.validKey);
  538. for (const element of this) {
  539. if (other.has(element))
  540. result.add(element);
  541. }
  542. return result;
  543. }
  544. difference(other) {
  545. const result = new SplayTreeSet(this.compare, this.validKey);
  546. for (const element of this) {
  547. if (!other.has(element))
  548. result.add(element);
  549. }
  550. return result;
  551. }
  552. union(other) {
  553. const u = this.clone();
  554. u.addAll(other);
  555. return u;
  556. }
  557. clone() {
  558. const set = new SplayTreeSet(this.compare, this.validKey);
  559. set.size = this.size;
  560. set.root = this.copyNode(this.root);
  561. return set;
  562. }
  563. copyNode(node) {
  564. if (node == null)
  565. return null;
  566. function copyChildren(node, dest) {
  567. let left;
  568. let right;
  569. do {
  570. left = node.left;
  571. right = node.right;
  572. if (left != null) {
  573. const newLeft = new SplayTreeSetNode(left.key);
  574. dest.left = newLeft;
  575. copyChildren(left, newLeft);
  576. }
  577. if (right != null) {
  578. const newRight = new SplayTreeSetNode(right.key);
  579. dest.right = newRight;
  580. node = right;
  581. dest = newRight;
  582. }
  583. } while (right != null);
  584. }
  585. const result = new SplayTreeSetNode(node.key);
  586. copyChildren(node, result);
  587. return result;
  588. }
  589. toSet() {
  590. return this.clone();
  591. }
  592. entries() {
  593. return new SplayTreeSetEntryIterableIterator(this.wrap());
  594. }
  595. keys() {
  596. return this[Symbol.iterator]();
  597. }
  598. values() {
  599. return this[Symbol.iterator]();
  600. }
  601. [Symbol.iterator]() {
  602. return new SplayTreeKeyIterableIterator(this.wrap());
  603. }
  604. [Symbol.toStringTag] = '[object Set]';
  605. }
  606. class SplayTreeIterableIterator {
  607. tree;
  608. path = new Array();
  609. modificationCount = null;
  610. splayCount;
  611. constructor(tree) {
  612. this.tree = tree;
  613. this.splayCount = tree.getSplayCount();
  614. }
  615. [Symbol.iterator]() {
  616. return this;
  617. }
  618. next() {
  619. if (this.moveNext())
  620. return { done: false, value: this.current() };
  621. return { done: true, value: null };
  622. }
  623. current() {
  624. if (!this.path.length)
  625. return null;
  626. const node = this.path[this.path.length - 1];
  627. return this.getValue(node);
  628. }
  629. rebuildPath(key) {
  630. this.path.splice(0, this.path.length);
  631. this.tree.splay(key);
  632. this.path.push(this.tree.getRoot());
  633. this.splayCount = this.tree.getSplayCount();
  634. }
  635. findLeftMostDescendent(node) {
  636. while (node != null) {
  637. this.path.push(node);
  638. node = node.left;
  639. }
  640. }
  641. moveNext() {
  642. if (this.modificationCount != this.tree.getModificationCount()) {
  643. if (this.modificationCount == null) {
  644. this.modificationCount = this.tree.getModificationCount();
  645. let node = this.tree.getRoot();
  646. while (node != null) {
  647. this.path.push(node);
  648. node = node.left;
  649. }
  650. return this.path.length > 0;
  651. }
  652. throw "Concurrent modification during iteration.";
  653. }
  654. if (!this.path.length)
  655. return false;
  656. if (this.splayCount != this.tree.getSplayCount()) {
  657. this.rebuildPath(this.path[this.path.length - 1].key);
  658. }
  659. let node = this.path[this.path.length - 1];
  660. let next = node.right;
  661. if (next != null) {
  662. while (next != null) {
  663. this.path.push(next);
  664. next = next.left;
  665. }
  666. return true;
  667. }
  668. this.path.pop();
  669. while (this.path.length && this.path[this.path.length - 1].right === node) {
  670. node = this.path.pop();
  671. }
  672. return this.path.length > 0;
  673. }
  674. }
  675. class SplayTreeKeyIterableIterator extends SplayTreeIterableIterator {
  676. getValue(node) {
  677. return node.key;
  678. }
  679. }
  680. class SplayTreeSetEntryIterableIterator extends SplayTreeIterableIterator {
  681. getValue(node) {
  682. return [node.key, node.key];
  683. }
  684. }
  685. class SplayTreeValueIterableIterator extends SplayTreeIterableIterator {
  686. constructor(map) {
  687. super(map);
  688. }
  689. getValue(node) {
  690. return node.value;
  691. }
  692. }
  693. class SplayTreeMapEntryIterableIterator extends SplayTreeIterableIterator {
  694. constructor(map) {
  695. super(map);
  696. }
  697. getValue(node) {
  698. return [node.key, node.value];
  699. }
  700. replaceValue(value) {
  701. if (this.modificationCount != this.tree.getModificationCount()) {
  702. throw "Concurrent modification during iteration.";
  703. }
  704. if (this.splayCount != this.tree.getSplayCount()) {
  705. this.rebuildPath(this.path[this.path.length - 1].key);
  706. }
  707. const last = this.path.pop();
  708. const newLast = last.replaceValue(value);
  709. if (!this.path.length) {
  710. this.tree.setRoot(newLast);
  711. }
  712. else {
  713. const parent = this.path[this.path.length - 1];
  714. if (last === parent.left) {
  715. parent.left = newLast;
  716. }
  717. else {
  718. parent.right = newLast;
  719. }
  720. }
  721. this.path.push(newLast);
  722. const count = this.tree.getSplayCount() + 1;
  723. this.tree.setSplayCount(count);
  724. this.splayCount = count;
  725. }
  726. }
  727. exports.SplayTreeMap = SplayTreeMap;
  728. exports.SplayTreeSet = SplayTreeSet;
  729. exports.version = version;
  730. Object.defineProperty(exports, '__esModule', { value: true });
  731. }));