Deque.js 14 KB

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