LinkedList.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. "use strict";
  2. var __generator = (this && this.__generator) || function (thisArg, body) {
  3. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  4. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  5. function verb(n) { return function (v) { return step([n, v]); }; }
  6. function step(op) {
  7. if (f) throw new TypeError("Generator is already executing.");
  8. while (_) try {
  9. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  10. if (y = 0, t) op = [op[0] & 2, t.value];
  11. switch (op[0]) {
  12. case 0: case 1: t = op; break;
  13. case 4: _.label++; return { value: op[1], done: false };
  14. case 5: _.label++; y = op[1]; op = [0]; continue;
  15. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  16. default:
  17. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  18. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  19. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  20. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  21. if (t[2]) _.ops.pop();
  22. _.trys.pop(); continue;
  23. }
  24. op = body.call(thisArg, _);
  25. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  26. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  27. }
  28. };
  29. var __read = (this && this.__read) || function (o, n) {
  30. var m = typeof Symbol === "function" && o[Symbol.iterator];
  31. if (!m) return o;
  32. var i = m.call(o), r, ar = [], e;
  33. try {
  34. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  35. }
  36. catch (error) { e = { error: error }; }
  37. finally {
  38. try {
  39. if (r && !r.done && (m = i["return"])) m.call(i);
  40. }
  41. finally { if (e) throw e.error; }
  42. }
  43. return ar;
  44. };
  45. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  46. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  47. if (ar || !(i in from)) {
  48. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  49. ar[i] = from[i];
  50. }
  51. }
  52. return to.concat(ar || Array.prototype.slice.call(from));
  53. };
  54. var __values = (this && this.__values) || function(o) {
  55. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  56. if (m) return m.call(o);
  57. if (o && typeof o.length === "number") return {
  58. next: function () {
  59. if (o && i >= o.length) o = void 0;
  60. return { value: o && o[i++], done: !o };
  61. }
  62. };
  63. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  64. };
  65. Object.defineProperty(exports, "__esModule", { value: true });
  66. exports.LinkedList = exports.ListItem = exports.END = void 0;
  67. exports.END = Symbol();
  68. var ListItem = (function () {
  69. function ListItem(data) {
  70. if (data === void 0) { data = null; }
  71. this.next = null;
  72. this.prev = null;
  73. this.data = data;
  74. }
  75. return ListItem;
  76. }());
  77. exports.ListItem = ListItem;
  78. var LinkedList = (function () {
  79. function LinkedList() {
  80. var args = [];
  81. for (var _i = 0; _i < arguments.length; _i++) {
  82. args[_i] = arguments[_i];
  83. }
  84. this.list = new ListItem(exports.END);
  85. this.list.next = this.list.prev = this.list;
  86. this.push.apply(this, __spreadArray([], __read(args), false));
  87. }
  88. LinkedList.prototype.isBefore = function (a, b) {
  89. return a < b;
  90. };
  91. LinkedList.prototype.push = function () {
  92. var e_1, _a;
  93. var args = [];
  94. for (var _i = 0; _i < arguments.length; _i++) {
  95. args[_i] = arguments[_i];
  96. }
  97. try {
  98. for (var args_1 = __values(args), args_1_1 = args_1.next(); !args_1_1.done; args_1_1 = args_1.next()) {
  99. var data = args_1_1.value;
  100. var item = new ListItem(data);
  101. item.next = this.list;
  102. item.prev = this.list.prev;
  103. this.list.prev = item;
  104. item.prev.next = item;
  105. }
  106. }
  107. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  108. finally {
  109. try {
  110. if (args_1_1 && !args_1_1.done && (_a = args_1.return)) _a.call(args_1);
  111. }
  112. finally { if (e_1) throw e_1.error; }
  113. }
  114. return this;
  115. };
  116. LinkedList.prototype.pop = function () {
  117. var item = this.list.prev;
  118. if (item.data === exports.END) {
  119. return null;
  120. }
  121. this.list.prev = item.prev;
  122. item.prev.next = this.list;
  123. item.next = item.prev = null;
  124. return item.data;
  125. };
  126. LinkedList.prototype.unshift = function () {
  127. var e_2, _a;
  128. var args = [];
  129. for (var _i = 0; _i < arguments.length; _i++) {
  130. args[_i] = arguments[_i];
  131. }
  132. try {
  133. for (var _b = __values(args.slice(0).reverse()), _c = _b.next(); !_c.done; _c = _b.next()) {
  134. var data = _c.value;
  135. var item = new ListItem(data);
  136. item.next = this.list.next;
  137. item.prev = this.list;
  138. this.list.next = item;
  139. item.next.prev = item;
  140. }
  141. }
  142. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  143. finally {
  144. try {
  145. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  146. }
  147. finally { if (e_2) throw e_2.error; }
  148. }
  149. return this;
  150. };
  151. LinkedList.prototype.shift = function () {
  152. var item = this.list.next;
  153. if (item.data === exports.END) {
  154. return null;
  155. }
  156. this.list.next = item.next;
  157. item.next.prev = this.list;
  158. item.next = item.prev = null;
  159. return item.data;
  160. };
  161. LinkedList.prototype.remove = function () {
  162. var e_3, _a;
  163. var items = [];
  164. for (var _i = 0; _i < arguments.length; _i++) {
  165. items[_i] = arguments[_i];
  166. }
  167. var map = new Map();
  168. try {
  169. for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
  170. var item_1 = items_1_1.value;
  171. map.set(item_1, true);
  172. }
  173. }
  174. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  175. finally {
  176. try {
  177. if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
  178. }
  179. finally { if (e_3) throw e_3.error; }
  180. }
  181. var item = this.list.next;
  182. while (item.data !== exports.END) {
  183. var next = item.next;
  184. if (map.has(item.data)) {
  185. item.prev.next = item.next;
  186. item.next.prev = item.prev;
  187. item.next = item.prev = null;
  188. }
  189. item = next;
  190. }
  191. };
  192. LinkedList.prototype.clear = function () {
  193. this.list.next.prev = this.list.prev.next = null;
  194. this.list.next = this.list.prev = this.list;
  195. return this;
  196. };
  197. LinkedList.prototype[Symbol.iterator] = function () {
  198. var current;
  199. return __generator(this, function (_a) {
  200. switch (_a.label) {
  201. case 0:
  202. current = this.list.next;
  203. _a.label = 1;
  204. case 1:
  205. if (!(current.data !== exports.END)) return [3, 3];
  206. return [4, current.data];
  207. case 2:
  208. _a.sent();
  209. current = current.next;
  210. return [3, 1];
  211. case 3: return [2];
  212. }
  213. });
  214. };
  215. LinkedList.prototype.reversed = function () {
  216. var current;
  217. return __generator(this, function (_a) {
  218. switch (_a.label) {
  219. case 0:
  220. current = this.list.prev;
  221. _a.label = 1;
  222. case 1:
  223. if (!(current.data !== exports.END)) return [3, 3];
  224. return [4, current.data];
  225. case 2:
  226. _a.sent();
  227. current = current.prev;
  228. return [3, 1];
  229. case 3: return [2];
  230. }
  231. });
  232. };
  233. LinkedList.prototype.insert = function (data, isBefore) {
  234. if (isBefore === void 0) { isBefore = null; }
  235. if (isBefore === null) {
  236. isBefore = this.isBefore.bind(this);
  237. }
  238. var item = new ListItem(data);
  239. var cur = this.list.next;
  240. while (cur.data !== exports.END && isBefore(cur.data, item.data)) {
  241. cur = cur.next;
  242. }
  243. item.prev = cur.prev;
  244. item.next = cur;
  245. cur.prev.next = cur.prev = item;
  246. return this;
  247. };
  248. LinkedList.prototype.sort = function (isBefore) {
  249. var e_4, _a;
  250. if (isBefore === void 0) { isBefore = null; }
  251. if (isBefore === null) {
  252. isBefore = this.isBefore.bind(this);
  253. }
  254. var lists = [];
  255. try {
  256. for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
  257. var item = _c.value;
  258. lists.push(new LinkedList(item));
  259. }
  260. }
  261. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  262. finally {
  263. try {
  264. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  265. }
  266. finally { if (e_4) throw e_4.error; }
  267. }
  268. this.list.next = this.list.prev = this.list;
  269. while (lists.length > 1) {
  270. var l1 = lists.shift();
  271. var l2 = lists.shift();
  272. l1.merge(l2, isBefore);
  273. lists.push(l1);
  274. }
  275. if (lists.length) {
  276. this.list = lists[0].list;
  277. }
  278. return this;
  279. };
  280. LinkedList.prototype.merge = function (list, isBefore) {
  281. var _a, _b, _c, _d, _e;
  282. if (isBefore === void 0) { isBefore = null; }
  283. if (isBefore === null) {
  284. isBefore = this.isBefore.bind(this);
  285. }
  286. var lcur = this.list.next;
  287. var mcur = list.list.next;
  288. while (lcur.data !== exports.END && mcur.data !== exports.END) {
  289. if (isBefore(mcur.data, lcur.data)) {
  290. _a = __read([lcur, mcur], 2), mcur.prev.next = _a[0], lcur.prev.next = _a[1];
  291. _b = __read([lcur.prev, mcur.prev], 2), mcur.prev = _b[0], lcur.prev = _b[1];
  292. _c = __read([list.list, this.list], 2), this.list.prev.next = _c[0], list.list.prev.next = _c[1];
  293. _d = __read([list.list.prev, this.list.prev], 2), this.list.prev = _d[0], list.list.prev = _d[1];
  294. _e = __read([mcur.next, lcur], 2), lcur = _e[0], mcur = _e[1];
  295. }
  296. else {
  297. lcur = lcur.next;
  298. }
  299. }
  300. if (mcur.data !== exports.END) {
  301. this.list.prev.next = list.list.next;
  302. list.list.next.prev = this.list.prev;
  303. list.list.prev.next = this.list;
  304. this.list.prev = list.list.prev;
  305. list.list.next = list.list.prev = list.list;
  306. }
  307. return this;
  308. };
  309. return LinkedList;
  310. }());
  311. exports.LinkedList = LinkedList;
  312. //# sourceMappingURL=LinkedList.js.map