data-structure-typed 1.47.3 → 1.47.5
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.
- package/CHANGELOG.md +1 -1
- package/benchmark/report.html +2 -2
- package/benchmark/report.json +12 -18
- package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/cjs/data-structures/hash/hash-map.js +10 -4
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/cjs/data-structures/hash/hash-table.js +50 -5
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -1
- package/dist/cjs/data-structures/heap/heap.js +51 -9
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +56 -56
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +117 -119
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +36 -36
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +58 -60
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +49 -49
- package/dist/cjs/data-structures/queue/deque.js +79 -72
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +45 -0
- package/dist/cjs/data-structures/queue/queue.js +77 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +18 -5
- package/dist/cjs/data-structures/stack/stack.js +56 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/cjs/data-structures/trie/trie.js +47 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/mjs/data-structures/hash/hash-map.js +10 -4
- package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/mjs/data-structures/hash/hash-table.js +50 -5
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -1
- package/dist/mjs/data-structures/heap/heap.js +51 -9
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +56 -56
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +117 -119
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +36 -36
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +58 -60
- package/dist/mjs/data-structures/queue/deque.d.ts +49 -49
- package/dist/mjs/data-structures/queue/deque.js +79 -72
- package/dist/mjs/data-structures/queue/queue.d.ts +45 -0
- package/dist/mjs/data-structures/queue/queue.js +77 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +18 -5
- package/dist/mjs/data-structures/stack/stack.js +56 -7
- package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/mjs/data-structures/trie/trie.js +47 -0
- package/dist/umd/data-structure-typed.js +545 -276
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/hash/hash-map.ts +13 -7
- package/src/data-structures/hash/hash-table.ts +59 -9
- package/src/data-structures/heap/heap.ts +60 -9
- package/src/data-structures/linked-list/doubly-linked-list.ts +129 -129
- package/src/data-structures/linked-list/singly-linked-list.ts +65 -65
- package/src/data-structures/queue/deque.ts +84 -77
- package/src/data-structures/queue/queue.ts +84 -0
- package/src/data-structures/stack/stack.ts +64 -8
- package/src/data-structures/trie/trie.ts +53 -0
- package/test/integration/conversion.test.ts +0 -0
- package/test/performance/data-structures/heap/heap.test.ts +13 -4
- package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
- package/test/unit/data-structures/heap/min-heap.test.ts +48 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/queue/queue.test.ts +37 -0
- package/test/unit/data-structures/stack/stack.test.ts +55 -5
- package/test/unit/data-structures/trie/trie.test.ts +33 -0
|
@@ -318,12 +318,14 @@ export declare class Deque<E> {
|
|
|
318
318
|
* Time Complexity: O(n)
|
|
319
319
|
* Space Complexity: O(1)
|
|
320
320
|
*
|
|
321
|
-
* The `
|
|
322
|
-
*
|
|
323
|
-
* @param callback -
|
|
324
|
-
*
|
|
321
|
+
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
322
|
+
* the callback function returns true, or undefined if no such element is found.
|
|
323
|
+
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
324
|
+
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
325
|
+
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
326
|
+
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
325
327
|
*/
|
|
326
|
-
|
|
328
|
+
find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined;
|
|
327
329
|
/**
|
|
328
330
|
* Time Complexity: O(n)
|
|
329
331
|
* Space Complexity: O(1)
|
|
@@ -332,14 +334,14 @@ export declare class Deque<E> {
|
|
|
332
334
|
* Time Complexity: O(n)
|
|
333
335
|
* Space Complexity: O(1)
|
|
334
336
|
*
|
|
335
|
-
* The
|
|
336
|
-
*
|
|
337
|
-
* @param
|
|
338
|
-
*
|
|
339
|
-
* @returns The
|
|
340
|
-
*
|
|
337
|
+
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
338
|
+
* or -1 if the element is not found.
|
|
339
|
+
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
340
|
+
* index of in the data structure.
|
|
341
|
+
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
342
|
+
* in the data structure. If the element is not found, it returns -1.
|
|
341
343
|
*/
|
|
342
|
-
|
|
344
|
+
indexOf(element: E): number;
|
|
343
345
|
/**
|
|
344
346
|
* Time Complexity: O(n)
|
|
345
347
|
* Space Complexity: O(n)
|
|
@@ -354,18 +356,30 @@ export declare class Deque<E> {
|
|
|
354
356
|
toArray(): E[];
|
|
355
357
|
/**
|
|
356
358
|
* Time Complexity: O(n)
|
|
357
|
-
* Space Complexity: O(
|
|
359
|
+
* Space Complexity: O(1)
|
|
358
360
|
*/
|
|
359
361
|
/**
|
|
360
362
|
* Time Complexity: O(n)
|
|
361
|
-
* Space Complexity: O(
|
|
363
|
+
* Space Complexity: O(1)
|
|
362
364
|
*
|
|
363
|
-
* The
|
|
364
|
-
*
|
|
365
|
-
|
|
366
|
-
|
|
365
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
366
|
+
* object to be iterated over using a for...of loop.
|
|
367
|
+
*/
|
|
368
|
+
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
369
|
+
/**
|
|
370
|
+
* Time Complexity: O(n)
|
|
371
|
+
* Space Complexity: O(1)
|
|
372
|
+
*/
|
|
373
|
+
/**
|
|
374
|
+
* Time Complexity: O(n)
|
|
375
|
+
* Space Complexity: O(1)
|
|
376
|
+
*
|
|
377
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
378
|
+
* each element.
|
|
379
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
380
|
+
* deque. It takes three parameters:
|
|
367
381
|
*/
|
|
368
|
-
|
|
382
|
+
forEach(callback: (element: E, index: number, deque: this) => void): void;
|
|
369
383
|
/**
|
|
370
384
|
* Time Complexity: O(n)
|
|
371
385
|
* Space Complexity: O(n)
|
|
@@ -381,7 +395,21 @@ export declare class Deque<E> {
|
|
|
381
395
|
* @returns The `filter` method is returning a new `Deque` object that contains only the elements
|
|
382
396
|
* that satisfy the given `predicate` function.
|
|
383
397
|
*/
|
|
384
|
-
filter(predicate: (element: E, index: number, deque:
|
|
398
|
+
filter(predicate: (element: E, index: number, deque: this) => boolean): Deque<E>;
|
|
399
|
+
/**
|
|
400
|
+
* Time Complexity: O(n)
|
|
401
|
+
* Space Complexity: O(n)
|
|
402
|
+
*/
|
|
403
|
+
/**
|
|
404
|
+
* Time Complexity: O(n)
|
|
405
|
+
* Space Complexity: O(n)
|
|
406
|
+
*
|
|
407
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
408
|
+
* returning a new deque with the results.
|
|
409
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
410
|
+
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
411
|
+
*/
|
|
412
|
+
map<T>(callback: (element: E, index: number, deque: this) => T): Deque<T>;
|
|
385
413
|
/**
|
|
386
414
|
* Time Complexity: O(n)
|
|
387
415
|
* Space Complexity: O(1)
|
|
@@ -399,35 +427,7 @@ export declare class Deque<E> {
|
|
|
399
427
|
* @returns the final value of the accumulator after iterating over all elements in the deque and
|
|
400
428
|
* applying the callback function to each element.
|
|
401
429
|
*/
|
|
402
|
-
reduce<T>(callback: (accumulator: T, element: E, index: number, deque:
|
|
403
|
-
/**
|
|
404
|
-
* Time Complexity: O(n)
|
|
405
|
-
* Space Complexity: O(1)
|
|
406
|
-
*/
|
|
407
|
-
/**
|
|
408
|
-
* Time Complexity: O(n)
|
|
409
|
-
* Space Complexity: O(1)
|
|
410
|
-
*
|
|
411
|
-
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
412
|
-
* or -1 if the element is not found.
|
|
413
|
-
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
414
|
-
* index of in the data structure.
|
|
415
|
-
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
416
|
-
* in the data structure. If the element is not found, it returns -1.
|
|
417
|
-
*/
|
|
418
|
-
indexOf(element: E): number;
|
|
419
|
-
/**
|
|
420
|
-
* Time Complexity: O(n)
|
|
421
|
-
* Space Complexity: O(1)
|
|
422
|
-
*/
|
|
423
|
-
/**
|
|
424
|
-
* Time Complexity: O(n)
|
|
425
|
-
* Space Complexity: O(1)
|
|
426
|
-
*
|
|
427
|
-
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
428
|
-
* object to be iterated over using a for...of loop.
|
|
429
|
-
*/
|
|
430
|
-
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
430
|
+
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T;
|
|
431
431
|
/**
|
|
432
432
|
* Time Complexity: O(n)
|
|
433
433
|
* Space Complexity: O(n)
|
|
@@ -592,15 +592,21 @@ export class Deque {
|
|
|
592
592
|
* Time Complexity: O(n)
|
|
593
593
|
* Space Complexity: O(1)
|
|
594
594
|
*
|
|
595
|
-
* The `
|
|
596
|
-
*
|
|
597
|
-
* @param callback -
|
|
598
|
-
*
|
|
595
|
+
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
596
|
+
* the callback function returns true, or undefined if no such element is found.
|
|
597
|
+
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
598
|
+
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
599
|
+
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
600
|
+
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
599
601
|
*/
|
|
600
|
-
|
|
602
|
+
find(callback) {
|
|
601
603
|
for (let i = 0; i < this.size; ++i) {
|
|
602
|
-
|
|
604
|
+
const element = this.getAt(i);
|
|
605
|
+
if (callback(element, i, this)) {
|
|
606
|
+
return element;
|
|
607
|
+
}
|
|
603
608
|
}
|
|
609
|
+
return undefined;
|
|
604
610
|
}
|
|
605
611
|
/**
|
|
606
612
|
* Time Complexity: O(n)
|
|
@@ -610,21 +616,20 @@ export class Deque {
|
|
|
610
616
|
* Time Complexity: O(n)
|
|
611
617
|
* Space Complexity: O(1)
|
|
612
618
|
*
|
|
613
|
-
* The
|
|
614
|
-
*
|
|
615
|
-
* @param
|
|
616
|
-
*
|
|
617
|
-
* @returns The
|
|
618
|
-
*
|
|
619
|
+
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
620
|
+
* or -1 if the element is not found.
|
|
621
|
+
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
622
|
+
* index of in the data structure.
|
|
623
|
+
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
624
|
+
* in the data structure. If the element is not found, it returns -1.
|
|
619
625
|
*/
|
|
620
|
-
|
|
626
|
+
indexOf(element) {
|
|
621
627
|
for (let i = 0; i < this.size; ++i) {
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
return element;
|
|
628
|
+
if (this.getAt(i) === element) {
|
|
629
|
+
return i;
|
|
625
630
|
}
|
|
626
631
|
}
|
|
627
|
-
return
|
|
632
|
+
return -1;
|
|
628
633
|
}
|
|
629
634
|
/**
|
|
630
635
|
* Time Complexity: O(n)
|
|
@@ -646,23 +651,39 @@ export class Deque {
|
|
|
646
651
|
}
|
|
647
652
|
/**
|
|
648
653
|
* Time Complexity: O(n)
|
|
649
|
-
* Space Complexity: O(
|
|
654
|
+
* Space Complexity: O(1)
|
|
650
655
|
*/
|
|
651
656
|
/**
|
|
652
657
|
* Time Complexity: O(n)
|
|
653
|
-
* Space Complexity: O(
|
|
658
|
+
* Space Complexity: O(1)
|
|
654
659
|
*
|
|
655
|
-
* The
|
|
656
|
-
*
|
|
657
|
-
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
658
|
-
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
660
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
661
|
+
* object to be iterated over using a for...of loop.
|
|
659
662
|
*/
|
|
660
|
-
|
|
661
|
-
const newDeque = new Deque([], this._bucketSize);
|
|
663
|
+
*[Symbol.iterator]() {
|
|
662
664
|
for (let i = 0; i < this.size; ++i) {
|
|
663
|
-
|
|
665
|
+
yield this.getAt(i);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Time Complexity: O(n)
|
|
670
|
+
* Space Complexity: O(1)
|
|
671
|
+
*/
|
|
672
|
+
/**
|
|
673
|
+
* Time Complexity: O(n)
|
|
674
|
+
* Space Complexity: O(1)
|
|
675
|
+
*
|
|
676
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
677
|
+
* each element.
|
|
678
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
679
|
+
* deque. It takes three parameters:
|
|
680
|
+
*/
|
|
681
|
+
forEach(callback) {
|
|
682
|
+
let index = 0;
|
|
683
|
+
for (const el of this) {
|
|
684
|
+
callback(el, index, this);
|
|
685
|
+
index++;
|
|
664
686
|
}
|
|
665
|
-
return newDeque;
|
|
666
687
|
}
|
|
667
688
|
/**
|
|
668
689
|
* Time Complexity: O(n)
|
|
@@ -681,11 +702,34 @@ export class Deque {
|
|
|
681
702
|
*/
|
|
682
703
|
filter(predicate) {
|
|
683
704
|
const newDeque = new Deque([], this._bucketSize);
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
if (predicate(
|
|
687
|
-
newDeque.push(
|
|
705
|
+
let index = 0;
|
|
706
|
+
for (const el of this) {
|
|
707
|
+
if (predicate(el, index, this)) {
|
|
708
|
+
newDeque.push(el);
|
|
688
709
|
}
|
|
710
|
+
index++;
|
|
711
|
+
}
|
|
712
|
+
return newDeque;
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* Time Complexity: O(n)
|
|
716
|
+
* Space Complexity: O(n)
|
|
717
|
+
*/
|
|
718
|
+
/**
|
|
719
|
+
* Time Complexity: O(n)
|
|
720
|
+
* Space Complexity: O(n)
|
|
721
|
+
*
|
|
722
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
723
|
+
* returning a new deque with the results.
|
|
724
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
725
|
+
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
726
|
+
*/
|
|
727
|
+
map(callback) {
|
|
728
|
+
const newDeque = new Deque([], this._bucketSize);
|
|
729
|
+
let index = 0;
|
|
730
|
+
for (const el of this) {
|
|
731
|
+
newDeque.push(callback(el, index, this));
|
|
732
|
+
index++;
|
|
689
733
|
}
|
|
690
734
|
return newDeque;
|
|
691
735
|
}
|
|
@@ -708,50 +752,13 @@ export class Deque {
|
|
|
708
752
|
*/
|
|
709
753
|
reduce(callback, initialValue) {
|
|
710
754
|
let accumulator = initialValue;
|
|
711
|
-
|
|
712
|
-
|
|
755
|
+
let index = 0;
|
|
756
|
+
for (const el of this) {
|
|
757
|
+
accumulator = callback(accumulator, el, index, this);
|
|
758
|
+
index++;
|
|
713
759
|
}
|
|
714
760
|
return accumulator;
|
|
715
761
|
}
|
|
716
|
-
/**
|
|
717
|
-
* Time Complexity: O(n)
|
|
718
|
-
* Space Complexity: O(1)
|
|
719
|
-
*/
|
|
720
|
-
/**
|
|
721
|
-
* Time Complexity: O(n)
|
|
722
|
-
* Space Complexity: O(1)
|
|
723
|
-
*
|
|
724
|
-
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
725
|
-
* or -1 if the element is not found.
|
|
726
|
-
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
727
|
-
* index of in the data structure.
|
|
728
|
-
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
729
|
-
* in the data structure. If the element is not found, it returns -1.
|
|
730
|
-
*/
|
|
731
|
-
indexOf(element) {
|
|
732
|
-
for (let i = 0; i < this.size; ++i) {
|
|
733
|
-
if (this.getAt(i) === element) {
|
|
734
|
-
return i;
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
return -1;
|
|
738
|
-
}
|
|
739
|
-
/**
|
|
740
|
-
* Time Complexity: O(n)
|
|
741
|
-
* Space Complexity: O(1)
|
|
742
|
-
*/
|
|
743
|
-
/**
|
|
744
|
-
* Time Complexity: O(n)
|
|
745
|
-
* Space Complexity: O(1)
|
|
746
|
-
*
|
|
747
|
-
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
748
|
-
* object to be iterated over using a for...of loop.
|
|
749
|
-
*/
|
|
750
|
-
*[Symbol.iterator]() {
|
|
751
|
-
for (let i = 0; i < this.size; ++i) {
|
|
752
|
-
yield this.getAt(i);
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
762
|
/**
|
|
756
763
|
* Time Complexity: O(n)
|
|
757
764
|
* Space Complexity: O(n)
|
|
@@ -206,4 +206,49 @@ export declare class Queue<E = any> {
|
|
|
206
206
|
*/
|
|
207
207
|
clone(): Queue<E>;
|
|
208
208
|
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
209
|
+
/**
|
|
210
|
+
* Time Complexity: O(n)
|
|
211
|
+
* Space Complexity: O(1)
|
|
212
|
+
*/
|
|
213
|
+
/**
|
|
214
|
+
* Time Complexity: O(n)
|
|
215
|
+
* Space Complexity: O(1)
|
|
216
|
+
*
|
|
217
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
218
|
+
* each element.
|
|
219
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
220
|
+
* deque. It takes three parameters:
|
|
221
|
+
*/
|
|
222
|
+
forEach(callback: (element: E, index: number, queue: this) => void): void;
|
|
223
|
+
/**
|
|
224
|
+
* Time Complexity: O(n)
|
|
225
|
+
* Space Complexity: O(n)
|
|
226
|
+
*/
|
|
227
|
+
/**
|
|
228
|
+
* Time Complexity: O(n)
|
|
229
|
+
* Space Complexity: O(n)
|
|
230
|
+
*
|
|
231
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
232
|
+
* predicate function.
|
|
233
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
234
|
+
* `index`, and `deque`.
|
|
235
|
+
* @returns The `filter` method is returning a new `Queue` object that contains only the elements
|
|
236
|
+
* that satisfy the given `predicate` function.
|
|
237
|
+
*/
|
|
238
|
+
filter(predicate: (element: E, index: number, queue: this) => boolean): Queue<E>;
|
|
239
|
+
/**
|
|
240
|
+
* Time Complexity: O(n)
|
|
241
|
+
* Space Complexity: O(n)
|
|
242
|
+
*/
|
|
243
|
+
/**
|
|
244
|
+
* Time Complexity: O(n)
|
|
245
|
+
* Space Complexity: O(n)
|
|
246
|
+
*
|
|
247
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
248
|
+
* returning a new deque with the results.
|
|
249
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
250
|
+
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
251
|
+
*/
|
|
252
|
+
map<T>(callback: (element: E, index: number, queue: this) => T): Queue<T>;
|
|
253
|
+
reduce<T>(callback: (accumulator: T, element: E, index: number, queue: this) => T, initialValue: T): T;
|
|
209
254
|
}
|
|
@@ -267,4 +267,81 @@ export class Queue {
|
|
|
267
267
|
yield item;
|
|
268
268
|
}
|
|
269
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Time Complexity: O(n)
|
|
272
|
+
* Space Complexity: O(1)
|
|
273
|
+
*/
|
|
274
|
+
/**
|
|
275
|
+
* Time Complexity: O(n)
|
|
276
|
+
* Space Complexity: O(1)
|
|
277
|
+
*
|
|
278
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
279
|
+
* each element.
|
|
280
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
281
|
+
* deque. It takes three parameters:
|
|
282
|
+
*/
|
|
283
|
+
forEach(callback) {
|
|
284
|
+
let index = 0;
|
|
285
|
+
for (const el of this) {
|
|
286
|
+
callback(el, index, this);
|
|
287
|
+
index++;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Time Complexity: O(n)
|
|
292
|
+
* Space Complexity: O(n)
|
|
293
|
+
*/
|
|
294
|
+
/**
|
|
295
|
+
* Time Complexity: O(n)
|
|
296
|
+
* Space Complexity: O(n)
|
|
297
|
+
*
|
|
298
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
299
|
+
* predicate function.
|
|
300
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
301
|
+
* `index`, and `deque`.
|
|
302
|
+
* @returns The `filter` method is returning a new `Queue` object that contains only the elements
|
|
303
|
+
* that satisfy the given `predicate` function.
|
|
304
|
+
*/
|
|
305
|
+
filter(predicate) {
|
|
306
|
+
const newDeque = new Queue([]);
|
|
307
|
+
let index = 0;
|
|
308
|
+
for (const el of this) {
|
|
309
|
+
if (predicate(el, index, this)) {
|
|
310
|
+
newDeque.push(el);
|
|
311
|
+
}
|
|
312
|
+
index++;
|
|
313
|
+
}
|
|
314
|
+
return newDeque;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Time Complexity: O(n)
|
|
318
|
+
* Space Complexity: O(n)
|
|
319
|
+
*/
|
|
320
|
+
/**
|
|
321
|
+
* Time Complexity: O(n)
|
|
322
|
+
* Space Complexity: O(n)
|
|
323
|
+
*
|
|
324
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
325
|
+
* returning a new deque with the results.
|
|
326
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
327
|
+
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
328
|
+
*/
|
|
329
|
+
map(callback) {
|
|
330
|
+
const newDeque = new Queue([]);
|
|
331
|
+
let index = 0;
|
|
332
|
+
for (const el of this) {
|
|
333
|
+
newDeque.push(callback(el, index, this));
|
|
334
|
+
index++;
|
|
335
|
+
}
|
|
336
|
+
return newDeque;
|
|
337
|
+
}
|
|
338
|
+
reduce(callback, initialValue) {
|
|
339
|
+
let accumulator = initialValue;
|
|
340
|
+
let index = 0;
|
|
341
|
+
for (const el of this) {
|
|
342
|
+
accumulator = callback(accumulator, el, index, this);
|
|
343
|
+
index++;
|
|
344
|
+
}
|
|
345
|
+
return accumulator;
|
|
346
|
+
}
|
|
270
347
|
}
|
|
@@ -17,6 +17,11 @@ export declare class Stack<E = any> {
|
|
|
17
17
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
18
18
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
19
19
|
*/
|
|
20
|
+
/**
|
|
21
|
+
* The size() function returns the number of elements in an array.
|
|
22
|
+
* @returns The size of the elements array.
|
|
23
|
+
*/
|
|
24
|
+
get size(): number;
|
|
20
25
|
/**
|
|
21
26
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
22
27
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
@@ -32,11 +37,6 @@ export declare class Stack<E = any> {
|
|
|
32
37
|
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
33
38
|
*/
|
|
34
39
|
isEmpty(): boolean;
|
|
35
|
-
/**
|
|
36
|
-
* The size() function returns the number of elements in an array.
|
|
37
|
-
* @returns The size of the elements array.
|
|
38
|
-
*/
|
|
39
|
-
size(): number;
|
|
40
40
|
/**
|
|
41
41
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
42
42
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -103,4 +103,17 @@ export declare class Stack<E = any> {
|
|
|
103
103
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
104
104
|
*/
|
|
105
105
|
clone(): Stack<E>;
|
|
106
|
+
/**
|
|
107
|
+
* Custom iterator for the Stack class.
|
|
108
|
+
* @returns An iterator object.
|
|
109
|
+
*/
|
|
110
|
+
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
111
|
+
/**
|
|
112
|
+
* Applies a function to each element of the stack.
|
|
113
|
+
* @param {function(E): void} callback - A function to apply to each element.
|
|
114
|
+
*/
|
|
115
|
+
forEach(callback: (element: E, index: number, stack: this) => void): void;
|
|
116
|
+
filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E>;
|
|
117
|
+
map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T>;
|
|
118
|
+
reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T;
|
|
106
119
|
}
|
|
@@ -21,6 +21,13 @@ export class Stack {
|
|
|
21
21
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
22
22
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
23
23
|
*/
|
|
24
|
+
/**
|
|
25
|
+
* The size() function returns the number of elements in an array.
|
|
26
|
+
* @returns The size of the elements array.
|
|
27
|
+
*/
|
|
28
|
+
get size() {
|
|
29
|
+
return this.elements.length;
|
|
30
|
+
}
|
|
24
31
|
/**
|
|
25
32
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
26
33
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
@@ -40,13 +47,6 @@ export class Stack {
|
|
|
40
47
|
isEmpty() {
|
|
41
48
|
return this.elements.length === 0;
|
|
42
49
|
}
|
|
43
|
-
/**
|
|
44
|
-
* The size() function returns the number of elements in an array.
|
|
45
|
-
* @returns The size of the elements array.
|
|
46
|
-
*/
|
|
47
|
-
size() {
|
|
48
|
-
return this.elements.length;
|
|
49
|
-
}
|
|
50
50
|
/**
|
|
51
51
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
52
52
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -130,4 +130,53 @@ export class Stack {
|
|
|
130
130
|
clone() {
|
|
131
131
|
return new Stack(this.elements.slice());
|
|
132
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Custom iterator for the Stack class.
|
|
135
|
+
* @returns An iterator object.
|
|
136
|
+
*/
|
|
137
|
+
*[Symbol.iterator]() {
|
|
138
|
+
for (let i = this.elements.length - 1; i >= 0; i--) {
|
|
139
|
+
yield this.elements[i];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Applies a function to each element of the stack.
|
|
144
|
+
* @param {function(E): void} callback - A function to apply to each element.
|
|
145
|
+
*/
|
|
146
|
+
forEach(callback) {
|
|
147
|
+
let index = 0;
|
|
148
|
+
for (const el of this) {
|
|
149
|
+
callback(el, index, this);
|
|
150
|
+
index++;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
filter(predicate) {
|
|
154
|
+
const newStack = new Stack();
|
|
155
|
+
let index = 0;
|
|
156
|
+
for (const el of this) {
|
|
157
|
+
if (predicate(el, index, this)) {
|
|
158
|
+
newStack.push(el);
|
|
159
|
+
}
|
|
160
|
+
index++;
|
|
161
|
+
}
|
|
162
|
+
return newStack;
|
|
163
|
+
}
|
|
164
|
+
map(callback) {
|
|
165
|
+
const newStack = new Stack();
|
|
166
|
+
let index = 0;
|
|
167
|
+
for (const el of this) {
|
|
168
|
+
newStack.push(callback(el, index, this));
|
|
169
|
+
index++;
|
|
170
|
+
}
|
|
171
|
+
return newStack;
|
|
172
|
+
}
|
|
173
|
+
reduce(callback, initialValue) {
|
|
174
|
+
let accumulator = initialValue;
|
|
175
|
+
let index = 0;
|
|
176
|
+
for (const el of this) {
|
|
177
|
+
accumulator = callback(accumulator, el, index, this);
|
|
178
|
+
index++;
|
|
179
|
+
}
|
|
180
|
+
return accumulator;
|
|
181
|
+
}
|
|
133
182
|
}
|
|
@@ -140,6 +140,11 @@ export declare class Trie {
|
|
|
140
140
|
* @returns {string[]} an array of strings.
|
|
141
141
|
*/
|
|
142
142
|
getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
|
|
143
|
+
[Symbol.iterator](): IterableIterator<string>;
|
|
144
|
+
forEach(callback: (word: string, index: number, trie: this) => void): void;
|
|
145
|
+
filter(predicate: (word: string, index: number, trie: this) => boolean): string[];
|
|
146
|
+
map(callback: (word: string, index: number, trie: this) => string): Trie;
|
|
147
|
+
reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T;
|
|
143
148
|
/**
|
|
144
149
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
145
150
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -306,6 +306,53 @@ export class Trie {
|
|
|
306
306
|
dfs(startNode, prefix);
|
|
307
307
|
return words;
|
|
308
308
|
}
|
|
309
|
+
*[Symbol.iterator]() {
|
|
310
|
+
function* _dfs(node, path) {
|
|
311
|
+
if (node.isEnd) {
|
|
312
|
+
yield path;
|
|
313
|
+
}
|
|
314
|
+
for (const [char, childNode] of node.children) {
|
|
315
|
+
yield* _dfs(childNode, path + char);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
yield* _dfs(this.root, '');
|
|
319
|
+
}
|
|
320
|
+
forEach(callback) {
|
|
321
|
+
let index = 0;
|
|
322
|
+
for (const word of this) {
|
|
323
|
+
callback(word, index, this);
|
|
324
|
+
index++;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
filter(predicate) {
|
|
328
|
+
const results = [];
|
|
329
|
+
let index = 0;
|
|
330
|
+
for (const word of this) {
|
|
331
|
+
if (predicate(word, index, this)) {
|
|
332
|
+
results.push(word);
|
|
333
|
+
}
|
|
334
|
+
index++;
|
|
335
|
+
}
|
|
336
|
+
return results;
|
|
337
|
+
}
|
|
338
|
+
map(callback) {
|
|
339
|
+
const newTrie = new Trie();
|
|
340
|
+
let index = 0;
|
|
341
|
+
for (const word of this) {
|
|
342
|
+
newTrie.add(callback(word, index, this));
|
|
343
|
+
index++;
|
|
344
|
+
}
|
|
345
|
+
return newTrie;
|
|
346
|
+
}
|
|
347
|
+
reduce(callback, initialValue) {
|
|
348
|
+
let accumulator = initialValue;
|
|
349
|
+
let index = 0;
|
|
350
|
+
for (const word of this) {
|
|
351
|
+
accumulator = callback(accumulator, word, index, this);
|
|
352
|
+
index++;
|
|
353
|
+
}
|
|
354
|
+
return accumulator;
|
|
355
|
+
}
|
|
309
356
|
/**
|
|
310
357
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
311
358
|
* Space Complexity: O(1) - Constant space.
|