c61c79ab9414afae48435911fd25862c77df8a43980fa66eba01e778d39e674c77db3db4ee21f5e776c9ee76c0e2bcfe8da2f7e903082619d38ffdf84d0f17 14 KB

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