Deque.js 8.9 KB

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