data-structure-typed 2.2.6 → 2.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/CONTRIBUTING.md +47 -1
  3. package/README.md +19 -8
  4. package/README_CN.md +119 -275
  5. package/benchmark/report.html +1 -1
  6. package/benchmark/report.json +20 -324
  7. package/dist/cjs/index.cjs +109 -107
  8. package/dist/cjs/index.cjs.map +1 -1
  9. package/dist/cjs-legacy/index.cjs +109 -107
  10. package/dist/cjs-legacy/index.cjs.map +1 -1
  11. package/dist/esm/index.mjs +109 -107
  12. package/dist/esm/index.mjs.map +1 -1
  13. package/dist/esm-legacy/index.mjs +109 -107
  14. package/dist/esm-legacy/index.mjs.map +1 -1
  15. package/dist/leetcode/avl-tree-counter.mjs +2957 -0
  16. package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
  17. package/dist/leetcode/avl-tree.mjs +2720 -0
  18. package/dist/leetcode/binary-tree.mjs +1594 -0
  19. package/dist/leetcode/bst.mjs +2398 -0
  20. package/dist/leetcode/deque.mjs +683 -0
  21. package/dist/leetcode/directed-graph.mjs +1733 -0
  22. package/dist/leetcode/doubly-linked-list.mjs +709 -0
  23. package/dist/leetcode/hash-map.mjs +493 -0
  24. package/dist/leetcode/heap.mjs +542 -0
  25. package/dist/leetcode/max-heap.mjs +375 -0
  26. package/dist/leetcode/max-priority-queue.mjs +383 -0
  27. package/dist/leetcode/min-heap.mjs +363 -0
  28. package/dist/leetcode/min-priority-queue.mjs +371 -0
  29. package/dist/leetcode/priority-queue.mjs +363 -0
  30. package/dist/leetcode/queue.mjs +943 -0
  31. package/dist/leetcode/red-black-tree.mjs +2765 -0
  32. package/dist/leetcode/singly-linked-list.mjs +754 -0
  33. package/dist/leetcode/stack.mjs +217 -0
  34. package/dist/leetcode/tree-counter.mjs +3039 -0
  35. package/dist/leetcode/tree-multi-map.mjs +2913 -0
  36. package/dist/leetcode/trie.mjs +413 -0
  37. package/dist/leetcode/undirected-graph.mjs +1650 -0
  38. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
  39. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  40. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
  41. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -23
  42. package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
  43. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  44. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
  45. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  46. package/dist/umd/data-structure-typed.js +105 -103
  47. package/dist/umd/data-structure-typed.js.map +1 -1
  48. package/dist/umd/data-structure-typed.min.js +2 -2
  49. package/dist/umd/data-structure-typed.min.js.map +1 -1
  50. package/package.json +48 -171
  51. package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
  52. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
  53. package/src/data-structures/binary-tree/avl-tree.ts +15 -15
  54. package/src/data-structures/binary-tree/binary-tree.ts +53 -55
  55. package/src/data-structures/binary-tree/bst.ts +21 -22
  56. package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
  57. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  58. package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
  59. package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
  60. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
  61. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
  62. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
  63. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
  64. package/test/unit/data-structures/binary-tree/bst.test.ts +99 -99
  65. package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
  66. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
  67. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +37 -37
  68. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
  69. package/tsup.config.js +50 -21
  70. package/tsup.leetcode.config.js +1 -1
  71. package/tsup.umd.config.js +29 -0
  72. package/tsup.node.config.js +0 -83
@@ -0,0 +1,683 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ var rangeCheck = __name((index, min, max, message = "Index out of bounds.") => {
4
+ if (index < min || index > max) throw new RangeError(message);
5
+ }, "rangeCheck");
6
+ var calcMinUnitsRequired = __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
7
+ var IterableElementBase = class {
8
+ static {
9
+ __name(this, "IterableElementBase");
10
+ }
11
+ constructor(options) {
12
+ if (options) {
13
+ const { toElementFn } = options;
14
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
15
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
16
+ }
17
+ }
18
+ _toElementFn;
19
+ get toElementFn() {
20
+ return this._toElementFn;
21
+ }
22
+ *[Symbol.iterator](...args) {
23
+ yield* this._getIterator(...args);
24
+ }
25
+ *values() {
26
+ for (const item of this) yield item;
27
+ }
28
+ every(predicate, thisArg) {
29
+ let index = 0;
30
+ for (const item of this) {
31
+ if (thisArg === void 0) {
32
+ if (!predicate(item, index++, this)) return false;
33
+ } else {
34
+ const fn = predicate;
35
+ if (!fn.call(thisArg, item, index++, this)) return false;
36
+ }
37
+ }
38
+ return true;
39
+ }
40
+ some(predicate, thisArg) {
41
+ let index = 0;
42
+ for (const item of this) {
43
+ if (thisArg === void 0) {
44
+ if (predicate(item, index++, this)) return true;
45
+ } else {
46
+ const fn = predicate;
47
+ if (fn.call(thisArg, item, index++, this)) return true;
48
+ }
49
+ }
50
+ return false;
51
+ }
52
+ forEach(callbackfn, thisArg) {
53
+ let index = 0;
54
+ for (const item of this) {
55
+ if (thisArg === void 0) {
56
+ callbackfn(item, index++, this);
57
+ } else {
58
+ const fn = callbackfn;
59
+ fn.call(thisArg, item, index++, this);
60
+ }
61
+ }
62
+ }
63
+ find(predicate, thisArg) {
64
+ let index = 0;
65
+ for (const item of this) {
66
+ if (thisArg === void 0) {
67
+ if (predicate(item, index++, this)) return item;
68
+ } else {
69
+ const fn = predicate;
70
+ if (fn.call(thisArg, item, index++, this)) return item;
71
+ }
72
+ }
73
+ return;
74
+ }
75
+ has(element) {
76
+ for (const ele of this) if (ele === element) return true;
77
+ return false;
78
+ }
79
+ reduce(callbackfn, initialValue) {
80
+ let index = 0;
81
+ const iter = this[Symbol.iterator]();
82
+ let acc;
83
+ if (arguments.length >= 2) {
84
+ acc = initialValue;
85
+ } else {
86
+ const first = iter.next();
87
+ if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
88
+ acc = first.value;
89
+ index = 1;
90
+ }
91
+ for (const value of iter) {
92
+ acc = callbackfn(acc, value, index++, this);
93
+ }
94
+ return acc;
95
+ }
96
+ toArray() {
97
+ return [...this];
98
+ }
99
+ toVisual() {
100
+ return [...this];
101
+ }
102
+ print() {
103
+ console.log(this.toVisual());
104
+ }
105
+ };
106
+ var LinearBase = class _LinearBase extends IterableElementBase {
107
+ static {
108
+ __name(this, "LinearBase");
109
+ }
110
+ constructor(options) {
111
+ super(options);
112
+ if (options) {
113
+ const { maxLen } = options;
114
+ if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
115
+ }
116
+ }
117
+ _maxLen = -1;
118
+ get maxLen() {
119
+ return this._maxLen;
120
+ }
121
+ indexOf(searchElement, fromIndex = 0) {
122
+ if (this.length === 0) return -1;
123
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
124
+ if (fromIndex < 0) fromIndex = 0;
125
+ for (let i = fromIndex; i < this.length; i++) {
126
+ const element = this.at(i);
127
+ if (element === searchElement) return i;
128
+ }
129
+ return -1;
130
+ }
131
+ lastIndexOf(searchElement, fromIndex = this.length - 1) {
132
+ if (this.length === 0) return -1;
133
+ if (fromIndex >= this.length) fromIndex = this.length - 1;
134
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
135
+ for (let i = fromIndex; i >= 0; i--) {
136
+ const element = this.at(i);
137
+ if (element === searchElement) return i;
138
+ }
139
+ return -1;
140
+ }
141
+ findIndex(predicate, thisArg) {
142
+ for (let i = 0; i < this.length; i++) {
143
+ const item = this.at(i);
144
+ if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
145
+ }
146
+ return -1;
147
+ }
148
+ concat(...items) {
149
+ const newList = this.clone();
150
+ for (const item of items) {
151
+ if (item instanceof _LinearBase) {
152
+ newList.pushMany(item);
153
+ } else {
154
+ newList.push(item);
155
+ }
156
+ }
157
+ return newList;
158
+ }
159
+ sort(compareFn) {
160
+ const arr = this.toArray();
161
+ arr.sort(compareFn);
162
+ this.clear();
163
+ for (const item of arr) this.push(item);
164
+ return this;
165
+ }
166
+ splice(start, deleteCount = 0, ...items) {
167
+ const removedList = this._createInstance();
168
+ start = start < 0 ? this.length + start : start;
169
+ start = Math.max(0, Math.min(start, this.length));
170
+ deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
171
+ for (let i = 0; i < deleteCount; i++) {
172
+ const removed = this.deleteAt(start);
173
+ if (removed !== void 0) {
174
+ removedList.push(removed);
175
+ }
176
+ }
177
+ for (let i = 0; i < items.length; i++) {
178
+ this.addAt(start + i, items[i]);
179
+ }
180
+ return removedList;
181
+ }
182
+ join(separator = ",") {
183
+ return this.toArray().join(separator);
184
+ }
185
+ toReversedArray() {
186
+ const array = [];
187
+ for (let i = this.length - 1; i >= 0; i--) {
188
+ array.push(this.at(i));
189
+ }
190
+ return array;
191
+ }
192
+ reduceRight(callbackfn, initialValue) {
193
+ let accumulator = initialValue ?? 0;
194
+ for (let i = this.length - 1; i >= 0; i--) {
195
+ accumulator = callbackfn(accumulator, this.at(i), i, this);
196
+ }
197
+ return accumulator;
198
+ }
199
+ slice(start = 0, end = this.length) {
200
+ start = start < 0 ? this.length + start : start;
201
+ end = end < 0 ? this.length + end : end;
202
+ const newList = this._createInstance();
203
+ for (let i = start; i < end; i++) {
204
+ newList.push(this.at(i));
205
+ }
206
+ return newList;
207
+ }
208
+ fill(value, start = 0, end = this.length) {
209
+ start = start < 0 ? this.length + start : start;
210
+ end = end < 0 ? this.length + end : end;
211
+ if (start < 0) start = 0;
212
+ if (end > this.length) end = this.length;
213
+ if (start >= end) return this;
214
+ for (let i = start; i < end; i++) {
215
+ this.setAt(i, value);
216
+ }
217
+ return this;
218
+ }
219
+ };
220
+ var Deque = class extends LinearBase {
221
+ static {
222
+ __name(this, "Deque");
223
+ }
224
+ _equals = Object.is;
225
+ constructor(elements = [], options) {
226
+ super(options);
227
+ if (options) {
228
+ const { bucketSize } = options;
229
+ if (typeof bucketSize === "number") this._bucketSize = bucketSize;
230
+ }
231
+ let _size;
232
+ if ("length" in elements) {
233
+ _size = typeof elements.length === "function" ? elements.length() : elements.length;
234
+ } else {
235
+ _size = typeof elements.size === "function" ? elements.size() : elements.size;
236
+ }
237
+ this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1;
238
+ for (let i = 0; i < this._bucketCount; ++i) {
239
+ this._buckets.push(new Array(this._bucketSize));
240
+ }
241
+ const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
242
+ this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
243
+ this._firstInBucket = this._lastInBucket = this._bucketSize - _size % this._bucketSize >> 1;
244
+ this.pushMany(elements);
245
+ }
246
+ _bucketSize = 1 << 12;
247
+ get bucketSize() {
248
+ return this._bucketSize;
249
+ }
250
+ _bucketFirst = 0;
251
+ get bucketFirst() {
252
+ return this._bucketFirst;
253
+ }
254
+ _firstInBucket = 0;
255
+ get firstInBucket() {
256
+ return this._firstInBucket;
257
+ }
258
+ _bucketLast = 0;
259
+ get bucketLast() {
260
+ return this._bucketLast;
261
+ }
262
+ _lastInBucket = 0;
263
+ get lastInBucket() {
264
+ return this._lastInBucket;
265
+ }
266
+ _bucketCount = 0;
267
+ get bucketCount() {
268
+ return this._bucketCount;
269
+ }
270
+ _buckets = [];
271
+ get buckets() {
272
+ return this._buckets;
273
+ }
274
+ _length = 0;
275
+ get length() {
276
+ return this._length;
277
+ }
278
+ get first() {
279
+ if (this._length === 0) return;
280
+ return this._buckets[this._bucketFirst][this._firstInBucket];
281
+ }
282
+ get last() {
283
+ if (this._length === 0) return;
284
+ return this._buckets[this._bucketLast][this._lastInBucket];
285
+ }
286
+ static fromArray(data, options) {
287
+ return new this(data, options);
288
+ }
289
+ push(element) {
290
+ if (this._length) {
291
+ if (this._lastInBucket < this._bucketSize - 1) {
292
+ this._lastInBucket += 1;
293
+ } else if (this._bucketLast < this._bucketCount - 1) {
294
+ this._bucketLast += 1;
295
+ this._lastInBucket = 0;
296
+ } else {
297
+ this._bucketLast = 0;
298
+ this._lastInBucket = 0;
299
+ }
300
+ if (this._bucketLast === this._bucketFirst && this._lastInBucket === this._firstInBucket) this._reallocate();
301
+ }
302
+ this._length += 1;
303
+ this._buckets[this._bucketLast][this._lastInBucket] = element;
304
+ if (this._maxLen > 0 && this._length > this._maxLen) this.shift();
305
+ return true;
306
+ }
307
+ pop() {
308
+ if (this._length === 0) return;
309
+ const element = this._buckets[this._bucketLast][this._lastInBucket];
310
+ if (this._length !== 1) {
311
+ if (this._lastInBucket > 0) {
312
+ this._lastInBucket -= 1;
313
+ } else if (this._bucketLast > 0) {
314
+ this._bucketLast -= 1;
315
+ this._lastInBucket = this._bucketSize - 1;
316
+ } else {
317
+ this._bucketLast = this._bucketCount - 1;
318
+ this._lastInBucket = this._bucketSize - 1;
319
+ }
320
+ }
321
+ this._length -= 1;
322
+ return element;
323
+ }
324
+ shift() {
325
+ if (this._length === 0) return;
326
+ const element = this._buckets[this._bucketFirst][this._firstInBucket];
327
+ if (this._length !== 1) {
328
+ if (this._firstInBucket < this._bucketSize - 1) {
329
+ this._firstInBucket += 1;
330
+ } else if (this._bucketFirst < this._bucketCount - 1) {
331
+ this._bucketFirst += 1;
332
+ this._firstInBucket = 0;
333
+ } else {
334
+ this._bucketFirst = 0;
335
+ this._firstInBucket = 0;
336
+ }
337
+ }
338
+ this._length -= 1;
339
+ return element;
340
+ }
341
+ unshift(element) {
342
+ if (this._length) {
343
+ if (this._firstInBucket > 0) {
344
+ this._firstInBucket -= 1;
345
+ } else if (this._bucketFirst > 0) {
346
+ this._bucketFirst -= 1;
347
+ this._firstInBucket = this._bucketSize - 1;
348
+ } else {
349
+ this._bucketFirst = this._bucketCount - 1;
350
+ this._firstInBucket = this._bucketSize - 1;
351
+ }
352
+ if (this._bucketFirst === this._bucketLast && this._firstInBucket === this._lastInBucket) this._reallocate();
353
+ }
354
+ this._length += 1;
355
+ this._buckets[this._bucketFirst][this._firstInBucket] = element;
356
+ if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
357
+ return true;
358
+ }
359
+ pushMany(elements) {
360
+ const ans = [];
361
+ for (const el of elements) {
362
+ if (this.toElementFn) {
363
+ ans.push(this.push(this.toElementFn(el)));
364
+ } else {
365
+ ans.push(this.push(el));
366
+ }
367
+ }
368
+ return ans;
369
+ }
370
+ unshiftMany(elements = []) {
371
+ const ans = [];
372
+ for (const el of elements) {
373
+ if (this.toElementFn) {
374
+ ans.push(this.unshift(this.toElementFn(el)));
375
+ } else {
376
+ ans.push(this.unshift(el));
377
+ }
378
+ }
379
+ return ans;
380
+ }
381
+ isEmpty() {
382
+ return this._length === 0;
383
+ }
384
+ clear() {
385
+ this._buckets = [new Array(this._bucketSize)];
386
+ this._bucketCount = 1;
387
+ this._bucketFirst = this._bucketLast = this._length = 0;
388
+ this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
389
+ }
390
+ at(pos) {
391
+ if (pos < 0 || pos >= this._length) return void 0;
392
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
393
+ return this._buckets[bucketIndex][indexInBucket];
394
+ }
395
+ setAt(pos, element) {
396
+ rangeCheck(pos, 0, this._length - 1);
397
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
398
+ this._buckets[bucketIndex][indexInBucket] = element;
399
+ return true;
400
+ }
401
+ addAt(pos, element, num = 1) {
402
+ const length = this._length;
403
+ rangeCheck(pos, 0, length);
404
+ if (pos === 0) {
405
+ while (num--) this.unshift(element);
406
+ } else if (pos === this._length) {
407
+ while (num--) this.push(element);
408
+ } else {
409
+ const arr = [];
410
+ for (let i = pos; i < this._length; ++i) {
411
+ const v = this.at(i);
412
+ if (v !== void 0) arr.push(v);
413
+ }
414
+ this.cut(pos - 1, true);
415
+ for (let i = 0; i < num; ++i) this.push(element);
416
+ for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
417
+ }
418
+ return true;
419
+ }
420
+ cut(pos, isCutSelf = false) {
421
+ if (isCutSelf) {
422
+ if (pos < 0) {
423
+ this.clear();
424
+ return this;
425
+ }
426
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
427
+ this._bucketLast = bucketIndex;
428
+ this._lastInBucket = indexInBucket;
429
+ this._length = pos + 1;
430
+ return this;
431
+ } else {
432
+ const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
433
+ newDeque._setBucketSize(this._bucketSize);
434
+ for (let i = 0; i <= pos; i++) {
435
+ const v = this.at(i);
436
+ if (v !== void 0) newDeque.push(v);
437
+ }
438
+ return newDeque;
439
+ }
440
+ }
441
+ splice(start, deleteCount = this._length - start, ...items) {
442
+ rangeCheck(start, 0, this._length);
443
+ if (deleteCount < 0) deleteCount = 0;
444
+ if (start + deleteCount > this._length) deleteCount = this._length - start;
445
+ const removed = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
446
+ removed._setBucketSize(this._bucketSize);
447
+ for (let i = 0; i < deleteCount; i++) {
448
+ const v = this.at(start + i);
449
+ if (v !== void 0) removed.push(v);
450
+ }
451
+ const tail = [];
452
+ for (let i = start + deleteCount; i < this._length; i++) {
453
+ const v = this.at(i);
454
+ if (v !== void 0) tail.push(v);
455
+ }
456
+ this.cut(start - 1, true);
457
+ for (const it of items) this.push(it);
458
+ for (const v of tail) this.push(v);
459
+ return removed;
460
+ }
461
+ cutRest(pos, isCutSelf = false) {
462
+ if (isCutSelf) {
463
+ if (pos < 0) {
464
+ return this;
465
+ }
466
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
467
+ this._bucketFirst = bucketIndex;
468
+ this._firstInBucket = indexInBucket;
469
+ this._length = this._length - pos;
470
+ return this;
471
+ } else {
472
+ const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
473
+ newDeque._setBucketSize(this._bucketSize);
474
+ if (pos < 0) pos = 0;
475
+ for (let i = pos; i < this._length; i++) {
476
+ const v = this.at(i);
477
+ if (v !== void 0) newDeque.push(v);
478
+ }
479
+ return newDeque;
480
+ }
481
+ }
482
+ deleteAt(pos) {
483
+ rangeCheck(pos, 0, this._length - 1);
484
+ let deleted;
485
+ if (pos === 0) {
486
+ return this.shift();
487
+ } else if (pos === this._length - 1) {
488
+ deleted = this.last;
489
+ this.pop();
490
+ return deleted;
491
+ } else {
492
+ const length = this._length - 1;
493
+ const { bucketIndex: targetBucket, indexInBucket: targetPointer } = this._getBucketAndPosition(pos);
494
+ deleted = this._buckets[targetBucket][targetPointer];
495
+ for (let i = pos; i < length; i++) {
496
+ const { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(i);
497
+ const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(i + 1);
498
+ this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
499
+ }
500
+ this.pop();
501
+ return deleted;
502
+ }
503
+ }
504
+ delete(element) {
505
+ const size = this._length;
506
+ if (size === 0) return false;
507
+ let i = 0;
508
+ let index = 0;
509
+ while (i < size) {
510
+ const oldElement = this.at(i);
511
+ if (!this._equals(oldElement, element)) {
512
+ this.setAt(index, oldElement);
513
+ index += 1;
514
+ }
515
+ i += 1;
516
+ }
517
+ this.cut(index - 1, true);
518
+ return true;
519
+ }
520
+ deleteWhere(predicate) {
521
+ for (let i = 0; i < this._length; i++) {
522
+ const v = this.at(i);
523
+ if (predicate(v, i, this)) {
524
+ this.deleteAt(i);
525
+ return true;
526
+ }
527
+ }
528
+ return false;
529
+ }
530
+ setEquality(equals) {
531
+ this._equals = equals;
532
+ return this;
533
+ }
534
+ reverse() {
535
+ this._buckets.reverse().forEach(function(bucket) {
536
+ bucket.reverse();
537
+ });
538
+ const { _bucketFirst, _bucketLast, _firstInBucket, _lastInBucket } = this;
539
+ this._bucketFirst = this._bucketCount - _bucketLast - 1;
540
+ this._bucketLast = this._bucketCount - _bucketFirst - 1;
541
+ this._firstInBucket = this._bucketSize - _lastInBucket - 1;
542
+ this._lastInBucket = this._bucketSize - _firstInBucket - 1;
543
+ return this;
544
+ }
545
+ unique() {
546
+ if (this._length <= 1) {
547
+ return this;
548
+ }
549
+ let index = 1;
550
+ let prev = this.at(0);
551
+ for (let i = 1; i < this._length; ++i) {
552
+ const cur = this.at(i);
553
+ if (!this._equals(cur, prev)) {
554
+ prev = cur;
555
+ this.setAt(index++, cur);
556
+ }
557
+ }
558
+ this.cut(index - 1, true);
559
+ return this;
560
+ }
561
+ shrinkToFit() {
562
+ if (this._length === 0) return;
563
+ const newBuckets = [];
564
+ if (this._bucketFirst === this._bucketLast) return;
565
+ else if (this._bucketFirst < this._bucketLast) {
566
+ for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
567
+ newBuckets.push(this._buckets[i]);
568
+ }
569
+ } else {
570
+ for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
571
+ newBuckets.push(this._buckets[i]);
572
+ }
573
+ for (let i = 0; i <= this._bucketLast; ++i) {
574
+ newBuckets.push(this._buckets[i]);
575
+ }
576
+ }
577
+ this._bucketFirst = 0;
578
+ this._bucketLast = newBuckets.length - 1;
579
+ this._buckets = newBuckets;
580
+ }
581
+ clone() {
582
+ return this._createLike(this, {
583
+ bucketSize: this.bucketSize,
584
+ toElementFn: this.toElementFn,
585
+ maxLen: this._maxLen
586
+ });
587
+ }
588
+ filter(predicate, thisArg) {
589
+ const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
590
+ out._setBucketSize(this._bucketSize);
591
+ let index = 0;
592
+ for (const el of this) {
593
+ if (predicate.call(thisArg, el, index, this)) out.push(el);
594
+ index++;
595
+ }
596
+ return out;
597
+ }
598
+ mapSame(callback, thisArg) {
599
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
600
+ out._setBucketSize(this._bucketSize);
601
+ let index = 0;
602
+ for (const v of this) {
603
+ const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
604
+ out.push(mv);
605
+ }
606
+ return out;
607
+ }
608
+ map(callback, options, thisArg) {
609
+ const out = this._createLike([], {
610
+ ...options ?? {},
611
+ bucketSize: this._bucketSize,
612
+ maxLen: this._maxLen
613
+ });
614
+ let index = 0;
615
+ for (const el of this) {
616
+ const mv = thisArg === void 0 ? callback(el, index, this) : callback.call(thisArg, el, index, this);
617
+ out.push(mv);
618
+ index++;
619
+ }
620
+ return out;
621
+ }
622
+ _setBucketSize(size) {
623
+ this._bucketSize = size;
624
+ }
625
+ *_getIterator() {
626
+ for (let i = 0; i < this._length; ++i) {
627
+ const v = this.at(i);
628
+ if (v !== void 0) yield v;
629
+ }
630
+ }
631
+ _reallocate(needBucketNum) {
632
+ const newBuckets = [];
633
+ const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
634
+ for (let i = 0; i < addBucketNum; ++i) {
635
+ newBuckets[i] = new Array(this._bucketSize);
636
+ }
637
+ for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
638
+ newBuckets[newBuckets.length] = this._buckets[i];
639
+ }
640
+ for (let i = 0; i < this._bucketLast; ++i) {
641
+ newBuckets[newBuckets.length] = this._buckets[i];
642
+ }
643
+ newBuckets[newBuckets.length] = [...this._buckets[this._bucketLast]];
644
+ this._bucketFirst = addBucketNum;
645
+ this._bucketLast = newBuckets.length - 1;
646
+ for (let i = 0; i < addBucketNum; ++i) {
647
+ newBuckets[newBuckets.length] = new Array(this._bucketSize);
648
+ }
649
+ this._buckets = newBuckets;
650
+ this._bucketCount = newBuckets.length;
651
+ }
652
+ _getBucketAndPosition(pos) {
653
+ let bucketIndex;
654
+ let indexInBucket;
655
+ const overallIndex = this._firstInBucket + pos;
656
+ bucketIndex = this._bucketFirst + Math.floor(overallIndex / this._bucketSize);
657
+ if (bucketIndex >= this._bucketCount) {
658
+ bucketIndex -= this._bucketCount;
659
+ }
660
+ indexInBucket = (overallIndex + 1) % this._bucketSize - 1;
661
+ if (indexInBucket < 0) {
662
+ indexInBucket = this._bucketSize - 1;
663
+ }
664
+ return { bucketIndex, indexInBucket };
665
+ }
666
+ _createInstance(options) {
667
+ const Ctor = this.constructor;
668
+ return new Ctor([], options);
669
+ }
670
+ _createLike(elements = [], options) {
671
+ const Ctor = this.constructor;
672
+ return new Ctor(elements, options);
673
+ }
674
+ *_getReverseIterator() {
675
+ for (let i = this._length - 1; i > -1; i--) {
676
+ const v = this.at(i);
677
+ if (v !== void 0) yield v;
678
+ }
679
+ }
680
+ };
681
+ export {
682
+ Deque
683
+ };