LinkList.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. var __extends = this && this.t || function() {
  2. var extendStatics = function(i, t) {
  3. extendStatics = Object.setPrototypeOf || {
  4. __proto__: []
  5. } instanceof Array && function(i, t) {
  6. i.__proto__ = t;
  7. } || function(i, t) {
  8. for (var n in t) if (Object.prototype.hasOwnProperty.call(t, n)) i[n] = t[n];
  9. };
  10. return extendStatics(i, t);
  11. };
  12. return function(i, t) {
  13. if (typeof t !== "function" && t !== null) throw new TypeError("Class extends value " + String(t) + " is not a constructor or null");
  14. extendStatics(i, t);
  15. function __() {
  16. this.constructor = i;
  17. }
  18. i.prototype = t === null ? Object.create(t) : (__.prototype = t.prototype, new __);
  19. };
  20. }();
  21. var __generator = this && this.i || function(i, t) {
  22. var n = {
  23. label: 0,
  24. sent: function() {
  25. if (s[0] & 1) throw s[1];
  26. return s[1];
  27. },
  28. trys: [],
  29. ops: []
  30. }, r, e, s, h;
  31. return h = {
  32. next: verb(0),
  33. throw: verb(1),
  34. return: verb(2)
  35. }, typeof Symbol === "function" && (h[Symbol.iterator] = function() {
  36. return this;
  37. }), h;
  38. function verb(i) {
  39. return function(t) {
  40. return step([ i, t ]);
  41. };
  42. }
  43. function step(h) {
  44. if (r) throw new TypeError("Generator is already executing.");
  45. while (n) try {
  46. if (r = 1, e && (s = h[0] & 2 ? e["return"] : h[0] ? e["throw"] || ((s = e["return"]) && s.call(e),
  47. 0) : e.next) && !(s = s.call(e, h[1])).done) return s;
  48. if (e = 0, s) h = [ h[0] & 2, s.value ];
  49. switch (h[0]) {
  50. case 0:
  51. case 1:
  52. s = h;
  53. break;
  54. case 4:
  55. n.label++;
  56. return {
  57. value: h[1],
  58. done: false
  59. };
  60. case 5:
  61. n.label++;
  62. e = h[1];
  63. h = [ 0 ];
  64. continue;
  65. case 7:
  66. h = n.ops.pop();
  67. n.trys.pop();
  68. continue;
  69. default:
  70. if (!(s = n.trys, s = s.length > 0 && s[s.length - 1]) && (h[0] === 6 || h[0] === 2)) {
  71. n = 0;
  72. continue;
  73. }
  74. if (h[0] === 3 && (!s || h[1] > s[0] && h[1] < s[3])) {
  75. n.label = h[1];
  76. break;
  77. }
  78. if (h[0] === 6 && n.label < s[1]) {
  79. n.label = s[1];
  80. s = h;
  81. break;
  82. }
  83. if (s && n.label < s[2]) {
  84. n.label = s[2];
  85. n.ops.push(h);
  86. break;
  87. }
  88. if (s[2]) n.ops.pop();
  89. n.trys.pop();
  90. continue;
  91. }
  92. h = t.call(i, n);
  93. } catch (i) {
  94. h = [ 6, i ];
  95. e = 0;
  96. } finally {
  97. r = s = 0;
  98. }
  99. if (h[0] & 5) throw h[1];
  100. return {
  101. value: h[0] ? h[1] : void 0,
  102. done: true
  103. };
  104. }
  105. };
  106. import SequentialContainer from "./Base";
  107. import { ContainerIterator } from "../ContainerBase";
  108. var LinkNode = function() {
  109. function LinkNode(i) {
  110. this.L = undefined;
  111. this.F = undefined;
  112. this.H = undefined;
  113. this.L = i;
  114. }
  115. return LinkNode;
  116. }();
  117. export { LinkNode };
  118. var LinkListIterator = function(i) {
  119. __extends(LinkListIterator, i);
  120. function LinkListIterator(t, n, r) {
  121. var e = i.call(this, r) || this;
  122. e.D = t;
  123. e.J = n;
  124. if (e.iteratorType === 0) {
  125. e.pre = function() {
  126. if (this.D.F === this.J) {
  127. throw new RangeError("LinkList iterator access denied!");
  128. }
  129. this.D = this.D.F;
  130. return this;
  131. };
  132. e.next = function() {
  133. if (this.D === this.J) {
  134. throw new RangeError("LinkList iterator access denied!");
  135. }
  136. this.D = this.D.H;
  137. return this;
  138. };
  139. } else {
  140. e.pre = function() {
  141. if (this.D.H === this.J) {
  142. throw new RangeError("LinkList iterator access denied!");
  143. }
  144. this.D = this.D.H;
  145. return this;
  146. };
  147. e.next = function() {
  148. if (this.D === this.J) {
  149. throw new RangeError("LinkList iterator access denied!");
  150. }
  151. this.D = this.D.F;
  152. return this;
  153. };
  154. }
  155. return e;
  156. }
  157. Object.defineProperty(LinkListIterator.prototype, "pointer", {
  158. get: function() {
  159. if (this.D === this.J) {
  160. throw new RangeError("LinkList iterator access denied!");
  161. }
  162. return this.D.L;
  163. },
  164. set: function(i) {
  165. if (this.D === this.J) {
  166. throw new RangeError("LinkList iterator access denied!");
  167. }
  168. this.D.L = i;
  169. },
  170. enumerable: false,
  171. configurable: true
  172. });
  173. LinkListIterator.prototype.equals = function(i) {
  174. return this.D === i.D;
  175. };
  176. LinkListIterator.prototype.copy = function() {
  177. return new LinkListIterator(this.D, this.J, this.iteratorType);
  178. };
  179. return LinkListIterator;
  180. }(ContainerIterator);
  181. export { LinkListIterator };
  182. var LinkList = function(i) {
  183. __extends(LinkList, i);
  184. function LinkList(t) {
  185. if (t === void 0) {
  186. t = [];
  187. }
  188. var n = i.call(this) || this;
  189. n.J = new LinkNode;
  190. n.K = undefined;
  191. n.U = undefined;
  192. t.forEach((function(i) {
  193. return n.pushBack(i);
  194. }));
  195. return n;
  196. }
  197. LinkList.prototype.clear = function() {
  198. this.o = 0;
  199. this.K = this.U = undefined;
  200. this.J.F = this.J.H = undefined;
  201. };
  202. LinkList.prototype.begin = function() {
  203. return new LinkListIterator(this.K || this.J, this.J);
  204. };
  205. LinkList.prototype.end = function() {
  206. return new LinkListIterator(this.J, this.J);
  207. };
  208. LinkList.prototype.rBegin = function() {
  209. return new LinkListIterator(this.U || this.J, this.J, 1);
  210. };
  211. LinkList.prototype.rEnd = function() {
  212. return new LinkListIterator(this.J, this.J, 1);
  213. };
  214. LinkList.prototype.front = function() {
  215. return this.K ? this.K.L : undefined;
  216. };
  217. LinkList.prototype.back = function() {
  218. return this.U ? this.U.L : undefined;
  219. };
  220. LinkList.prototype.forEach = function(i) {
  221. if (!this.o) return;
  222. var t = this.K;
  223. var n = 0;
  224. while (t !== this.J) {
  225. i(t.L, n++);
  226. t = t.H;
  227. }
  228. };
  229. LinkList.prototype.getElementByPos = function(i) {
  230. if (i < 0 || i > this.o - 1) {
  231. throw new RangeError;
  232. }
  233. var t = this.K;
  234. while (i--) {
  235. t = t.H;
  236. }
  237. return t.L;
  238. };
  239. LinkList.prototype.eraseElementByPos = function(i) {
  240. if (i < 0 || i > this.o - 1) {
  241. throw new RangeError;
  242. }
  243. if (i === 0) this.popFront(); else if (i === this.o - 1) this.popBack(); else {
  244. var t = this.K;
  245. while (i--) {
  246. t = t.H;
  247. }
  248. t = t;
  249. var n = t.F;
  250. var r = t.H;
  251. r.F = n;
  252. n.H = r;
  253. this.o -= 1;
  254. }
  255. };
  256. LinkList.prototype.eraseElementByValue = function(i) {
  257. while (this.K && this.K.L === i) this.popFront();
  258. while (this.U && this.U.L === i) this.popBack();
  259. if (!this.K) return;
  260. var t = this.K;
  261. while (t !== this.J) {
  262. if (t.L === i) {
  263. var n = t.F;
  264. var r = t.H;
  265. r.F = n;
  266. n.H = r;
  267. this.o -= 1;
  268. }
  269. t = t.H;
  270. }
  271. };
  272. LinkList.prototype.eraseElementByIterator = function(i) {
  273. var t = i.D;
  274. if (t === this.J) {
  275. throw new RangeError("Invalid iterator");
  276. }
  277. i = i.next();
  278. if (this.K === t) this.popFront(); else if (this.U === t) this.popBack(); else {
  279. var n = t.F;
  280. var r = t.H;
  281. r.F = n;
  282. n.H = r;
  283. this.o -= 1;
  284. }
  285. return i;
  286. };
  287. LinkList.prototype.pushBack = function(i) {
  288. this.o += 1;
  289. var t = new LinkNode(i);
  290. if (!this.U) {
  291. this.K = this.U = t;
  292. this.J.H = this.K;
  293. this.K.F = this.J;
  294. } else {
  295. this.U.H = t;
  296. t.F = this.U;
  297. this.U = t;
  298. }
  299. this.U.H = this.J;
  300. this.J.F = this.U;
  301. };
  302. LinkList.prototype.popBack = function() {
  303. if (!this.U) return;
  304. this.o -= 1;
  305. if (this.K === this.U) {
  306. this.K = this.U = undefined;
  307. this.J.H = undefined;
  308. } else {
  309. this.U = this.U.F;
  310. this.U.H = this.J;
  311. }
  312. this.J.F = this.U;
  313. };
  314. LinkList.prototype.setElementByPos = function(i, t) {
  315. if (i < 0 || i > this.o - 1) {
  316. throw new RangeError;
  317. }
  318. var n = this.K;
  319. while (i--) {
  320. n = n.H;
  321. }
  322. n.L = t;
  323. };
  324. LinkList.prototype.insert = function(i, t, n) {
  325. if (n === void 0) {
  326. n = 1;
  327. }
  328. if (i < 0 || i > this.o) {
  329. throw new RangeError;
  330. }
  331. if (n <= 0) return;
  332. if (i === 0) {
  333. while (n--) this.pushFront(t);
  334. } else if (i === this.o) {
  335. while (n--) this.pushBack(t);
  336. } else {
  337. var r = this.K;
  338. for (var e = 1; e < i; ++e) {
  339. r = r.H;
  340. }
  341. var s = r.H;
  342. this.o += n;
  343. while (n--) {
  344. r.H = new LinkNode(t);
  345. r.H.F = r;
  346. r = r.H;
  347. }
  348. r.H = s;
  349. s.F = r;
  350. }
  351. };
  352. LinkList.prototype.find = function(i) {
  353. if (!this.K) return this.end();
  354. var t = this.K;
  355. while (t !== this.J) {
  356. if (t.L === i) {
  357. return new LinkListIterator(t, this.J);
  358. }
  359. t = t.H;
  360. }
  361. return this.end();
  362. };
  363. LinkList.prototype.reverse = function() {
  364. if (this.o <= 1) return;
  365. var i = this.K;
  366. var t = this.U;
  367. var n = 0;
  368. while (n << 1 < this.o) {
  369. var r = i.L;
  370. i.L = t.L;
  371. t.L = r;
  372. i = i.H;
  373. t = t.F;
  374. n += 1;
  375. }
  376. };
  377. LinkList.prototype.unique = function() {
  378. if (this.o <= 1) return;
  379. var i = this.K;
  380. while (i !== this.J) {
  381. var t = i;
  382. while (t.H && t.L === t.H.L) {
  383. t = t.H;
  384. this.o -= 1;
  385. }
  386. i.H = t.H;
  387. i.H.F = i;
  388. i = i.H;
  389. }
  390. };
  391. LinkList.prototype.sort = function(i) {
  392. if (this.o <= 1) return;
  393. var t = [];
  394. this.forEach((function(i) {
  395. return t.push(i);
  396. }));
  397. t.sort(i);
  398. var n = this.K;
  399. t.forEach((function(i) {
  400. n.L = i;
  401. n = n.H;
  402. }));
  403. };
  404. LinkList.prototype.pushFront = function(i) {
  405. this.o += 1;
  406. var t = new LinkNode(i);
  407. if (!this.K) {
  408. this.K = this.U = t;
  409. this.U.H = this.J;
  410. this.J.F = this.U;
  411. } else {
  412. t.H = this.K;
  413. this.K.F = t;
  414. this.K = t;
  415. }
  416. this.J.H = this.K;
  417. this.K.F = this.J;
  418. };
  419. LinkList.prototype.popFront = function() {
  420. if (!this.K) return;
  421. this.o -= 1;
  422. if (this.K === this.U) {
  423. this.K = this.U = undefined;
  424. this.J.F = this.U;
  425. } else {
  426. this.K = this.K.H;
  427. this.K.F = this.J;
  428. }
  429. this.J.H = this.K;
  430. };
  431. LinkList.prototype.merge = function(i) {
  432. var t = this;
  433. if (!this.K) {
  434. i.forEach((function(i) {
  435. return t.pushBack(i);
  436. }));
  437. return;
  438. }
  439. var n = this.K;
  440. i.forEach((function(i) {
  441. while (n && n !== t.J && n.L <= i) {
  442. n = n.H;
  443. }
  444. if (n === t.J) {
  445. t.pushBack(i);
  446. n = t.U;
  447. } else if (n === t.K) {
  448. t.pushFront(i);
  449. n = t.K;
  450. } else {
  451. t.o += 1;
  452. var r = n.F;
  453. r.H = new LinkNode(i);
  454. r.H.F = r;
  455. r.H.H = n;
  456. n.F = r.H;
  457. }
  458. }));
  459. };
  460. LinkList.prototype[Symbol.iterator] = function() {
  461. return function() {
  462. var i;
  463. return __generator(this, (function(t) {
  464. switch (t.label) {
  465. case 0:
  466. if (!this.K) return [ 2 ];
  467. i = this.K;
  468. t.label = 1;
  469. case 1:
  470. if (!(i !== this.J)) return [ 3, 3 ];
  471. return [ 4, i.L ];
  472. case 2:
  473. t.sent();
  474. i = i.H;
  475. return [ 3, 1 ];
  476. case 3:
  477. return [ 2 ];
  478. }
  479. }));
  480. }.bind(this)();
  481. };
  482. return LinkList;
  483. }(SequentialContainer);
  484. export default LinkList;