index.cjs 19 KB

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