index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. "use strict";
  2. Object.defineProperty(exports, "t", {
  3. value: true
  4. });
  5. class TreeNode {
  6. constructor(t, e, s = 1) {
  7. this.i = undefined;
  8. this.h = undefined;
  9. this.o = undefined;
  10. this.u = t;
  11. this.l = e;
  12. this.p = s;
  13. }
  14. I() {
  15. let t = this;
  16. const e = t.o.o === t;
  17. if (e && t.p === 1) {
  18. t = t.h;
  19. } else if (t.i) {
  20. t = t.i;
  21. while (t.h) {
  22. t = t.h;
  23. }
  24. } else {
  25. if (e) {
  26. return t.o;
  27. }
  28. let s = t.o;
  29. while (s.i === t) {
  30. t = s;
  31. s = t.o;
  32. }
  33. t = s;
  34. }
  35. return t;
  36. }
  37. B() {
  38. let t = this;
  39. if (t.h) {
  40. t = t.h;
  41. while (t.i) {
  42. t = t.i;
  43. }
  44. return t;
  45. } else {
  46. let e = t.o;
  47. while (e.h === t) {
  48. t = e;
  49. e = t.o;
  50. }
  51. if (t.h !== e) {
  52. return e;
  53. } else return t;
  54. }
  55. }
  56. _() {
  57. const t = this.o;
  58. const e = this.h;
  59. const s = e.i;
  60. if (t.o === this) t.o = e; else if (t.i === this) t.i = e; else t.h = e;
  61. e.o = t;
  62. e.i = this;
  63. this.o = e;
  64. this.h = s;
  65. if (s) s.o = this;
  66. return e;
  67. }
  68. g() {
  69. const t = this.o;
  70. const e = this.i;
  71. const s = e.h;
  72. if (t.o === this) t.o = e; else if (t.i === this) t.i = e; else t.h = e;
  73. e.o = t;
  74. e.h = this;
  75. this.o = e;
  76. this.i = s;
  77. if (s) s.o = this;
  78. return e;
  79. }
  80. }
  81. class TreeNodeEnableIndex extends TreeNode {
  82. constructor() {
  83. super(...arguments);
  84. this.M = 1;
  85. }
  86. _() {
  87. const t = super._();
  88. this.O();
  89. t.O();
  90. return t;
  91. }
  92. g() {
  93. const t = super.g();
  94. this.O();
  95. t.O();
  96. return t;
  97. }
  98. O() {
  99. this.M = 1;
  100. if (this.i) {
  101. this.M += this.i.M;
  102. }
  103. if (this.h) {
  104. this.M += this.h.M;
  105. }
  106. }
  107. }
  108. class ContainerIterator {
  109. constructor(t = 0) {
  110. this.iteratorType = t;
  111. }
  112. equals(t) {
  113. return this.T === t.T;
  114. }
  115. }
  116. class Base {
  117. constructor() {
  118. this.m = 0;
  119. }
  120. get length() {
  121. return this.m;
  122. }
  123. size() {
  124. return this.m;
  125. }
  126. empty() {
  127. return this.m === 0;
  128. }
  129. }
  130. class Container extends Base {}
  131. function throwIteratorAccessError() {
  132. throw new RangeError("Iterator access denied!");
  133. }
  134. class TreeContainer extends Container {
  135. constructor(t = function(t, e) {
  136. if (t < e) return -1;
  137. if (t > e) return 1;
  138. return 0;
  139. }, e = false) {
  140. super();
  141. this.v = undefined;
  142. this.A = t;
  143. this.enableIndex = e;
  144. this.N = e ? TreeNodeEnableIndex : TreeNode;
  145. this.C = new this.N;
  146. }
  147. R(t, e) {
  148. let s = this.C;
  149. while (t) {
  150. const i = this.A(t.u, e);
  151. if (i < 0) {
  152. t = t.h;
  153. } else if (i > 0) {
  154. s = t;
  155. t = t.i;
  156. } else return t;
  157. }
  158. return s;
  159. }
  160. K(t, e) {
  161. let s = this.C;
  162. while (t) {
  163. const i = this.A(t.u, e);
  164. if (i <= 0) {
  165. t = t.h;
  166. } else {
  167. s = t;
  168. t = t.i;
  169. }
  170. }
  171. return s;
  172. }
  173. L(t, e) {
  174. let s = this.C;
  175. while (t) {
  176. const i = this.A(t.u, e);
  177. if (i < 0) {
  178. s = t;
  179. t = t.h;
  180. } else if (i > 0) {
  181. t = t.i;
  182. } else return t;
  183. }
  184. return s;
  185. }
  186. k(t, e) {
  187. let s = this.C;
  188. while (t) {
  189. const i = this.A(t.u, e);
  190. if (i < 0) {
  191. s = t;
  192. t = t.h;
  193. } else {
  194. t = t.i;
  195. }
  196. }
  197. return s;
  198. }
  199. P(t) {
  200. while (true) {
  201. const e = t.o;
  202. if (e === this.C) return;
  203. if (t.p === 1) {
  204. t.p = 0;
  205. return;
  206. }
  207. if (t === e.i) {
  208. const s = e.h;
  209. if (s.p === 1) {
  210. s.p = 0;
  211. e.p = 1;
  212. if (e === this.v) {
  213. this.v = e._();
  214. } else e._();
  215. } else {
  216. if (s.h && s.h.p === 1) {
  217. s.p = e.p;
  218. e.p = 0;
  219. s.h.p = 0;
  220. if (e === this.v) {
  221. this.v = e._();
  222. } else e._();
  223. return;
  224. } else if (s.i && s.i.p === 1) {
  225. s.p = 1;
  226. s.i.p = 0;
  227. s.g();
  228. } else {
  229. s.p = 1;
  230. t = e;
  231. }
  232. }
  233. } else {
  234. const s = e.i;
  235. if (s.p === 1) {
  236. s.p = 0;
  237. e.p = 1;
  238. if (e === this.v) {
  239. this.v = e.g();
  240. } else e.g();
  241. } else {
  242. if (s.i && s.i.p === 1) {
  243. s.p = e.p;
  244. e.p = 0;
  245. s.i.p = 0;
  246. if (e === this.v) {
  247. this.v = e.g();
  248. } else e.g();
  249. return;
  250. } else if (s.h && s.h.p === 1) {
  251. s.p = 1;
  252. s.h.p = 0;
  253. s._();
  254. } else {
  255. s.p = 1;
  256. t = e;
  257. }
  258. }
  259. }
  260. }
  261. }
  262. S(t) {
  263. if (this.m === 1) {
  264. this.clear();
  265. return;
  266. }
  267. let e = t;
  268. while (e.i || e.h) {
  269. if (e.h) {
  270. e = e.h;
  271. while (e.i) e = e.i;
  272. } else {
  273. e = e.i;
  274. }
  275. const s = t.u;
  276. t.u = e.u;
  277. e.u = s;
  278. const i = t.l;
  279. t.l = e.l;
  280. e.l = i;
  281. t = e;
  282. }
  283. if (this.C.i === e) {
  284. this.C.i = e.o;
  285. } else if (this.C.h === e) {
  286. this.C.h = e.o;
  287. }
  288. this.P(e);
  289. let s = e.o;
  290. if (e === s.i) {
  291. s.i = undefined;
  292. } else s.h = undefined;
  293. this.m -= 1;
  294. this.v.p = 0;
  295. if (this.enableIndex) {
  296. while (s !== this.C) {
  297. s.M -= 1;
  298. s = s.o;
  299. }
  300. }
  301. }
  302. U(t) {
  303. const e = typeof t === "number" ? t : undefined;
  304. const s = typeof t === "function" ? t : undefined;
  305. const i = typeof t === "undefined" ? [] : undefined;
  306. let r = 0;
  307. let n = this.v;
  308. const h = [];
  309. while (h.length || n) {
  310. if (n) {
  311. h.push(n);
  312. n = n.i;
  313. } else {
  314. n = h.pop();
  315. if (r === e) return n;
  316. i && i.push(n);
  317. s && s(n, r, this);
  318. r += 1;
  319. n = n.h;
  320. }
  321. }
  322. return i;
  323. }
  324. j(t) {
  325. while (true) {
  326. const e = t.o;
  327. if (e.p === 0) return;
  328. const s = e.o;
  329. if (e === s.i) {
  330. const i = s.h;
  331. if (i && i.p === 1) {
  332. i.p = e.p = 0;
  333. if (s === this.v) return;
  334. s.p = 1;
  335. t = s;
  336. continue;
  337. } else if (t === e.h) {
  338. t.p = 0;
  339. if (t.i) {
  340. t.i.o = e;
  341. }
  342. if (t.h) {
  343. t.h.o = s;
  344. }
  345. e.h = t.i;
  346. s.i = t.h;
  347. t.i = e;
  348. t.h = s;
  349. if (s === this.v) {
  350. this.v = t;
  351. this.C.o = t;
  352. } else {
  353. const e = s.o;
  354. if (e.i === s) {
  355. e.i = t;
  356. } else e.h = t;
  357. }
  358. t.o = s.o;
  359. e.o = t;
  360. s.o = t;
  361. s.p = 1;
  362. } else {
  363. e.p = 0;
  364. if (s === this.v) {
  365. this.v = s.g();
  366. } else s.g();
  367. s.p = 1;
  368. return;
  369. }
  370. } else {
  371. const i = s.i;
  372. if (i && i.p === 1) {
  373. i.p = e.p = 0;
  374. if (s === this.v) return;
  375. s.p = 1;
  376. t = s;
  377. continue;
  378. } else if (t === e.i) {
  379. t.p = 0;
  380. if (t.i) {
  381. t.i.o = s;
  382. }
  383. if (t.h) {
  384. t.h.o = e;
  385. }
  386. s.h = t.i;
  387. e.i = t.h;
  388. t.i = s;
  389. t.h = e;
  390. if (s === this.v) {
  391. this.v = t;
  392. this.C.o = t;
  393. } else {
  394. const e = s.o;
  395. if (e.i === s) {
  396. e.i = t;
  397. } else e.h = t;
  398. }
  399. t.o = s.o;
  400. e.o = t;
  401. s.o = t;
  402. s.p = 1;
  403. } else {
  404. e.p = 0;
  405. if (s === this.v) {
  406. this.v = s._();
  407. } else s._();
  408. s.p = 1;
  409. return;
  410. }
  411. }
  412. if (this.enableIndex) {
  413. e.O();
  414. s.O();
  415. t.O();
  416. }
  417. return;
  418. }
  419. }
  420. q(t, e, s) {
  421. if (this.v === undefined) {
  422. this.m += 1;
  423. this.v = new this.N(t, e, 0);
  424. this.v.o = this.C;
  425. this.C.o = this.C.i = this.C.h = this.v;
  426. return this.m;
  427. }
  428. let i;
  429. const r = this.C.i;
  430. const n = this.A(r.u, t);
  431. if (n === 0) {
  432. r.l = e;
  433. return this.m;
  434. } else if (n > 0) {
  435. r.i = new this.N(t, e);
  436. r.i.o = r;
  437. i = r.i;
  438. this.C.i = i;
  439. } else {
  440. const r = this.C.h;
  441. const n = this.A(r.u, t);
  442. if (n === 0) {
  443. r.l = e;
  444. return this.m;
  445. } else if (n < 0) {
  446. r.h = new this.N(t, e);
  447. r.h.o = r;
  448. i = r.h;
  449. this.C.h = i;
  450. } else {
  451. if (s !== undefined) {
  452. const r = s.T;
  453. if (r !== this.C) {
  454. const s = this.A(r.u, t);
  455. if (s === 0) {
  456. r.l = e;
  457. return this.m;
  458. } else if (s > 0) {
  459. const s = r.I();
  460. const n = this.A(s.u, t);
  461. if (n === 0) {
  462. s.l = e;
  463. return this.m;
  464. } else if (n < 0) {
  465. i = new this.N(t, e);
  466. if (s.h === undefined) {
  467. s.h = i;
  468. i.o = s;
  469. } else {
  470. r.i = i;
  471. i.o = r;
  472. }
  473. }
  474. }
  475. }
  476. }
  477. if (i === undefined) {
  478. i = this.v;
  479. while (true) {
  480. const s = this.A(i.u, t);
  481. if (s > 0) {
  482. if (i.i === undefined) {
  483. i.i = new this.N(t, e);
  484. i.i.o = i;
  485. i = i.i;
  486. break;
  487. }
  488. i = i.i;
  489. } else if (s < 0) {
  490. if (i.h === undefined) {
  491. i.h = new this.N(t, e);
  492. i.h.o = i;
  493. i = i.h;
  494. break;
  495. }
  496. i = i.h;
  497. } else {
  498. i.l = e;
  499. return this.m;
  500. }
  501. }
  502. }
  503. }
  504. }
  505. if (this.enableIndex) {
  506. let t = i.o;
  507. while (t !== this.C) {
  508. t.M += 1;
  509. t = t.o;
  510. }
  511. }
  512. this.j(i);
  513. this.m += 1;
  514. return this.m;
  515. }
  516. H(t, e) {
  517. while (t) {
  518. const s = this.A(t.u, e);
  519. if (s < 0) {
  520. t = t.h;
  521. } else if (s > 0) {
  522. t = t.i;
  523. } else return t;
  524. }
  525. return t || this.C;
  526. }
  527. clear() {
  528. this.m = 0;
  529. this.v = undefined;
  530. this.C.o = undefined;
  531. this.C.i = this.C.h = undefined;
  532. }
  533. updateKeyByIterator(t, e) {
  534. const s = t.T;
  535. if (s === this.C) {
  536. throwIteratorAccessError();
  537. }
  538. if (this.m === 1) {
  539. s.u = e;
  540. return true;
  541. }
  542. const i = s.B().u;
  543. if (s === this.C.i) {
  544. if (this.A(i, e) > 0) {
  545. s.u = e;
  546. return true;
  547. }
  548. return false;
  549. }
  550. const r = s.I().u;
  551. if (s === this.C.h) {
  552. if (this.A(r, e) < 0) {
  553. s.u = e;
  554. return true;
  555. }
  556. return false;
  557. }
  558. if (this.A(r, e) >= 0 || this.A(i, e) <= 0) return false;
  559. s.u = e;
  560. return true;
  561. }
  562. eraseElementByPos(t) {
  563. if (t < 0 || t > this.m - 1) {
  564. throw new RangeError;
  565. }
  566. const e = this.U(t);
  567. this.S(e);
  568. return this.m;
  569. }
  570. eraseElementByKey(t) {
  571. if (this.m === 0) return false;
  572. const e = this.H(this.v, t);
  573. if (e === this.C) return false;
  574. this.S(e);
  575. return true;
  576. }
  577. eraseElementByIterator(t) {
  578. const e = t.T;
  579. if (e === this.C) {
  580. throwIteratorAccessError();
  581. }
  582. const s = e.h === undefined;
  583. const i = t.iteratorType === 0;
  584. if (i) {
  585. if (s) t.next();
  586. } else {
  587. if (!s || e.i === undefined) t.next();
  588. }
  589. this.S(e);
  590. return t;
  591. }
  592. getHeight() {
  593. if (this.m === 0) return 0;
  594. function traversal(t) {
  595. if (!t) return 0;
  596. return Math.max(traversal(t.i), traversal(t.h)) + 1;
  597. }
  598. return traversal(this.v);
  599. }
  600. }
  601. class TreeIterator extends ContainerIterator {
  602. constructor(t, e, s) {
  603. super(s);
  604. this.T = t;
  605. this.C = e;
  606. if (this.iteratorType === 0) {
  607. this.pre = function() {
  608. if (this.T === this.C.i) {
  609. throwIteratorAccessError();
  610. }
  611. this.T = this.T.I();
  612. return this;
  613. };
  614. this.next = function() {
  615. if (this.T === this.C) {
  616. throwIteratorAccessError();
  617. }
  618. this.T = this.T.B();
  619. return this;
  620. };
  621. } else {
  622. this.pre = function() {
  623. if (this.T === this.C.h) {
  624. throwIteratorAccessError();
  625. }
  626. this.T = this.T.B();
  627. return this;
  628. };
  629. this.next = function() {
  630. if (this.T === this.C) {
  631. throwIteratorAccessError();
  632. }
  633. this.T = this.T.I();
  634. return this;
  635. };
  636. }
  637. }
  638. get index() {
  639. let t = this.T;
  640. const e = this.C.o;
  641. if (t === this.C) {
  642. if (e) {
  643. return e.M - 1;
  644. }
  645. return 0;
  646. }
  647. let s = 0;
  648. if (t.i) {
  649. s += t.i.M;
  650. }
  651. while (t !== e) {
  652. const e = t.o;
  653. if (t === e.h) {
  654. s += 1;
  655. if (e.i) {
  656. s += e.i.M;
  657. }
  658. }
  659. t = e;
  660. }
  661. return s;
  662. }
  663. isAccessible() {
  664. return this.T !== this.C;
  665. }
  666. }
  667. class OrderedMapIterator extends TreeIterator {
  668. constructor(t, e, s, i) {
  669. super(t, e, i);
  670. this.container = s;
  671. }
  672. get pointer() {
  673. if (this.T === this.C) {
  674. throwIteratorAccessError();
  675. }
  676. const t = this;
  677. return new Proxy([], {
  678. get(e, s) {
  679. if (s === "0") return t.T.u; else if (s === "1") return t.T.l;
  680. e[0] = t.T.u;
  681. e[1] = t.T.l;
  682. return e[s];
  683. },
  684. set(e, s, i) {
  685. if (s !== "1") {
  686. throw new TypeError("prop must be 1");
  687. }
  688. t.T.l = i;
  689. return true;
  690. }
  691. });
  692. }
  693. copy() {
  694. return new OrderedMapIterator(this.T, this.C, this.container, this.iteratorType);
  695. }
  696. }
  697. class OrderedMap extends TreeContainer {
  698. constructor(t = [], e, s) {
  699. super(e, s);
  700. const i = this;
  701. t.forEach((function(t) {
  702. i.setElement(t[0], t[1]);
  703. }));
  704. }
  705. begin() {
  706. return new OrderedMapIterator(this.C.i || this.C, this.C, this);
  707. }
  708. end() {
  709. return new OrderedMapIterator(this.C, this.C, this);
  710. }
  711. rBegin() {
  712. return new OrderedMapIterator(this.C.h || this.C, this.C, this, 1);
  713. }
  714. rEnd() {
  715. return new OrderedMapIterator(this.C, this.C, this, 1);
  716. }
  717. front() {
  718. if (this.m === 0) return;
  719. const t = this.C.i;
  720. return [ t.u, t.l ];
  721. }
  722. back() {
  723. if (this.m === 0) return;
  724. const t = this.C.h;
  725. return [ t.u, t.l ];
  726. }
  727. lowerBound(t) {
  728. const e = this.R(this.v, t);
  729. return new OrderedMapIterator(e, this.C, this);
  730. }
  731. upperBound(t) {
  732. const e = this.K(this.v, t);
  733. return new OrderedMapIterator(e, this.C, this);
  734. }
  735. reverseLowerBound(t) {
  736. const e = this.L(this.v, t);
  737. return new OrderedMapIterator(e, this.C, this);
  738. }
  739. reverseUpperBound(t) {
  740. const e = this.k(this.v, t);
  741. return new OrderedMapIterator(e, this.C, this);
  742. }
  743. forEach(t) {
  744. this.U((function(e, s, i) {
  745. t([ e.u, e.l ], s, i);
  746. }));
  747. }
  748. setElement(t, e, s) {
  749. return this.q(t, e, s);
  750. }
  751. getElementByPos(t) {
  752. if (t < 0 || t > this.m - 1) {
  753. throw new RangeError;
  754. }
  755. const e = this.U(t);
  756. return [ e.u, e.l ];
  757. }
  758. find(t) {
  759. const e = this.H(this.v, t);
  760. return new OrderedMapIterator(e, this.C, this);
  761. }
  762. getElementByKey(t) {
  763. const e = this.H(this.v, t);
  764. return e.l;
  765. }
  766. union(t) {
  767. const e = this;
  768. t.forEach((function(t) {
  769. e.setElement(t[0], t[1]);
  770. }));
  771. return this.m;
  772. }
  773. * [Symbol.iterator]() {
  774. const t = this.m;
  775. const e = this.U();
  776. for (let s = 0; s < t; ++s) {
  777. const t = e[s];
  778. yield [ t.u, t.l ];
  779. }
  780. }
  781. }
  782. exports.OrderedMap = OrderedMap;
  783. //# sourceMappingURL=index.js.map