e33397ff6adfeacb7be7fa196cf50303e1a0118f4725295922a404a7ca10eca5d47bd03090e6cce611d67447531b84109493c51fc257ec754d6cd9e4fee78b 9.0 KB

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