directed-graph-typed 2.0.5 → 2.1.1

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 (101) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  2. package/dist/data-structures/base/iterable-element-base.js +149 -107
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  4. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  5. package/dist/data-structures/base/linear-base.d.ts +250 -192
  6. package/dist/data-structures/base/linear-base.js +137 -274
  7. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  8. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  11. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  12. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  14. package/dist/data-structures/binary-tree/binary-tree.js +602 -873
  15. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  16. package/dist/data-structures/binary-tree/bst.js +505 -481
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  18. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  20. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  23. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  24. package/dist/data-structures/graph/abstract-graph.js +267 -237
  25. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  26. package/dist/data-structures/graph/directed-graph.js +146 -233
  27. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  28. package/dist/data-structures/graph/map-graph.js +56 -59
  29. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  30. package/dist/data-structures/graph/undirected-graph.js +129 -149
  31. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  32. package/dist/data-structures/hash/hash-map.js +270 -457
  33. package/dist/data-structures/heap/heap.d.ts +214 -289
  34. package/dist/data-structures/heap/heap.js +340 -349
  35. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  36. package/dist/data-structures/heap/max-heap.js +11 -66
  37. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  38. package/dist/data-structures/heap/min-heap.js +11 -66
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  42. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  43. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  44. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  45. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  46. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  47. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  48. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  49. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  50. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  51. package/dist/data-structures/queue/deque.d.ts +227 -254
  52. package/dist/data-structures/queue/deque.js +309 -348
  53. package/dist/data-structures/queue/queue.d.ts +180 -201
  54. package/dist/data-structures/queue/queue.js +265 -248
  55. package/dist/data-structures/stack/stack.d.ts +124 -102
  56. package/dist/data-structures/stack/stack.js +181 -125
  57. package/dist/data-structures/trie/trie.d.ts +164 -165
  58. package/dist/data-structures/trie/trie.js +189 -172
  59. package/dist/interfaces/binary-tree.d.ts +56 -6
  60. package/dist/interfaces/graph.d.ts +16 -0
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  63. package/dist/types/utils/utils.d.ts +1 -0
  64. package/dist/utils/utils.d.ts +1 -1
  65. package/dist/utils/utils.js +2 -1
  66. package/package.json +2 -2
  67. package/src/data-structures/base/iterable-element-base.ts +238 -115
  68. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  69. package/src/data-structures/base/linear-base.ts +271 -277
  70. package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
  72. package/src/data-structures/binary-tree/avl-tree.ts +237 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +665 -896
  74. package/src/data-structures/binary-tree/bst.ts +565 -572
  75. package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
  76. package/src/data-structures/binary-tree/tree-counter.ts +195 -219
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
  78. package/src/data-structures/graph/abstract-graph.ts +339 -264
  79. package/src/data-structures/graph/directed-graph.ts +146 -236
  80. package/src/data-structures/graph/map-graph.ts +63 -60
  81. package/src/data-structures/graph/undirected-graph.ts +129 -152
  82. package/src/data-structures/hash/hash-map.ts +274 -496
  83. package/src/data-structures/heap/heap.ts +389 -402
  84. package/src/data-structures/heap/max-heap.ts +12 -76
  85. package/src/data-structures/heap/min-heap.ts +13 -76
  86. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  87. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  88. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  89. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  90. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  91. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  92. package/src/data-structures/queue/deque.ts +381 -357
  93. package/src/data-structures/queue/queue.ts +310 -264
  94. package/src/data-structures/stack/stack.ts +217 -131
  95. package/src/data-structures/trie/trie.ts +240 -175
  96. package/src/interfaces/binary-tree.ts +240 -6
  97. package/src/interfaces/graph.ts +37 -0
  98. package/src/types/data-structures/base/base.ts +5 -5
  99. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  100. package/src/types/utils/utils.ts +2 -0
  101. package/src/utils/utils.ts +9 -14
@@ -1,9 +1,20 @@
1
1
  "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Pablo Zeng
6
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
10
  exports.Deque = void 0;
4
11
  const utils_1 = require("../../utils");
5
12
  const linear_base_1 = require("../base/linear-base");
6
13
  /**
14
+ * Deque implemented with circular buckets allowing O(1) amortized push/pop at both ends.
15
+ * @remarks Time O(1), Space O(1)
16
+ * @template E
17
+ * @template R
7
18
  * 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
8
19
  * 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
9
20
  * 3. Continuous Memory Allocation: Since it is based on an array, all elements are stored contiguously in memory, which can bring cache friendliness and efficient memory access.
@@ -100,18 +111,15 @@ const linear_base_1 = require("../base/linear-base");
100
111
  */
101
112
  class Deque extends linear_base_1.LinearBase {
102
113
  /**
103
- * The constructor initializes a Deque object with optional iterable of elements and options.
104
- * @param elements - An iterable object (such as an array or a Set) that contains the initial
105
- * elements to be added to the deque. It can also be an object with a `length` or `size` property
106
- * that represents the number of elements in the iterable object. If no elements are provided, an
107
- * empty deque
108
- * @param {DequeOptions} [options] - The `options` parameter is an optional object that can contain
109
- * configuration options for the deque. In this code, it is used to set the `bucketSize` option,
110
- * which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
111
- * or is not a number
114
+ * Create a Deque and optionally bulk-insert elements.
115
+ * @remarks Time O(N), Space O(N)
116
+ * @param [elements] - Iterable (or iterable-like) of elements/records to insert.
117
+ * @param [options] - Options such as bucketSize, toElementFn, and maxLen.
118
+ * @returns New Deque instance.
112
119
  */
113
120
  constructor(elements = [], options) {
114
121
  super(options);
122
+ this._equals = Object.is;
115
123
  this._bucketSize = 1 << 12;
116
124
  this._bucketFirst = 0;
117
125
  this._firstInBucket = 0;
@@ -127,16 +135,10 @@ class Deque extends linear_base_1.LinearBase {
127
135
  }
128
136
  let _size;
129
137
  if ('length' in elements) {
130
- if (elements.length instanceof Function)
131
- _size = elements.length();
132
- else
133
- _size = elements.length;
138
+ _size = typeof elements.length === 'function' ? elements.length() : elements.length;
134
139
  }
135
140
  else {
136
- if (elements.size instanceof Function)
137
- _size = elements.size();
138
- else
139
- _size = elements.size;
141
+ _size = typeof elements.size === 'function' ? elements.size() : elements.size;
140
142
  }
141
143
  this._bucketCount = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize) || 1;
142
144
  for (let i = 0; i < this._bucketCount; ++i) {
@@ -147,34 +149,74 @@ class Deque extends linear_base_1.LinearBase {
147
149
  this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
148
150
  this.pushMany(elements);
149
151
  }
152
+ /**
153
+ * Get the current bucket size.
154
+ * @remarks Time O(1), Space O(1)
155
+ * @returns Bucket capacity per bucket.
156
+ */
150
157
  get bucketSize() {
151
158
  return this._bucketSize;
152
159
  }
160
+ /**
161
+ * Get the index of the first bucket in use.
162
+ * @remarks Time O(1), Space O(1)
163
+ * @returns Zero-based bucket index.
164
+ */
153
165
  get bucketFirst() {
154
166
  return this._bucketFirst;
155
167
  }
168
+ /**
169
+ * Get the index inside the first bucket.
170
+ * @remarks Time O(1), Space O(1)
171
+ * @returns Zero-based index within the first bucket.
172
+ */
156
173
  get firstInBucket() {
157
174
  return this._firstInBucket;
158
175
  }
176
+ /**
177
+ * Get the index of the last bucket in use.
178
+ * @remarks Time O(1), Space O(1)
179
+ * @returns Zero-based bucket index.
180
+ */
159
181
  get bucketLast() {
160
182
  return this._bucketLast;
161
183
  }
184
+ /**
185
+ * Get the index inside the last bucket.
186
+ * @remarks Time O(1), Space O(1)
187
+ * @returns Zero-based index within the last bucket.
188
+ */
162
189
  get lastInBucket() {
163
190
  return this._lastInBucket;
164
191
  }
192
+ /**
193
+ * Get the number of buckets allocated.
194
+ * @remarks Time O(1), Space O(1)
195
+ * @returns Bucket count.
196
+ */
165
197
  get bucketCount() {
166
198
  return this._bucketCount;
167
199
  }
200
+ /**
201
+ * Get the internal buckets array.
202
+ * @remarks Time O(1), Space O(1)
203
+ * @returns Array of buckets storing values.
204
+ */
168
205
  get buckets() {
169
206
  return this._buckets;
170
207
  }
208
+ /**
209
+ * Get the number of elements in the deque.
210
+ * @remarks Time O(1), Space O(1)
211
+ * @returns Current length.
212
+ */
171
213
  get length() {
172
214
  return this._length;
173
215
  }
174
216
  /**
175
- * The function returns the first element in a collection if it exists, otherwise it returns
176
- * undefined.
177
- * @returns The first element of the collection, of type E, is being returned.
217
+ * Get the first element without removing it.
218
+ * @remarks Time O(1), Space O(1)
219
+ * @returns First element or undefined.
178
220
  */
179
221
  get first() {
180
222
  if (this._length === 0)
@@ -182,8 +224,9 @@ class Deque extends linear_base_1.LinearBase {
182
224
  return this._buckets[this._bucketFirst][this._firstInBucket];
183
225
  }
184
226
  /**
185
- * The last function returns the last element in the queue.
186
- * @return The last element in the array
227
+ * Get the last element without removing it.
228
+ * @remarks Time O(1), Space O(1)
229
+ * @returns Last element or undefined.
187
230
  */
188
231
  get last() {
189
232
  if (this._length === 0)
@@ -191,13 +234,23 @@ class Deque extends linear_base_1.LinearBase {
191
234
  return this._buckets[this._bucketLast][this._lastInBucket];
192
235
  }
193
236
  /**
194
- * Time Complexity - Amortized O(1) (possible reallocation),
195
- * Space Complexity - O(n) (due to potential resizing).
196
- *
197
- * The push function adds an element to a data structure and reallocates memory if necessary.
198
- * @param {E} element - The `element` parameter represents the value that you want to add to the data
199
- * structure.
200
- * @returns The size of the data structure after the element has been pushed.
237
+ * Create a Deque from an array of elements.
238
+ * @remarks Time O(N), Space O(N)
239
+ * @template E
240
+ * @template R
241
+ * @param this - Constructor (subclass) to instantiate.
242
+ * @param data - Array of elements to insert in order.
243
+ * @param [options] - Options forwarded to the constructor.
244
+ * @returns A new Deque populated from the array.
245
+ */
246
+ static fromArray(data, options) {
247
+ return new this(data, options);
248
+ }
249
+ /**
250
+ * Append one element at the back.
251
+ * @remarks Time O(1) amortized, Space O(1)
252
+ * @param element - Element to append.
253
+ * @returns True when appended.
201
254
  */
202
255
  push(element) {
203
256
  if (this._length) {
@@ -222,12 +275,9 @@ class Deque extends linear_base_1.LinearBase {
222
275
  return true;
223
276
  }
224
277
  /**
225
- * Time Complexity: O(1)
226
- * Space Complexity: O(1)
227
- *
228
- * The `pop()` function removes and returns the last element from a data structure, updating the
229
- * internal state variables accordingly.
230
- * @returns The element that was removed from the data structure is being returned.
278
+ * Remove and return the last element.
279
+ * @remarks Time O(1), Space O(1)
280
+ * @returns Removed element or undefined.
231
281
  */
232
282
  pop() {
233
283
  if (this._length === 0)
@@ -250,13 +300,9 @@ class Deque extends linear_base_1.LinearBase {
250
300
  return element;
251
301
  }
252
302
  /**
253
- * Time Complexity: O(1)
254
- * Space Complexity: O(1)
255
- *
256
- * The `shift()` function removes and returns the first element from a data structure, updating the
257
- * internal state variables accordingly.
258
- * @returns The element that is being removed from the beginning of the data structure is being
259
- * returned.
303
+ * Remove and return the first element.
304
+ * @remarks Time O(1) amortized, Space O(1)
305
+ * @returns Removed element or undefined.
260
306
  */
261
307
  shift() {
262
308
  if (this._length === 0)
@@ -279,14 +325,10 @@ class Deque extends linear_base_1.LinearBase {
279
325
  return element;
280
326
  }
281
327
  /**
282
- * Time Complexity: Amortized O(1)
283
- * Space Complexity: O(n)
284
- *
285
- * The `unshift` function adds an element to the beginning of an array-like data structure and
286
- * returns the new size of the structure.
287
- * @param {E} element - The `element` parameter represents the element that you want to add to the
288
- * beginning of the data structure.
289
- * @returns The size of the data structure after the element has been added.
328
+ * Prepend one element at the front.
329
+ * @remarks Time O(1) amortized, Space O(1)
330
+ * @param element - Element to prepend.
331
+ * @returns True when prepended.
290
332
  */
291
333
  unshift(element) {
292
334
  if (this._length) {
@@ -311,18 +353,10 @@ class Deque extends linear_base_1.LinearBase {
311
353
  return true;
312
354
  }
313
355
  /**
314
- * Time Complexity: O(k)
315
- * Space Complexity: O(k)
316
- *
317
- * The function `pushMany` iterates over elements and pushes them into an array after applying a
318
- * transformation function if provided.
319
- * @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
320
- * parameter in the `pushMany` function is expected to be an iterable containing elements of type `E`
321
- * or `R`. It can be either an `IterableWithSizeOrLength<E>` or an `IterableWithSizeOrLength<R>`. The
322
- * function iterates over each element
323
- * @returns The `pushMany` function is returning an array of boolean values, where each value
324
- * represents the result of calling the `push` method on the current object instance with the
325
- * corresponding element from the input `elements` iterable.
356
+ * Append a sequence of elements.
357
+ * @remarks Time O(N), Space O(1)
358
+ * @param elements - Iterable (or iterable-like) of elements/records.
359
+ * @returns Array of per-element success flags.
326
360
  */
327
361
  pushMany(elements) {
328
362
  const ans = [];
@@ -337,17 +371,10 @@ class Deque extends linear_base_1.LinearBase {
337
371
  return ans;
338
372
  }
339
373
  /**
340
- * Time Complexity: O(k)
341
- * Space Complexity: O(k)
342
- *
343
- * The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of
344
- * an array, optionally converting them using a provided function.
345
- * @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
346
- * parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It
347
- * can be an array or any other iterable data structure that has a known size or length. The function
348
- * iterates over each element in the `elements` iterable and
349
- * @returns The `unshiftMany` function returns an array of boolean values indicating whether each
350
- * element was successfully added to the beginning of the array.
374
+ * Prepend a sequence of elements.
375
+ * @remarks Time O(N), Space O(1)
376
+ * @param [elements] - Iterable (or iterable-like) of elements/records.
377
+ * @returns Array of per-element success flags.
351
378
  */
352
379
  unshiftMany(elements = []) {
353
380
  const ans = [];
@@ -362,21 +389,17 @@ class Deque extends linear_base_1.LinearBase {
362
389
  return ans;
363
390
  }
364
391
  /**
365
- * Time Complexity: O(1)
366
- * Space Complexity: O(1)
367
- *
368
- * The function checks if the size of an object is equal to zero and returns a boolean value.
369
- * @returns A boolean value indicating whether the size of the object is 0 or not.
392
+ * Check whether the deque is empty.
393
+ * @remarks Time O(1), Space O(1)
394
+ * @returns True if length is 0.
370
395
  */
371
396
  isEmpty() {
372
397
  return this._length === 0;
373
398
  }
374
399
  /**
375
- * Time Complexity: O(1)
376
- * Space Complexity: O(1)
377
- *
378
- * The clear() function resets the state of the object by initializing all variables to their default
379
- * values.
400
+ * Remove all elements and reset structure.
401
+ * @remarks Time O(1), Space O(1)
402
+ * @returns void
380
403
  */
381
404
  clear() {
382
405
  this._buckets = [new Array(this._bucketSize)];
@@ -385,29 +408,23 @@ class Deque extends linear_base_1.LinearBase {
385
408
  this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
386
409
  }
387
410
  /**
388
- * Time Complexity: O(1)
389
- * Space Complexity: O(1)
390
- *
391
- * The `at` function retrieves an element at a specified position in an array-like data structure.
392
- * @param {number} pos - The `pos` parameter represents the position of the element that you want to
393
- * retrieve from the data structure. It is of type `number` and should be a valid index within the
394
- * range of the data structure.
395
- * @returns The element at the specified position in the data structure is being returned.
411
+ * Get the element at a given position.
412
+ * @remarks Time O(1), Space O(1)
413
+ * @param pos - Zero-based position from the front.
414
+ * @returns Element or undefined.
396
415
  */
397
416
  at(pos) {
398
- (0, utils_1.rangeCheck)(pos, 0, this._length - 1);
417
+ if (pos < 0 || pos >= this._length)
418
+ return undefined;
399
419
  const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
400
420
  return this._buckets[bucketIndex][indexInBucket];
401
421
  }
402
422
  /**
403
- * Time Complexity: O(1)
404
- * Space Complexity: O(1)
405
- *
406
- * The `setAt` function sets an element at a specific position in an array-like data structure.
407
- * @param {number} pos - The `pos` parameter represents the position at which the element needs to be
408
- * set. It is of type `number`.
409
- * @param {E} element - The `element` parameter is the value that you want to set at the specified
410
- * position in the data structure.
423
+ * Replace the element at a given position.
424
+ * @remarks Time O(1), Space O(1)
425
+ * @param pos - Zero-based position from the front.
426
+ * @param element - New element value.
427
+ * @returns True if updated.
411
428
  */
412
429
  setAt(pos, element) {
413
430
  (0, utils_1.rangeCheck)(pos, 0, this._length - 1);
@@ -416,19 +433,12 @@ class Deque extends linear_base_1.LinearBase {
416
433
  return true;
417
434
  }
418
435
  /**
419
- * Time Complexity: O(n)
420
- * Space Complexity: O(n)
421
- *
422
- * The `addAt` function inserts one or more elements at a specified position in an array-like data
423
- * structure.
424
- * @param {number} pos - The `pos` parameter represents the position at which the element(s) should
425
- * be inserted. It is of type `number`.
426
- * @param {E} element - The `element` parameter represents the element that you want to insert into
427
- * the array at the specified position.
428
- * @param [num=1] - The `num` parameter represents the number of times the `element` should be
429
- * inserted at the specified position (`pos`). By default, it is set to 1, meaning that the `element`
430
- * will be inserted once. However, you can provide a different value for `num` if you want
431
- * @returns The size of the array after the insertion is being returned.
436
+ * Insert repeated copies of an element at a position.
437
+ * @remarks Time O(N), Space O(1)
438
+ * @param pos - Zero-based position from the front.
439
+ * @param element - Element to insert.
440
+ * @param [num] - Number of times to insert (default 1).
441
+ * @returns True if inserted.
432
442
  */
433
443
  addAt(pos, element, num = 1) {
434
444
  const length = this._length;
@@ -444,7 +454,9 @@ class Deque extends linear_base_1.LinearBase {
444
454
  else {
445
455
  const arr = [];
446
456
  for (let i = pos; i < this._length; ++i) {
447
- arr.push(this.at(i));
457
+ const v = this.at(i);
458
+ if (v !== undefined)
459
+ arr.push(v);
448
460
  }
449
461
  this.cut(pos - 1, true);
450
462
  for (let i = 0; i < num; ++i)
@@ -455,15 +467,11 @@ class Deque extends linear_base_1.LinearBase {
455
467
  return true;
456
468
  }
457
469
  /**
458
- * Time Complexity: O(1)
459
- * Space Complexity: O(1)
460
- *
461
- * The `cut` function updates the state of the object based on the given position and returns the
462
- * updated size.
463
- * @param {number} pos - The `pos` parameter represents the position at which the string should be
464
- * cut. It is a number that indicates the index of the character where the cut should be made.
465
- * @param {boolean} isCutSelf - If true, the original deque will not be cut, and return a new deque
466
- * @returns The method is returning the updated size of the data structure.
470
+ * Cut the deque to keep items up to index; optionally mutate in-place.
471
+ * @remarks Time O(N), Space O(1)
472
+ * @param pos - Last index to keep.
473
+ * @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
474
+ * @returns This deque if in-place; otherwise a new deque of the prefix.
467
475
  */
468
476
  cut(pos, isCutSelf = false) {
469
477
  if (isCutSelf) {
@@ -478,79 +486,56 @@ class Deque extends linear_base_1.LinearBase {
478
486
  return this;
479
487
  }
480
488
  else {
481
- const newDeque = this._createInstance({
482
- bucketSize: this._bucketSize,
483
- toElementFn: this._toElementFn,
484
- maxLen: this._maxLen
485
- });
489
+ const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
490
+ newDeque._setBucketSize(this._bucketSize);
486
491
  for (let i = 0; i <= pos; i++) {
487
- newDeque.push(this.at(i));
492
+ const v = this.at(i);
493
+ if (v !== undefined)
494
+ newDeque.push(v);
488
495
  }
489
496
  return newDeque;
490
497
  }
491
498
  }
492
499
  /**
493
- * Time Complexity: O(n)
494
- * Space Complexity: O(1)
495
- *
496
- * The `splice` function in TypeScript overrides the default behavior to remove and insert elements
497
- * in a Deque data structure while ensuring the starting position and delete count are within bounds.
498
- * @param {number} start - The `start` parameter in the `splice` method represents the index at which
499
- * to start changing the array. Items will be removed or added starting from this index.
500
- * @param {number} deleteCount - The `deleteCount` parameter in the `splice` method represents the
501
- * number of elements to remove from the array starting at the specified `start` index. If
502
- * `deleteCount` is not provided, it defaults to the number of elements from the `start` index to the
503
- * end of the array (`
504
- * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
505
- * will be inserted into the deque at the specified `start` index. These elements will be inserted in
506
- * place of the elements that are removed based on the `start` and `deleteCount` parameters.
507
- * @returns The `splice` method is returning the array `deletedElements` which contains the elements
508
- * that were removed from the Deque during the splice operation.
500
+ * Remove and/or insert elements at a position (array-like behavior).
501
+ * @remarks Time O(N + M), Space O(M)
502
+ * @param start - Start index (clamped to [0, length]).
503
+ * @param [deleteCount] - Number of elements to remove (default: length - start).
504
+ * @param [items] - Elements to insert after `start`.
505
+ * @returns A new deque containing the removed elements (typed as `this`).
509
506
  */
510
507
  splice(start, deleteCount = this._length - start, ...items) {
511
- // Check whether the starting position is legal
512
508
  (0, utils_1.rangeCheck)(start, 0, this._length);
513
- // Adjust the value of deleteCount
514
509
  if (deleteCount < 0)
515
510
  deleteCount = 0;
516
511
  if (start + deleteCount > this._length)
517
512
  deleteCount = this._length - start;
518
- // Save deleted elements
519
- const deletedElements = this._createInstance();
520
- // Add removed elements to the result
513
+ const removed = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
514
+ removed._setBucketSize(this._bucketSize);
521
515
  for (let i = 0; i < deleteCount; i++) {
522
- deletedElements.push(this.at(start + i));
516
+ const v = this.at(start + i);
517
+ if (v !== undefined)
518
+ removed.push(v);
523
519
  }
524
- // Calculate the range that needs to be deleted
525
- const elementsAfter = [];
520
+ const tail = [];
526
521
  for (let i = start + deleteCount; i < this._length; i++) {
527
- elementsAfter.push(this.at(i));
522
+ const v = this.at(i);
523
+ if (v !== undefined)
524
+ tail.push(v);
528
525
  }
529
- // Adjust the length of the current Deque
530
526
  this.cut(start - 1, true);
531
- for (const item of items) {
532
- this.push(item);
533
- }
534
- // Insert subsequent elements back
535
- for (const element of elementsAfter) {
536
- this.push(element);
537
- }
538
- return deletedElements;
539
- }
540
- /**
541
- * Time Complexity: O(1)
542
- * Space Complexity: O(1) or O(n)
543
- *
544
- * The `cutRest` function cuts the elements from a specified position in a deque and returns a new
545
- * deque with the cut elements.
546
- * @param {number} pos - The `pos` parameter represents the position from which to cut the Deque. It
547
- * is a number that indicates the index of the element in the Deque where the cut should start.
548
- * @param [isCutSelf=false] - isCutSelf is a boolean parameter that determines whether the original
549
- * Deque should be modified or a new Deque should be created. If isCutSelf is true, the original
550
- * Deque will be modified by cutting off elements starting from the specified position. If isCutSelf
551
- * is false, a new De
552
- * @returns The function `cutRest` returns either the modified original deque (`this`) or a new deque
553
- * (`newDeque`) depending on the value of the `isCutSelf` parameter.
527
+ for (const it of items)
528
+ this.push(it);
529
+ for (const v of tail)
530
+ this.push(v);
531
+ return removed;
532
+ }
533
+ /**
534
+ * Cut the deque to keep items from index onward; optionally mutate in-place.
535
+ * @remarks Time O(N), Space O(1)
536
+ * @param pos - First index to keep.
537
+ * @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
538
+ * @returns This deque if in-place; otherwise a new deque of the suffix.
554
539
  */
555
540
  cutRest(pos, isCutSelf = false) {
556
541
  if (isCutSelf) {
@@ -564,45 +549,36 @@ class Deque extends linear_base_1.LinearBase {
564
549
  return this;
565
550
  }
566
551
  else {
567
- const newDeque = this._createInstance({
568
- bucketSize: this._bucketSize,
569
- toElementFn: this._toElementFn,
570
- maxLen: this._maxLen
571
- });
552
+ const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
553
+ newDeque._setBucketSize(this._bucketSize);
572
554
  if (pos < 0)
573
555
  pos = 0;
574
556
  for (let i = pos; i < this._length; i++) {
575
- newDeque.push(this.at(i));
557
+ const v = this.at(i);
558
+ if (v !== undefined)
559
+ newDeque.push(v);
576
560
  }
577
561
  return newDeque;
578
562
  }
579
563
  }
580
564
  /**
581
- * Time Complexity: O(n)
582
- * Space Complexity: O(1) or O(n)
583
- *
584
- * The `deleteAt` function removes an element at a specified position in an array-like data
585
- * structure.
586
- * @param {number} pos - The `pos` parameter in the `deleteAt` function represents the position at
587
- * which an element needs to be deleted from the data structure. It is of type `number` and indicates
588
- * the index of the element to be deleted.
589
- * @returns The size of the data structure after the deletion operation is performed.
565
+ * Delete the element at a given position.
566
+ * @remarks Time O(N), Space O(1)
567
+ * @param pos - Zero-based position from the front.
568
+ * @returns Removed element or undefined.
590
569
  */
591
570
  deleteAt(pos) {
592
571
  (0, utils_1.rangeCheck)(pos, 0, this._length - 1);
593
572
  let deleted;
594
573
  if (pos === 0) {
595
- //If it is the first element, use shift() directly
596
574
  return this.shift();
597
575
  }
598
576
  else if (pos === this._length - 1) {
599
- // If it is the last element, just use pop()
600
577
  deleted = this.last;
601
578
  this.pop();
602
579
  return deleted;
603
580
  }
604
581
  else {
605
- // Delete the middle element
606
582
  const length = this._length - 1;
607
583
  const { bucketIndex: targetBucket, indexInBucket: targetPointer } = this._getBucketAndPosition(pos);
608
584
  deleted = this._buckets[targetBucket][targetPointer];
@@ -611,20 +587,15 @@ class Deque extends linear_base_1.LinearBase {
611
587
  const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(i + 1);
612
588
  this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
613
589
  }
614
- // Remove last duplicate element
615
590
  this.pop();
616
591
  return deleted;
617
592
  }
618
593
  }
619
594
  /**
620
- * Time Complexity: O(n)
621
- * Space Complexity: O(1)
622
- *
623
- * The `delete` function removes all occurrences of a specified element from an array-like data
624
- * structure.
625
- * @param {E} element - The `element` parameter represents the element that you want to delete from
626
- * the data structure.
627
- * @returns The size of the data structure after the element has been deleted.
595
+ * Delete the first occurrence of a value.
596
+ * @remarks Time O(N), Space O(1)
597
+ * @param element - Element to remove (using the configured equality).
598
+ * @returns True if an element was removed.
628
599
  */
629
600
  delete(element) {
630
601
  const size = this._length;
@@ -634,7 +605,7 @@ class Deque extends linear_base_1.LinearBase {
634
605
  let index = 0;
635
606
  while (i < size) {
636
607
  const oldElement = this.at(i);
637
- if (oldElement !== element) {
608
+ if (!this._equals(oldElement, element)) {
638
609
  this.setAt(index, oldElement);
639
610
  index += 1;
640
611
  }
@@ -643,50 +614,36 @@ class Deque extends linear_base_1.LinearBase {
643
614
  this.cut(index - 1, true);
644
615
  return true;
645
616
  }
646
- // /**
647
- // * Time Complexity: O(n)
648
- // * Space Complexity: O(1)
649
- // *
650
- // * This function overrides the indexOf method to search for an element within a custom data
651
- // * structure.
652
- // * @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
653
- // * within the data structure. The `indexOf` method will return the index of the first occurrence of
654
- // * this element within the data structure.
655
- // * @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
656
- // * index at which to start searching for the `searchElement` within the data structure. If provided,
657
- // * the search will begin at this index instead of the beginning of the data structure.
658
- // * @returns The indexOf method is returning the index of the searchElement if it is found in the data
659
- // * structure, or -1 if the searchElement is not found.
660
- // */
661
- // override indexOf(searchElement: E, fromIndex: number = 0): number {
662
- // let index = fromIndex;
663
- // let bucketIndex = this._bucketFirst;
664
- // let indexInBucket = this._firstInBucket + fromIndex;
665
- //
666
- // for (let i = 0; i < this._length; i++) {
667
- // if (this._buckets[bucketIndex][indexInBucket] === searchElement) {
668
- // return index;
669
- // }
670
- // index++;
671
- // indexInBucket++;
672
- // if (indexInBucket >= this._bucketSize) {
673
- // bucketIndex++;
674
- // indexInBucket = 0;
675
- // }
676
- // if (bucketIndex >= this._bucketCount) {
677
- // bucketIndex = 0;
678
- // }
679
- // }
680
- // return -1;
681
- // }
682
- /**
683
- * Time Complexity: O(n)
684
- * Space Complexity: O(1)
685
- *
686
- * The reverse() function reverses the order of the buckets and the elements within each bucket in a
687
- * data structure.
688
- * @returns The reverse() method is returning the object itself (this) after performing the reverse
689
- * operation on the buckets and updating the relevant properties.
617
+ /**
618
+ * Delete the first element matching a predicate.
619
+ * @remarks Time O(N), Space O(1)
620
+ * @param predicate - Function (value, index, deque) → boolean.
621
+ * @returns True if a match was removed.
622
+ */
623
+ deleteWhere(predicate) {
624
+ for (let i = 0; i < this._length; i++) {
625
+ const v = this.at(i);
626
+ if (predicate(v, i, this)) {
627
+ this.deleteAt(i);
628
+ return true;
629
+ }
630
+ }
631
+ return false;
632
+ }
633
+ /**
634
+ * Set the equality comparator used by delete operations.
635
+ * @remarks Time O(1), Space O(1)
636
+ * @param equals - Equality predicate (a, b) → boolean.
637
+ * @returns This deque.
638
+ */
639
+ setEquality(equals) {
640
+ this._equals = equals;
641
+ return this;
642
+ }
643
+ /**
644
+ * Reverse the deque by reversing buckets and pointers.
645
+ * @remarks Time O(N), Space O(N)
646
+ * @returns This deque.
690
647
  */
691
648
  reverse() {
692
649
  this._buckets.reverse().forEach(function (bucket) {
@@ -700,12 +657,9 @@ class Deque extends linear_base_1.LinearBase {
700
657
  return this;
701
658
  }
702
659
  /**
703
- * Time Complexity: O(n)
704
- * Space Complexity: O(1)
705
- *
706
- * The `unique()` function removes duplicate elements from an array-like data structure and returns
707
- * the number of unique elements.
708
- * @returns The size of the modified array is being returned.
660
+ * Deduplicate consecutive equal elements in-place.
661
+ * @remarks Time O(N), Space O(1)
662
+ * @returns This deque.
709
663
  */
710
664
  unique() {
711
665
  if (this._length <= 1) {
@@ -715,7 +669,7 @@ class Deque extends linear_base_1.LinearBase {
715
669
  let prev = this.at(0);
716
670
  for (let i = 1; i < this._length; ++i) {
717
671
  const cur = this.at(i);
718
- if (cur !== prev) {
672
+ if (!this._equals(cur, prev)) {
719
673
  prev = cur;
720
674
  this.setAt(index++, cur);
721
675
  }
@@ -724,13 +678,9 @@ class Deque extends linear_base_1.LinearBase {
724
678
  return this;
725
679
  }
726
680
  /**
727
- * Time Complexity: O(n)
728
- * Space Complexity: O(n)
729
- *
730
- * The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
731
- * memory usage.
732
- * @returns Nothing is being returned. The function is using the `return` statement to exit early if
733
- * `this._length` is 0, but it does not return any value.
681
+ * Trim unused buckets to fit exactly the active range.
682
+ * @remarks Time O(N), Space O(1)
683
+ * @returns void
734
684
  */
735
685
  shrinkToFit() {
736
686
  if (this._length === 0)
@@ -756,99 +706,98 @@ class Deque extends linear_base_1.LinearBase {
756
706
  this._buckets = newBuckets;
757
707
  }
758
708
  /**
759
- * Time Complexity: O(n)
760
- * Space Complexity: O(n)
761
- *
762
- * The `clone()` function returns a new instance of the `Deque` class with the same elements and
763
- * bucket size as the original instance.
764
- * @returns The `clone()` method is returning a new instance of the `Deque` class with the same
765
- * elements as the original deque (`this`) and the same bucket size.
709
+ * Deep clone this deque, preserving options.
710
+ * @remarks Time O(N), Space O(N)
711
+ * @returns A new deque with the same content and options.
766
712
  */
767
713
  clone() {
768
- return new Deque(this, {
714
+ return this._createLike(this, {
769
715
  bucketSize: this.bucketSize,
770
716
  toElementFn: this.toElementFn,
771
717
  maxLen: this._maxLen
772
718
  });
773
719
  }
774
720
  /**
775
- * Time Complexity: O(n)
776
- * Space Complexity: O(n)
777
- *
778
- * The `filter` function creates a new deque containing elements from the original deque that satisfy
779
- * a given predicate function.
780
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
781
- * the current element being iterated over, the index of the current element, and the deque itself.
782
- * It should return a boolean value indicating whether the element should be included in the filtered
783
- * deque or not.
784
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
785
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
786
- * passed as the `this` value to the `predicate` function. If `thisArg` is
787
- * @returns The `filter` method is returning a new `Deque` object that contains the elements that
788
- * satisfy the given predicate function.
721
+ * Filter elements into a new deque of the same class.
722
+ * @remarks Time O(N), Space O(N)
723
+ * @param predicate - Predicate (value, index, deque) → boolean to keep element.
724
+ * @param [thisArg] - Value for `this` inside the predicate.
725
+ * @returns A new deque with kept elements.
789
726
  */
790
727
  filter(predicate, thisArg) {
791
- const newDeque = this._createInstance({
792
- bucketSize: this._bucketSize,
793
- toElementFn: this.toElementFn,
794
- maxLen: this._maxLen
795
- });
728
+ const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
729
+ out._setBucketSize(this._bucketSize);
796
730
  let index = 0;
797
731
  for (const el of this) {
798
- if (predicate.call(thisArg, el, index, this)) {
799
- newDeque.push(el);
800
- }
732
+ if (predicate.call(thisArg, el, index, this))
733
+ out.push(el);
801
734
  index++;
802
735
  }
803
- return newDeque;
804
- }
805
- /**
806
- * Time Complexity: O(n)
807
- * Space Complexity: O(n)
808
- *
809
- * The `map` function takes a callback function and applies it to each element in the deque,
810
- * returning a new deque with the results.
811
- * @param callback - The callback parameter is a function that will be called for each element in the
812
- * deque. It takes three arguments: the current element, the index of the element, and the deque
813
- * itself. It should return a value of type EM.
814
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
815
- * transform the raw element (`RM`) into a new element (`EM`) before adding it to the new deque. If
816
- * provided, this function will be called for each raw element in the original deque.
817
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
818
- * specify the value of `this` within the callback function. It is used to set the context or scope
819
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
820
- * value of
821
- * @returns a new Deque object with elements of type EM and raw elements of type RM.
822
- */
823
- map(callback, toElementFn, thisArg) {
824
- const newDeque = new Deque([], { bucketSize: this._bucketSize, toElementFn, maxLen: this._maxLen });
736
+ return out;
737
+ }
738
+ /**
739
+ * Map elements into a new deque of the same element type.
740
+ * @remarks Time O(N), Space O(N)
741
+ * @param callback - Mapping function (value, index, deque) → newValue.
742
+ * @param [thisArg] - Value for `this` inside the callback.
743
+ * @returns A new deque with mapped values.
744
+ */
745
+ mapSame(callback, thisArg) {
746
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
747
+ out._setBucketSize(this._bucketSize);
748
+ let index = 0;
749
+ for (const v of this) {
750
+ const mv = thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
751
+ out.push(mv);
752
+ }
753
+ return out;
754
+ }
755
+ /**
756
+ * Map elements into a new deque (possibly different element type).
757
+ * @remarks Time O(N), Space O(N)
758
+ * @template EM
759
+ * @template RM
760
+ * @param callback - Mapping function (value, index, deque) → newElement.
761
+ * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
762
+ * @param [thisArg] - Value for `this` inside the callback.
763
+ * @returns A new Deque with mapped elements.
764
+ */
765
+ map(callback, options, thisArg) {
766
+ const out = this._createLike([], Object.assign(Object.assign({}, (options !== null && options !== void 0 ? options : {})), { bucketSize: this._bucketSize, maxLen: this._maxLen }));
825
767
  let index = 0;
826
768
  for (const el of this) {
827
- newDeque.push(callback.call(thisArg, el, index, this));
769
+ const mv = thisArg === undefined ? callback(el, index, this) : callback.call(thisArg, el, index, this);
770
+ out.push(mv);
828
771
  index++;
829
772
  }
830
- return newDeque;
773
+ return out;
831
774
  }
832
775
  /**
833
- * Time Complexity: O(n)
834
- * Space Complexity: O(1)
835
- *
836
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
837
- * object to be iterated over using a for...of loop.
776
+ * (Protected) Set the internal bucket size.
777
+ * @remarks Time O(1), Space O(1)
778
+ * @param size - Bucket capacity to assign.
779
+ * @returns void
780
+ */
781
+ _setBucketSize(size) {
782
+ this._bucketSize = size;
783
+ }
784
+ /**
785
+ * (Protected) Iterate elements from front to back.
786
+ * @remarks Time O(N), Space O(1)
787
+ * @returns Iterator of elements.
838
788
  */
839
789
  *_getIterator() {
840
790
  for (let i = 0; i < this._length; ++i) {
841
- yield this.at(i);
791
+ const v = this.at(i);
792
+ if (v !== undefined)
793
+ yield v;
842
794
  }
843
795
  }
844
796
  /**
845
- * Time Complexity: O(n)
846
- * Space Complexity: O(n)
847
- *
848
- * The `_reallocate` function reallocates the buckets in an array, adding new buckets if needed.
849
- * @param {number} [needBucketNum] - The `needBucketNum` parameter is an optional number that
850
- * specifies the number of new buckets needed. If not provided, it will default to half of the
851
- * current bucket count (`this._bucketCount >> 1`) or 1 if the current bucket count is less than 2.
797
+ * (Protected) Reallocate buckets to make room near the ends.
798
+ * @remarks Time O(N), Space O(N)
799
+ * @param [needBucketNum] - How many extra buckets to add; defaults to half of current.
800
+ * @returns void
852
801
  */
853
802
  _reallocate(needBucketNum) {
854
803
  const newBuckets = [];
@@ -872,13 +821,10 @@ class Deque extends linear_base_1.LinearBase {
872
821
  this._bucketCount = newBuckets.length;
873
822
  }
874
823
  /**
875
- * Time Complexity: O(1)
876
- * Space Complexity: O(1)
877
- *
878
- * The function calculates the bucket index and index within the bucket based on the given position.
879
- * @param {number} pos - The `pos` parameter represents the position within the data structure. It is
880
- * a number that indicates the index or position of an element within the structure.
881
- * @returns an object with two properties: "bucketIndex" and "indexInBucket".
824
+ * (Protected) Translate a logical position to bucket/offset.
825
+ * @remarks Time O(1), Space O(1)
826
+ * @param pos - Zero-based position.
827
+ * @returns An object containing bucketIndex and indexInBucket.
882
828
  */
883
829
  _getBucketAndPosition(pos) {
884
830
  let bucketIndex;
@@ -895,23 +841,38 @@ class Deque extends linear_base_1.LinearBase {
895
841
  return { bucketIndex, indexInBucket };
896
842
  }
897
843
  /**
898
- * The function `_createInstance` returns a new instance of the `Deque` class with the specified
899
- * options.
900
- * @param [options] - The `options` parameter in the `_createInstance` method is of type
901
- * `DequeOptions<E, R>`, which is an optional parameter that allows you to pass additional
902
- * configuration options when creating a new instance of the `Deque` class.
903
- * @returns An instance of the `Deque` class with an empty array and the provided options, casted as
904
- * `this`.
844
+ * (Protected) Create an empty instance of the same concrete class.
845
+ * @remarks Time O(1), Space O(1)
846
+ * @param [options] - Options forwarded to the constructor.
847
+ * @returns An empty like-kind deque instance.
905
848
  */
906
849
  _createInstance(options) {
907
- return new Deque([], options);
850
+ const Ctor = this.constructor;
851
+ return new Ctor([], options);
852
+ }
853
+ /**
854
+ * (Protected) Create a like-kind deque seeded by elements.
855
+ * @remarks Time O(N), Space O(N)
856
+ * @template T
857
+ * @template RR
858
+ * @param [elements] - Iterable used to seed the new deque.
859
+ * @param [options] - Options forwarded to the constructor.
860
+ * @returns A like-kind Deque instance.
861
+ */
862
+ _createLike(elements = [], options) {
863
+ const Ctor = this.constructor;
864
+ return new Ctor(elements, options);
908
865
  }
909
866
  /**
910
- * This function returns an iterator that iterates over elements in reverse order.
867
+ * (Protected) Iterate elements from back to front.
868
+ * @remarks Time O(N), Space O(1)
869
+ * @returns Iterator of elements.
911
870
  */
912
871
  *_getReverseIterator() {
913
872
  for (let i = this._length - 1; i > -1; i--) {
914
- yield this.at(i);
873
+ const v = this.at(i);
874
+ if (v !== undefined)
875
+ yield v;
915
876
  }
916
877
  }
917
878
  }