LinkList.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. "use strict";
  2. Object.defineProperty(exports, "t", {
  3. value: true
  4. });
  5. exports.default = exports.LinkNode = exports.LinkListIterator = void 0;
  6. var _Base = _interopRequireDefault(require("./Base"));
  7. var _ContainerBase = require("../ContainerBase");
  8. function _interopRequireDefault(t) {
  9. return t && t.t ? t : {
  10. default: t
  11. };
  12. }
  13. class LinkNode {
  14. constructor(t) {
  15. this.L = undefined;
  16. this.j = undefined;
  17. this.O = undefined;
  18. this.L = t;
  19. }
  20. }
  21. exports.LinkNode = LinkNode;
  22. class LinkListIterator extends _ContainerBase.ContainerIterator {
  23. constructor(t, i, s) {
  24. super(s);
  25. this.I = t;
  26. this.S = i;
  27. if (this.iteratorType === 0) {
  28. this.pre = function() {
  29. if (this.I.j === this.S) {
  30. throw new RangeError("LinkList iterator access denied!");
  31. }
  32. this.I = this.I.j;
  33. return this;
  34. };
  35. this.next = function() {
  36. if (this.I === this.S) {
  37. throw new RangeError("LinkList iterator access denied!");
  38. }
  39. this.I = this.I.O;
  40. return this;
  41. };
  42. } else {
  43. this.pre = function() {
  44. if (this.I.O === this.S) {
  45. throw new RangeError("LinkList iterator access denied!");
  46. }
  47. this.I = this.I.O;
  48. return this;
  49. };
  50. this.next = function() {
  51. if (this.I === this.S) {
  52. throw new RangeError("LinkList iterator access denied!");
  53. }
  54. this.I = this.I.j;
  55. return this;
  56. };
  57. }
  58. }
  59. get pointer() {
  60. if (this.I === this.S) {
  61. throw new RangeError("LinkList iterator access denied!");
  62. }
  63. return this.I.L;
  64. }
  65. set pointer(t) {
  66. if (this.I === this.S) {
  67. throw new RangeError("LinkList iterator access denied!");
  68. }
  69. this.I.L = t;
  70. }
  71. equals(t) {
  72. return this.I === t.I;
  73. }
  74. copy() {
  75. return new LinkListIterator(this.I, this.S, this.iteratorType);
  76. }
  77. }
  78. exports.LinkListIterator = LinkListIterator;
  79. class LinkList extends _Base.default {
  80. constructor(t = []) {
  81. super();
  82. this.S = new LinkNode;
  83. this.V = undefined;
  84. this.G = undefined;
  85. t.forEach((t => this.pushBack(t)));
  86. }
  87. clear() {
  88. this.o = 0;
  89. this.V = this.G = undefined;
  90. this.S.j = this.S.O = undefined;
  91. }
  92. begin() {
  93. return new LinkListIterator(this.V || this.S, this.S);
  94. }
  95. end() {
  96. return new LinkListIterator(this.S, this.S);
  97. }
  98. rBegin() {
  99. return new LinkListIterator(this.G || this.S, this.S, 1);
  100. }
  101. rEnd() {
  102. return new LinkListIterator(this.S, this.S, 1);
  103. }
  104. front() {
  105. return this.V ? this.V.L : undefined;
  106. }
  107. back() {
  108. return this.G ? this.G.L : undefined;
  109. }
  110. forEach(t) {
  111. if (!this.o) return;
  112. let i = this.V;
  113. let s = 0;
  114. while (i !== this.S) {
  115. t(i.L, s++);
  116. i = i.O;
  117. }
  118. }
  119. getElementByPos(t) {
  120. if (t < 0 || t > this.o - 1) {
  121. throw new RangeError;
  122. }
  123. let i = this.V;
  124. while (t--) {
  125. i = i.O;
  126. }
  127. return i.L;
  128. }
  129. eraseElementByPos(t) {
  130. if (t < 0 || t > this.o - 1) {
  131. throw new RangeError;
  132. }
  133. if (t === 0) this.popFront(); else if (t === this.o - 1) this.popBack(); else {
  134. let i = this.V;
  135. while (t--) {
  136. i = i.O;
  137. }
  138. i = i;
  139. const s = i.j;
  140. const e = i.O;
  141. e.j = s;
  142. s.O = e;
  143. this.o -= 1;
  144. }
  145. }
  146. eraseElementByValue(t) {
  147. while (this.V && this.V.L === t) this.popFront();
  148. while (this.G && this.G.L === t) this.popBack();
  149. if (!this.V) return;
  150. let i = this.V;
  151. while (i !== this.S) {
  152. if (i.L === t) {
  153. const t = i.j;
  154. const s = i.O;
  155. s.j = t;
  156. t.O = s;
  157. this.o -= 1;
  158. }
  159. i = i.O;
  160. }
  161. }
  162. eraseElementByIterator(t) {
  163. const i = t.I;
  164. if (i === this.S) {
  165. throw new RangeError("Invalid iterator");
  166. }
  167. t = t.next();
  168. if (this.V === i) this.popFront(); else if (this.G === i) this.popBack(); else {
  169. const t = i.j;
  170. const s = i.O;
  171. s.j = t;
  172. t.O = s;
  173. this.o -= 1;
  174. }
  175. return t;
  176. }
  177. pushBack(t) {
  178. this.o += 1;
  179. const i = new LinkNode(t);
  180. if (!this.G) {
  181. this.V = this.G = i;
  182. this.S.O = this.V;
  183. this.V.j = this.S;
  184. } else {
  185. this.G.O = i;
  186. i.j = this.G;
  187. this.G = i;
  188. }
  189. this.G.O = this.S;
  190. this.S.j = this.G;
  191. }
  192. popBack() {
  193. if (!this.G) return;
  194. this.o -= 1;
  195. if (this.V === this.G) {
  196. this.V = this.G = undefined;
  197. this.S.O = undefined;
  198. } else {
  199. this.G = this.G.j;
  200. this.G.O = this.S;
  201. }
  202. this.S.j = this.G;
  203. }
  204. setElementByPos(t, i) {
  205. if (t < 0 || t > this.o - 1) {
  206. throw new RangeError;
  207. }
  208. let s = this.V;
  209. while (t--) {
  210. s = s.O;
  211. }
  212. s.L = i;
  213. }
  214. insert(t, i, s = 1) {
  215. if (t < 0 || t > this.o) {
  216. throw new RangeError;
  217. }
  218. if (s <= 0) return;
  219. if (t === 0) {
  220. while (s--) this.pushFront(i);
  221. } else if (t === this.o) {
  222. while (s--) this.pushBack(i);
  223. } else {
  224. let e = this.V;
  225. for (let i = 1; i < t; ++i) {
  226. e = e.O;
  227. }
  228. const h = e.O;
  229. this.o += s;
  230. while (s--) {
  231. e.O = new LinkNode(i);
  232. e.O.j = e;
  233. e = e.O;
  234. }
  235. e.O = h;
  236. h.j = e;
  237. }
  238. }
  239. find(t) {
  240. if (!this.V) return this.end();
  241. let i = this.V;
  242. while (i !== this.S) {
  243. if (i.L === t) {
  244. return new LinkListIterator(i, this.S);
  245. }
  246. i = i.O;
  247. }
  248. return this.end();
  249. }
  250. reverse() {
  251. if (this.o <= 1) return;
  252. let t = this.V;
  253. let i = this.G;
  254. let s = 0;
  255. while (s << 1 < this.o) {
  256. const e = t.L;
  257. t.L = i.L;
  258. i.L = e;
  259. t = t.O;
  260. i = i.j;
  261. s += 1;
  262. }
  263. }
  264. unique() {
  265. if (this.o <= 1) return;
  266. let t = this.V;
  267. while (t !== this.S) {
  268. let i = t;
  269. while (i.O && i.L === i.O.L) {
  270. i = i.O;
  271. this.o -= 1;
  272. }
  273. t.O = i.O;
  274. t.O.j = t;
  275. t = t.O;
  276. }
  277. }
  278. sort(t) {
  279. if (this.o <= 1) return;
  280. const i = [];
  281. this.forEach((t => i.push(t)));
  282. i.sort(t);
  283. let s = this.V;
  284. i.forEach((t => {
  285. s.L = t;
  286. s = s.O;
  287. }));
  288. }
  289. pushFront(t) {
  290. this.o += 1;
  291. const i = new LinkNode(t);
  292. if (!this.V) {
  293. this.V = this.G = i;
  294. this.G.O = this.S;
  295. this.S.j = this.G;
  296. } else {
  297. i.O = this.V;
  298. this.V.j = i;
  299. this.V = i;
  300. }
  301. this.S.O = this.V;
  302. this.V.j = this.S;
  303. }
  304. popFront() {
  305. if (!this.V) return;
  306. this.o -= 1;
  307. if (this.V === this.G) {
  308. this.V = this.G = undefined;
  309. this.S.j = this.G;
  310. } else {
  311. this.V = this.V.O;
  312. this.V.j = this.S;
  313. }
  314. this.S.O = this.V;
  315. }
  316. merge(t) {
  317. if (!this.V) {
  318. t.forEach((t => this.pushBack(t)));
  319. return;
  320. }
  321. let i = this.V;
  322. t.forEach((t => {
  323. while (i && i !== this.S && i.L <= t) {
  324. i = i.O;
  325. }
  326. if (i === this.S) {
  327. this.pushBack(t);
  328. i = this.G;
  329. } else if (i === this.V) {
  330. this.pushFront(t);
  331. i = this.V;
  332. } else {
  333. this.o += 1;
  334. const s = i.j;
  335. s.O = new LinkNode(t);
  336. s.O.j = s;
  337. s.O.O = i;
  338. i.j = s.O;
  339. }
  340. }));
  341. }
  342. [Symbol.iterator]() {
  343. return function*() {
  344. if (!this.V) return;
  345. let t = this.V;
  346. while (t !== this.S) {
  347. yield t.L;
  348. t = t.O;
  349. }
  350. }.bind(this)();
  351. }
  352. }
  353. var _default = LinkList;
  354. exports.default = _default;