directed-graph-typed 1.48.0 → 1.49.0
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/avl-tree.d.ts +28 -19
- package/dist/data-structures/binary-tree/avl-tree.js +22 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -152
- package/dist/data-structures/binary-tree/binary-tree.js +241 -215
- package/dist/data-structures/binary-tree/bst.d.ts +64 -48
- package/dist/data-structures/binary-tree/bst.js +94 -65
- package/dist/data-structures/binary-tree/rb-tree.d.ts +39 -39
- package/dist/data-structures/binary-tree/rb-tree.js +42 -49
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +60 -34
- package/dist/data-structures/binary-tree/tree-multimap.js +59 -27
- package/dist/data-structures/graph/abstract-graph.d.ts +92 -53
- package/dist/data-structures/graph/abstract-graph.js +130 -103
- package/dist/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/data-structures/graph/directed-graph.js +111 -65
- package/dist/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/data-structures/graph/map-graph.js +8 -8
- package/dist/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/data-structures/graph/undirected-graph.js +117 -54
- package/dist/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/data-structures/hash/hash-map.js +314 -82
- 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 +42 -55
- package/dist/data-structures/linked-list/doubly-linked-list.js +50 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -55
- package/dist/data-structures/linked-list/singly-linked-list.js +44 -77
- package/dist/data-structures/queue/deque.d.ts +35 -167
- package/dist/data-structures/queue/deque.js +43 -249
- 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/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +11 -8
- package/dist/types/common.js +6 -1
- 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/binary-tree/avl-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -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/avl-tree.ts +37 -25
- package/src/data-structures/binary-tree/binary-tree.ts +336 -296
- package/src/data-structures/binary-tree/bst.ts +135 -89
- package/src/data-structures/binary-tree/rb-tree.ts +60 -69
- package/src/data-structures/binary-tree/tree-multimap.ts +86 -49
- package/src/data-structures/graph/abstract-graph.ts +136 -104
- package/src/data-structures/graph/directed-graph.ts +114 -65
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +124 -56
- package/src/data-structures/hash/hash-map.ts +335 -84
- 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 +54 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -84
- package/src/data-structures/queue/deque.ts +43 -275
- package/src/data-structures/queue/queue.ts +71 -86
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- 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/binary-tree/avl-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/index.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
import { IterableWithSizeOrLength } from "../../types";
|
|
10
|
+
import { ElementCallback, IterableWithSizeOrLength } from "../../types";
|
|
11
11
|
import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
|
12
|
+
import { IterableElementBase } from "../base";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Deque can provide random access with O(1) time complexity
|
|
@@ -17,7 +18,7 @@ import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
|
|
17
18
|
* Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
|
|
18
19
|
*/
|
|
19
20
|
|
|
20
|
-
export class Deque<E> {
|
|
21
|
+
export class Deque<E> extends IterableElementBase<E> {
|
|
21
22
|
protected _bucketFirst = 0;
|
|
22
23
|
protected _firstInBucket = 0;
|
|
23
24
|
protected _bucketLast = 0;
|
|
@@ -35,7 +36,7 @@ export class Deque<E> {
|
|
|
35
36
|
* stored in each bucket. It determines the size of each bucket in the data structure.
|
|
36
37
|
*/
|
|
37
38
|
constructor(elements: IterableWithSizeOrLength<E> = [], bucketSize = (1 << 12)) {
|
|
38
|
-
|
|
39
|
+
super();
|
|
39
40
|
let _size: number;
|
|
40
41
|
if ('length' in elements) {
|
|
41
42
|
if (elements.length instanceof Function) _size = elements.length(); else _size = elements.length;
|
|
@@ -119,10 +120,10 @@ export class Deque<E> {
|
|
|
119
120
|
* Time Complexity: O(1) - Removes the last element.
|
|
120
121
|
* Space Complexity: O(1) - Operates in-place.
|
|
121
122
|
*
|
|
122
|
-
* The function "
|
|
123
|
+
* The function "pollLast" removes and returns the last element of an array.
|
|
123
124
|
* @returns The last element of the array is being returned.
|
|
124
125
|
*/
|
|
125
|
-
|
|
126
|
+
pollLast(): E | undefined {
|
|
126
127
|
return this.pop();
|
|
127
128
|
}
|
|
128
129
|
|
|
@@ -142,11 +143,11 @@ export class Deque<E> {
|
|
|
142
143
|
* Time Complexity: O(1) - Removes the first element.
|
|
143
144
|
* Space Complexity: O(1) - In-place operation.
|
|
144
145
|
*
|
|
145
|
-
* The function "
|
|
146
|
-
* @returns The method `
|
|
146
|
+
* The function "pollFirst" removes and returns the first element of an array.
|
|
147
|
+
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
147
148
|
* from the beginning. If the array is empty, it will return `undefined`.
|
|
148
149
|
*/
|
|
149
|
-
|
|
150
|
+
pollFirst(): E | undefined {
|
|
150
151
|
return this.shift();
|
|
151
152
|
}
|
|
152
153
|
|
|
@@ -699,67 +700,31 @@ export class Deque<E> {
|
|
|
699
700
|
return arr;
|
|
700
701
|
}
|
|
701
702
|
|
|
702
|
-
/**
|
|
703
|
-
* Time Complexity: O(n)
|
|
704
|
-
* Space Complexity: O(1)
|
|
705
|
-
*/
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* Time Complexity: O(n)
|
|
709
|
-
* Space Complexity: O(1)
|
|
710
|
-
*
|
|
711
|
-
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
712
|
-
* object to be iterated over using a for...of loop.
|
|
713
|
-
*/
|
|
714
|
-
* [Symbol.iterator]() {
|
|
715
|
-
for (let i = 0; i < this.size; ++i) {
|
|
716
|
-
yield this.getAt(i);
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
/**
|
|
721
|
-
* Time Complexity: O(n)
|
|
722
|
-
* Space Complexity: O(1)
|
|
723
|
-
*/
|
|
724
|
-
|
|
725
|
-
/**
|
|
726
|
-
* Time Complexity: O(n)
|
|
727
|
-
* Space Complexity: O(1)
|
|
728
|
-
*
|
|
729
|
-
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
730
|
-
* each element.
|
|
731
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
732
|
-
* deque. It takes three parameters:
|
|
733
|
-
*/
|
|
734
|
-
forEach(callback: (element: E, index: number, deque: this) => void) {
|
|
735
|
-
let index = 0;
|
|
736
|
-
for (const el of this) {
|
|
737
|
-
callback(el, index, this);
|
|
738
|
-
index++;
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
|
|
742
703
|
/**
|
|
743
704
|
* Time Complexity: O(n)
|
|
744
705
|
* Space Complexity: O(n)
|
|
745
706
|
*/
|
|
746
|
-
|
|
747
707
|
/**
|
|
748
708
|
* Time Complexity: O(n)
|
|
749
709
|
* Space Complexity: O(n)
|
|
750
710
|
*
|
|
751
|
-
* The `filter` function creates a new deque containing
|
|
752
|
-
* predicate function.
|
|
753
|
-
* @param predicate - The `predicate` parameter is a function that takes three arguments:
|
|
754
|
-
*
|
|
755
|
-
*
|
|
756
|
-
*
|
|
757
|
-
|
|
758
|
-
|
|
711
|
+
* The `filter` function creates a new deque containing elements from the original deque that satisfy
|
|
712
|
+
* a given predicate function.
|
|
713
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
714
|
+
* the current element being iterated over, the index of the current element, and the deque itself.
|
|
715
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
716
|
+
* deque or not.
|
|
717
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
718
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
719
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
720
|
+
* @returns The `filter` method is returning a new `Deque` object that contains the elements that
|
|
721
|
+
* satisfy the given predicate function.
|
|
722
|
+
*/
|
|
723
|
+
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Deque<E> {
|
|
759
724
|
const newDeque = new Deque<E>([], this._bucketSize);
|
|
760
725
|
let index = 0;
|
|
761
726
|
for (const el of this) {
|
|
762
|
-
if (predicate(el, index, this)) {
|
|
727
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
763
728
|
newDeque.push(el);
|
|
764
729
|
}
|
|
765
730
|
index++;
|
|
@@ -771,21 +736,24 @@ export class Deque<E> {
|
|
|
771
736
|
* Time Complexity: O(n)
|
|
772
737
|
* Space Complexity: O(n)
|
|
773
738
|
*/
|
|
774
|
-
|
|
775
739
|
/**
|
|
776
740
|
* Time Complexity: O(n)
|
|
777
741
|
* Space Complexity: O(n)
|
|
778
742
|
*
|
|
779
|
-
* The `map` function
|
|
780
|
-
*
|
|
781
|
-
* @param callback - The `callback` parameter is a function that
|
|
782
|
-
*
|
|
743
|
+
* The `map` function creates a new Deque by applying a callback function to each element of the
|
|
744
|
+
* original Deque.
|
|
745
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
746
|
+
* the deque. It takes three arguments:
|
|
747
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
748
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
749
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
750
|
+
* @returns a new Deque object with the mapped values.
|
|
783
751
|
*/
|
|
784
|
-
map<T>(callback:
|
|
752
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T> {
|
|
785
753
|
const newDeque = new Deque<T>([], this._bucketSize);
|
|
786
754
|
let index = 0;
|
|
787
755
|
for (const el of this) {
|
|
788
|
-
newDeque.push(callback(el, index, this));
|
|
756
|
+
newDeque.push(callback.call(thisArg, el, index, this));
|
|
789
757
|
index++;
|
|
790
758
|
}
|
|
791
759
|
return newDeque;
|
|
@@ -793,34 +761,24 @@ export class Deque<E> {
|
|
|
793
761
|
|
|
794
762
|
/**
|
|
795
763
|
* Time Complexity: O(n)
|
|
796
|
-
* Space Complexity: O(
|
|
764
|
+
* Space Complexity: O(n)
|
|
797
765
|
*/
|
|
798
766
|
|
|
767
|
+
print(): void {
|
|
768
|
+
console.log([...this])
|
|
769
|
+
}
|
|
770
|
+
|
|
799
771
|
/**
|
|
800
772
|
* Time Complexity: O(n)
|
|
801
773
|
* Space Complexity: O(1)
|
|
802
774
|
*
|
|
803
|
-
* The
|
|
804
|
-
*
|
|
805
|
-
* @param callback - The `callback` parameter is a function that takes four arguments:
|
|
806
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
807
|
-
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
808
|
-
* the elements of the deque.
|
|
809
|
-
* @returns the final value of the accumulator after iterating over all elements in the deque and
|
|
810
|
-
* applying the callback function to each element.
|
|
775
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
776
|
+
* object to be iterated over using a for...of loop.
|
|
811
777
|
*/
|
|
812
|
-
|
|
813
|
-
let
|
|
814
|
-
|
|
815
|
-
for (const el of this) {
|
|
816
|
-
accumulator = callback(accumulator, el, index, this);
|
|
817
|
-
index++;
|
|
778
|
+
protected* _getIterator() {
|
|
779
|
+
for (let i = 0; i < this.size; ++i) {
|
|
780
|
+
yield this.getAt(i);
|
|
818
781
|
}
|
|
819
|
-
return accumulator;
|
|
820
|
-
}
|
|
821
|
-
|
|
822
|
-
print(): void {
|
|
823
|
-
console.log([...this])
|
|
824
782
|
}
|
|
825
783
|
|
|
826
784
|
/**
|
|
@@ -891,194 +849,4 @@ export class Deque<E> {
|
|
|
891
849
|
|
|
892
850
|
return { bucketIndex, indexInBucket };
|
|
893
851
|
}
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
// O(1) time complexity of obtaining the element
|
|
897
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
898
|
-
// todo tested slowest one
|
|
899
|
-
export class ObjectDeque<E = number> {
|
|
900
|
-
constructor(capacity?: number) {
|
|
901
|
-
if (capacity !== undefined) this._capacity = capacity;
|
|
902
|
-
}
|
|
903
|
-
|
|
904
|
-
protected _nodes: { [key: number]: E } = {};
|
|
905
|
-
|
|
906
|
-
get nodes(): { [p: number]: E } {
|
|
907
|
-
return this._nodes;
|
|
908
|
-
}
|
|
909
|
-
|
|
910
|
-
protected _capacity = Number.MAX_SAFE_INTEGER;
|
|
911
|
-
|
|
912
|
-
get capacity(): number {
|
|
913
|
-
return this._capacity;
|
|
914
|
-
}
|
|
915
|
-
|
|
916
|
-
protected _first = -1;
|
|
917
|
-
|
|
918
|
-
get first(): number {
|
|
919
|
-
return this._first;
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
protected _last = -1;
|
|
923
|
-
|
|
924
|
-
get last(): number {
|
|
925
|
-
return this._last;
|
|
926
|
-
}
|
|
927
|
-
|
|
928
|
-
protected _size = 0;
|
|
929
|
-
|
|
930
|
-
get size(): number {
|
|
931
|
-
return this._size;
|
|
932
|
-
}
|
|
933
|
-
|
|
934
|
-
/**
|
|
935
|
-
* Time Complexity: O(1)
|
|
936
|
-
* Space Complexity: O(1)
|
|
937
|
-
*/
|
|
938
|
-
|
|
939
|
-
/**
|
|
940
|
-
* Time Complexity: O(1)
|
|
941
|
-
* Space Complexity: O(1)
|
|
942
|
-
*
|
|
943
|
-
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
944
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
945
|
-
* structure.
|
|
946
|
-
*/
|
|
947
|
-
addFirst(element: E) {
|
|
948
|
-
if (this.size === 0) {
|
|
949
|
-
const mid = Math.floor(this.capacity / 2);
|
|
950
|
-
this._first = mid;
|
|
951
|
-
this._last = mid;
|
|
952
|
-
} else {
|
|
953
|
-
this._first--;
|
|
954
|
-
}
|
|
955
|
-
this.nodes[this.first] = element;
|
|
956
|
-
this._size++;
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
/**
|
|
960
|
-
* Time Complexity: O(1)
|
|
961
|
-
* Space Complexity: O(1)
|
|
962
|
-
*/
|
|
963
|
-
|
|
964
|
-
/**
|
|
965
|
-
* Time Complexity: O(1)
|
|
966
|
-
* Space Complexity: O(1)
|
|
967
|
-
*
|
|
968
|
-
* The addLast function adds an element to the end of an array-like data structure.
|
|
969
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
970
|
-
*/
|
|
971
|
-
addLast(element: E) {
|
|
972
|
-
if (this.size === 0) {
|
|
973
|
-
const mid = Math.floor(this.capacity / 2);
|
|
974
|
-
this._first = mid;
|
|
975
|
-
this._last = mid;
|
|
976
|
-
} else {
|
|
977
|
-
this._last++;
|
|
978
|
-
}
|
|
979
|
-
this.nodes[this.last] = element;
|
|
980
|
-
this._size++;
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
/**
|
|
984
|
-
* Time Complexity: O(1)
|
|
985
|
-
* Space Complexity: O(1)
|
|
986
|
-
*/
|
|
987
|
-
|
|
988
|
-
/**
|
|
989
|
-
* Time Complexity: O(1)
|
|
990
|
-
* Space Complexity: O(1)
|
|
991
|
-
*
|
|
992
|
-
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
993
|
-
* @returns The element of the first element in the data structure.
|
|
994
|
-
*/
|
|
995
|
-
popFirst() {
|
|
996
|
-
if (!this.size) return;
|
|
997
|
-
const element = this.getFirst();
|
|
998
|
-
delete this.nodes[this.first];
|
|
999
|
-
this._first++;
|
|
1000
|
-
this._size--;
|
|
1001
|
-
return element;
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
/**
|
|
1005
|
-
* Time Complexity: O(1)
|
|
1006
|
-
* Space Complexity: O(1)
|
|
1007
|
-
*/
|
|
1008
|
-
|
|
1009
|
-
/**
|
|
1010
|
-
* Time Complexity: O(1)
|
|
1011
|
-
* Space Complexity: O(1)
|
|
1012
|
-
*
|
|
1013
|
-
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
1014
|
-
* @returns The element at the first position of the `_nodes` array.
|
|
1015
|
-
*/
|
|
1016
|
-
getFirst() {
|
|
1017
|
-
if (this.size) return this.nodes[this.first];
|
|
1018
|
-
}
|
|
1019
|
-
|
|
1020
|
-
/**
|
|
1021
|
-
* Time Complexity: O(1)
|
|
1022
|
-
* Space Complexity: O(1)
|
|
1023
|
-
*/
|
|
1024
|
-
|
|
1025
|
-
/**
|
|
1026
|
-
* Time Complexity: O(1)
|
|
1027
|
-
* Space Complexity: O(1)
|
|
1028
|
-
*
|
|
1029
|
-
* The `popLast()` function removes and returns the last element in a data structure.
|
|
1030
|
-
* @returns The element that was removed from the data structure.
|
|
1031
|
-
*/
|
|
1032
|
-
popLast() {
|
|
1033
|
-
if (!this.size) return;
|
|
1034
|
-
const element = this.getLast();
|
|
1035
|
-
delete this.nodes[this.last];
|
|
1036
|
-
this._last--;
|
|
1037
|
-
this._size--;
|
|
1038
|
-
|
|
1039
|
-
return element;
|
|
1040
|
-
}
|
|
1041
|
-
|
|
1042
|
-
/**
|
|
1043
|
-
* Time Complexity: O(1)
|
|
1044
|
-
* Space Complexity: O(1)
|
|
1045
|
-
*/
|
|
1046
|
-
|
|
1047
|
-
/**
|
|
1048
|
-
* Time Complexity: O(1)
|
|
1049
|
-
* Space Complexity: O(1)
|
|
1050
|
-
*
|
|
1051
|
-
* The `getLast()` function returns the last element in an array-like data structure.
|
|
1052
|
-
* @returns The last element in the array "_nodes" is being returned.
|
|
1053
|
-
*/
|
|
1054
|
-
getLast() {
|
|
1055
|
-
if (this.size) return this.nodes[this.last];
|
|
1056
|
-
}
|
|
1057
|
-
|
|
1058
|
-
/**
|
|
1059
|
-
* Time Complexity: O(1)
|
|
1060
|
-
* Space Complexity: O(1)
|
|
1061
|
-
*/
|
|
1062
|
-
|
|
1063
|
-
/**
|
|
1064
|
-
* Time Complexity: O(1)
|
|
1065
|
-
* Space Complexity: O(1)
|
|
1066
|
-
*
|
|
1067
|
-
* The get function returns the element at the specified index in an array-like data structure.
|
|
1068
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
1069
|
-
* retrieve from the array.
|
|
1070
|
-
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
1071
|
-
* index, `undefined` is returned.
|
|
1072
|
-
*/
|
|
1073
|
-
get(index: number) {
|
|
1074
|
-
return this.nodes[this.first + index] || undefined;
|
|
1075
|
-
}
|
|
1076
|
-
|
|
1077
|
-
/**
|
|
1078
|
-
* The function checks if the size of a data structure is less than or equal to zero.
|
|
1079
|
-
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
1080
|
-
*/
|
|
1081
|
-
isEmpty() {
|
|
1082
|
-
return this.size <= 0;
|
|
1083
|
-
}
|
|
1084
852
|
}
|
|
@@ -4,42 +4,10 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
import { SinglyLinkedList } from '../linked-list';
|
|
7
|
+
import { IterableElementBase } from "../base";
|
|
8
|
+
import { ElementCallback } from "../../types";
|
|
7
9
|
|
|
8
|
-
export class
|
|
9
|
-
/**
|
|
10
|
-
* The enqueue function adds a value to the end of an array.
|
|
11
|
-
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
12
|
-
*/
|
|
13
|
-
enqueue(value: E) {
|
|
14
|
-
this.push(value);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
19
|
-
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
20
|
-
*/
|
|
21
|
-
dequeue(): E | undefined {
|
|
22
|
-
return this.shift();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
27
|
-
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
|
-
*/
|
|
29
|
-
getFirst(): E | undefined {
|
|
30
|
-
return this.head?.value;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
35
|
-
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
36
|
-
*/
|
|
37
|
-
peek(): E | undefined {
|
|
38
|
-
return this.getFirst();
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export class Queue<E = any> {
|
|
10
|
+
export class Queue<E = any> extends IterableElementBase<E> {
|
|
43
11
|
/**
|
|
44
12
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
45
13
|
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
|
@@ -47,6 +15,7 @@ export class Queue<E = any> {
|
|
|
47
15
|
* initialized as an empty array.
|
|
48
16
|
*/
|
|
49
17
|
constructor(elements?: E[]) {
|
|
18
|
+
super();
|
|
50
19
|
this._nodes = elements || [];
|
|
51
20
|
this._offset = 0;
|
|
52
21
|
}
|
|
@@ -304,57 +273,62 @@ export class Queue<E = any> {
|
|
|
304
273
|
console.log([...this]);
|
|
305
274
|
}
|
|
306
275
|
|
|
307
|
-
* [Symbol.iterator]() {
|
|
308
|
-
for (const item of this.nodes) {
|
|
309
|
-
yield item;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
276
|
/**
|
|
314
277
|
* Time Complexity: O(n)
|
|
315
|
-
* Space Complexity: O(
|
|
278
|
+
* Space Complexity: O(n)
|
|
316
279
|
*/
|
|
317
280
|
|
|
318
281
|
/**
|
|
319
282
|
* Time Complexity: O(n)
|
|
320
|
-
* Space Complexity: O(
|
|
283
|
+
* Space Complexity: O(n)
|
|
321
284
|
*
|
|
322
|
-
* The `
|
|
323
|
-
*
|
|
324
|
-
* @param
|
|
325
|
-
*
|
|
326
|
-
|
|
327
|
-
|
|
285
|
+
* The `filter` function creates a new `Queue` object containing elements from the original `Queue`
|
|
286
|
+
* that satisfy a given predicate function.
|
|
287
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
288
|
+
* the current element being iterated over, the index of the current element, and the queue itself.
|
|
289
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
290
|
+
* queue or not.
|
|
291
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
292
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
293
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
294
|
+
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
|
295
|
+
* satisfy the given predicate function.
|
|
296
|
+
*/
|
|
297
|
+
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Queue<E> {
|
|
298
|
+
const newDeque = new Queue<E>([]);
|
|
328
299
|
let index = 0;
|
|
329
300
|
for (const el of this) {
|
|
330
|
-
|
|
301
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
302
|
+
newDeque.push(el);
|
|
303
|
+
}
|
|
331
304
|
index++;
|
|
332
305
|
}
|
|
306
|
+
return newDeque;
|
|
333
307
|
}
|
|
334
308
|
|
|
335
309
|
/**
|
|
336
310
|
* Time Complexity: O(n)
|
|
337
311
|
* Space Complexity: O(n)
|
|
338
312
|
*/
|
|
339
|
-
|
|
340
313
|
/**
|
|
341
314
|
* Time Complexity: O(n)
|
|
342
315
|
* Space Complexity: O(n)
|
|
343
316
|
*
|
|
344
|
-
* The `
|
|
345
|
-
*
|
|
346
|
-
* @param
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
317
|
+
* The `map` function takes a callback function and applies it to each element in the queue,
|
|
318
|
+
* returning a new queue with the results.
|
|
319
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
320
|
+
* queue. It takes three arguments: the current element, the index of the current element, and the
|
|
321
|
+
* queue itself. The callback function should return a new value that will be added to the new queue.
|
|
322
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
323
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
324
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
325
|
+
* @returns The `map` function is returning a new `Queue` object with the transformed elements.
|
|
326
|
+
*/
|
|
327
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Queue<T> {
|
|
328
|
+
const newDeque = new Queue<T>([]);
|
|
353
329
|
let index = 0;
|
|
354
330
|
for (const el of this) {
|
|
355
|
-
|
|
356
|
-
newDeque.push(el);
|
|
357
|
-
}
|
|
331
|
+
newDeque.push(callback.call(thisArg, el, index, this));
|
|
358
332
|
index++;
|
|
359
333
|
}
|
|
360
334
|
return newDeque;
|
|
@@ -365,32 +339,43 @@ export class Queue<E = any> {
|
|
|
365
339
|
* Space Complexity: O(n)
|
|
366
340
|
*/
|
|
367
341
|
|
|
342
|
+
protected* _getIterator() {
|
|
343
|
+
for (const item of this.nodes) {
|
|
344
|
+
yield item;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
368
350
|
/**
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
373
|
-
* returning a new deque with the results.
|
|
374
|
-
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
375
|
-
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
351
|
+
* The enqueue function adds a value to the end of an array.
|
|
352
|
+
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
376
353
|
*/
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
let index = 0;
|
|
380
|
-
for (const el of this) {
|
|
381
|
-
newDeque.push(callback(el, index, this));
|
|
382
|
-
index++;
|
|
383
|
-
}
|
|
384
|
-
return newDeque;
|
|
354
|
+
enqueue(value: E) {
|
|
355
|
+
this.push(value);
|
|
385
356
|
}
|
|
386
357
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
358
|
+
/**
|
|
359
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
360
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
361
|
+
*/
|
|
362
|
+
dequeue(): E | undefined {
|
|
363
|
+
return this.shift();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
368
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
369
|
+
*/
|
|
370
|
+
getFirst(): E | undefined {
|
|
371
|
+
return this.head?.value;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
376
|
+
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
377
|
+
*/
|
|
378
|
+
peek(): E | undefined {
|
|
379
|
+
return this.getFirst();
|
|
395
380
|
}
|
|
396
381
|
}
|