bst-typed 1.48.1 → 1.48.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.
- package/dist/data-structures/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +36 -69
- package/dist/data-structures/binary-tree/binary-tree.js +78 -129
- package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/data-structures/graph/abstract-graph.js +50 -27
- package/dist/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/data-structures/hash/hash-map.js +69 -173
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/data-structures/queue/deque.d.ts +29 -51
- package/dist/data-structures/queue/deque.js +36 -71
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/binary-tree.ts +82 -138
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +76 -185
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
- package/src/data-structures/queue/deque.ts +40 -82
- package/src/data-structures/queue/queue.ts +72 -87
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/index.ts +1 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
4
|
+
const base_1 = require("../base");
|
|
4
5
|
/**
|
|
5
6
|
* data-structure-typed
|
|
6
7
|
*
|
|
@@ -20,11 +21,12 @@ class SinglyLinkedListNode {
|
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
23
|
-
class SinglyLinkedList {
|
|
24
|
+
class SinglyLinkedList extends base_1.IterableElementBase {
|
|
24
25
|
/**
|
|
25
26
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
26
27
|
*/
|
|
27
28
|
constructor(elements) {
|
|
29
|
+
super();
|
|
28
30
|
this._head = undefined;
|
|
29
31
|
this._tail = undefined;
|
|
30
32
|
this._length = 0;
|
|
@@ -609,54 +611,31 @@ class SinglyLinkedList {
|
|
|
609
611
|
return count;
|
|
610
612
|
}
|
|
611
613
|
/**
|
|
612
|
-
*
|
|
613
|
-
*/
|
|
614
|
-
*[Symbol.iterator]() {
|
|
615
|
-
let current = this.head;
|
|
616
|
-
while (current) {
|
|
617
|
-
yield current.value;
|
|
618
|
-
current = current.next;
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
623
|
-
* Space Complexity: O(1)
|
|
624
|
-
*/
|
|
625
|
-
/**
|
|
626
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
627
|
-
* Space Complexity: O(1)
|
|
628
|
-
*
|
|
629
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
630
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
631
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
632
|
-
* current node in the linked list.
|
|
633
|
-
*/
|
|
634
|
-
forEach(callback) {
|
|
635
|
-
let index = 0;
|
|
636
|
-
for (const el of this) {
|
|
637
|
-
callback(el, index, this);
|
|
638
|
-
index++;
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
/**
|
|
642
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
614
|
+
* Time Complexity: O(n)
|
|
643
615
|
* Space Complexity: O(n)
|
|
644
616
|
*/
|
|
645
617
|
/**
|
|
646
|
-
* Time Complexity: O(n)
|
|
618
|
+
* Time Complexity: O(n)
|
|
647
619
|
* Space Complexity: O(n)
|
|
648
620
|
*
|
|
649
|
-
* The `filter` function
|
|
650
|
-
*
|
|
651
|
-
*
|
|
652
|
-
*
|
|
653
|
-
*
|
|
654
|
-
|
|
655
|
-
|
|
621
|
+
* The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current
|
|
622
|
+
* list and applying a callback function to each element to determine if it should be included in the
|
|
623
|
+
* filtered list.
|
|
624
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
625
|
+
* list. It takes three arguments: the current element, the index of the current element, and the
|
|
626
|
+
* list itself. The callback function should return a boolean value indicating whether the current
|
|
627
|
+
* element should be included in the filtered list or not
|
|
628
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
629
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
630
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
631
|
+
* @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
|
|
632
|
+
* elements that pass the filter condition specified by the `callback` function.
|
|
633
|
+
*/
|
|
634
|
+
filter(callback, thisArg) {
|
|
656
635
|
const filteredList = new SinglyLinkedList();
|
|
657
636
|
let index = 0;
|
|
658
637
|
for (const current of this) {
|
|
659
|
-
if (callback(current, index, this)) {
|
|
638
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
660
639
|
filteredList.push(current);
|
|
661
640
|
}
|
|
662
641
|
index++;
|
|
@@ -668,21 +647,24 @@ class SinglyLinkedList {
|
|
|
668
647
|
* Space Complexity: O(n)
|
|
669
648
|
*/
|
|
670
649
|
/**
|
|
671
|
-
* Time Complexity: O(n)
|
|
650
|
+
* Time Complexity: O(n)
|
|
672
651
|
* Space Complexity: O(n)
|
|
673
652
|
*
|
|
674
|
-
* The `map` function
|
|
675
|
-
*
|
|
676
|
-
* @param callback - The callback parameter is a function that
|
|
677
|
-
* the
|
|
678
|
-
*
|
|
679
|
-
*
|
|
680
|
-
|
|
681
|
-
|
|
653
|
+
* The `map` function creates a new SinglyLinkedList by applying a callback function to each element
|
|
654
|
+
* of the original list.
|
|
655
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
656
|
+
* the linked list. It takes three arguments:
|
|
657
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
658
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
659
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
660
|
+
* @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
|
|
661
|
+
* of applying the provided `callback` function to each element in the original list.
|
|
662
|
+
*/
|
|
663
|
+
map(callback, thisArg) {
|
|
682
664
|
const mappedList = new SinglyLinkedList();
|
|
683
665
|
let index = 0;
|
|
684
666
|
for (const current of this) {
|
|
685
|
-
mappedList.push(callback(current, index, this));
|
|
667
|
+
mappedList.push(callback.call(thisArg, current, index, this));
|
|
686
668
|
index++;
|
|
687
669
|
}
|
|
688
670
|
return mappedList;
|
|
@@ -691,30 +673,15 @@ class SinglyLinkedList {
|
|
|
691
673
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
692
674
|
* Space Complexity: O(n)
|
|
693
675
|
*/
|
|
694
|
-
/**
|
|
695
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
696
|
-
* Space Complexity: O(n)
|
|
697
|
-
*
|
|
698
|
-
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
699
|
-
* single value.
|
|
700
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
701
|
-
* used to perform a specific operation on each element of the linked list.
|
|
702
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
703
|
-
* point for the reduction operation.
|
|
704
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
705
|
-
* elements in the linked list.
|
|
706
|
-
*/
|
|
707
|
-
reduce(callback, initialValue) {
|
|
708
|
-
let accumulator = initialValue;
|
|
709
|
-
let index = 0;
|
|
710
|
-
for (const current of this) {
|
|
711
|
-
accumulator = callback(accumulator, current, index, this);
|
|
712
|
-
index++;
|
|
713
|
-
}
|
|
714
|
-
return accumulator;
|
|
715
|
-
}
|
|
716
676
|
print() {
|
|
717
677
|
console.log([...this]);
|
|
718
678
|
}
|
|
679
|
+
*_getIterator() {
|
|
680
|
+
let current = this.head;
|
|
681
|
+
while (current) {
|
|
682
|
+
yield current.value;
|
|
683
|
+
current = current.next;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
719
686
|
}
|
|
720
687
|
exports.SinglyLinkedList = SinglyLinkedList;
|
|
@@ -5,14 +5,15 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { IterableWithSizeOrLength } from "../../types";
|
|
8
|
+
import { ElementCallback, IterableWithSizeOrLength } from "../../types";
|
|
9
|
+
import { IterableElementBase } from "../base";
|
|
9
10
|
/**
|
|
10
11
|
* Deque can provide random access with O(1) time complexity
|
|
11
12
|
* Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
|
|
12
13
|
* Deque may experience performance jitter, but DoublyLinkedList will not
|
|
13
14
|
* Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
|
|
14
15
|
*/
|
|
15
|
-
export declare class Deque<E> {
|
|
16
|
+
export declare class Deque<E> extends IterableElementBase<E> {
|
|
16
17
|
protected _bucketFirst: number;
|
|
17
18
|
protected _firstInBucket: number;
|
|
18
19
|
protected _bucketLast: number;
|
|
@@ -354,32 +355,6 @@ export declare class Deque<E> {
|
|
|
354
355
|
* @returns The `toArray()` method is returning an array of elements of type `E`.
|
|
355
356
|
*/
|
|
356
357
|
toArray(): E[];
|
|
357
|
-
/**
|
|
358
|
-
* Time Complexity: O(n)
|
|
359
|
-
* Space Complexity: O(1)
|
|
360
|
-
*/
|
|
361
|
-
/**
|
|
362
|
-
* Time Complexity: O(n)
|
|
363
|
-
* Space Complexity: O(1)
|
|
364
|
-
*
|
|
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:
|
|
381
|
-
*/
|
|
382
|
-
forEach(callback: (element: E, index: number, deque: this) => void): void;
|
|
383
358
|
/**
|
|
384
359
|
* Time Complexity: O(n)
|
|
385
360
|
* Space Complexity: O(n)
|
|
@@ -388,14 +363,19 @@ export declare class Deque<E> {
|
|
|
388
363
|
* Time Complexity: O(n)
|
|
389
364
|
* Space Complexity: O(n)
|
|
390
365
|
*
|
|
391
|
-
* The `filter` function creates a new deque containing
|
|
392
|
-
* predicate function.
|
|
393
|
-
* @param predicate - The `predicate` parameter is a function that takes three arguments:
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
366
|
+
* The `filter` function creates a new deque containing elements from the original deque that satisfy
|
|
367
|
+
* a given predicate function.
|
|
368
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
369
|
+
* the current element being iterated over, the index of the current element, and the deque itself.
|
|
370
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
371
|
+
* deque or not.
|
|
372
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
373
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
374
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
375
|
+
* @returns The `filter` method is returning a new `Deque` object that contains the elements that
|
|
376
|
+
* satisfy the given predicate function.
|
|
397
377
|
*/
|
|
398
|
-
filter(predicate:
|
|
378
|
+
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Deque<E>;
|
|
399
379
|
/**
|
|
400
380
|
* Time Complexity: O(n)
|
|
401
381
|
* Space Complexity: O(n)
|
|
@@ -404,31 +384,29 @@ export declare class Deque<E> {
|
|
|
404
384
|
* Time Complexity: O(n)
|
|
405
385
|
* Space Complexity: O(n)
|
|
406
386
|
*
|
|
407
|
-
* The `map` function
|
|
408
|
-
*
|
|
409
|
-
* @param callback - The `callback` parameter is a function that
|
|
410
|
-
*
|
|
387
|
+
* The `map` function creates a new Deque by applying a callback function to each element of the
|
|
388
|
+
* original Deque.
|
|
389
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
390
|
+
* the deque. It takes three arguments:
|
|
391
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
392
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
393
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
394
|
+
* @returns a new Deque object with the mapped values.
|
|
411
395
|
*/
|
|
412
|
-
map<T>(callback:
|
|
396
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T>;
|
|
413
397
|
/**
|
|
414
398
|
* Time Complexity: O(n)
|
|
415
|
-
* Space Complexity: O(
|
|
399
|
+
* Space Complexity: O(n)
|
|
416
400
|
*/
|
|
401
|
+
print(): void;
|
|
417
402
|
/**
|
|
418
403
|
* Time Complexity: O(n)
|
|
419
404
|
* Space Complexity: O(1)
|
|
420
405
|
*
|
|
421
|
-
* The
|
|
422
|
-
*
|
|
423
|
-
* @param callback - The `callback` parameter is a function that takes four arguments:
|
|
424
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
425
|
-
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
426
|
-
* the elements of the deque.
|
|
427
|
-
* @returns the final value of the accumulator after iterating over all elements in the deque and
|
|
428
|
-
* applying the callback function to each element.
|
|
406
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
407
|
+
* object to be iterated over using a for...of loop.
|
|
429
408
|
*/
|
|
430
|
-
|
|
431
|
-
print(): void;
|
|
409
|
+
protected _getIterator(): Generator<E, void, unknown>;
|
|
432
410
|
/**
|
|
433
411
|
* Time Complexity: O(n)
|
|
434
412
|
* Space Complexity: O(n)
|
|
@@ -9,13 +9,14 @@
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.ObjectDeque = exports.Deque = void 0;
|
|
11
11
|
const utils_1 = require("../../utils");
|
|
12
|
+
const base_1 = require("../base");
|
|
12
13
|
/**
|
|
13
14
|
* Deque can provide random access with O(1) time complexity
|
|
14
15
|
* Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
|
|
15
16
|
* Deque may experience performance jitter, but DoublyLinkedList will not
|
|
16
17
|
* Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
|
|
17
18
|
*/
|
|
18
|
-
class Deque {
|
|
19
|
+
class Deque extends base_1.IterableElementBase {
|
|
19
20
|
/**
|
|
20
21
|
* The constructor initializes a data structure with a specified bucket size and populates it with
|
|
21
22
|
* elements from an iterable.
|
|
@@ -26,6 +27,7 @@ class Deque {
|
|
|
26
27
|
* stored in each bucket. It determines the size of each bucket in the data structure.
|
|
27
28
|
*/
|
|
28
29
|
constructor(elements = [], bucketSize = (1 << 12)) {
|
|
30
|
+
super();
|
|
29
31
|
this._bucketFirst = 0;
|
|
30
32
|
this._firstInBucket = 0;
|
|
31
33
|
this._bucketLast = 0;
|
|
@@ -651,42 +653,6 @@ class Deque {
|
|
|
651
653
|
}
|
|
652
654
|
return arr;
|
|
653
655
|
}
|
|
654
|
-
/**
|
|
655
|
-
* Time Complexity: O(n)
|
|
656
|
-
* Space Complexity: O(1)
|
|
657
|
-
*/
|
|
658
|
-
/**
|
|
659
|
-
* Time Complexity: O(n)
|
|
660
|
-
* Space Complexity: O(1)
|
|
661
|
-
*
|
|
662
|
-
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
663
|
-
* object to be iterated over using a for...of loop.
|
|
664
|
-
*/
|
|
665
|
-
*[Symbol.iterator]() {
|
|
666
|
-
for (let i = 0; i < this.size; ++i) {
|
|
667
|
-
yield this.getAt(i);
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
/**
|
|
671
|
-
* Time Complexity: O(n)
|
|
672
|
-
* Space Complexity: O(1)
|
|
673
|
-
*/
|
|
674
|
-
/**
|
|
675
|
-
* Time Complexity: O(n)
|
|
676
|
-
* Space Complexity: O(1)
|
|
677
|
-
*
|
|
678
|
-
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
679
|
-
* each element.
|
|
680
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
681
|
-
* deque. It takes three parameters:
|
|
682
|
-
*/
|
|
683
|
-
forEach(callback) {
|
|
684
|
-
let index = 0;
|
|
685
|
-
for (const el of this) {
|
|
686
|
-
callback(el, index, this);
|
|
687
|
-
index++;
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
656
|
/**
|
|
691
657
|
* Time Complexity: O(n)
|
|
692
658
|
* Space Complexity: O(n)
|
|
@@ -695,18 +661,23 @@ class Deque {
|
|
|
695
661
|
* Time Complexity: O(n)
|
|
696
662
|
* Space Complexity: O(n)
|
|
697
663
|
*
|
|
698
|
-
* The `filter` function creates a new deque containing
|
|
699
|
-
* predicate function.
|
|
700
|
-
* @param predicate - The `predicate` parameter is a function that takes three arguments:
|
|
701
|
-
*
|
|
702
|
-
*
|
|
703
|
-
*
|
|
704
|
-
|
|
705
|
-
|
|
664
|
+
* The `filter` function creates a new deque containing elements from the original deque that satisfy
|
|
665
|
+
* a given predicate function.
|
|
666
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
667
|
+
* the current element being iterated over, the index of the current element, and the deque itself.
|
|
668
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
669
|
+
* deque or not.
|
|
670
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
671
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
672
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
673
|
+
* @returns The `filter` method is returning a new `Deque` object that contains the elements that
|
|
674
|
+
* satisfy the given predicate function.
|
|
675
|
+
*/
|
|
676
|
+
filter(predicate, thisArg) {
|
|
706
677
|
const newDeque = new Deque([], this._bucketSize);
|
|
707
678
|
let index = 0;
|
|
708
679
|
for (const el of this) {
|
|
709
|
-
if (predicate(el, index, this)) {
|
|
680
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
710
681
|
newDeque.push(el);
|
|
711
682
|
}
|
|
712
683
|
index++;
|
|
@@ -721,48 +692,42 @@ class Deque {
|
|
|
721
692
|
* Time Complexity: O(n)
|
|
722
693
|
* Space Complexity: O(n)
|
|
723
694
|
*
|
|
724
|
-
* The `map` function
|
|
725
|
-
*
|
|
726
|
-
* @param callback - The `callback` parameter is a function that
|
|
727
|
-
*
|
|
695
|
+
* The `map` function creates a new Deque by applying a callback function to each element of the
|
|
696
|
+
* original Deque.
|
|
697
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
698
|
+
* the deque. It takes three arguments:
|
|
699
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
700
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
701
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
702
|
+
* @returns a new Deque object with the mapped values.
|
|
728
703
|
*/
|
|
729
|
-
map(callback) {
|
|
704
|
+
map(callback, thisArg) {
|
|
730
705
|
const newDeque = new Deque([], this._bucketSize);
|
|
731
706
|
let index = 0;
|
|
732
707
|
for (const el of this) {
|
|
733
|
-
newDeque.push(callback(el, index, this));
|
|
708
|
+
newDeque.push(callback.call(thisArg, el, index, this));
|
|
734
709
|
index++;
|
|
735
710
|
}
|
|
736
711
|
return newDeque;
|
|
737
712
|
}
|
|
738
713
|
/**
|
|
739
714
|
* Time Complexity: O(n)
|
|
740
|
-
* Space Complexity: O(
|
|
715
|
+
* Space Complexity: O(n)
|
|
741
716
|
*/
|
|
717
|
+
print() {
|
|
718
|
+
console.log([...this]);
|
|
719
|
+
}
|
|
742
720
|
/**
|
|
743
721
|
* Time Complexity: O(n)
|
|
744
722
|
* Space Complexity: O(1)
|
|
745
723
|
*
|
|
746
|
-
* The
|
|
747
|
-
*
|
|
748
|
-
* @param callback - The `callback` parameter is a function that takes four arguments:
|
|
749
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
750
|
-
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
751
|
-
* the elements of the deque.
|
|
752
|
-
* @returns the final value of the accumulator after iterating over all elements in the deque and
|
|
753
|
-
* applying the callback function to each element.
|
|
724
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
725
|
+
* object to be iterated over using a for...of loop.
|
|
754
726
|
*/
|
|
755
|
-
|
|
756
|
-
let
|
|
757
|
-
|
|
758
|
-
for (const el of this) {
|
|
759
|
-
accumulator = callback(accumulator, el, index, this);
|
|
760
|
-
index++;
|
|
727
|
+
*_getIterator() {
|
|
728
|
+
for (let i = 0; i < this.size; ++i) {
|
|
729
|
+
yield this.getAt(i);
|
|
761
730
|
}
|
|
762
|
-
return accumulator;
|
|
763
|
-
}
|
|
764
|
-
print() {
|
|
765
|
-
console.log([...this]);
|
|
766
731
|
}
|
|
767
732
|
/**
|
|
768
733
|
* Time Complexity: O(n)
|
|
@@ -4,29 +4,9 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
import { SinglyLinkedList } from '../linked-list';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
11
|
-
*/
|
|
12
|
-
enqueue(value: E): void;
|
|
13
|
-
/**
|
|
14
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
15
|
-
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
16
|
-
*/
|
|
17
|
-
dequeue(): E | undefined;
|
|
18
|
-
/**
|
|
19
|
-
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
20
|
-
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
21
|
-
*/
|
|
22
|
-
getFirst(): E | undefined;
|
|
23
|
-
/**
|
|
24
|
-
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
25
|
-
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
26
|
-
*/
|
|
27
|
-
peek(): E | undefined;
|
|
28
|
-
}
|
|
29
|
-
export declare class Queue<E = any> {
|
|
7
|
+
import { IterableElementBase } from "../base";
|
|
8
|
+
import { ElementCallback } from "../../types";
|
|
9
|
+
export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
30
10
|
/**
|
|
31
11
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
32
12
|
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
|
@@ -206,21 +186,27 @@ export declare class Queue<E = any> {
|
|
|
206
186
|
*/
|
|
207
187
|
clone(): Queue<E>;
|
|
208
188
|
print(): void;
|
|
209
|
-
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
210
189
|
/**
|
|
211
190
|
* Time Complexity: O(n)
|
|
212
|
-
* Space Complexity: O(
|
|
191
|
+
* Space Complexity: O(n)
|
|
213
192
|
*/
|
|
214
193
|
/**
|
|
215
194
|
* Time Complexity: O(n)
|
|
216
|
-
* Space Complexity: O(
|
|
195
|
+
* Space Complexity: O(n)
|
|
217
196
|
*
|
|
218
|
-
* The `
|
|
219
|
-
*
|
|
220
|
-
* @param
|
|
221
|
-
*
|
|
222
|
-
|
|
223
|
-
|
|
197
|
+
* The `filter` function creates a new `Queue` object containing elements from the original `Queue`
|
|
198
|
+
* that satisfy a given predicate function.
|
|
199
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
200
|
+
* the current element being iterated over, the index of the current element, and the queue itself.
|
|
201
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
202
|
+
* queue or not.
|
|
203
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
204
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
205
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
206
|
+
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
|
207
|
+
* satisfy the given predicate function.
|
|
208
|
+
*/
|
|
209
|
+
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Queue<E>;
|
|
224
210
|
/**
|
|
225
211
|
* Time Complexity: O(n)
|
|
226
212
|
* Space Complexity: O(n)
|
|
@@ -229,27 +215,42 @@ export declare class Queue<E = any> {
|
|
|
229
215
|
* Time Complexity: O(n)
|
|
230
216
|
* Space Complexity: O(n)
|
|
231
217
|
*
|
|
232
|
-
* The `
|
|
233
|
-
*
|
|
234
|
-
* @param
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
218
|
+
* The `map` function takes a callback function and applies it to each element in the queue,
|
|
219
|
+
* returning a new queue with the results.
|
|
220
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
221
|
+
* queue. It takes three arguments: the current element, the index of the current element, and the
|
|
222
|
+
* queue itself. The callback function should return a new value that will be added to the new queue.
|
|
223
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
224
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
225
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
226
|
+
* @returns The `map` function is returning a new `Queue` object with the transformed elements.
|
|
238
227
|
*/
|
|
239
|
-
|
|
228
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Queue<T>;
|
|
240
229
|
/**
|
|
241
230
|
* Time Complexity: O(n)
|
|
242
231
|
* Space Complexity: O(n)
|
|
243
232
|
*/
|
|
233
|
+
protected _getIterator(): Generator<E, void, unknown>;
|
|
234
|
+
}
|
|
235
|
+
export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
244
236
|
/**
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
249
|
-
* returning a new deque with the results.
|
|
250
|
-
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
251
|
-
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
237
|
+
* The enqueue function adds a value to the end of an array.
|
|
238
|
+
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
252
239
|
*/
|
|
253
|
-
|
|
254
|
-
|
|
240
|
+
enqueue(value: E): void;
|
|
241
|
+
/**
|
|
242
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
243
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
244
|
+
*/
|
|
245
|
+
dequeue(): E | undefined;
|
|
246
|
+
/**
|
|
247
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
248
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
249
|
+
*/
|
|
250
|
+
getFirst(): E | undefined;
|
|
251
|
+
/**
|
|
252
|
+
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
253
|
+
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
254
|
+
*/
|
|
255
|
+
peek(): E | undefined;
|
|
255
256
|
}
|