data-structure-typed 2.0.1 → 2.0.2

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 (45) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/binary-tree/binary-tree.js +8 -9
  3. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  4. package/dist/cjs/types/utils/utils.d.ts +1 -7
  5. package/dist/cjs/utils/utils.d.ts +3 -49
  6. package/dist/cjs/utils/utils.js +13 -82
  7. package/dist/cjs/utils/utils.js.map +1 -1
  8. package/dist/esm/data-structures/binary-tree/binary-tree.js +8 -9
  9. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
  10. package/dist/esm/types/utils/utils.d.ts +1 -7
  11. package/dist/esm/utils/utils.d.ts +3 -49
  12. package/dist/esm/utils/utils.js +10 -68
  13. package/dist/esm/utils/utils.js.map +1 -1
  14. package/dist/umd/data-structure-typed.js +18 -66
  15. package/dist/umd/data-structure-typed.min.js +2 -2
  16. package/dist/umd/data-structure-typed.min.js.map +1 -1
  17. package/package.json +1 -1
  18. package/src/data-structures/binary-tree/binary-tree.ts +9 -10
  19. package/src/types/utils/utils.ts +1 -6
  20. package/src/utils/utils.ts +11 -83
  21. package/test/unit/data-structures/queue/queue.test.ts +1 -1
  22. package/test/unit/utils/utils.test.ts +35 -2
  23. package/dist/individuals/binary-tree/avl-tree-counter.mjs +0 -4701
  24. package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +0 -4514
  25. package/dist/individuals/binary-tree/avl-tree.mjs +0 -4321
  26. package/dist/individuals/binary-tree/binary-tree.mjs +0 -3097
  27. package/dist/individuals/binary-tree/bst.mjs +0 -3858
  28. package/dist/individuals/binary-tree/red-black-tree.mjs +0 -4391
  29. package/dist/individuals/binary-tree/tree-counter.mjs +0 -4806
  30. package/dist/individuals/binary-tree/tree-multi-map.mjs +0 -4582
  31. package/dist/individuals/graph/directed-graph.mjs +0 -2910
  32. package/dist/individuals/graph/undirected-graph.mjs +0 -2745
  33. package/dist/individuals/hash/hash-map.mjs +0 -1040
  34. package/dist/individuals/heap/heap.mjs +0 -909
  35. package/dist/individuals/heap/max-heap.mjs +0 -671
  36. package/dist/individuals/heap/min-heap.mjs +0 -659
  37. package/dist/individuals/linked-list/doubly-linked-list.mjs +0 -1495
  38. package/dist/individuals/linked-list/singly-linked-list.mjs +0 -1479
  39. package/dist/individuals/priority-queue/max-priority-queue.mjs +0 -768
  40. package/dist/individuals/priority-queue/min-priority-queue.mjs +0 -757
  41. package/dist/individuals/priority-queue/priority-queue.mjs +0 -670
  42. package/dist/individuals/queue/deque.mjs +0 -1262
  43. package/dist/individuals/queue/queue.mjs +0 -1865
  44. package/dist/individuals/stack/stack.mjs +0 -415
  45. package/dist/individuals/trie/trie.mjs +0 -687
@@ -1,1262 +0,0 @@
1
- // src/utils/utils.ts
2
- var THUNK_SYMBOL = Symbol("thunk");
3
- var rangeCheck = (index, min, max, message = "Index out of bounds.") => {
4
- if (index < min || index > max) throw new RangeError(message);
5
- };
6
- var calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
7
-
8
- // src/data-structures/base/iterable-element-base.ts
9
- var IterableElementBase = class {
10
- /**
11
- * The protected constructor initializes the options for the IterableElementBase class, including the
12
- * toElementFn function.
13
- * @param [options] - An optional object that contains the following properties:
14
- */
15
- constructor(options) {
16
- if (options) {
17
- const { toElementFn } = options;
18
- if (typeof toElementFn === "function") this._toElementFn = toElementFn;
19
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
20
- }
21
- }
22
- _toElementFn;
23
- get toElementFn() {
24
- return this._toElementFn;
25
- }
26
- /**
27
- * Time Complexity: O(n)
28
- * Space Complexity: O(1)
29
- *
30
- * The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
31
- * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
32
- * allows the function to accept any number of arguments as an array. In this case, the `args`
33
- * parameter is used to pass any number of arguments to the `_getIterator` method.
34
- */
35
- *[Symbol.iterator](...args) {
36
- yield* this._getIterator(...args);
37
- }
38
- /**
39
- * Time Complexity: O(n)
40
- * Space Complexity: O(n)
41
- *
42
- * The function returns an iterator that yields all the values in the object.
43
- */
44
- *values() {
45
- for (const item of this) {
46
- yield item;
47
- }
48
- }
49
- /**
50
- * Time Complexity: O(n)
51
- * Space Complexity: O(1)
52
- *
53
- * The `every` function checks if every element in the array satisfies a given predicate.
54
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
55
- * the current element being processed, its index, and the array it belongs to. It should return a
56
- * boolean value indicating whether the element satisfies a certain condition or not.
57
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
58
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
59
- * passed as the `this` value to the `predicate` function. If `thisArg` is
60
- * @returns The `every` method is returning a boolean value. It returns `true` if every element in
61
- * the array satisfies the provided predicate function, and `false` otherwise.
62
- */
63
- every(predicate, thisArg) {
64
- let index = 0;
65
- for (const item of this) {
66
- if (!predicate.call(thisArg, item, index++, this)) {
67
- return false;
68
- }
69
- }
70
- return true;
71
- }
72
- /**
73
- * Time Complexity: O(n)
74
- * Space Complexity: O(1)
75
- *
76
- * The "some" function checks if at least one element in a collection satisfies a given predicate.
77
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
78
- * `value`, `index`, and `array`. It should return a boolean value indicating whether the current
79
- * element satisfies the condition.
80
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
81
- * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
82
- * it will be passed as the `this` value to the `predicate` function. If `thisArg
83
- * @returns a boolean value. It returns true if the predicate function returns true for any element
84
- * in the collection, and false otherwise.
85
- */
86
- some(predicate, thisArg) {
87
- let index = 0;
88
- for (const item of this) {
89
- if (predicate.call(thisArg, item, index++, this)) {
90
- return true;
91
- }
92
- }
93
- return false;
94
- }
95
- /**
96
- * Time Complexity: O(n)
97
- * Space Complexity: O(1)
98
- *
99
- * The `forEach` function iterates over each element in an array-like object and calls a callback
100
- * function for each element.
101
- * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
102
- * the array. It takes three arguments: the current element being processed, the index of the current
103
- * element, and the array that forEach was called upon.
104
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
105
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
106
- * be passed as the `this` value to the `callbackfn` function. If `thisArg
107
- */
108
- forEach(callbackfn, thisArg) {
109
- let index = 0;
110
- for (const item of this) {
111
- callbackfn.call(thisArg, item, index++, this);
112
- }
113
- }
114
- /**
115
- * Time Complexity: O(n)
116
- * Space Complexity: O(1)
117
- *
118
- * The `find` function iterates over the elements of an array-like object and returns the first
119
- * element that satisfies the provided callback function.
120
- * @param predicate - The predicate parameter is a function that will be called for each element in
121
- * the array. It takes three arguments: the current element being processed, the index of the current
122
- * element, and the array itself. The function should return a boolean value indicating whether the
123
- * current element matches the desired condition.
124
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
125
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
126
- * be passed as the `this` value to the `callbackfn` function. If `thisArg
127
- * @returns The `find` method returns the first element in the array that satisfies the provided
128
- * callback function. If no element satisfies the callback function, `undefined` is returned.
129
- */
130
- find(predicate, thisArg) {
131
- let index = 0;
132
- for (const item of this) {
133
- if (predicate.call(thisArg, item, index++, this)) return item;
134
- }
135
- return;
136
- }
137
- /**
138
- * Time Complexity: O(n)
139
- * Space Complexity: O(1)
140
- *
141
- * The function checks if a given element exists in a collection.
142
- * @param {E} element - The parameter "element" is of type E, which means it can be any type. It
143
- * represents the element that we want to check for existence in the collection.
144
- * @returns a boolean value. It returns true if the element is found in the collection, and false
145
- * otherwise.
146
- */
147
- has(element) {
148
- for (const ele of this) {
149
- if (ele === element) return true;
150
- }
151
- return false;
152
- }
153
- /**
154
- * Time Complexity: O(n)
155
- * Space Complexity: O(1)
156
- *
157
- * The `reduce` function iterates over the elements of an array-like object and applies a callback
158
- * function to reduce them into a single value.
159
- * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
160
- * the array. It takes four arguments:
161
- * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
162
- * is the value that the accumulator starts with before the reduction operation begins.
163
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
164
- * all the elements in the array and applying the callback function to each element.
165
- */
166
- reduce(callbackfn, initialValue) {
167
- let accumulator = initialValue ?? 0;
168
- let index = 0;
169
- for (const item of this) {
170
- accumulator = callbackfn(accumulator, item, index++, this);
171
- }
172
- return accumulator;
173
- }
174
- /**
175
- * Time Complexity: O(n)
176
- * Space Complexity: O(n)
177
- *
178
- * The `toArray` function converts a linked list into an array.
179
- * @returns The `toArray()` method is returning an array of type `E[]`.
180
- */
181
- toArray() {
182
- return [...this];
183
- }
184
- /**
185
- * Time Complexity: O(n)
186
- * Space Complexity: O(n)
187
- *
188
- * The print function logs the elements of an array to the console.
189
- */
190
- toVisual() {
191
- return [...this];
192
- }
193
- /**
194
- * Time Complexity: O(n)
195
- * Space Complexity: O(n)
196
- *
197
- * The print function logs the elements of an array to the console.
198
- */
199
- print() {
200
- console.log(this.toVisual());
201
- }
202
- };
203
-
204
- // src/data-structures/base/linear-base.ts
205
- var LinearBase = class _LinearBase extends IterableElementBase {
206
- /**
207
- * The constructor initializes the LinearBase class with optional options, setting the maximum length
208
- * if provided.
209
- * @param [options] - The `options` parameter is an optional object that can be passed to the
210
- * constructor. It is of type `LinearBaseOptions<E, R>`. The constructor checks if the `options`
211
- * object is provided and then extracts the `maxLen` property from it. If `maxLen` is a
212
- */
213
- constructor(options) {
214
- super(options);
215
- if (options) {
216
- const { maxLen } = options;
217
- if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
218
- }
219
- }
220
- _maxLen = -1;
221
- get maxLen() {
222
- return this._maxLen;
223
- }
224
- /**
225
- * Time Complexity: O(n)
226
- * Space Complexity: O(1)
227
- *
228
- * The function indexOf searches for a specified element starting from a given index in an array-like
229
- * object and returns the index of the first occurrence, or -1 if not found.
230
- * @param {E} searchElement - The `searchElement` parameter in the `indexOf` function represents the
231
- * element that you want to find within the array. The function will search for this element starting
232
- * from the `fromIndex` (if provided) up to the end of the array. If the `searchElement` is found
233
- * within the
234
- * @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` function represents the
235
- * index at which to start searching for the `searchElement` within the array. If provided, the
236
- * search will begin at this index and continue to the end of the array. If `fromIndex` is not
237
- * specified, the default
238
- * @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
239
- * array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
240
- */
241
- indexOf(searchElement, fromIndex = 0) {
242
- if (this.length === 0) return -1;
243
- if (fromIndex < 0) fromIndex = this.length + fromIndex;
244
- if (fromIndex < 0) fromIndex = 0;
245
- for (let i = fromIndex; i < this.length; i++) {
246
- const element = this.at(i);
247
- if (element === searchElement) return i;
248
- }
249
- return -1;
250
- }
251
- /**
252
- * Time Complexity: O(n)
253
- * Space Complexity: O(1)
254
- *
255
- * The function `lastIndexOf` in TypeScript returns the index of the last occurrence of a specified
256
- * element in an array.
257
- * @param {E} searchElement - The `searchElement` parameter is the element that you want to find the
258
- * last index of within the array. The `lastIndexOf` method will search the array starting from the
259
- * `fromIndex` (or the end of the array if not specified) and return the index of the last occurrence
260
- * of the
261
- * @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
262
- * index at which to start searching for the `searchElement` in the array. By default, it starts
263
- * searching from the last element of the array (`this.length - 1`). If a specific `fromIndex` is
264
- * provided
265
- * @returns The last index of the `searchElement` in the array is being returned. If the
266
- * `searchElement` is not found in the array, -1 is returned.
267
- */
268
- lastIndexOf(searchElement, fromIndex = this.length - 1) {
269
- if (this.length === 0) return -1;
270
- if (fromIndex >= this.length) fromIndex = this.length - 1;
271
- if (fromIndex < 0) fromIndex = this.length + fromIndex;
272
- for (let i = fromIndex; i >= 0; i--) {
273
- const element = this.at(i);
274
- if (element === searchElement) return i;
275
- }
276
- return -1;
277
- }
278
- /**
279
- * Time Complexity: O(n)
280
- * Space Complexity: O(1)
281
- *
282
- * The `findIndex` function iterates over an array and returns the index of the first element that
283
- * satisfies the provided predicate function.
284
- * @param predicate - The `predicate` parameter in the `findIndex` function is a callback function
285
- * that takes three arguments: `item`, `index`, and the array `this`. It should return a boolean
286
- * value indicating whether the current element satisfies the condition being checked for.
287
- * @param {any} [thisArg] - The `thisArg` parameter in the `findIndex` function is an optional
288
- * parameter that specifies the value to use as `this` when executing the `predicate` function. If
289
- * provided, the `predicate` function will be called with `thisArg` as its `this` value. If `
290
- * @returns The `findIndex` method is returning the index of the first element in the array that
291
- * satisfies the provided predicate function. If no such element is found, it returns -1.
292
- */
293
- findIndex(predicate, thisArg) {
294
- for (let i = 0; i < this.length; i++) {
295
- const item = this.at(i);
296
- if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
297
- }
298
- return -1;
299
- }
300
- /**
301
- * Time Complexity: O(n + m)
302
- * Space Complexity: O(n + m)
303
- *
304
- * The `concat` function in TypeScript concatenates multiple items into a new list, handling both
305
- * individual elements and instances of `LinearBase`.
306
- * @param {(E | this)[]} items - The `concat` method takes in an array of items, where
307
- * each item can be either of type `E` or an instance of `LinearBase<E, R>`.
308
- * @returns The `concat` method is returning a new instance of the class that it belongs to, with the
309
- * items passed as arguments concatenated to it.
310
- */
311
- concat(...items) {
312
- const newList = this.clone();
313
- for (const item of items) {
314
- if (item instanceof _LinearBase) {
315
- newList.pushMany(item);
316
- } else {
317
- newList.push(item);
318
- }
319
- }
320
- return newList;
321
- }
322
- /**
323
- * Time Complexity: O(n log n)
324
- * Space Complexity: O(n)
325
- *
326
- * The `sort` function in TypeScript sorts the elements of a collection using a specified comparison
327
- * function.
328
- * @param [compareFn] - The `compareFn` parameter is a function that defines the sort order. It takes
329
- * two elements `a` and `b` as input and returns a number indicating their relative order. If the
330
- * returned value is negative, `a` comes before `b`. If the returned value is positive, `
331
- * @returns The `sort` method is returning the instance of the object on which it is called (this),
332
- * after sorting the elements based on the provided comparison function (compareFn).
333
- */
334
- sort(compareFn) {
335
- const arr = this.toArray();
336
- arr.sort(compareFn);
337
- this.clear();
338
- for (const item of arr) this.push(item);
339
- return this;
340
- }
341
- /**
342
- * Time Complexity: O(n + m)
343
- * Space Complexity: O(m)
344
- *
345
- * The `splice` function in TypeScript removes elements from an array and optionally inserts new
346
- * elements at the specified index.
347
- * @param {number} start - The `start` parameter in the `splice` method indicates the index at which
348
- * to start modifying the array. If `start` is a negative number, it will count from the end of the
349
- * array.
350
- * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
351
- * number of elements to remove from the array starting at the specified `start` index. If
352
- * `deleteCount` is not provided or is 0, no elements are removed, and only new elements are inserted
353
- * at the `start`
354
- * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
355
- * will be inserted into the array at the specified `start` index. These elements can be of any type
356
- * and you can pass multiple elements separated by commas. The `splice` method will insert these
357
- * items into the array at the
358
- * @returns The `splice` method returns a list of elements that were removed from the original list
359
- * during the operation.
360
- */
361
- splice(start, deleteCount = 0, ...items) {
362
- const removedList = this._createInstance();
363
- start = start < 0 ? this.length + start : start;
364
- start = Math.max(0, Math.min(start, this.length));
365
- deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
366
- for (let i = 0; i < deleteCount; i++) {
367
- const removed = this.deleteAt(start);
368
- if (removed !== void 0) {
369
- removedList.push(removed);
370
- }
371
- }
372
- for (let i = 0; i < items.length; i++) {
373
- this.addAt(start + i, items[i]);
374
- }
375
- return removedList;
376
- }
377
- /**
378
- * Time Complexity: O(n)
379
- * Space Complexity: O(1)
380
- *
381
- * The `join` function in TypeScript returns a string by joining the elements of an array with a
382
- * specified separator.
383
- * @param {string} [separator=,] - The `separator` parameter is a string that specifies the character
384
- * or characters that will be used to separate each element when joining them into a single string.
385
- * By default, the separator is set to a comma (`,`), but you can provide a different separator if
386
- * needed.
387
- * @returns The `join` method is being returned, which takes an optional `separator` parameter
388
- * (defaulting to a comma) and returns a string created by joining all elements of the array after
389
- * converting it to an array.
390
- */
391
- join(separator = ",") {
392
- return this.toArray().join(separator);
393
- }
394
- /**
395
- * Time Complexity: O(n)
396
- * Space Complexity: O(n)
397
- *
398
- * The function `toReversedArray` takes an array and returns a new array with its elements in reverse
399
- * order.
400
- * @returns The `toReversedArray()` function returns an array of elements of type `E` in reverse
401
- * order.
402
- */
403
- toReversedArray() {
404
- const array = [];
405
- for (let i = this.length - 1; i >= 0; i--) {
406
- array.push(this.at(i));
407
- }
408
- return array;
409
- }
410
- /**
411
- * Time Complexity: O(n)
412
- * Space Complexity: O(1)
413
- *
414
- * The `reduceRight` function in TypeScript iterates over an array from right to left and applies a
415
- * callback function to each element, accumulating a single result.
416
- * @param callbackfn - The `callbackfn` parameter in the `reduceRight` method is a function that will
417
- * be called on each element in the array from right to left. It takes four arguments:
418
- * @param {U} [initialValue] - The `initialValue` parameter in the `reduceRight` method is an
419
- * optional parameter that specifies the initial value of the accumulator. If provided, the
420
- * `accumulator` will start with this initial value before iterating over the elements of the array.
421
- * If `initialValue` is not provided, the accumulator will
422
- * @returns The `reduceRight` method is returning the final accumulated value after applying the
423
- * callback function to each element in the array from right to left.
424
- */
425
- reduceRight(callbackfn, initialValue) {
426
- let accumulator = initialValue ?? 0;
427
- for (let i = this.length - 1; i >= 0; i--) {
428
- accumulator = callbackfn(accumulator, this.at(i), i, this);
429
- }
430
- return accumulator;
431
- }
432
- /**
433
- * Time Complexity: O(m)
434
- * Space Complexity: O(m)
435
- *
436
- * The `slice` function in TypeScript creates a new instance by extracting a portion of elements from
437
- * the original instance based on the specified start and end indices.
438
- * @param {number} [start=0] - The `start` parameter in the `slice` method represents the index at
439
- * which to begin extracting elements from an array-like object. If no `start` parameter is provided,
440
- * the default value is 0, meaning the extraction will start from the beginning of the array.
441
- * @param {number} end - The `end` parameter in the `slice` method represents the index at which to
442
- * end the slicing. By default, if no `end` parameter is provided, it will slice until the end of the
443
- * array (i.e., `this.length`).
444
- * @returns The `slice` method is returning a new instance of the object with elements sliced from
445
- * the specified start index (default is 0) to the specified end index (default is the length of the
446
- * object).
447
- */
448
- slice(start = 0, end = this.length) {
449
- start = start < 0 ? this.length + start : start;
450
- end = end < 0 ? this.length + end : end;
451
- const newList = this._createInstance();
452
- for (let i = start; i < end; i++) {
453
- newList.push(this.at(i));
454
- }
455
- return newList;
456
- }
457
- /**
458
- * Time Complexity: O(n)
459
- * Space Complexity: O(1)
460
- *
461
- * The `fill` function in TypeScript fills a specified range in an array-like object with a given
462
- * value.
463
- * @param {E} value - The `value` parameter in the `fill` method represents the element that will be
464
- * used to fill the specified range in the array.
465
- * @param [start=0] - The `start` parameter specifies the index at which to start filling the array
466
- * with the specified value. If not provided, it defaults to 0, indicating the beginning of the
467
- * array.
468
- * @param end - The `end` parameter in the `fill` function represents the index at which the filling
469
- * of values should stop. It specifies the end of the range within the array where the `value` should
470
- * be filled.
471
- * @returns The `fill` method is returning the modified object (`this`) after filling the specified
472
- * range with the provided value.
473
- */
474
- fill(value, start = 0, end = this.length) {
475
- start = start < 0 ? this.length + start : start;
476
- end = end < 0 ? this.length + end : end;
477
- if (start < 0) start = 0;
478
- if (end > this.length) end = this.length;
479
- if (start >= end) return this;
480
- for (let i = start; i < end; i++) {
481
- this.setAt(i, value);
482
- }
483
- return this;
484
- }
485
- };
486
-
487
- // src/data-structures/queue/deque.ts
488
- var Deque = class _Deque extends LinearBase {
489
- /**
490
- * The constructor initializes a Deque object with optional iterable of elements and options.
491
- * @param elements - An iterable object (such as an array or a Set) that contains the initial
492
- * elements to be added to the deque. It can also be an object with a `length` or `size` property
493
- * that represents the number of elements in the iterable object. If no elements are provided, an
494
- * empty deque
495
- * @param {DequeOptions} [options] - The `options` parameter is an optional object that can contain
496
- * configuration options for the deque. In this code, it is used to set the `bucketSize` option,
497
- * which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
498
- * or is not a number
499
- */
500
- constructor(elements = [], options) {
501
- super(options);
502
- if (options) {
503
- const { bucketSize } = options;
504
- if (typeof bucketSize === "number") this._bucketSize = bucketSize;
505
- }
506
- let _size;
507
- if ("length" in elements) {
508
- if (elements.length instanceof Function) _size = elements.length();
509
- else _size = elements.length;
510
- } else {
511
- if (elements.size instanceof Function) _size = elements.size();
512
- else _size = elements.size;
513
- }
514
- this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1;
515
- for (let i = 0; i < this._bucketCount; ++i) {
516
- this._buckets.push(new Array(this._bucketSize));
517
- }
518
- const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
519
- this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
520
- this._firstInBucket = this._lastInBucket = this._bucketSize - _size % this._bucketSize >> 1;
521
- this.pushMany(elements);
522
- }
523
- _bucketSize = 1 << 12;
524
- get bucketSize() {
525
- return this._bucketSize;
526
- }
527
- _bucketFirst = 0;
528
- get bucketFirst() {
529
- return this._bucketFirst;
530
- }
531
- _firstInBucket = 0;
532
- get firstInBucket() {
533
- return this._firstInBucket;
534
- }
535
- _bucketLast = 0;
536
- get bucketLast() {
537
- return this._bucketLast;
538
- }
539
- _lastInBucket = 0;
540
- get lastInBucket() {
541
- return this._lastInBucket;
542
- }
543
- _bucketCount = 0;
544
- get bucketCount() {
545
- return this._bucketCount;
546
- }
547
- _buckets = [];
548
- get buckets() {
549
- return this._buckets;
550
- }
551
- _length = 0;
552
- get length() {
553
- return this._length;
554
- }
555
- /**
556
- * The function returns the first element in a collection if it exists, otherwise it returns
557
- * undefined.
558
- * @returns The first element of the collection, of type E, is being returned.
559
- */
560
- get first() {
561
- if (this._length === 0) return;
562
- return this._buckets[this._bucketFirst][this._firstInBucket];
563
- }
564
- /**
565
- * The last function returns the last element in the queue.
566
- * @return The last element in the array
567
- */
568
- get last() {
569
- if (this._length === 0) return;
570
- return this._buckets[this._bucketLast][this._lastInBucket];
571
- }
572
- /**
573
- * Time Complexity - Amortized O(1) (possible reallocation),
574
- * Space Complexity - O(n) (due to potential resizing).
575
- *
576
- * The push function adds an element to a data structure and reallocates memory if necessary.
577
- * @param {E} element - The `element` parameter represents the value that you want to add to the data
578
- * structure.
579
- * @returns The size of the data structure after the element has been pushed.
580
- */
581
- push(element) {
582
- if (this._length) {
583
- if (this._lastInBucket < this._bucketSize - 1) {
584
- this._lastInBucket += 1;
585
- } else if (this._bucketLast < this._bucketCount - 1) {
586
- this._bucketLast += 1;
587
- this._lastInBucket = 0;
588
- } else {
589
- this._bucketLast = 0;
590
- this._lastInBucket = 0;
591
- }
592
- if (this._bucketLast === this._bucketFirst && this._lastInBucket === this._firstInBucket) this._reallocate();
593
- }
594
- this._length += 1;
595
- this._buckets[this._bucketLast][this._lastInBucket] = element;
596
- if (this._maxLen > 0 && this._length > this._maxLen) this.shift();
597
- return true;
598
- }
599
- /**
600
- * Time Complexity: O(1)
601
- * Space Complexity: O(1)
602
- *
603
- * The `pop()` function removes and returns the last element from a data structure, updating the
604
- * internal state variables accordingly.
605
- * @returns The element that was removed from the data structure is being returned.
606
- */
607
- pop() {
608
- if (this._length === 0) return;
609
- const element = this._buckets[this._bucketLast][this._lastInBucket];
610
- if (this._length !== 1) {
611
- if (this._lastInBucket > 0) {
612
- this._lastInBucket -= 1;
613
- } else if (this._bucketLast > 0) {
614
- this._bucketLast -= 1;
615
- this._lastInBucket = this._bucketSize - 1;
616
- } else {
617
- this._bucketLast = this._bucketCount - 1;
618
- this._lastInBucket = this._bucketSize - 1;
619
- }
620
- }
621
- this._length -= 1;
622
- return element;
623
- }
624
- /**
625
- * Time Complexity: O(1)
626
- * Space Complexity: O(1)
627
- *
628
- * The `shift()` function removes and returns the first element from a data structure, updating the
629
- * internal state variables accordingly.
630
- * @returns The element that is being removed from the beginning of the data structure is being
631
- * returned.
632
- */
633
- shift() {
634
- if (this._length === 0) return;
635
- const element = this._buckets[this._bucketFirst][this._firstInBucket];
636
- if (this._length !== 1) {
637
- if (this._firstInBucket < this._bucketSize - 1) {
638
- this._firstInBucket += 1;
639
- } else if (this._bucketFirst < this._bucketCount - 1) {
640
- this._bucketFirst += 1;
641
- this._firstInBucket = 0;
642
- } else {
643
- this._bucketFirst = 0;
644
- this._firstInBucket = 0;
645
- }
646
- }
647
- this._length -= 1;
648
- return element;
649
- }
650
- /**
651
- * Time Complexity: Amortized O(1)
652
- * Space Complexity: O(n)
653
- *
654
- * The `unshift` function adds an element to the beginning of an array-like data structure and
655
- * returns the new size of the structure.
656
- * @param {E} element - The `element` parameter represents the element that you want to add to the
657
- * beginning of the data structure.
658
- * @returns The size of the data structure after the element has been added.
659
- */
660
- unshift(element) {
661
- if (this._length) {
662
- if (this._firstInBucket > 0) {
663
- this._firstInBucket -= 1;
664
- } else if (this._bucketFirst > 0) {
665
- this._bucketFirst -= 1;
666
- this._firstInBucket = this._bucketSize - 1;
667
- } else {
668
- this._bucketFirst = this._bucketCount - 1;
669
- this._firstInBucket = this._bucketSize - 1;
670
- }
671
- if (this._bucketFirst === this._bucketLast && this._firstInBucket === this._lastInBucket) this._reallocate();
672
- }
673
- this._length += 1;
674
- this._buckets[this._bucketFirst][this._firstInBucket] = element;
675
- if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
676
- return true;
677
- }
678
- /**
679
- * Time Complexity: O(k)
680
- * Space Complexity: O(k)
681
- *
682
- * The function `pushMany` iterates over elements and pushes them into an array after applying a
683
- * transformation function if provided.
684
- * @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
685
- * parameter in the `pushMany` function is expected to be an iterable containing elements of type `E`
686
- * or `R`. It can be either an `IterableWithSizeOrLength<E>` or an `IterableWithSizeOrLength<R>`. The
687
- * function iterates over each element
688
- * @returns The `pushMany` function is returning an array of boolean values, where each value
689
- * represents the result of calling the `push` method on the current object instance with the
690
- * corresponding element from the input `elements` iterable.
691
- */
692
- pushMany(elements) {
693
- const ans = [];
694
- for (const el of elements) {
695
- if (this.toElementFn) {
696
- ans.push(this.push(this.toElementFn(el)));
697
- } else {
698
- ans.push(this.push(el));
699
- }
700
- }
701
- return ans;
702
- }
703
- /**
704
- * Time Complexity: O(k)
705
- * Space Complexity: O(k)
706
- *
707
- * The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of
708
- * an array, optionally converting them using a provided function.
709
- * @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
710
- * parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It
711
- * can be an array or any other iterable data structure that has a known size or length. The function
712
- * iterates over each element in the `elements` iterable and
713
- * @returns The `unshiftMany` function returns an array of boolean values indicating whether each
714
- * element was successfully added to the beginning of the array.
715
- */
716
- unshiftMany(elements = []) {
717
- const ans = [];
718
- for (const el of elements) {
719
- if (this.toElementFn) {
720
- ans.push(this.unshift(this.toElementFn(el)));
721
- } else {
722
- ans.push(this.unshift(el));
723
- }
724
- }
725
- return ans;
726
- }
727
- /**
728
- * Time Complexity: O(1)
729
- * Space Complexity: O(1)
730
- *
731
- * The function checks if the size of an object is equal to zero and returns a boolean value.
732
- * @returns A boolean value indicating whether the size of the object is 0 or not.
733
- */
734
- isEmpty() {
735
- return this._length === 0;
736
- }
737
- /**
738
- * Time Complexity: O(1)
739
- * Space Complexity: O(1)
740
- *
741
- * The clear() function resets the state of the object by initializing all variables to their default
742
- * values.
743
- */
744
- clear() {
745
- this._buckets = [new Array(this._bucketSize)];
746
- this._bucketCount = 1;
747
- this._bucketFirst = this._bucketLast = this._length = 0;
748
- this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
749
- }
750
- /**
751
- * Time Complexity: O(1)
752
- * Space Complexity: O(1)
753
- *
754
- * The `at` function retrieves an element at a specified position in an array-like data structure.
755
- * @param {number} pos - The `pos` parameter represents the position of the element that you want to
756
- * retrieve from the data structure. It is of type `number` and should be a valid index within the
757
- * range of the data structure.
758
- * @returns The element at the specified position in the data structure is being returned.
759
- */
760
- at(pos) {
761
- rangeCheck(pos, 0, this._length - 1);
762
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
763
- return this._buckets[bucketIndex][indexInBucket];
764
- }
765
- /**
766
- * Time Complexity: O(1)
767
- * Space Complexity: O(1)
768
- *
769
- * The `setAt` function sets an element at a specific position in an array-like data structure.
770
- * @param {number} pos - The `pos` parameter represents the position at which the element needs to be
771
- * set. It is of type `number`.
772
- * @param {E} element - The `element` parameter is the value that you want to set at the specified
773
- * position in the data structure.
774
- */
775
- setAt(pos, element) {
776
- rangeCheck(pos, 0, this._length - 1);
777
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
778
- this._buckets[bucketIndex][indexInBucket] = element;
779
- return true;
780
- }
781
- /**
782
- * Time Complexity: O(n)
783
- * Space Complexity: O(n)
784
- *
785
- * The `addAt` function inserts one or more elements at a specified position in an array-like data
786
- * structure.
787
- * @param {number} pos - The `pos` parameter represents the position at which the element(s) should
788
- * be inserted. It is of type `number`.
789
- * @param {E} element - The `element` parameter represents the element that you want to insert into
790
- * the array at the specified position.
791
- * @param [num=1] - The `num` parameter represents the number of times the `element` should be
792
- * inserted at the specified position (`pos`). By default, it is set to 1, meaning that the `element`
793
- * will be inserted once. However, you can provide a different value for `num` if you want
794
- * @returns The size of the array after the insertion is being returned.
795
- */
796
- addAt(pos, element, num = 1) {
797
- const length = this._length;
798
- rangeCheck(pos, 0, length);
799
- if (pos === 0) {
800
- while (num--) this.unshift(element);
801
- } else if (pos === this._length) {
802
- while (num--) this.push(element);
803
- } else {
804
- const arr = [];
805
- for (let i = pos; i < this._length; ++i) {
806
- arr.push(this.at(i));
807
- }
808
- this.cut(pos - 1, true);
809
- for (let i = 0; i < num; ++i) this.push(element);
810
- for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
811
- }
812
- return true;
813
- }
814
- /**
815
- * Time Complexity: O(1)
816
- * Space Complexity: O(1)
817
- *
818
- * The `cut` function updates the state of the object based on the given position and returns the
819
- * updated size.
820
- * @param {number} pos - The `pos` parameter represents the position at which the string should be
821
- * cut. It is a number that indicates the index of the character where the cut should be made.
822
- * @param {boolean} isCutSelf - If true, the original deque will not be cut, and return a new deque
823
- * @returns The method is returning the updated size of the data structure.
824
- */
825
- cut(pos, isCutSelf = false) {
826
- if (isCutSelf) {
827
- if (pos < 0) {
828
- this.clear();
829
- return this;
830
- }
831
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
832
- this._bucketLast = bucketIndex;
833
- this._lastInBucket = indexInBucket;
834
- this._length = pos + 1;
835
- return this;
836
- } else {
837
- const newDeque = this._createInstance({
838
- bucketSize: this._bucketSize,
839
- toElementFn: this._toElementFn,
840
- maxLen: this._maxLen
841
- });
842
- for (let i = 0; i <= pos; i++) {
843
- newDeque.push(this.at(i));
844
- }
845
- return newDeque;
846
- }
847
- }
848
- /**
849
- * Time Complexity: O(n)
850
- * Space Complexity: O(1)
851
- *
852
- * The `splice` function in TypeScript overrides the default behavior to remove and insert elements
853
- * in a Deque data structure while ensuring the starting position and delete count are within bounds.
854
- * @param {number} start - The `start` parameter in the `splice` method represents the index at which
855
- * to start changing the array. Items will be removed or added starting from this index.
856
- * @param {number} deleteCount - The `deleteCount` parameter in the `splice` method represents the
857
- * number of elements to remove from the array starting at the specified `start` index. If
858
- * `deleteCount` is not provided, it defaults to the number of elements from the `start` index to the
859
- * end of the array (`
860
- * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
861
- * will be inserted into the deque at the specified `start` index. These elements will be inserted in
862
- * place of the elements that are removed based on the `start` and `deleteCount` parameters.
863
- * @returns The `splice` method is returning the array `deletedElements` which contains the elements
864
- * that were removed from the Deque during the splice operation.
865
- */
866
- splice(start, deleteCount = this._length - start, ...items) {
867
- rangeCheck(start, 0, this._length);
868
- if (deleteCount < 0) deleteCount = 0;
869
- if (start + deleteCount > this._length) deleteCount = this._length - start;
870
- const deletedElements = this._createInstance();
871
- for (let i = 0; i < deleteCount; i++) {
872
- deletedElements.push(this.at(start + i));
873
- }
874
- const elementsAfter = [];
875
- for (let i = start + deleteCount; i < this._length; i++) {
876
- elementsAfter.push(this.at(i));
877
- }
878
- this.cut(start - 1, true);
879
- for (const item of items) {
880
- this.push(item);
881
- }
882
- for (const element of elementsAfter) {
883
- this.push(element);
884
- }
885
- return deletedElements;
886
- }
887
- /**
888
- * Time Complexity: O(1)
889
- * Space Complexity: O(1) or O(n)
890
- *
891
- * The `cutRest` function cuts the elements from a specified position in a deque and returns a new
892
- * deque with the cut elements.
893
- * @param {number} pos - The `pos` parameter represents the position from which to cut the Deque. It
894
- * is a number that indicates the index of the element in the Deque where the cut should start.
895
- * @param [isCutSelf=false] - isCutSelf is a boolean parameter that determines whether the original
896
- * Deque should be modified or a new Deque should be created. If isCutSelf is true, the original
897
- * Deque will be modified by cutting off elements starting from the specified position. If isCutSelf
898
- * is false, a new De
899
- * @returns The function `cutRest` returns either the modified original deque (`this`) or a new deque
900
- * (`newDeque`) depending on the value of the `isCutSelf` parameter.
901
- */
902
- cutRest(pos, isCutSelf = false) {
903
- if (isCutSelf) {
904
- if (pos < 0) {
905
- return this;
906
- }
907
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
908
- this._bucketFirst = bucketIndex;
909
- this._firstInBucket = indexInBucket;
910
- this._length = this._length - pos;
911
- return this;
912
- } else {
913
- const newDeque = this._createInstance({
914
- bucketSize: this._bucketSize,
915
- toElementFn: this._toElementFn,
916
- maxLen: this._maxLen
917
- });
918
- if (pos < 0) pos = 0;
919
- for (let i = pos; i < this._length; i++) {
920
- newDeque.push(this.at(i));
921
- }
922
- return newDeque;
923
- }
924
- }
925
- /**
926
- * Time Complexity: O(n)
927
- * Space Complexity: O(1) or O(n)
928
- *
929
- * The `deleteAt` function removes an element at a specified position in an array-like data
930
- * structure.
931
- * @param {number} pos - The `pos` parameter in the `deleteAt` function represents the position at
932
- * which an element needs to be deleted from the data structure. It is of type `number` and indicates
933
- * the index of the element to be deleted.
934
- * @returns The size of the data structure after the deletion operation is performed.
935
- */
936
- deleteAt(pos) {
937
- rangeCheck(pos, 0, this._length - 1);
938
- let deleted;
939
- if (pos === 0) {
940
- return this.shift();
941
- } else if (pos === this._length - 1) {
942
- deleted = this.last;
943
- this.pop();
944
- return deleted;
945
- } else {
946
- const length = this._length - 1;
947
- const { bucketIndex: targetBucket, indexInBucket: targetPointer } = this._getBucketAndPosition(pos);
948
- deleted = this._buckets[targetBucket][targetPointer];
949
- for (let i = pos; i < length; i++) {
950
- const { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(i);
951
- const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(i + 1);
952
- this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
953
- }
954
- this.pop();
955
- return deleted;
956
- }
957
- }
958
- /**
959
- * Time Complexity: O(n)
960
- * Space Complexity: O(1)
961
- *
962
- * The `delete` function removes all occurrences of a specified element from an array-like data
963
- * structure.
964
- * @param {E} element - The `element` parameter represents the element that you want to delete from
965
- * the data structure.
966
- * @returns The size of the data structure after the element has been deleted.
967
- */
968
- delete(element) {
969
- const size = this._length;
970
- if (size === 0) return false;
971
- let i = 0;
972
- let index = 0;
973
- while (i < size) {
974
- const oldElement = this.at(i);
975
- if (oldElement !== element) {
976
- this.setAt(index, oldElement);
977
- index += 1;
978
- }
979
- i += 1;
980
- }
981
- this.cut(index - 1, true);
982
- return true;
983
- }
984
- // /**
985
- // * Time Complexity: O(n)
986
- // * Space Complexity: O(1)
987
- // *
988
- // * This function overrides the indexOf method to search for an element within a custom data
989
- // * structure.
990
- // * @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
991
- // * within the data structure. The `indexOf` method will return the index of the first occurrence of
992
- // * this element within the data structure.
993
- // * @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
994
- // * index at which to start searching for the `searchElement` within the data structure. If provided,
995
- // * the search will begin at this index instead of the beginning of the data structure.
996
- // * @returns The indexOf method is returning the index of the searchElement if it is found in the data
997
- // * structure, or -1 if the searchElement is not found.
998
- // */
999
- // override indexOf(searchElement: E, fromIndex: number = 0): number {
1000
- // let index = fromIndex;
1001
- // let bucketIndex = this._bucketFirst;
1002
- // let indexInBucket = this._firstInBucket + fromIndex;
1003
- //
1004
- // for (let i = 0; i < this._length; i++) {
1005
- // if (this._buckets[bucketIndex][indexInBucket] === searchElement) {
1006
- // return index;
1007
- // }
1008
- // index++;
1009
- // indexInBucket++;
1010
- // if (indexInBucket >= this._bucketSize) {
1011
- // bucketIndex++;
1012
- // indexInBucket = 0;
1013
- // }
1014
- // if (bucketIndex >= this._bucketCount) {
1015
- // bucketIndex = 0;
1016
- // }
1017
- // }
1018
- // return -1;
1019
- // }
1020
- /**
1021
- * Time Complexity: O(n)
1022
- * Space Complexity: O(1)
1023
- *
1024
- * The reverse() function reverses the order of the buckets and the elements within each bucket in a
1025
- * data structure.
1026
- * @returns The reverse() method is returning the object itself (this) after performing the reverse
1027
- * operation on the buckets and updating the relevant properties.
1028
- */
1029
- reverse() {
1030
- this._buckets.reverse().forEach(function(bucket) {
1031
- bucket.reverse();
1032
- });
1033
- const { _bucketFirst, _bucketLast, _firstInBucket, _lastInBucket } = this;
1034
- this._bucketFirst = this._bucketCount - _bucketLast - 1;
1035
- this._bucketLast = this._bucketCount - _bucketFirst - 1;
1036
- this._firstInBucket = this._bucketSize - _lastInBucket - 1;
1037
- this._lastInBucket = this._bucketSize - _firstInBucket - 1;
1038
- return this;
1039
- }
1040
- /**
1041
- * Time Complexity: O(n)
1042
- * Space Complexity: O(1)
1043
- *
1044
- * The `unique()` function removes duplicate elements from an array-like data structure and returns
1045
- * the number of unique elements.
1046
- * @returns The size of the modified array is being returned.
1047
- */
1048
- unique() {
1049
- if (this._length <= 1) {
1050
- return this;
1051
- }
1052
- let index = 1;
1053
- let prev = this.at(0);
1054
- for (let i = 1; i < this._length; ++i) {
1055
- const cur = this.at(i);
1056
- if (cur !== prev) {
1057
- prev = cur;
1058
- this.setAt(index++, cur);
1059
- }
1060
- }
1061
- this.cut(index - 1, true);
1062
- return this;
1063
- }
1064
- /**
1065
- * Time Complexity: O(n)
1066
- * Space Complexity: O(n)
1067
- *
1068
- * The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
1069
- * memory usage.
1070
- * @returns Nothing is being returned. The function is using the `return` statement to exit early if
1071
- * `this._length` is 0, but it does not return any value.
1072
- */
1073
- shrinkToFit() {
1074
- if (this._length === 0) return;
1075
- const newBuckets = [];
1076
- if (this._bucketFirst === this._bucketLast) return;
1077
- else if (this._bucketFirst < this._bucketLast) {
1078
- for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
1079
- newBuckets.push(this._buckets[i]);
1080
- }
1081
- } else {
1082
- for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
1083
- newBuckets.push(this._buckets[i]);
1084
- }
1085
- for (let i = 0; i <= this._bucketLast; ++i) {
1086
- newBuckets.push(this._buckets[i]);
1087
- }
1088
- }
1089
- this._bucketFirst = 0;
1090
- this._bucketLast = newBuckets.length - 1;
1091
- this._buckets = newBuckets;
1092
- }
1093
- /**
1094
- * Time Complexity: O(n)
1095
- * Space Complexity: O(n)
1096
- *
1097
- * The `clone()` function returns a new instance of the `Deque` class with the same elements and
1098
- * bucket size as the original instance.
1099
- * @returns The `clone()` method is returning a new instance of the `Deque` class with the same
1100
- * elements as the original deque (`this`) and the same bucket size.
1101
- */
1102
- clone() {
1103
- return new _Deque(this, {
1104
- bucketSize: this.bucketSize,
1105
- toElementFn: this.toElementFn,
1106
- maxLen: this._maxLen
1107
- });
1108
- }
1109
- /**
1110
- * Time Complexity: O(n)
1111
- * Space Complexity: O(n)
1112
- *
1113
- * The `filter` function creates a new deque containing elements from the original deque that satisfy
1114
- * a given predicate function.
1115
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
1116
- * the current element being iterated over, the index of the current element, and the deque itself.
1117
- * It should return a boolean value indicating whether the element should be included in the filtered
1118
- * deque or not.
1119
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1120
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
1121
- * passed as the `this` value to the `predicate` function. If `thisArg` is
1122
- * @returns The `filter` method is returning a new `Deque` object that contains the elements that
1123
- * satisfy the given predicate function.
1124
- */
1125
- filter(predicate, thisArg) {
1126
- const newDeque = this._createInstance({
1127
- bucketSize: this._bucketSize,
1128
- toElementFn: this.toElementFn,
1129
- maxLen: this._maxLen
1130
- });
1131
- let index = 0;
1132
- for (const el of this) {
1133
- if (predicate.call(thisArg, el, index, this)) {
1134
- newDeque.push(el);
1135
- }
1136
- index++;
1137
- }
1138
- return newDeque;
1139
- }
1140
- /**
1141
- * Time Complexity: O(n)
1142
- * Space Complexity: O(n)
1143
- *
1144
- * The `map` function takes a callback function and applies it to each element in the deque,
1145
- * returning a new deque with the results.
1146
- * @param callback - The callback parameter is a function that will be called for each element in the
1147
- * deque. It takes three arguments: the current element, the index of the element, and the deque
1148
- * itself. It should return a value of type EM.
1149
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
1150
- * transform the raw element (`RM`) into a new element (`EM`) before adding it to the new deque. If
1151
- * provided, this function will be called for each raw element in the original deque.
1152
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1153
- * specify the value of `this` within the callback function. It is used to set the context or scope
1154
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
1155
- * value of
1156
- * @returns a new Deque object with elements of type EM and raw elements of type RM.
1157
- */
1158
- map(callback, toElementFn, thisArg) {
1159
- const newDeque = new _Deque([], { bucketSize: this._bucketSize, toElementFn, maxLen: this._maxLen });
1160
- let index = 0;
1161
- for (const el of this) {
1162
- newDeque.push(callback.call(thisArg, el, index, this));
1163
- index++;
1164
- }
1165
- return newDeque;
1166
- }
1167
- /**
1168
- * Time Complexity: O(n)
1169
- * Space Complexity: O(1)
1170
- *
1171
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
1172
- * object to be iterated over using a for...of loop.
1173
- */
1174
- *_getIterator() {
1175
- for (let i = 0; i < this._length; ++i) {
1176
- yield this.at(i);
1177
- }
1178
- }
1179
- /**
1180
- * Time Complexity: O(n)
1181
- * Space Complexity: O(n)
1182
- *
1183
- * The `_reallocate` function reallocates the buckets in an array, adding new buckets if needed.
1184
- * @param {number} [needBucketNum] - The `needBucketNum` parameter is an optional number that
1185
- * specifies the number of new buckets needed. If not provided, it will default to half of the
1186
- * current bucket count (`this._bucketCount >> 1`) or 1 if the current bucket count is less than 2.
1187
- */
1188
- _reallocate(needBucketNum) {
1189
- const newBuckets = [];
1190
- const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
1191
- for (let i = 0; i < addBucketNum; ++i) {
1192
- newBuckets[i] = new Array(this._bucketSize);
1193
- }
1194
- for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
1195
- newBuckets[newBuckets.length] = this._buckets[i];
1196
- }
1197
- for (let i = 0; i < this._bucketLast; ++i) {
1198
- newBuckets[newBuckets.length] = this._buckets[i];
1199
- }
1200
- newBuckets[newBuckets.length] = [...this._buckets[this._bucketLast]];
1201
- this._bucketFirst = addBucketNum;
1202
- this._bucketLast = newBuckets.length - 1;
1203
- for (let i = 0; i < addBucketNum; ++i) {
1204
- newBuckets[newBuckets.length] = new Array(this._bucketSize);
1205
- }
1206
- this._buckets = newBuckets;
1207
- this._bucketCount = newBuckets.length;
1208
- }
1209
- /**
1210
- * Time Complexity: O(1)
1211
- * Space Complexity: O(1)
1212
- *
1213
- * The function calculates the bucket index and index within the bucket based on the given position.
1214
- * @param {number} pos - The `pos` parameter represents the position within the data structure. It is
1215
- * a number that indicates the index or position of an element within the structure.
1216
- * @returns an object with two properties: "bucketIndex" and "indexInBucket".
1217
- */
1218
- _getBucketAndPosition(pos) {
1219
- let bucketIndex;
1220
- let indexInBucket;
1221
- const overallIndex = this._firstInBucket + pos;
1222
- bucketIndex = this._bucketFirst + Math.floor(overallIndex / this._bucketSize);
1223
- if (bucketIndex >= this._bucketCount) {
1224
- bucketIndex -= this._bucketCount;
1225
- }
1226
- indexInBucket = (overallIndex + 1) % this._bucketSize - 1;
1227
- if (indexInBucket < 0) {
1228
- indexInBucket = this._bucketSize - 1;
1229
- }
1230
- return { bucketIndex, indexInBucket };
1231
- }
1232
- /**
1233
- * The function `_createInstance` returns a new instance of the `Deque` class with the specified
1234
- * options.
1235
- * @param [options] - The `options` parameter in the `_createInstance` method is of type
1236
- * `DequeOptions<E, R>`, which is an optional parameter that allows you to pass additional
1237
- * configuration options when creating a new instance of the `Deque` class.
1238
- * @returns An instance of the `Deque` class with an empty array and the provided options, casted as
1239
- * `this`.
1240
- */
1241
- _createInstance(options) {
1242
- return new _Deque([], options);
1243
- }
1244
- /**
1245
- * This function returns an iterator that iterates over elements in reverse order.
1246
- */
1247
- *_getReverseIterator() {
1248
- for (let i = this._length - 1; i > -1; i--) {
1249
- yield this.at(i);
1250
- }
1251
- }
1252
- };
1253
- export {
1254
- Deque
1255
- };
1256
- /**
1257
- * data-structure-typed
1258
- *
1259
- * @author Pablo Zeng
1260
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
1261
- * @license MIT License
1262
- */