graph-typed 1.46.1 → 1.46.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.
|
@@ -23,6 +23,20 @@ export class DequeIterator<E> {
|
|
|
23
23
|
index: number;
|
|
24
24
|
readonly deque: Deque<E>;
|
|
25
25
|
|
|
26
|
+
/**
|
|
27
|
+
* The constructor initializes the index, iterate direction, and prev/next functions for a
|
|
28
|
+
* DequeIterator object.
|
|
29
|
+
* @param {number} index - The index parameter represents the current index position of the iterator
|
|
30
|
+
* within the deque. It is a number that indicates the position of the element that the iterator is
|
|
31
|
+
* currently pointing to.
|
|
32
|
+
* @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a
|
|
33
|
+
* double-ended queue data structure, which allows elements to be added or removed from both ends.
|
|
34
|
+
* @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies
|
|
35
|
+
* the direction in which the iterator should iterate over the elements of the `deque`. It has a
|
|
36
|
+
* default value of `IterateDirection.DEFAULT`.
|
|
37
|
+
* @returns The constructor is not returning anything. It is used to initialize the properties of the
|
|
38
|
+
* object being created.
|
|
39
|
+
*/
|
|
26
40
|
constructor(index: number, deque: Deque<E>, iterateDirection = IterateDirection.DEFAULT) {
|
|
27
41
|
this.index = index;
|
|
28
42
|
this.iterateDirection = iterateDirection;
|
|
@@ -94,13 +108,22 @@ export class Deque<E> {
|
|
|
94
108
|
protected _bucketCount = 0;
|
|
95
109
|
protected readonly _bucketSize: number;
|
|
96
110
|
|
|
111
|
+
/**
|
|
112
|
+
* The constructor initializes a data structure with a specified bucket size and populates it with
|
|
113
|
+
* elements from an iterable.
|
|
114
|
+
* @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that
|
|
115
|
+
* contains the initial elements to be stored in the data structure. It can also be an object with a
|
|
116
|
+
* `length` property or a `size` property, which represents the number of elements in the iterable.
|
|
117
|
+
* @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be
|
|
118
|
+
* stored in each bucket. It determines the size of each bucket in the data structure.
|
|
119
|
+
*/
|
|
97
120
|
constructor(elements: IterableWithSizeOrLength<E> = [], bucketSize = (1 << 12)) {
|
|
98
121
|
|
|
99
|
-
let _size;
|
|
122
|
+
let _size: number;
|
|
100
123
|
if ('length' in elements) {
|
|
101
|
-
_size = elements.length;
|
|
124
|
+
if (elements.length instanceof Function) _size = elements.length(); else _size = elements.length;
|
|
102
125
|
} else {
|
|
103
|
-
_size = elements.size;
|
|
126
|
+
if (elements.size instanceof Function) _size = elements.size();else _size = elements.size;
|
|
104
127
|
}
|
|
105
128
|
|
|
106
129
|
this._bucketSize = bucketSize;
|
|
@@ -129,6 +152,11 @@ export class Deque<E> {
|
|
|
129
152
|
return this._size;
|
|
130
153
|
}
|
|
131
154
|
|
|
155
|
+
/**
|
|
156
|
+
* The function returns the first element in a collection if it exists, otherwise it returns
|
|
157
|
+
* undefined.
|
|
158
|
+
* @returns The first element of the collection, of type E, is being returned.
|
|
159
|
+
*/
|
|
132
160
|
get first(): E | undefined {
|
|
133
161
|
if (this.size === 0) return;
|
|
134
162
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -139,15 +167,6 @@ export class Deque<E> {
|
|
|
139
167
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
140
168
|
}
|
|
141
169
|
|
|
142
|
-
/**
|
|
143
|
-
* Time Complexity: Amortized O(1) - Generally constant time, but resizing when the deque is full leads to O(n).
|
|
144
|
-
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
145
|
-
*/
|
|
146
|
-
|
|
147
|
-
empty() {
|
|
148
|
-
return this._size === 0;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
170
|
/**
|
|
152
171
|
* Time Complexity: O(1) - Removes the last element.
|
|
153
172
|
* Space Complexity: O(1) - Operates in-place.
|
|
@@ -214,6 +233,10 @@ export class Deque<E> {
|
|
|
214
233
|
return this.shift();
|
|
215
234
|
}
|
|
216
235
|
|
|
236
|
+
/**
|
|
237
|
+
* The clear() function resets the state of the object by initializing all variables to their default
|
|
238
|
+
* values.
|
|
239
|
+
*/
|
|
217
240
|
clear() {
|
|
218
241
|
this._buckets = [new Array(this._bucketSize)];
|
|
219
242
|
this._bucketCount = 1;
|
|
@@ -221,22 +244,55 @@ export class Deque<E> {
|
|
|
221
244
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
222
245
|
}
|
|
223
246
|
|
|
247
|
+
/**
|
|
248
|
+
* The `begin()` function returns a new iterator for a deque starting from the first element.
|
|
249
|
+
* @returns A new instance of the DequeIterator class is being returned.
|
|
250
|
+
*/
|
|
224
251
|
begin() {
|
|
225
252
|
return new DequeIterator<E>(0, this);
|
|
226
253
|
}
|
|
227
254
|
|
|
255
|
+
/**
|
|
256
|
+
* The `end()` function returns a new `DequeIterator` object with the size and reference to the
|
|
257
|
+
* current deque.
|
|
258
|
+
* @returns A new instance of the DequeIterator class is being returned.
|
|
259
|
+
*/
|
|
228
260
|
end() {
|
|
229
261
|
return new DequeIterator<E>(this.size, this);
|
|
230
262
|
}
|
|
231
263
|
|
|
264
|
+
/**
|
|
265
|
+
* The reverseBegin function returns a new DequeIterator object that starts at the last element of
|
|
266
|
+
* the deque and iterates in reverse direction.
|
|
267
|
+
* @returns A new instance of the DequeIterator class is being returned.
|
|
268
|
+
*/
|
|
232
269
|
reverseBegin() {
|
|
233
270
|
return new DequeIterator<E>(this.size - 1, this, IterateDirection.REVERSE);
|
|
234
271
|
}
|
|
235
272
|
|
|
273
|
+
/**
|
|
274
|
+
* The reverseEnd() function returns a new DequeIterator object that iterates over the elements of a
|
|
275
|
+
* Deque in reverse order.
|
|
276
|
+
* @returns A new instance of the DequeIterator class is being returned.
|
|
277
|
+
*/
|
|
236
278
|
reverseEnd() {
|
|
237
279
|
return new DequeIterator<E>(-1, this, IterateDirection.REVERSE);
|
|
238
280
|
}
|
|
239
281
|
|
|
282
|
+
/**
|
|
283
|
+
* Time Complexity - Amortized O(1) (possible reallocation)
|
|
284
|
+
* Space Complexity - O(n) (due to potential resizing).
|
|
285
|
+
*/
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Time Complexity - Amortized O(1) (possible reallocation),
|
|
289
|
+
* Space Complexity - O(n) (due to potential resizing).
|
|
290
|
+
*
|
|
291
|
+
* The push function adds an element to a data structure and reallocates memory if necessary.
|
|
292
|
+
* @param {E} element - The `element` parameter represents the value that you want to add to the data
|
|
293
|
+
* structure.
|
|
294
|
+
* @returns The size of the data structure after the element has been pushed.
|
|
295
|
+
*/
|
|
240
296
|
push(element: E) {
|
|
241
297
|
if (this.size) {
|
|
242
298
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -258,6 +314,19 @@ export class Deque<E> {
|
|
|
258
314
|
return this.size;
|
|
259
315
|
}
|
|
260
316
|
|
|
317
|
+
/**
|
|
318
|
+
* Time Complexity: O(1)
|
|
319
|
+
* Space Complexity: O(1)
|
|
320
|
+
*/
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Time Complexity: O(1)
|
|
324
|
+
* Space Complexity: O(1)
|
|
325
|
+
*
|
|
326
|
+
* The `pop()` function removes and returns the last element from a data structure, updating the
|
|
327
|
+
* internal state variables accordingly.
|
|
328
|
+
* @returns The element that was removed from the data structure is being returned.
|
|
329
|
+
*/
|
|
261
330
|
pop() {
|
|
262
331
|
if (this.size === 0) return;
|
|
263
332
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -276,6 +345,21 @@ export class Deque<E> {
|
|
|
276
345
|
return element;
|
|
277
346
|
}
|
|
278
347
|
|
|
348
|
+
/**
|
|
349
|
+
* Time Complexity: Amortized O(1)
|
|
350
|
+
* Space Complexity: O(n)
|
|
351
|
+
*/
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Time Complexity: Amortized O(1)
|
|
355
|
+
* Space Complexity: O(n)
|
|
356
|
+
*
|
|
357
|
+
* The `unshift` function adds an element to the beginning of an array-like data structure and
|
|
358
|
+
* returns the new size of the structure.
|
|
359
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the
|
|
360
|
+
* beginning of the data structure.
|
|
361
|
+
* @returns The size of the data structure after the element has been added.
|
|
362
|
+
*/
|
|
279
363
|
unshift(element: E) {
|
|
280
364
|
if (this.size) {
|
|
281
365
|
if (this._firstInBucket > 0) {
|
|
@@ -297,6 +381,21 @@ export class Deque<E> {
|
|
|
297
381
|
return this.size;
|
|
298
382
|
}
|
|
299
383
|
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Time Complexity: O(1)
|
|
387
|
+
* Space Complexity: O(1)
|
|
388
|
+
*/
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Time Complexity: O(1)
|
|
392
|
+
* Space Complexity: O(1)
|
|
393
|
+
*
|
|
394
|
+
* The `shift()` function removes and returns the first element from a data structure, updating the
|
|
395
|
+
* internal state variables accordingly.
|
|
396
|
+
* @returns The element that is being removed from the beginning of the data structure is being
|
|
397
|
+
* returned.
|
|
398
|
+
*/
|
|
300
399
|
shift() {
|
|
301
400
|
if (this.size === 0) return;
|
|
302
401
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -315,6 +414,22 @@ export class Deque<E> {
|
|
|
315
414
|
return element;
|
|
316
415
|
}
|
|
317
416
|
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Time Complexity: O(1)
|
|
420
|
+
* Space Complexity: O(1)
|
|
421
|
+
*/
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Time Complexity: O(1)
|
|
425
|
+
* Space Complexity: O(1)
|
|
426
|
+
*
|
|
427
|
+
* The `getAt` function retrieves an element at a specified position in an array-like data structure.
|
|
428
|
+
* @param {number} pos - The `pos` parameter represents the position of the element that you want to
|
|
429
|
+
* retrieve from the data structure. It is of type `number` and should be a valid index within the
|
|
430
|
+
* range of the data structure.
|
|
431
|
+
* @returns The element at the specified position in the data structure is being returned.
|
|
432
|
+
*/
|
|
318
433
|
getAt(pos: number): E {
|
|
319
434
|
rangeCheck!(pos, 0, this.size - 1);
|
|
320
435
|
const {
|
|
@@ -324,6 +439,22 @@ export class Deque<E> {
|
|
|
324
439
|
return this._buckets[bucketIndex][indexInBucket]!;
|
|
325
440
|
}
|
|
326
441
|
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Time Complexity: O(1)
|
|
445
|
+
* Space Complexity: O(1)
|
|
446
|
+
*/
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Time Complexity: O(1)
|
|
450
|
+
* Space Complexity: O(1)
|
|
451
|
+
*
|
|
452
|
+
* The `setAt` function sets an element at a specific position in an array-like data structure.
|
|
453
|
+
* @param {number} pos - The `pos` parameter represents the position at which the element needs to be
|
|
454
|
+
* set. It is of type `number`.
|
|
455
|
+
* @param {E} element - The `element` parameter is the value that you want to set at the specified
|
|
456
|
+
* position in the data structure.
|
|
457
|
+
*/
|
|
327
458
|
setAt(pos: number, element: E) {
|
|
328
459
|
rangeCheck!(pos, 0, this.size - 1);
|
|
329
460
|
const {
|
|
@@ -333,6 +464,26 @@ export class Deque<E> {
|
|
|
333
464
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
334
465
|
}
|
|
335
466
|
|
|
467
|
+
/**
|
|
468
|
+
* Time Complexity: O(n)
|
|
469
|
+
* Space Complexity: O(n)
|
|
470
|
+
*/
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Time Complexity: O(n)
|
|
474
|
+
* Space Complexity: O(n)
|
|
475
|
+
*
|
|
476
|
+
* The `insertAt` function inserts one or more elements at a specified position in an array-like data
|
|
477
|
+
* structure.
|
|
478
|
+
* @param {number} pos - The `pos` parameter represents the position at which the element(s) should
|
|
479
|
+
* be inserted. It is of type `number`.
|
|
480
|
+
* @param {E} element - The `element` parameter represents the element that you want to insert into
|
|
481
|
+
* the array at the specified position.
|
|
482
|
+
* @param [num=1] - The `num` parameter represents the number of times the `element` should be
|
|
483
|
+
* inserted at the specified position (`pos`). By default, it is set to 1, meaning that the `element`
|
|
484
|
+
* will be inserted once. However, you can provide a different value for `num` if you want
|
|
485
|
+
* @returns The size of the array after the insertion is being returned.
|
|
486
|
+
*/
|
|
336
487
|
insertAt(pos: number, element: E, num = 1) {
|
|
337
488
|
const length = this.size;
|
|
338
489
|
rangeCheck!(pos, 0, length);
|
|
@@ -352,6 +503,21 @@ export class Deque<E> {
|
|
|
352
503
|
return this.size;
|
|
353
504
|
}
|
|
354
505
|
|
|
506
|
+
/**
|
|
507
|
+
* Time Complexity: O(1)
|
|
508
|
+
* Space Complexity: O(1)
|
|
509
|
+
*/
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Time Complexity: O(1)
|
|
513
|
+
* Space Complexity: O(1)
|
|
514
|
+
*
|
|
515
|
+
* The `cut` function updates the state of the object based on the given position and returns the
|
|
516
|
+
* updated size.
|
|
517
|
+
* @param {number} pos - The `pos` parameter represents the position at which the string should be
|
|
518
|
+
* cut. It is a number that indicates the index of the character where the cut should be made.
|
|
519
|
+
* @returns The method is returning the updated size of the data structure.
|
|
520
|
+
*/
|
|
355
521
|
cut(pos: number) {
|
|
356
522
|
if (pos < 0) {
|
|
357
523
|
this.clear();
|
|
@@ -367,6 +533,22 @@ export class Deque<E> {
|
|
|
367
533
|
return this.size;
|
|
368
534
|
}
|
|
369
535
|
|
|
536
|
+
/**
|
|
537
|
+
* Time Complexity: O(n)
|
|
538
|
+
* Space Complexity: O(1)
|
|
539
|
+
*/
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Time Complexity: O(n)
|
|
543
|
+
* Space Complexity: O(1)
|
|
544
|
+
*
|
|
545
|
+
* The `deleteAt` function removes an element at a specified position in an array-like data
|
|
546
|
+
* structure.
|
|
547
|
+
* @param {number} pos - The `pos` parameter in the `deleteAt` function represents the position at
|
|
548
|
+
* which an element needs to be deleted from the data structure. It is of type `number` and indicates
|
|
549
|
+
* the index of the element to be deleted.
|
|
550
|
+
* @returns The size of the data structure after the deletion operation is performed.
|
|
551
|
+
*/
|
|
370
552
|
deleteAt(pos: number) {
|
|
371
553
|
rangeCheck!(pos, 0, this.size - 1);
|
|
372
554
|
if (pos === 0) this.shift();
|
|
@@ -391,6 +573,21 @@ export class Deque<E> {
|
|
|
391
573
|
return this.size;
|
|
392
574
|
}
|
|
393
575
|
|
|
576
|
+
/**
|
|
577
|
+
* Time Complexity: O(n)
|
|
578
|
+
* Space Complexity: O(1)
|
|
579
|
+
*/
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Time Complexity: O(n)
|
|
583
|
+
* Space Complexity: O(1)
|
|
584
|
+
*
|
|
585
|
+
* The `delete` function removes all occurrences of a specified element from an array-like data
|
|
586
|
+
* structure.
|
|
587
|
+
* @param {E} element - The `element` parameter represents the element that you want to delete from
|
|
588
|
+
* the data structure.
|
|
589
|
+
* @returns The size of the data structure after the element has been deleted.
|
|
590
|
+
*/
|
|
394
591
|
delete(element: E) {
|
|
395
592
|
const size = this.size;
|
|
396
593
|
if (size === 0) return 0;
|
|
@@ -408,6 +605,20 @@ export class Deque<E> {
|
|
|
408
605
|
return this.size;
|
|
409
606
|
}
|
|
410
607
|
|
|
608
|
+
/**
|
|
609
|
+
* Time Complexity: O(n)
|
|
610
|
+
* Space Complexity: O(1)
|
|
611
|
+
*/
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Time Complexity: O(n)
|
|
615
|
+
* Space Complexity: O(1)
|
|
616
|
+
*
|
|
617
|
+
* The function deletes an element from a deque using an iterator and returns the next iterator.
|
|
618
|
+
* @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object
|
|
619
|
+
* that is used to iterate over elements in a deque (double-ended queue).
|
|
620
|
+
* @returns the updated iterator after deleting an element from the deque.
|
|
621
|
+
*/
|
|
411
622
|
deleteByIterator(iter: DequeIterator<E>) {
|
|
412
623
|
const index = iter.index;
|
|
413
624
|
this.deleteAt(index);
|
|
@@ -415,6 +626,20 @@ export class Deque<E> {
|
|
|
415
626
|
return iter;
|
|
416
627
|
}
|
|
417
628
|
|
|
629
|
+
/**
|
|
630
|
+
* Time Complexity: O(n)
|
|
631
|
+
* Space Complexity: O(1)
|
|
632
|
+
*/
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Time Complexity: O(n)
|
|
636
|
+
* Space Complexity: O(1)
|
|
637
|
+
*
|
|
638
|
+
* The function `findIterator` searches for an element in a deque and returns an iterator pointing to
|
|
639
|
+
* the element if found, otherwise it returns an iterator pointing to the end of the deque.
|
|
640
|
+
* @param {E} element - The `element` parameter is the element that you want to find in the deque.
|
|
641
|
+
* @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object.
|
|
642
|
+
*/
|
|
418
643
|
findIterator(element: E) {
|
|
419
644
|
for (let i = 0; i < this.size; ++i) {
|
|
420
645
|
if (this.getAt(i) === element) {
|
|
@@ -424,6 +649,20 @@ export class Deque<E> {
|
|
|
424
649
|
return this.end();
|
|
425
650
|
}
|
|
426
651
|
|
|
652
|
+
/**
|
|
653
|
+
* Time Complexity: O(n)
|
|
654
|
+
* Space Complexity: O(1)
|
|
655
|
+
*/
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Time Complexity: O(n)
|
|
659
|
+
* Space Complexity: O(1)
|
|
660
|
+
*
|
|
661
|
+
* The reverse() function reverses the order of the buckets and the elements within each bucket in a
|
|
662
|
+
* data structure.
|
|
663
|
+
* @returns The reverse() method is returning the object itself (this) after performing the reverse
|
|
664
|
+
* operation on the buckets and updating the relevant properties.
|
|
665
|
+
*/
|
|
427
666
|
reverse() {
|
|
428
667
|
this._buckets.reverse().forEach(function (bucket) {
|
|
429
668
|
bucket.reverse();
|
|
@@ -436,6 +675,19 @@ export class Deque<E> {
|
|
|
436
675
|
return this;
|
|
437
676
|
}
|
|
438
677
|
|
|
678
|
+
/**
|
|
679
|
+
* Time Complexity: O(n)
|
|
680
|
+
* Space Complexity: O(1)
|
|
681
|
+
*/
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Time Complexity: O(n)
|
|
685
|
+
* Space Complexity: O(1)
|
|
686
|
+
*
|
|
687
|
+
* The `unique()` function removes duplicate elements from an array-like data structure and returns
|
|
688
|
+
* the number of unique elements.
|
|
689
|
+
* @returns The size of the modified array is being returned.
|
|
690
|
+
*/
|
|
439
691
|
unique() {
|
|
440
692
|
if (this.size <= 1) {
|
|
441
693
|
return this.size;
|
|
@@ -453,6 +705,21 @@ export class Deque<E> {
|
|
|
453
705
|
return this.size;
|
|
454
706
|
}
|
|
455
707
|
|
|
708
|
+
/**
|
|
709
|
+
* Time Complexity: O(n log n)
|
|
710
|
+
* Space Complexity: O(n)
|
|
711
|
+
*/
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* Time Complexity: O(n log n)
|
|
715
|
+
* Space Complexity: O(n)
|
|
716
|
+
*
|
|
717
|
+
* The `sort` function sorts the elements in a data structure using a provided comparator function.
|
|
718
|
+
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
719
|
+
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
720
|
+
* the elements in the sorted array.
|
|
721
|
+
* @returns The method is returning the sorted instance of the object on which the method is called.
|
|
722
|
+
*/
|
|
456
723
|
sort(comparator?: (x: E, y: E) => number) {
|
|
457
724
|
const arr: E[] = [];
|
|
458
725
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -465,6 +732,20 @@ export class Deque<E> {
|
|
|
465
732
|
return this;
|
|
466
733
|
}
|
|
467
734
|
|
|
735
|
+
/**
|
|
736
|
+
* Time Complexity: O(n)
|
|
737
|
+
* Space Complexity: O(n)
|
|
738
|
+
*/
|
|
739
|
+
|
|
740
|
+
/**
|
|
741
|
+
* Time Complexity: O(n)
|
|
742
|
+
* Space Complexity: O(n)
|
|
743
|
+
*
|
|
744
|
+
* The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
|
|
745
|
+
* memory usage.
|
|
746
|
+
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
|
|
747
|
+
* `this.size` is 0, but it does not return any value.
|
|
748
|
+
*/
|
|
468
749
|
shrinkToFit() {
|
|
469
750
|
if (this.size === 0) return;
|
|
470
751
|
const newBuckets = [];
|
|
@@ -486,12 +767,42 @@ export class Deque<E> {
|
|
|
486
767
|
this._buckets = newBuckets;
|
|
487
768
|
}
|
|
488
769
|
|
|
770
|
+
/**
|
|
771
|
+
* Time Complexity: O(n)
|
|
772
|
+
* Space Complexity: O(1)
|
|
773
|
+
*/
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* Time Complexity: O(n)
|
|
777
|
+
* Space Complexity: O(1)
|
|
778
|
+
*
|
|
779
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
780
|
+
* each element.
|
|
781
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
782
|
+
* deque. It takes three parameters:
|
|
783
|
+
*/
|
|
489
784
|
forEach(callback: (element: E, index: number, deque: Deque<E>) => void) {
|
|
490
785
|
for (let i = 0; i < this.size; ++i) {
|
|
491
786
|
callback(this.getAt(i), i, this);
|
|
492
787
|
}
|
|
493
788
|
}
|
|
494
789
|
|
|
790
|
+
/**
|
|
791
|
+
* Time Complexity: O(n)
|
|
792
|
+
* Space Complexity: O(1)
|
|
793
|
+
*/
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* Time Complexity: O(n)
|
|
797
|
+
* Space Complexity: O(1)
|
|
798
|
+
*
|
|
799
|
+
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
800
|
+
* the callback function returns true, or undefined if no such element is found.
|
|
801
|
+
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
802
|
+
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
803
|
+
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
804
|
+
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
805
|
+
*/
|
|
495
806
|
find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined {
|
|
496
807
|
for (let i = 0; i < this.size; ++i) {
|
|
497
808
|
const element = this.getAt(i);
|
|
@@ -502,6 +813,18 @@ export class Deque<E> {
|
|
|
502
813
|
return undefined;
|
|
503
814
|
}
|
|
504
815
|
|
|
816
|
+
/**
|
|
817
|
+
* Time Complexity: O(n)
|
|
818
|
+
* Space Complexity: O(n)
|
|
819
|
+
*/
|
|
820
|
+
|
|
821
|
+
/**
|
|
822
|
+
* Time Complexity: O(n)
|
|
823
|
+
* Space Complexity: O(n)
|
|
824
|
+
*
|
|
825
|
+
* The `toArray` function converts the elements of a data structure into an array.
|
|
826
|
+
* @returns The `toArray()` method is returning an array of elements of type `E`.
|
|
827
|
+
*/
|
|
505
828
|
toArray(): E[] {
|
|
506
829
|
const arr: E[] = [];
|
|
507
830
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -510,6 +833,20 @@ export class Deque<E> {
|
|
|
510
833
|
return arr;
|
|
511
834
|
}
|
|
512
835
|
|
|
836
|
+
/**
|
|
837
|
+
* Time Complexity: O(n)
|
|
838
|
+
* Space Complexity: O(n)
|
|
839
|
+
*/
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* Time Complexity: O(n)
|
|
843
|
+
* Space Complexity: O(n)
|
|
844
|
+
*
|
|
845
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
846
|
+
* returning a new deque with the results.
|
|
847
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
848
|
+
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
849
|
+
*/
|
|
513
850
|
map<T>(callback: (element: E, index: number, deque: Deque<E>) => T): Deque<T> {
|
|
514
851
|
const newDeque = new Deque<T>([], this._bucketSize);
|
|
515
852
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -518,6 +855,22 @@ export class Deque<E> {
|
|
|
518
855
|
return newDeque;
|
|
519
856
|
}
|
|
520
857
|
|
|
858
|
+
/**
|
|
859
|
+
* Time Complexity: O(n)
|
|
860
|
+
* Space Complexity: O(n)
|
|
861
|
+
*/
|
|
862
|
+
|
|
863
|
+
/**
|
|
864
|
+
* Time Complexity: O(n)
|
|
865
|
+
* Space Complexity: O(n)
|
|
866
|
+
*
|
|
867
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
868
|
+
* predicate function.
|
|
869
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
870
|
+
* `index`, and `deque`.
|
|
871
|
+
* @returns The `filter` method is returning a new `Deque` object that contains only the elements
|
|
872
|
+
* that satisfy the given `predicate` function.
|
|
873
|
+
*/
|
|
521
874
|
filter(predicate: (element: E, index: number, deque: Deque<E>) => boolean): Deque<E> {
|
|
522
875
|
const newDeque = new Deque<E>([], this._bucketSize);
|
|
523
876
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -529,6 +882,24 @@ export class Deque<E> {
|
|
|
529
882
|
return newDeque;
|
|
530
883
|
}
|
|
531
884
|
|
|
885
|
+
/**
|
|
886
|
+
* Time Complexity: O(n)
|
|
887
|
+
* Space Complexity: O(1)
|
|
888
|
+
*/
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* Time Complexity: O(n)
|
|
892
|
+
* Space Complexity: O(1)
|
|
893
|
+
*
|
|
894
|
+
* The `reduce` function iterates over the elements of a deque and applies a callback function to
|
|
895
|
+
* each element, accumulating a single value.
|
|
896
|
+
* @param callback - The `callback` parameter is a function that takes four arguments:
|
|
897
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
898
|
+
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
899
|
+
* the elements of the deque.
|
|
900
|
+
* @returns the final value of the accumulator after iterating over all elements in the deque and
|
|
901
|
+
* applying the callback function to each element.
|
|
902
|
+
*/
|
|
532
903
|
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: Deque<E>) => T, initialValue: T): T {
|
|
533
904
|
let accumulator = initialValue;
|
|
534
905
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -537,6 +908,22 @@ export class Deque<E> {
|
|
|
537
908
|
return accumulator;
|
|
538
909
|
}
|
|
539
910
|
|
|
911
|
+
/**
|
|
912
|
+
* Time Complexity: O(n)
|
|
913
|
+
* Space Complexity: O(1)
|
|
914
|
+
*/
|
|
915
|
+
|
|
916
|
+
/**
|
|
917
|
+
* Time Complexity: O(n)
|
|
918
|
+
* Space Complexity: O(1)
|
|
919
|
+
*
|
|
920
|
+
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
921
|
+
* or -1 if the element is not found.
|
|
922
|
+
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
923
|
+
* index of in the data structure.
|
|
924
|
+
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
925
|
+
* in the data structure. If the element is not found, it returns -1.
|
|
926
|
+
*/
|
|
540
927
|
indexOf(element: E): number {
|
|
541
928
|
for (let i = 0; i < this.size; ++i) {
|
|
542
929
|
if (this.getAt(i) === element) {
|
|
@@ -546,12 +933,38 @@ export class Deque<E> {
|
|
|
546
933
|
return -1;
|
|
547
934
|
}
|
|
548
935
|
|
|
936
|
+
/**
|
|
937
|
+
* Time Complexity: O(n)
|
|
938
|
+
* Space Complexity: O(1)
|
|
939
|
+
*/
|
|
940
|
+
|
|
941
|
+
/**
|
|
942
|
+
* Time Complexity: O(n)
|
|
943
|
+
* Space Complexity: O(1)
|
|
944
|
+
*
|
|
945
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
946
|
+
* object to be iterated over using a for...of loop.
|
|
947
|
+
*/
|
|
549
948
|
* [Symbol.iterator]() {
|
|
550
949
|
for (let i = 0; i < this.size; ++i) {
|
|
551
950
|
yield this.getAt(i);
|
|
552
951
|
}
|
|
553
952
|
}
|
|
554
953
|
|
|
954
|
+
/**
|
|
955
|
+
* Time Complexity: O(n)
|
|
956
|
+
* Space Complexity: O(n)
|
|
957
|
+
*/
|
|
958
|
+
|
|
959
|
+
/**
|
|
960
|
+
* Time Complexity: O(n)
|
|
961
|
+
* Space Complexity: O(n)
|
|
962
|
+
*
|
|
963
|
+
* The `_reallocate` function reallocates the buckets in an array, adding new buckets if needed.
|
|
964
|
+
* @param {number} [needBucketNum] - The `needBucketNum` parameter is an optional number that
|
|
965
|
+
* specifies the number of new buckets needed. If not provided, it will default to half of the
|
|
966
|
+
* current bucket count (`this._bucketCount >> 1`) or 1 if the current bucket count is less than 2.
|
|
967
|
+
*/
|
|
555
968
|
protected _reallocate(needBucketNum?: number) {
|
|
556
969
|
const newBuckets = [];
|
|
557
970
|
const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
|
|
@@ -574,6 +987,20 @@ export class Deque<E> {
|
|
|
574
987
|
this._bucketCount = newBuckets.length;
|
|
575
988
|
}
|
|
576
989
|
|
|
990
|
+
/**
|
|
991
|
+
* Time Complexity: O(1)
|
|
992
|
+
* Space Complexity: O(1)
|
|
993
|
+
*/
|
|
994
|
+
|
|
995
|
+
/**
|
|
996
|
+
* Time Complexity: O(1)
|
|
997
|
+
* Space Complexity: O(1)
|
|
998
|
+
*
|
|
999
|
+
* The function calculates the bucket index and index within the bucket based on the given position.
|
|
1000
|
+
* @param {number} pos - The `pos` parameter represents the position within the data structure. It is
|
|
1001
|
+
* a number that indicates the index or position of an element within the structure.
|
|
1002
|
+
* @returns an object with two properties: "bucketIndex" and "indexInBucket".
|
|
1003
|
+
*/
|
|
577
1004
|
protected _getBucketAndPosition(pos: number) {
|
|
578
1005
|
let bucketIndex: number;
|
|
579
1006
|
let indexInBucket: number;
|
package/src/types/helpers.ts
CHANGED
|
@@ -16,11 +16,11 @@ export const enum IterateDirection {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export interface IterableWithSize<T> extends Iterable<T> {
|
|
19
|
-
size: number;
|
|
19
|
+
size: number | ((...args: any[]) => number);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export interface IterableWithLength<T> extends Iterable<T> {
|
|
23
|
-
length: number;
|
|
23
|
+
length: number | ((...args: any[]) => number);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>
|