86060eb4ca2991bea12333f5caff1b340066af3d76d9860eae63e63f97dc6d07ba843f9e1f8637459e4451d1aea9d840013be19ac8938024d2efa726246d06 7.4 KB

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