f7bf064477782638fd5d06cb399ed1cfd96ac2ccb40573490d1b2996600d67cd1ec10e0361c03eada4c2f848d4b38ac9630a35873e27784d1e434b171c62d3 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. var __extends = this && this.t || function() {
  2. var extendStatics = function(e, r) {
  3. extendStatics = Object.setPrototypeOf || {
  4. __proto__: []
  5. } instanceof Array && function(e, r) {
  6. e.__proto__ = r;
  7. } || function(e, r) {
  8. for (var i in r) if (Object.prototype.hasOwnProperty.call(r, i)) e[i] = r[i];
  9. };
  10. return extendStatics(e, r);
  11. };
  12. return function(e, r) {
  13. if (typeof r !== "function" && r !== null) throw new TypeError("Class extends value " + String(r) + " is not a constructor or null");
  14. extendStatics(e, r);
  15. function __() {
  16. this.constructor = e;
  17. }
  18. e.prototype = r === null ? Object.create(r) : (__.prototype = r.prototype, new __);
  19. };
  20. }();
  21. var __read = this && this.q || function(e, r) {
  22. var i = typeof Symbol === "function" && e[Symbol.iterator];
  23. if (!i) return e;
  24. var t = i.call(e), n, s = [], f;
  25. try {
  26. while ((r === void 0 || r-- > 0) && !(n = t.next()).done) s.push(n.value);
  27. } catch (e) {
  28. f = {
  29. error: e
  30. };
  31. } finally {
  32. try {
  33. if (n && !n.done && (i = t["return"])) i.call(t);
  34. } finally {
  35. if (f) throw f.error;
  36. }
  37. }
  38. return s;
  39. };
  40. var __values = this && this.V || function(e) {
  41. var r = typeof Symbol === "function" && Symbol.iterator, i = r && e[r], t = 0;
  42. if (i) return i.call(e);
  43. if (e && typeof e.length === "number") return {
  44. next: function() {
  45. if (e && t >= e.length) e = void 0;
  46. return {
  47. value: e && e[t++],
  48. done: !e
  49. };
  50. }
  51. };
  52. throw new TypeError(r ? "Object is not iterable." : "Symbol.iterator is not defined.");
  53. };
  54. import { TreeNode, TreeNodeEnableIndex } from "./TreeNode";
  55. import { Container } from "../../ContainerBase";
  56. import { throwIteratorAccessError } from "../../../utils/throwError";
  57. var TreeContainer = function(e) {
  58. __extends(TreeContainer, e);
  59. function TreeContainer(r, i) {
  60. if (r === void 0) {
  61. r = function(e, r) {
  62. if (e < r) return -1;
  63. if (e > r) return 1;
  64. return 0;
  65. };
  66. }
  67. if (i === void 0) {
  68. i = false;
  69. }
  70. var t = e.call(this) || this;
  71. t.W = undefined;
  72. t.$ = r;
  73. if (i) {
  74. t.re = TreeNodeEnableIndex;
  75. t.v = function(e, r, i) {
  76. var t = this.se(e, r, i);
  77. if (t) {
  78. var n = t.rr;
  79. while (n !== this.h) {
  80. n.tr += 1;
  81. n = n.rr;
  82. }
  83. var s = this.fe(t);
  84. if (s) {
  85. var f = s, h = f.parentNode, u = f.grandParent, a = f.curNode;
  86. h.ie();
  87. u.ie();
  88. a.ie();
  89. }
  90. }
  91. return this.M;
  92. };
  93. t.G = function(e) {
  94. var r = this.he(e);
  95. while (r !== this.h) {
  96. r.tr -= 1;
  97. r = r.rr;
  98. }
  99. };
  100. } else {
  101. t.re = TreeNode;
  102. t.v = function(e, r, i) {
  103. var t = this.se(e, r, i);
  104. if (t) this.fe(t);
  105. return this.M;
  106. };
  107. t.G = t.he;
  108. }
  109. t.h = new t.re;
  110. return t;
  111. }
  112. TreeContainer.prototype.U = function(e, r) {
  113. var i = this.h;
  114. while (e) {
  115. var t = this.$(e.u, r);
  116. if (t < 0) {
  117. e = e.N;
  118. } else if (t > 0) {
  119. i = e;
  120. e = e.K;
  121. } else return e;
  122. }
  123. return i;
  124. };
  125. TreeContainer.prototype.X = function(e, r) {
  126. var i = this.h;
  127. while (e) {
  128. var t = this.$(e.u, r);
  129. if (t <= 0) {
  130. e = e.N;
  131. } else {
  132. i = e;
  133. e = e.K;
  134. }
  135. }
  136. return i;
  137. };
  138. TreeContainer.prototype.Y = function(e, r) {
  139. var i = this.h;
  140. while (e) {
  141. var t = this.$(e.u, r);
  142. if (t < 0) {
  143. i = e;
  144. e = e.N;
  145. } else if (t > 0) {
  146. e = e.K;
  147. } else return e;
  148. }
  149. return i;
  150. };
  151. TreeContainer.prototype.Z = function(e, r) {
  152. var i = this.h;
  153. while (e) {
  154. var t = this.$(e.u, r);
  155. if (t < 0) {
  156. i = e;
  157. e = e.N;
  158. } else {
  159. e = e.K;
  160. }
  161. }
  162. return i;
  163. };
  164. TreeContainer.prototype.ue = function(e) {
  165. while (true) {
  166. var r = e.rr;
  167. if (r === this.h) return;
  168. if (e.ee === 1) {
  169. e.ee = 0;
  170. return;
  171. }
  172. if (e === r.K) {
  173. var i = r.N;
  174. if (i.ee === 1) {
  175. i.ee = 0;
  176. r.ee = 1;
  177. if (r === this.W) {
  178. this.W = r.ne();
  179. } else r.ne();
  180. } else {
  181. if (i.N && i.N.ee === 1) {
  182. i.ee = r.ee;
  183. r.ee = 0;
  184. i.N.ee = 0;
  185. if (r === this.W) {
  186. this.W = r.ne();
  187. } else r.ne();
  188. return;
  189. } else if (i.K && i.K.ee === 1) {
  190. i.ee = 1;
  191. i.K.ee = 0;
  192. i.te();
  193. } else {
  194. i.ee = 1;
  195. e = r;
  196. }
  197. }
  198. } else {
  199. var i = r.K;
  200. if (i.ee === 1) {
  201. i.ee = 0;
  202. r.ee = 1;
  203. if (r === this.W) {
  204. this.W = r.te();
  205. } else r.te();
  206. } else {
  207. if (i.K && i.K.ee === 1) {
  208. i.ee = r.ee;
  209. r.ee = 0;
  210. i.K.ee = 0;
  211. if (r === this.W) {
  212. this.W = r.te();
  213. } else r.te();
  214. return;
  215. } else if (i.N && i.N.ee === 1) {
  216. i.ee = 1;
  217. i.N.ee = 0;
  218. i.ne();
  219. } else {
  220. i.ee = 1;
  221. e = r;
  222. }
  223. }
  224. }
  225. }
  226. };
  227. TreeContainer.prototype.he = function(e) {
  228. var r, i;
  229. if (this.M === 1) {
  230. this.clear();
  231. return this.h;
  232. }
  233. var t = e;
  234. while (t.K || t.N) {
  235. if (t.N) {
  236. t = t.N;
  237. while (t.K) t = t.K;
  238. } else {
  239. t = t.K;
  240. }
  241. r = __read([ t.u, e.u ], 2), e.u = r[0], t.u = r[1];
  242. i = __read([ t.p, e.p ], 2), e.p = i[0], t.p = i[1];
  243. e = t;
  244. }
  245. if (this.h.K === t) {
  246. this.h.K = t.rr;
  247. } else if (this.h.N === t) {
  248. this.h.N = t.rr;
  249. }
  250. this.ue(t);
  251. var n = t.rr;
  252. if (t === n.K) {
  253. n.K = undefined;
  254. } else n.N = undefined;
  255. this.M -= 1;
  256. this.W.ee = 0;
  257. return n;
  258. };
  259. TreeContainer.prototype.ae = function(e, r) {
  260. if (e === undefined) return false;
  261. var i = this.ae(e.K, r);
  262. if (i) return true;
  263. if (r(e)) return true;
  264. return this.ae(e.N, r);
  265. };
  266. TreeContainer.prototype.fe = function(e) {
  267. while (true) {
  268. var r = e.rr;
  269. if (r.ee === 0) return;
  270. var i = r.rr;
  271. if (r === i.K) {
  272. var t = i.N;
  273. if (t && t.ee === 1) {
  274. t.ee = r.ee = 0;
  275. if (i === this.W) return;
  276. i.ee = 1;
  277. e = i;
  278. continue;
  279. } else if (e === r.N) {
  280. e.ee = 0;
  281. if (e.K) e.K.rr = r;
  282. if (e.N) e.N.rr = i;
  283. r.N = e.K;
  284. i.K = e.N;
  285. e.K = r;
  286. e.N = i;
  287. if (i === this.W) {
  288. this.W = e;
  289. this.h.rr = e;
  290. } else {
  291. var n = i.rr;
  292. if (n.K === i) {
  293. n.K = e;
  294. } else n.N = e;
  295. }
  296. e.rr = i.rr;
  297. r.rr = e;
  298. i.rr = e;
  299. i.ee = 1;
  300. return {
  301. parentNode: r,
  302. grandParent: i,
  303. curNode: e
  304. };
  305. } else {
  306. r.ee = 0;
  307. if (i === this.W) {
  308. this.W = i.te();
  309. } else i.te();
  310. i.ee = 1;
  311. }
  312. } else {
  313. var t = i.K;
  314. if (t && t.ee === 1) {
  315. t.ee = r.ee = 0;
  316. if (i === this.W) return;
  317. i.ee = 1;
  318. e = i;
  319. continue;
  320. } else if (e === r.K) {
  321. e.ee = 0;
  322. if (e.K) e.K.rr = i;
  323. if (e.N) e.N.rr = r;
  324. i.N = e.K;
  325. r.K = e.N;
  326. e.K = i;
  327. e.N = r;
  328. if (i === this.W) {
  329. this.W = e;
  330. this.h.rr = e;
  331. } else {
  332. var n = i.rr;
  333. if (n.K === i) {
  334. n.K = e;
  335. } else n.N = e;
  336. }
  337. e.rr = i.rr;
  338. r.rr = e;
  339. i.rr = e;
  340. i.ee = 1;
  341. return {
  342. parentNode: r,
  343. grandParent: i,
  344. curNode: e
  345. };
  346. } else {
  347. r.ee = 0;
  348. if (i === this.W) {
  349. this.W = i.ne();
  350. } else i.ne();
  351. i.ee = 1;
  352. }
  353. }
  354. return;
  355. }
  356. };
  357. TreeContainer.prototype.se = function(e, r, i) {
  358. if (this.W === undefined) {
  359. this.M += 1;
  360. this.W = new this.re(e, r);
  361. this.W.ee = 0;
  362. this.W.rr = this.h;
  363. this.h.rr = this.W;
  364. this.h.K = this.W;
  365. this.h.N = this.W;
  366. return;
  367. }
  368. var t;
  369. var n = this.h.K;
  370. var s = this.$(n.u, e);
  371. if (s === 0) {
  372. n.p = r;
  373. return;
  374. } else if (s > 0) {
  375. n.K = new this.re(e, r);
  376. n.K.rr = n;
  377. t = n.K;
  378. this.h.K = t;
  379. } else {
  380. var f = this.h.N;
  381. var h = this.$(f.u, e);
  382. if (h === 0) {
  383. f.p = r;
  384. return;
  385. } else if (h < 0) {
  386. f.N = new this.re(e, r);
  387. f.N.rr = f;
  388. t = f.N;
  389. this.h.N = t;
  390. } else {
  391. if (i !== undefined) {
  392. var u = i.o;
  393. if (u !== this.h) {
  394. var a = this.$(u.u, e);
  395. if (a === 0) {
  396. u.p = r;
  397. return;
  398. } else if (a > 0) {
  399. var o = u.L();
  400. var l = this.$(o.u, e);
  401. if (l === 0) {
  402. o.p = r;
  403. return;
  404. } else if (l < 0) {
  405. t = new this.re(e, r);
  406. if (o.N === undefined) {
  407. o.N = t;
  408. t.rr = o;
  409. } else {
  410. u.K = t;
  411. t.rr = u;
  412. }
  413. }
  414. }
  415. }
  416. }
  417. if (t === undefined) {
  418. t = this.W;
  419. while (true) {
  420. var v = this.$(t.u, e);
  421. if (v > 0) {
  422. if (t.K === undefined) {
  423. t.K = new this.re(e, r);
  424. t.K.rr = t;
  425. t = t.K;
  426. break;
  427. }
  428. t = t.K;
  429. } else if (v < 0) {
  430. if (t.N === undefined) {
  431. t.N = new this.re(e, r);
  432. t.N.rr = t;
  433. t = t.N;
  434. break;
  435. }
  436. t = t.N;
  437. } else {
  438. t.p = r;
  439. return;
  440. }
  441. }
  442. }
  443. }
  444. }
  445. this.M += 1;
  446. return t;
  447. };
  448. TreeContainer.prototype.g = function(e, r) {
  449. while (e) {
  450. var i = this.$(e.u, r);
  451. if (i < 0) {
  452. e = e.N;
  453. } else if (i > 0) {
  454. e = e.K;
  455. } else return e;
  456. }
  457. return e || this.h;
  458. };
  459. TreeContainer.prototype.clear = function() {
  460. this.M = 0;
  461. this.W = undefined;
  462. this.h.rr = undefined;
  463. this.h.K = this.h.N = undefined;
  464. };
  465. TreeContainer.prototype.updateKeyByIterator = function(e, r) {
  466. var i = e.o;
  467. if (i === this.h) {
  468. throwIteratorAccessError();
  469. }
  470. if (this.M === 1) {
  471. i.u = r;
  472. return true;
  473. }
  474. if (i === this.h.K) {
  475. if (this.$(i.m().u, r) > 0) {
  476. i.u = r;
  477. return true;
  478. }
  479. return false;
  480. }
  481. if (i === this.h.N) {
  482. if (this.$(i.L().u, r) < 0) {
  483. i.u = r;
  484. return true;
  485. }
  486. return false;
  487. }
  488. var t = i.L().u;
  489. if (this.$(t, r) >= 0) return false;
  490. var n = i.m().u;
  491. if (this.$(n, r) <= 0) return false;
  492. i.u = r;
  493. return true;
  494. };
  495. TreeContainer.prototype.eraseElementByPos = function(e) {
  496. if (e < 0 || e > this.M - 1) {
  497. throw new RangeError;
  498. }
  499. var r = 0;
  500. var i = this;
  501. this.ae(this.W, (function(t) {
  502. if (e === r) {
  503. i.G(t);
  504. return true;
  505. }
  506. r += 1;
  507. return false;
  508. }));
  509. return this.M;
  510. };
  511. TreeContainer.prototype.eraseElementByKey = function(e) {
  512. if (this.M === 0) return false;
  513. var r = this.g(this.W, e);
  514. if (r === this.h) return false;
  515. this.G(r);
  516. return true;
  517. };
  518. TreeContainer.prototype.eraseElementByIterator = function(e) {
  519. var r = e.o;
  520. if (r === this.h) {
  521. throwIteratorAccessError();
  522. }
  523. var i = r.N === undefined;
  524. var t = e.iteratorType === 0;
  525. if (t) {
  526. if (i) e.next();
  527. } else {
  528. if (!i || r.K === undefined) e.next();
  529. }
  530. this.G(r);
  531. return e;
  532. };
  533. TreeContainer.prototype.forEach = function(e) {
  534. var r, i;
  535. var t = 0;
  536. try {
  537. for (var n = __values(this), s = n.next(); !s.done; s = n.next()) {
  538. var f = s.value;
  539. e(f, t++, this);
  540. }
  541. } catch (e) {
  542. r = {
  543. error: e
  544. };
  545. } finally {
  546. try {
  547. if (s && !s.done && (i = n.return)) i.call(n);
  548. } finally {
  549. if (r) throw r.error;
  550. }
  551. }
  552. };
  553. TreeContainer.prototype.getElementByPos = function(e) {
  554. var r, i;
  555. if (e < 0 || e > this.M - 1) {
  556. throw new RangeError;
  557. }
  558. var t;
  559. var n = 0;
  560. try {
  561. for (var s = __values(this), f = s.next(); !f.done; f = s.next()) {
  562. var h = f.value;
  563. if (n === e) {
  564. t = h;
  565. break;
  566. }
  567. n += 1;
  568. }
  569. } catch (e) {
  570. r = {
  571. error: e
  572. };
  573. } finally {
  574. try {
  575. if (f && !f.done && (i = s.return)) i.call(s);
  576. } finally {
  577. if (r) throw r.error;
  578. }
  579. }
  580. return t;
  581. };
  582. TreeContainer.prototype.getHeight = function() {
  583. if (this.M === 0) return 0;
  584. var traversal = function(e) {
  585. if (!e) return 0;
  586. return Math.max(traversal(e.K), traversal(e.N)) + 1;
  587. };
  588. return traversal(this.W);
  589. };
  590. return TreeContainer;
  591. }(Container);
  592. export default TreeContainer;
  593. //# sourceMappingURL=index.js.map