0fe35fd515968db5c808b973c42642c52a3d94b3f96cf18a2f6423656b0caae41923cf8caaaa33d2814b916e6c3b707ac45bcd7bfefffaad9e3d48f6b996a5 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. "use strict";
  2. Object.defineProperty(exports, "t", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _TreeNode = require("./TreeNode");
  7. var _ContainerBase = require("../../ContainerBase");
  8. var _throwError = require("../../../utils/throwError");
  9. class TreeContainer extends _ContainerBase.Container {
  10. constructor(e = function(e, t) {
  11. if (e < t) return -1;
  12. if (e > t) return 1;
  13. return 0;
  14. }, t = false) {
  15. super();
  16. this.Y = undefined;
  17. this.v = e;
  18. if (t) {
  19. this.re = _TreeNode.TreeNodeEnableIndex;
  20. this.M = function(e, t, i) {
  21. const s = this.ne(e, t, i);
  22. if (s) {
  23. let e = s.tt;
  24. while (e !== this.h) {
  25. e.rt += 1;
  26. e = e.tt;
  27. }
  28. const t = this.he(s);
  29. if (t) {
  30. const {parentNode: e, grandParent: i, curNode: s} = t;
  31. e.ie();
  32. i.ie();
  33. s.ie();
  34. }
  35. }
  36. return this.i;
  37. };
  38. this.V = function(e) {
  39. let t = this.fe(e);
  40. while (t !== this.h) {
  41. t.rt -= 1;
  42. t = t.tt;
  43. }
  44. };
  45. } else {
  46. this.re = _TreeNode.TreeNode;
  47. this.M = function(e, t, i) {
  48. const s = this.ne(e, t, i);
  49. if (s) this.he(s);
  50. return this.i;
  51. };
  52. this.V = this.fe;
  53. }
  54. this.h = new this.re;
  55. }
  56. X(e, t) {
  57. let i = this.h;
  58. while (e) {
  59. const s = this.v(e.u, t);
  60. if (s < 0) {
  61. e = e.W;
  62. } else if (s > 0) {
  63. i = e;
  64. e = e.U;
  65. } else return e;
  66. }
  67. return i;
  68. }
  69. Z(e, t) {
  70. let i = this.h;
  71. while (e) {
  72. const s = this.v(e.u, t);
  73. if (s <= 0) {
  74. e = e.W;
  75. } else {
  76. i = e;
  77. e = e.U;
  78. }
  79. }
  80. return i;
  81. }
  82. $(e, t) {
  83. let i = this.h;
  84. while (e) {
  85. const s = this.v(e.u, t);
  86. if (s < 0) {
  87. i = e;
  88. e = e.W;
  89. } else if (s > 0) {
  90. e = e.U;
  91. } else return e;
  92. }
  93. return i;
  94. }
  95. rr(e, t) {
  96. let i = this.h;
  97. while (e) {
  98. const s = this.v(e.u, t);
  99. if (s < 0) {
  100. i = e;
  101. e = e.W;
  102. } else {
  103. e = e.U;
  104. }
  105. }
  106. return i;
  107. }
  108. ue(e) {
  109. while (true) {
  110. const t = e.tt;
  111. if (t === this.h) return;
  112. if (e.ee === 1) {
  113. e.ee = 0;
  114. return;
  115. }
  116. if (e === t.U) {
  117. const i = t.W;
  118. if (i.ee === 1) {
  119. i.ee = 0;
  120. t.ee = 1;
  121. if (t === this.Y) {
  122. this.Y = t.te();
  123. } else t.te();
  124. } else {
  125. if (i.W && i.W.ee === 1) {
  126. i.ee = t.ee;
  127. t.ee = 0;
  128. i.W.ee = 0;
  129. if (t === this.Y) {
  130. this.Y = t.te();
  131. } else t.te();
  132. return;
  133. } else if (i.U && i.U.ee === 1) {
  134. i.ee = 1;
  135. i.U.ee = 0;
  136. i.se();
  137. } else {
  138. i.ee = 1;
  139. e = t;
  140. }
  141. }
  142. } else {
  143. const i = t.U;
  144. if (i.ee === 1) {
  145. i.ee = 0;
  146. t.ee = 1;
  147. if (t === this.Y) {
  148. this.Y = t.se();
  149. } else t.se();
  150. } else {
  151. if (i.U && i.U.ee === 1) {
  152. i.ee = t.ee;
  153. t.ee = 0;
  154. i.U.ee = 0;
  155. if (t === this.Y) {
  156. this.Y = t.se();
  157. } else t.se();
  158. return;
  159. } else if (i.W && i.W.ee === 1) {
  160. i.ee = 1;
  161. i.W.ee = 0;
  162. i.te();
  163. } else {
  164. i.ee = 1;
  165. e = t;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. fe(e) {
  172. if (this.i === 1) {
  173. this.clear();
  174. return this.h;
  175. }
  176. let t = e;
  177. while (t.U || t.W) {
  178. if (t.W) {
  179. t = t.W;
  180. while (t.U) t = t.U;
  181. } else {
  182. t = t.U;
  183. }
  184. [e.u, t.u] = [ t.u, e.u ];
  185. [e.l, t.l] = [ t.l, e.l ];
  186. e = t;
  187. }
  188. if (this.h.U === t) {
  189. this.h.U = t.tt;
  190. } else if (this.h.W === t) {
  191. this.h.W = t.tt;
  192. }
  193. this.ue(t);
  194. const i = t.tt;
  195. if (t === i.U) {
  196. i.U = undefined;
  197. } else i.W = undefined;
  198. this.i -= 1;
  199. this.Y.ee = 0;
  200. return i;
  201. }
  202. oe(e, t) {
  203. if (e === undefined) return false;
  204. const i = this.oe(e.U, t);
  205. if (i) return true;
  206. if (t(e)) return true;
  207. return this.oe(e.W, t);
  208. }
  209. he(e) {
  210. while (true) {
  211. const t = e.tt;
  212. if (t.ee === 0) return;
  213. const i = t.tt;
  214. if (t === i.U) {
  215. const s = i.W;
  216. if (s && s.ee === 1) {
  217. s.ee = t.ee = 0;
  218. if (i === this.Y) return;
  219. i.ee = 1;
  220. e = i;
  221. continue;
  222. } else if (e === t.W) {
  223. e.ee = 0;
  224. if (e.U) e.U.tt = t;
  225. if (e.W) e.W.tt = i;
  226. t.W = e.U;
  227. i.U = e.W;
  228. e.U = t;
  229. e.W = i;
  230. if (i === this.Y) {
  231. this.Y = e;
  232. this.h.tt = e;
  233. } else {
  234. const t = i.tt;
  235. if (t.U === i) {
  236. t.U = e;
  237. } else t.W = e;
  238. }
  239. e.tt = i.tt;
  240. t.tt = e;
  241. i.tt = e;
  242. i.ee = 1;
  243. return {
  244. parentNode: t,
  245. grandParent: i,
  246. curNode: e
  247. };
  248. } else {
  249. t.ee = 0;
  250. if (i === this.Y) {
  251. this.Y = i.se();
  252. } else i.se();
  253. i.ee = 1;
  254. }
  255. } else {
  256. const s = i.U;
  257. if (s && s.ee === 1) {
  258. s.ee = t.ee = 0;
  259. if (i === this.Y) return;
  260. i.ee = 1;
  261. e = i;
  262. continue;
  263. } else if (e === t.U) {
  264. e.ee = 0;
  265. if (e.U) e.U.tt = i;
  266. if (e.W) e.W.tt = t;
  267. i.W = e.U;
  268. t.U = e.W;
  269. e.U = i;
  270. e.W = t;
  271. if (i === this.Y) {
  272. this.Y = e;
  273. this.h.tt = e;
  274. } else {
  275. const t = i.tt;
  276. if (t.U === i) {
  277. t.U = e;
  278. } else t.W = e;
  279. }
  280. e.tt = i.tt;
  281. t.tt = e;
  282. i.tt = e;
  283. i.ee = 1;
  284. return {
  285. parentNode: t,
  286. grandParent: i,
  287. curNode: e
  288. };
  289. } else {
  290. t.ee = 0;
  291. if (i === this.Y) {
  292. this.Y = i.te();
  293. } else i.te();
  294. i.ee = 1;
  295. }
  296. }
  297. return;
  298. }
  299. }
  300. ne(e, t, i) {
  301. if (this.Y === undefined) {
  302. this.i += 1;
  303. this.Y = new this.re(e, t);
  304. this.Y.ee = 0;
  305. this.Y.tt = this.h;
  306. this.h.tt = this.Y;
  307. this.h.U = this.Y;
  308. this.h.W = this.Y;
  309. return;
  310. }
  311. let s;
  312. const r = this.h.U;
  313. const n = this.v(r.u, e);
  314. if (n === 0) {
  315. r.l = t;
  316. return;
  317. } else if (n > 0) {
  318. r.U = new this.re(e, t);
  319. r.U.tt = r;
  320. s = r.U;
  321. this.h.U = s;
  322. } else {
  323. const r = this.h.W;
  324. const n = this.v(r.u, e);
  325. if (n === 0) {
  326. r.l = t;
  327. return;
  328. } else if (n < 0) {
  329. r.W = new this.re(e, t);
  330. r.W.tt = r;
  331. s = r.W;
  332. this.h.W = s;
  333. } else {
  334. if (i !== undefined) {
  335. const r = i.o;
  336. if (r !== this.h) {
  337. const i = this.v(r.u, e);
  338. if (i === 0) {
  339. r.l = t;
  340. return;
  341. } else if (i > 0) {
  342. const i = r.L();
  343. const n = this.v(i.u, e);
  344. if (n === 0) {
  345. i.l = t;
  346. return;
  347. } else if (n < 0) {
  348. s = new this.re(e, t);
  349. if (i.W === undefined) {
  350. i.W = s;
  351. s.tt = i;
  352. } else {
  353. r.U = s;
  354. s.tt = r;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. if (s === undefined) {
  361. s = this.Y;
  362. while (true) {
  363. const i = this.v(s.u, e);
  364. if (i > 0) {
  365. if (s.U === undefined) {
  366. s.U = new this.re(e, t);
  367. s.U.tt = s;
  368. s = s.U;
  369. break;
  370. }
  371. s = s.U;
  372. } else if (i < 0) {
  373. if (s.W === undefined) {
  374. s.W = new this.re(e, t);
  375. s.W.tt = s;
  376. s = s.W;
  377. break;
  378. }
  379. s = s.W;
  380. } else {
  381. s.l = t;
  382. return;
  383. }
  384. }
  385. }
  386. }
  387. }
  388. this.i += 1;
  389. return s;
  390. }
  391. I(e, t) {
  392. while (e) {
  393. const i = this.v(e.u, t);
  394. if (i < 0) {
  395. e = e.W;
  396. } else if (i > 0) {
  397. e = e.U;
  398. } else return e;
  399. }
  400. return e || this.h;
  401. }
  402. clear() {
  403. this.i = 0;
  404. this.Y = undefined;
  405. this.h.tt = undefined;
  406. this.h.U = this.h.W = undefined;
  407. }
  408. updateKeyByIterator(e, t) {
  409. const i = e.o;
  410. if (i === this.h) {
  411. (0, _throwError.throwIteratorAccessError)();
  412. }
  413. if (this.i === 1) {
  414. i.u = t;
  415. return true;
  416. }
  417. if (i === this.h.U) {
  418. if (this.v(i.B().u, t) > 0) {
  419. i.u = t;
  420. return true;
  421. }
  422. return false;
  423. }
  424. if (i === this.h.W) {
  425. if (this.v(i.L().u, t) < 0) {
  426. i.u = t;
  427. return true;
  428. }
  429. return false;
  430. }
  431. const s = i.L().u;
  432. if (this.v(s, t) >= 0) return false;
  433. const r = i.B().u;
  434. if (this.v(r, t) <= 0) return false;
  435. i.u = t;
  436. return true;
  437. }
  438. eraseElementByPos(e) {
  439. if (e < 0 || e > this.i - 1) {
  440. throw new RangeError;
  441. }
  442. let t = 0;
  443. const i = this;
  444. this.oe(this.Y, (function(s) {
  445. if (e === t) {
  446. i.V(s);
  447. return true;
  448. }
  449. t += 1;
  450. return false;
  451. }));
  452. return this.i;
  453. }
  454. eraseElementByKey(e) {
  455. if (this.i === 0) return false;
  456. const t = this.I(this.Y, e);
  457. if (t === this.h) return false;
  458. this.V(t);
  459. return true;
  460. }
  461. eraseElementByIterator(e) {
  462. const t = e.o;
  463. if (t === this.h) {
  464. (0, _throwError.throwIteratorAccessError)();
  465. }
  466. const i = t.W === undefined;
  467. const s = e.iteratorType === 0;
  468. if (s) {
  469. if (i) e.next();
  470. } else {
  471. if (!i || t.U === undefined) e.next();
  472. }
  473. this.V(t);
  474. return e;
  475. }
  476. forEach(e) {
  477. let t = 0;
  478. for (const i of this) e(i, t++, this);
  479. }
  480. getElementByPos(e) {
  481. if (e < 0 || e > this.i - 1) {
  482. throw new RangeError;
  483. }
  484. let t;
  485. let i = 0;
  486. for (const s of this) {
  487. if (i === e) {
  488. t = s;
  489. break;
  490. }
  491. i += 1;
  492. }
  493. return t;
  494. }
  495. getHeight() {
  496. if (this.i === 0) return 0;
  497. const traversal = function(e) {
  498. if (!e) return 0;
  499. return Math.max(traversal(e.U), traversal(e.W)) + 1;
  500. };
  501. return traversal(this.Y);
  502. }
  503. }
  504. var _default = TreeContainer;
  505. exports.default = _default;
  506. //# sourceMappingURL=index.js.map