data-structure-typed 0.8.18 → 1.12.9
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/.dependency-cruiser.js +449 -0
- package/.idea/data-structure-typed.iml +2 -0
- package/.idea/modules.xml +1 -1
- package/README.md +298 -2
- package/dist/data-structures/binary-tree/aa-tree.js +5 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +58 -5
- package/dist/data-structures/binary-tree/avl-tree.js +150 -46
- package/dist/data-structures/binary-tree/b-tree.js +5 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +28 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +41 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +230 -36
- package/dist/data-structures/binary-tree/binary-tree.js +747 -369
- package/dist/data-structures/binary-tree/bst.d.ts +20 -8
- package/dist/data-structures/binary-tree/bst.js +164 -107
- package/dist/data-structures/binary-tree/rb-tree.js +5 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -3
- package/dist/data-structures/binary-tree/segment-tree.js +95 -61
- package/dist/data-structures/binary-tree/splay-tree.js +5 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +5 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +35 -11
- package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +168 -46
- package/dist/data-structures/graph/abstract-graph.js +712 -323
- package/dist/data-structures/graph/directed-graph.d.ts +114 -12
- package/dist/data-structures/graph/directed-graph.js +372 -128
- package/dist/data-structures/graph/undirected-graph.d.ts +67 -3
- package/dist/data-structures/graph/undirected-graph.js +233 -81
- package/dist/data-structures/hash/coordinate-map.d.ts +33 -1
- package/dist/data-structures/hash/coordinate-map.js +70 -20
- package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
- package/dist/data-structures/hash/coordinate-set.js +58 -15
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/heap/heap.d.ts +26 -37
- package/dist/data-structures/heap/heap.js +56 -60
- package/dist/data-structures/heap/max-heap.d.ts +8 -2
- package/dist/data-structures/heap/max-heap.js +32 -9
- package/dist/data-structures/heap/min-heap.d.ts +9 -2
- package/dist/data-structures/heap/min-heap.js +33 -9
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -7
- package/dist/data-structures/linked-list/doubly-linked-list.js +101 -61
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -19
- package/dist/data-structures/linked-list/singly-linked-list.js +312 -174
- package/dist/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/data-structures/matrix/matrix.js +19 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +84 -4
- package/dist/data-structures/matrix/matrix2d.js +158 -61
- package/dist/data-structures/matrix/navigator.d.ts +34 -16
- package/dist/data-structures/matrix/navigator.js +65 -18
- package/dist/data-structures/matrix/vector2d.d.ts +153 -29
- package/dist/data-structures/matrix/vector2d.js +249 -102
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/priority-queue.d.ts +145 -21
- package/dist/data-structures/priority-queue/priority-queue.js +285 -116
- package/dist/data-structures/queue/deque.d.ts +69 -0
- package/dist/data-structures/queue/deque.js +151 -56
- package/dist/data-structures/queue/queue.d.ts +34 -37
- package/dist/data-structures/queue/queue.js +59 -61
- package/dist/data-structures/stack/stack.d.ts +29 -35
- package/dist/data-structures/stack/stack.js +51 -56
- package/dist/data-structures/trie/trie.d.ts +36 -6
- package/dist/data-structures/trie/trie.js +256 -83
- package/dist/data-structures/types/abstract-graph.d.ts +29 -0
- package/dist/data-structures/types/abstract-graph.js +2 -0
- package/dist/data-structures/types/avl-tree.d.ts +5 -0
- package/dist/data-structures/types/avl-tree.js +2 -0
- package/dist/data-structures/types/binary-tree.d.ts +16 -0
- package/dist/data-structures/types/binary-tree.js +2 -0
- package/dist/data-structures/types/bst.d.ts +7 -0
- package/dist/data-structures/types/bst.js +2 -0
- package/dist/data-structures/types/directed-graph.d.ts +10 -0
- package/dist/data-structures/types/directed-graph.js +2 -0
- package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/types/doubly-linked-list.js +2 -0
- package/dist/data-structures/types/heap.d.ts +7 -0
- package/dist/data-structures/types/heap.js +2 -0
- package/dist/data-structures/types/index.d.ts +13 -0
- package/dist/data-structures/types/index.js +29 -0
- package/dist/data-structures/types/navigator.d.ts +14 -0
- package/dist/data-structures/types/navigator.js +2 -0
- package/dist/data-structures/types/priority-queue.d.ts +7 -0
- package/dist/data-structures/types/priority-queue.js +2 -0
- package/dist/data-structures/types/segment-tree.d.ts +1 -0
- package/dist/data-structures/types/segment-tree.js +2 -0
- package/dist/data-structures/types/singly-linked-list.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +5 -0
- package/dist/data-structures/types/tree-multiset.js +2 -0
- package/dist/utils/trampoline.d.ts +14 -0
- package/dist/utils/trampoline.js +130 -0
- package/dist/utils/types/index.js +17 -0
- package/dist/{types → utils}/types/utils.d.ts +15 -1
- package/dist/{types → utils/types}/utils.js +21 -19
- package/dist/{utils.d.ts → utils/utils.d.ts} +5 -22
- package/dist/utils/utils.js +651 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +85 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2046 -0
- package/docs/classes/AVLTreeNode.html +423 -0
- package/docs/classes/AaTree.html +117 -0
- package/docs/classes/AbstractEdge.html +198 -0
- package/docs/classes/AbstractGraph.html +891 -0
- package/docs/classes/AbstractVertex.html +164 -0
- package/docs/classes/ArrayDeque.html +384 -0
- package/docs/classes/BST.html +1893 -0
- package/docs/classes/BSTNode.html +425 -0
- package/docs/classes/BTree.html +117 -0
- package/docs/classes/BinaryIndexedTree.html +244 -0
- package/docs/classes/BinaryTree.html +1754 -0
- package/docs/classes/BinaryTreeNode.html +396 -0
- package/docs/classes/Character.html +165 -0
- package/docs/classes/CoordinateMap.html +394 -0
- package/docs/classes/CoordinateSet.html +355 -0
- package/docs/classes/Deque.html +617 -0
- package/docs/classes/DirectedEdge.html +247 -0
- package/docs/classes/DirectedGraph.html +1207 -0
- package/docs/classes/DirectedVertex.html +154 -0
- package/docs/classes/DoublyLinkedList.html +619 -0
- package/docs/classes/DoublyLinkedListNode.html +160 -0
- package/docs/classes/Heap.html +315 -0
- package/docs/classes/Matrix2D.html +447 -0
- package/docs/classes/MatrixNTI2D.html +181 -0
- package/docs/classes/MaxHeap.html +325 -0
- package/docs/classes/MaxPriorityQueue.html +668 -0
- package/docs/classes/MinHeap.html +326 -0
- package/docs/classes/MinPriorityQueue.html +668 -0
- package/docs/classes/Navigator.html +285 -0
- package/docs/classes/ObjectDeque.html +289 -0
- package/docs/classes/PriorityQueue.html +643 -0
- package/docs/classes/Queue.html +337 -0
- package/docs/classes/RBTree.html +117 -0
- package/docs/classes/SegmentTree.html +234 -0
- package/docs/classes/SegmentTreeNode.html +302 -0
- package/docs/classes/SinglyLinkedList.html +1035 -0
- package/docs/classes/SinglyLinkedListNode.html +304 -0
- package/docs/classes/SplayTree.html +117 -0
- package/docs/classes/Stack.html +313 -0
- package/docs/classes/TreeMultiSet.html +1897 -0
- package/docs/classes/Trie.html +317 -0
- package/docs/classes/TrieNode.html +221 -0
- package/docs/classes/TwoThreeTree.html +117 -0
- package/docs/classes/UndirectedEdge.html +220 -0
- package/docs/classes/UndirectedGraph.html +1006 -0
- package/docs/classes/UndirectedVertex.html +154 -0
- package/docs/classes/Vector2D.html +746 -0
- package/docs/enums/CP.html +126 -0
- package/docs/enums/FamilyPosition.html +126 -0
- package/docs/enums/LoopType.html +119 -0
- package/docs/index.html +288 -0
- package/docs/modules.html +146 -0
- package/jest.config.js +5 -0
- package/package.json +33 -47
- package/rename_clear_files.sh +29 -0
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/data-structures/binary-tree/avl-tree.ts +58 -6
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +31 -4
- package/src/data-structures/binary-tree/binary-tree.ts +460 -145
- package/src/data-structures/binary-tree/bst.ts +31 -25
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/segment-tree.ts +25 -12
- package/src/data-structures/binary-tree/tree-multiset.ts +5 -4
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +224 -108
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +132 -26
- package/src/data-structures/graph/undirected-graph.ts +78 -11
- package/src/data-structures/hash/coordinate-map.ts +33 -1
- package/src/data-structures/hash/coordinate-set.ts +25 -0
- package/src/data-structures/hash/index.ts +5 -0
- package/src/data-structures/heap/heap.ts +27 -41
- package/src/data-structures/heap/max-heap.ts +8 -2
- package/src/data-structures/heap/min-heap.ts +9 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -17
- package/src/data-structures/linked-list/singly-linked-list.ts +56 -39
- package/src/data-structures/matrix/matrix.ts +11 -0
- package/src/data-structures/matrix/matrix2d.ts +90 -10
- package/src/data-structures/matrix/navigator.ts +34 -14
- package/src/data-structures/matrix/vector2d.ts +187 -63
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/priority-queue.ts +200 -78
- package/src/data-structures/queue/deque.ts +71 -2
- package/src/data-structures/queue/queue.ts +37 -40
- package/src/data-structures/stack/stack.ts +32 -38
- package/src/data-structures/trie/trie.ts +83 -14
- package/src/data-structures/types/abstract-graph.ts +51 -0
- package/src/data-structures/types/avl-tree.ts +6 -0
- package/src/data-structures/types/binary-tree.ts +15 -0
- package/src/data-structures/types/bst.ts +5 -0
- package/src/data-structures/types/directed-graph.ts +18 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +8 -0
- package/src/data-structures/types/index.ts +13 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/trampoline.ts +51 -0
- package/src/utils/types/index.ts +1 -0
- package/src/{types → utils/types}/utils.ts +27 -5
- package/src/{utils.ts → utils/utils.ts} +41 -131
- package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
- package/{dist/types/data-structures/graph/index.d.ts → tests/unit/data-structures/graph/index.ts} +1 -1
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
- package/tsconfig.json +9 -6
- package/dist/data-structures/trampoline.d.ts +0 -25
- package/dist/data-structures/trampoline.js +0 -52
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/pair.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/index.d.ts +0 -9
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/utils.d.ts +0 -46
- package/dist/utils.js +0 -569
- package/src/data-structures/trampoline.ts +0 -91
- package/src/types/index.ts +0 -1
- /package/dist/{types/data-structures/hash/hash-table.d.ts → data-structures/types/singly-linked-list.d.ts} +0 -0
- /package/dist/{types → utils}/index.d.ts +0 -0
- /package/dist/{types → utils}/index.js +0 -0
- /package/dist/{types → utils}/types/index.d.ts +0 -0
- /package/{src/types/patches/index.d.ts → tests/unit/data-structures/graph/abstract-graph.ts} +0 -0
|
@@ -1,25 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
comparator: PriorityQueueComparator<T>;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export type PriorityQueueDFSOrderPattern = 'pre' | 'in' | 'post';
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
import type {PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions} from '../types';
|
|
10
6
|
|
|
11
7
|
export class PriorityQueue<T = number> {
|
|
12
8
|
protected nodes: T[] = [];
|
|
13
9
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const aKey = a as unknown as number, bKey = b as unknown as number;
|
|
20
|
-
return aKey - bKey;
|
|
21
|
-
};
|
|
22
|
-
|
|
10
|
+
/**
|
|
11
|
+
* The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
|
|
12
|
+
* function.
|
|
13
|
+
* @param options - The `options` parameter is an object that contains the following properties:
|
|
14
|
+
*/
|
|
23
15
|
constructor(options: PriorityQueueOptions<T>) {
|
|
24
16
|
const {nodes, comparator, isFix = true} = options;
|
|
25
17
|
this._comparator = comparator;
|
|
@@ -31,75 +23,57 @@ export class PriorityQueue<T = number> {
|
|
|
31
23
|
}
|
|
32
24
|
}
|
|
33
25
|
|
|
34
|
-
|
|
35
|
-
return this.
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
protected _swap(a: number, b: number) {
|
|
39
|
-
const temp = this.nodes[a];
|
|
40
|
-
this.nodes[a] = this.nodes[b];
|
|
41
|
-
this.nodes[b] = temp;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
protected _isValidIndex(index: number): boolean {
|
|
45
|
-
return index > -1 && index < this.nodes.length;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
protected _getParent(child: number): number {
|
|
49
|
-
return Math.floor((child - 1) / 2);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
protected _getLeft(parent: number): number {
|
|
53
|
-
return (2 * parent) + 1;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
protected _getRight(parent: number): number {
|
|
57
|
-
return (2 * parent) + 2;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
protected _getComparedChild(parent: number) {
|
|
61
|
-
let min = parent;
|
|
62
|
-
const left = this._getLeft(parent), right = this._getRight(parent);
|
|
63
|
-
|
|
64
|
-
if (left < this.size && this._compare(min, left)) {
|
|
65
|
-
min = left;
|
|
66
|
-
}
|
|
67
|
-
if (right < this.size && this._compare(min, right)) {
|
|
68
|
-
min = right;
|
|
69
|
-
}
|
|
70
|
-
return min;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
protected _heapifyUp(start: number) {
|
|
74
|
-
while (start > 0 && this._compare(this._getParent(start), start)) {
|
|
75
|
-
const parent = this._getParent(start);
|
|
76
|
-
this._swap(start, parent);
|
|
77
|
-
start = parent;
|
|
78
|
-
}
|
|
26
|
+
get size(): number {
|
|
27
|
+
return this.nodes.length;
|
|
79
28
|
}
|
|
80
29
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
30
|
+
/**
|
|
31
|
+
* The `heapify` function creates a new PriorityQueue instance and fixes the heap property.
|
|
32
|
+
* @param options - The "options" parameter is an object that contains the configuration options for the PriorityQueue.
|
|
33
|
+
* It can include properties such as "comparator" which specifies the comparison function used to order the elements in
|
|
34
|
+
* the priority queue, and "initialValues" which is an array of initial values to be added to the priority
|
|
35
|
+
* @returns a new instance of the PriorityQueue class after performing the heapify operation on it.
|
|
36
|
+
*/
|
|
37
|
+
static heapify<T>(options: PriorityQueueOptions<T>) {
|
|
38
|
+
const heap = new PriorityQueue(options);
|
|
39
|
+
heap._fix();
|
|
40
|
+
return heap;
|
|
88
41
|
}
|
|
89
42
|
|
|
90
|
-
|
|
91
|
-
|
|
43
|
+
/**
|
|
44
|
+
* The function checks if a priority queue is valid by creating a new priority queue with a fix option and then calling
|
|
45
|
+
* the isValid method.
|
|
46
|
+
* @param options - An object containing options for creating a priority queue. The options object should have the
|
|
47
|
+
* following properties:
|
|
48
|
+
* @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
|
|
49
|
+
*/
|
|
50
|
+
static isPriorityQueueified<T>(options: Omit<PriorityQueueOptions<T>, 'isFix'>) {
|
|
51
|
+
return new PriorityQueue({...options, isFix: true}).isValid();
|
|
92
52
|
}
|
|
93
53
|
|
|
54
|
+
/**
|
|
55
|
+
* The "offer" function adds a node to the heap and ensures that the heap property is maintained.
|
|
56
|
+
* @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
|
|
57
|
+
* that needs to be added to the heap.
|
|
58
|
+
*/
|
|
94
59
|
offer(node: T) {
|
|
95
60
|
this.nodes.push(node);
|
|
96
61
|
this._heapifyUp(this.size - 1);
|
|
97
62
|
}
|
|
98
63
|
|
|
64
|
+
/**
|
|
65
|
+
* The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
|
|
66
|
+
* @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
|
|
67
|
+
* Otherwise, it returns `null`.
|
|
68
|
+
*/
|
|
99
69
|
peek(): T | null {
|
|
100
70
|
return this.size ? this.nodes[0] : null;
|
|
101
71
|
}
|
|
102
72
|
|
|
73
|
+
/**
|
|
74
|
+
* The `poll` function removes and returns the top element from a heap data structure.
|
|
75
|
+
* @returns The `poll()` method returns a value of type `T` or `null`.
|
|
76
|
+
*/
|
|
103
77
|
poll(): T | null {
|
|
104
78
|
let res: T | null = null;
|
|
105
79
|
if (this.size > 1) {
|
|
@@ -112,27 +86,55 @@ export class PriorityQueue<T = number> {
|
|
|
112
86
|
return res;
|
|
113
87
|
}
|
|
114
88
|
|
|
89
|
+
/**
|
|
90
|
+
* The `leaf` function returns the last element in the `nodes` array or `null` if the array is empty.
|
|
91
|
+
* @returns The method `leaf()` is returning the last element (`T`) in the `nodes` array if it exists. If the array is
|
|
92
|
+
* empty or the last element is `null`, then it returns `null`.
|
|
93
|
+
*/
|
|
115
94
|
leaf(): T | null {
|
|
116
95
|
return this.nodes[this.size - 1] ?? null;
|
|
117
96
|
}
|
|
118
97
|
|
|
98
|
+
/**
|
|
99
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value indicating whether the
|
|
100
|
+
* object is empty or not.
|
|
101
|
+
* @returns The method `isEmpty()` is returning a boolean value indicating whether the size of the object is equal to
|
|
102
|
+
* 0.
|
|
103
|
+
*/
|
|
119
104
|
isEmpty() {
|
|
120
105
|
return this.size === 0;
|
|
121
106
|
}
|
|
122
107
|
|
|
108
|
+
/**
|
|
109
|
+
* The clear function clears the nodes array.
|
|
110
|
+
*/
|
|
123
111
|
clear() {
|
|
124
112
|
this.nodes = [];
|
|
125
113
|
}
|
|
126
114
|
|
|
115
|
+
/**
|
|
116
|
+
* The toArray function returns an array containing all the elements in the nodes property.
|
|
117
|
+
* @returns An array of type T, which is the elements of the nodes property.
|
|
118
|
+
*/
|
|
127
119
|
toArray(): T[] {
|
|
128
120
|
return [...this.nodes];
|
|
129
121
|
}
|
|
130
122
|
|
|
123
|
+
/**
|
|
124
|
+
* The `clone` function returns a new instance of the `PriorityQueue` class with the same nodes and comparator as the
|
|
125
|
+
* original instance.
|
|
126
|
+
* @returns The `clone()` method is returning a new instance of the `PriorityQueue` class with the same `nodes` and
|
|
127
|
+
* `comparator` properties as the original instance.
|
|
128
|
+
*/
|
|
131
129
|
clone(): PriorityQueue<T> {
|
|
132
130
|
return new PriorityQueue<T>({nodes: this.nodes, comparator: this._comparator});
|
|
133
131
|
}
|
|
134
132
|
|
|
135
133
|
// --- start additional methods ---
|
|
134
|
+
/**
|
|
135
|
+
* The `isValid` function recursively checks if a binary tree satisfies a certain condition.
|
|
136
|
+
* @returns The function `isValid()` returns a boolean value.
|
|
137
|
+
*/
|
|
136
138
|
isValid(): boolean {
|
|
137
139
|
const isValidRecursive = (parentIndex: number): boolean => {
|
|
138
140
|
let isValidLeft = true;
|
|
@@ -156,6 +158,10 @@ export class PriorityQueue<T = number> {
|
|
|
156
158
|
return isValidRecursive(0);
|
|
157
159
|
}
|
|
158
160
|
|
|
161
|
+
/**
|
|
162
|
+
* The function sorts the elements in a data structure and returns them in ascending order.
|
|
163
|
+
* @returns The `sort()` function is returning an array of type `T[]`.
|
|
164
|
+
*/
|
|
159
165
|
sort(): T[] {
|
|
160
166
|
const visitedNode: T[] = [];
|
|
161
167
|
while (this.size !== 0) {
|
|
@@ -165,6 +171,13 @@ export class PriorityQueue<T = number> {
|
|
|
165
171
|
return visitedNode;
|
|
166
172
|
}
|
|
167
173
|
|
|
174
|
+
/**
|
|
175
|
+
* The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
|
|
176
|
+
* based on the specified traversal order.
|
|
177
|
+
* @param {PriorityQueueDFSOrderPattern} dfsMode - The dfsMode parameter is a string that specifies the order in which
|
|
178
|
+
* the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values:
|
|
179
|
+
* @returns an array of type `(T | null)[]`.
|
|
180
|
+
*/
|
|
168
181
|
DFS(dfsMode: PriorityQueueDFSOrderPattern): (T | null)[] {
|
|
169
182
|
const visitedNode: (T | null)[] = [];
|
|
170
183
|
|
|
@@ -194,14 +207,123 @@ export class PriorityQueue<T = number> {
|
|
|
194
207
|
return visitedNode;
|
|
195
208
|
}
|
|
196
209
|
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
210
|
+
protected readonly _comparator: PriorityQueueComparator<T> = (a: T, b: T) => {
|
|
211
|
+
const aKey = a as unknown as number, bKey = b as unknown as number;
|
|
212
|
+
return aKey - bKey;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The function compares two numbers using a custom comparator function.
|
|
217
|
+
* @param {number} a - The parameter "a" is a number that represents the index of a node in an array.
|
|
218
|
+
* @param {number} b - The parameter "b" is a number.
|
|
219
|
+
* @returns the result of the comparison between the elements at indices `a` and `b` in the `nodes` array. The
|
|
220
|
+
* comparison is done using the `_comparator` function, and if the result is greater than 0, `true` is returned,
|
|
221
|
+
* indicating that the element at index `a` is greater than the element at index `b`.
|
|
222
|
+
*/
|
|
223
|
+
protected _compare(a: number, b: number) {
|
|
224
|
+
return this._comparator(this.nodes[a], this.nodes[b]) > 0;
|
|
201
225
|
}
|
|
202
226
|
|
|
203
|
-
|
|
204
|
-
|
|
227
|
+
/**
|
|
228
|
+
* The function swaps two elements in an array.
|
|
229
|
+
* @param {number} a - The parameter "a" is a number that represents the index of an element in an array.
|
|
230
|
+
* @param {number} b - The parameter "b" is a number.
|
|
231
|
+
*/
|
|
232
|
+
protected _swap(a: number, b: number) {
|
|
233
|
+
const temp = this.nodes[a];
|
|
234
|
+
this.nodes[a] = this.nodes[b];
|
|
235
|
+
this.nodes[b] = temp;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* The function checks if a given index is valid within an array.
|
|
240
|
+
* @param {number} index - The parameter "index" is of type number and represents the index value that needs to be
|
|
241
|
+
* checked for validity.
|
|
242
|
+
* @returns A boolean value indicating whether the given index is valid or not.
|
|
243
|
+
*/
|
|
244
|
+
protected _isValidIndex(index: number): boolean {
|
|
245
|
+
return index > -1 && index < this.nodes.length;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* The function returns the index of the parent node given the index of a child node in a binary tree.
|
|
250
|
+
* @param {number} child - The "child" parameter is a number representing the index of a child node in a binary tree.
|
|
251
|
+
* @returns the parent of the given child node.
|
|
252
|
+
*/
|
|
253
|
+
protected _getParent(child: number): number {
|
|
254
|
+
return Math.floor((child - 1) / 2);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The function returns the index of the left child node in a binary tree given the index of its parent node.
|
|
259
|
+
* @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
|
|
260
|
+
* @returns the left child of a given parent node in a binary tree.
|
|
261
|
+
*/
|
|
262
|
+
protected _getLeft(parent: number): number {
|
|
263
|
+
return (2 * parent) + 1;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* The function returns the index of the right child node in a binary tree given the index of its parent node.
|
|
268
|
+
* @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
|
|
269
|
+
* @returns the right child of a given parent node in a binary tree.
|
|
270
|
+
*/
|
|
271
|
+
protected _getRight(parent: number): number {
|
|
272
|
+
return (2 * parent) + 2;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* The function returns the index of the smallest child node of a given parent node.
|
|
277
|
+
* @param {number} parent - The parent parameter is a number that represents the index of the parent node in a binary
|
|
278
|
+
* tree.
|
|
279
|
+
* @returns the minimum value between the parent node and its left and right child nodes.
|
|
280
|
+
*/
|
|
281
|
+
protected _getComparedChild(parent: number) {
|
|
282
|
+
let min = parent;
|
|
283
|
+
const left = this._getLeft(parent), right = this._getRight(parent);
|
|
284
|
+
|
|
285
|
+
if (left < this.size && this._compare(min, left)) {
|
|
286
|
+
min = left;
|
|
287
|
+
}
|
|
288
|
+
if (right < this.size && this._compare(min, right)) {
|
|
289
|
+
min = right;
|
|
290
|
+
}
|
|
291
|
+
return min;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* The function `_heapifyUp` is used to maintain the heap property by moving an element up the heap until it is in the
|
|
296
|
+
* correct position.
|
|
297
|
+
* @param {number} start - The start parameter is the index of the element that needs to be moved up in the heap.
|
|
298
|
+
*/
|
|
299
|
+
protected _heapifyUp(start: number) {
|
|
300
|
+
while (start > 0 && this._compare(this._getParent(start), start)) {
|
|
301
|
+
const parent = this._getParent(start);
|
|
302
|
+
this._swap(start, parent);
|
|
303
|
+
start = parent;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* The function performs a heapify operation by comparing and swapping elements in a binary heap.
|
|
309
|
+
* @param {number} start - The start parameter is the index of the element in the heap from where the heapifyDown
|
|
310
|
+
* operation should start.
|
|
311
|
+
*/
|
|
312
|
+
protected _heapifyDown(start: number) {
|
|
313
|
+
let min = this._getComparedChild(start);
|
|
314
|
+
while (this._compare(start, min)) {
|
|
315
|
+
this._swap(min, start);
|
|
316
|
+
start = min;
|
|
317
|
+
min = this._getComparedChild(start);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* The _fix function performs a heapify operation on the elements of the heap starting from the middle and moving
|
|
323
|
+
* towards the root.
|
|
324
|
+
*/
|
|
325
|
+
protected _fix() {
|
|
326
|
+
for (let i = Math.floor(this.size / 2); i > -1; i--) this._heapifyDown(i);
|
|
205
327
|
}
|
|
206
328
|
|
|
207
329
|
// --- end additional methods ---
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
import {DoublyLinkedList} from '../linked-list';
|
|
2
6
|
|
|
3
7
|
// O(n) time complexity of obtaining the value
|
|
@@ -50,7 +54,7 @@ export class ObjectDeque<T> {
|
|
|
50
54
|
|
|
51
55
|
pollFirst() {
|
|
52
56
|
if (!this._size) return;
|
|
53
|
-
|
|
57
|
+
const value = this.peekFirst();
|
|
54
58
|
delete this._nodes[this._first];
|
|
55
59
|
this._first++;
|
|
56
60
|
this._size--;
|
|
@@ -63,7 +67,7 @@ export class ObjectDeque<T> {
|
|
|
63
67
|
|
|
64
68
|
pollLast() {
|
|
65
69
|
if (!this._size) return;
|
|
66
|
-
|
|
70
|
+
const value = this.peekLast();
|
|
67
71
|
delete this._nodes[this._last];
|
|
68
72
|
this._last--;
|
|
69
73
|
this._size--;
|
|
@@ -93,46 +97,111 @@ export class ArrayDeque<T> {
|
|
|
93
97
|
return this._nodes.length;
|
|
94
98
|
}
|
|
95
99
|
|
|
100
|
+
/**
|
|
101
|
+
* The function "offerLast" adds a value to the end of an array.
|
|
102
|
+
* @param {T} value - The value parameter represents the value that you want to add to the end of the array.
|
|
103
|
+
* @returns The return value is the new length of the array after the value has been added.
|
|
104
|
+
*/
|
|
96
105
|
offerLast(value: T) {
|
|
97
106
|
return this._nodes.push(value);
|
|
98
107
|
}
|
|
99
108
|
|
|
109
|
+
/**
|
|
110
|
+
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
111
|
+
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
112
|
+
*/
|
|
100
113
|
pollLast(): T | null {
|
|
101
114
|
return this._nodes.pop() ?? null;
|
|
102
115
|
}
|
|
103
116
|
|
|
117
|
+
/**
|
|
118
|
+
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
119
|
+
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
120
|
+
* empty.
|
|
121
|
+
*/
|
|
104
122
|
pollFirst(): T | null {
|
|
105
123
|
return this._nodes.shift() ?? null;
|
|
106
124
|
}
|
|
107
125
|
|
|
126
|
+
/**
|
|
127
|
+
* The function "offerFirst" adds a value to the beginning of an array.
|
|
128
|
+
* @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
129
|
+
* @returns The return value of the `offerFirst` function is the new length of the array `_nodes` after adding the
|
|
130
|
+
* `value` at the beginning.
|
|
131
|
+
*/
|
|
108
132
|
offerFirst(value: T) {
|
|
109
133
|
return this._nodes.unshift(value);
|
|
110
134
|
}
|
|
111
135
|
|
|
136
|
+
/**
|
|
137
|
+
* The `peekFirst` function returns the first element of an array or null if the array is empty.
|
|
138
|
+
* @returns The function `peekFirst()` is returning the first element (`T`) of the `_nodes` array. If the array is
|
|
139
|
+
* empty, it will return `null`.
|
|
140
|
+
*/
|
|
112
141
|
peekFirst(): T | null {
|
|
113
142
|
return this._nodes[0] ?? null;
|
|
114
143
|
}
|
|
115
144
|
|
|
145
|
+
/**
|
|
146
|
+
* The `peekLast` function returns the last element of an array or null if the array is empty.
|
|
147
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
148
|
+
*/
|
|
116
149
|
peekLast(): T | null {
|
|
117
150
|
return this._nodes[this._nodes.length - 1] ?? null;
|
|
118
151
|
}
|
|
119
152
|
|
|
153
|
+
/**
|
|
154
|
+
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
155
|
+
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
156
|
+
* retrieve from the array.
|
|
157
|
+
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
158
|
+
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
159
|
+
*/
|
|
120
160
|
get(index: number): T | null {
|
|
121
161
|
return this._nodes[index] ?? null;
|
|
122
162
|
}
|
|
123
163
|
|
|
164
|
+
/**
|
|
165
|
+
* The set function assigns a value to a specific index in an array.
|
|
166
|
+
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
167
|
+
* that you want to set a new value for.
|
|
168
|
+
* @param {T} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
169
|
+
* _nodes array.
|
|
170
|
+
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
171
|
+
*/
|
|
124
172
|
set(index: number, value: T) {
|
|
125
173
|
return this._nodes[index] = value;
|
|
126
174
|
}
|
|
127
175
|
|
|
176
|
+
/**
|
|
177
|
+
* The insert function adds a value at a specified index in an array.
|
|
178
|
+
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
179
|
+
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
180
|
+
* from 0, so the first element of the array has an index of 0, the second element has
|
|
181
|
+
* @param {T} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
182
|
+
* index.
|
|
183
|
+
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
184
|
+
* are being removed, an empty array will be returned.
|
|
185
|
+
*/
|
|
128
186
|
insert(index: number, value: T) {
|
|
129
187
|
return this._nodes.splice(index, 0, value);
|
|
130
188
|
}
|
|
131
189
|
|
|
190
|
+
/**
|
|
191
|
+
* The remove function removes an element from an array at a specified index.
|
|
192
|
+
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
193
|
+
* is a number that represents the index of the element to be removed.
|
|
194
|
+
* @returns The method is returning an array containing the removed element.
|
|
195
|
+
*/
|
|
132
196
|
remove(index: number) {
|
|
133
197
|
return this._nodes.splice(index, 1);
|
|
134
198
|
}
|
|
135
199
|
|
|
200
|
+
/**
|
|
201
|
+
* The function checks if an array called "_nodes" is empty.
|
|
202
|
+
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
203
|
+
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
204
|
+
*/
|
|
136
205
|
isEmpty() {
|
|
137
206
|
return this._nodes.length === 0;
|
|
138
207
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license MIT
|
|
3
|
-
* @copyright
|
|
4
|
-
*
|
|
3
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
5
4
|
* @class
|
|
6
5
|
*/
|
|
7
6
|
export class Queue<T> {
|
|
@@ -9,8 +8,10 @@ export class Queue<T> {
|
|
|
9
8
|
protected _offset: number;
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @param {
|
|
11
|
+
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
12
|
+
* @param {T[]} [elements] - The `elements` parameter is an optional array of elements of type `T`. If provided, it
|
|
13
|
+
* will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
|
|
14
|
+
* initialized as an empty array.
|
|
14
15
|
*/
|
|
15
16
|
constructor(elements?: T[]) {
|
|
16
17
|
this._nodes = elements || [];
|
|
@@ -18,9 +19,21 @@ export class Queue<T> {
|
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
|
-
*
|
|
22
|
+
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
22
23
|
* @public
|
|
23
|
-
* @
|
|
24
|
+
* @static
|
|
25
|
+
* @param {T[]} elements - The "elements" parameter is an array of elements of type T.
|
|
26
|
+
* @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
|
|
27
|
+
* array.
|
|
28
|
+
*/
|
|
29
|
+
static fromArray<T>(elements: T[]): Queue<T> {
|
|
30
|
+
return new Queue(elements);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The offer function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
|
|
35
|
+
* @param {T} element - The `element` parameter represents the element that you want to add to the queue.
|
|
36
|
+
* @returns The `offer` method is returning a `Queue<T>` object.
|
|
24
37
|
*/
|
|
25
38
|
offer(element: T): Queue<T> {
|
|
26
39
|
this._nodes.push(element);
|
|
@@ -28,9 +41,9 @@ export class Queue<T> {
|
|
|
28
41
|
}
|
|
29
42
|
|
|
30
43
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* @returns
|
|
44
|
+
* The `poll` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
45
|
+
* necessary to optimize performance.
|
|
46
|
+
* @returns The function `poll()` returns either the first element in the queue or `null` if the queue is empty.
|
|
34
47
|
*/
|
|
35
48
|
poll(): T | null {
|
|
36
49
|
if (this.size() === 0) return null;
|
|
@@ -48,53 +61,49 @@ export class Queue<T> {
|
|
|
48
61
|
}
|
|
49
62
|
|
|
50
63
|
/**
|
|
51
|
-
*
|
|
52
|
-
* @
|
|
53
|
-
*
|
|
64
|
+
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
65
|
+
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
66
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
54
67
|
*/
|
|
55
68
|
peek(): T | null {
|
|
56
69
|
return this.size() > 0 ? this._nodes[this._offset] : null;
|
|
57
70
|
}
|
|
58
71
|
|
|
59
72
|
/**
|
|
60
|
-
*
|
|
61
|
-
* @
|
|
62
|
-
*
|
|
73
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
74
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
75
|
+
* array is empty, it returns `null`.
|
|
63
76
|
*/
|
|
64
77
|
peekLast(): T | null {
|
|
65
78
|
return this.size() > 0 ? this._nodes[this._nodes.length - 1] : null;
|
|
66
79
|
}
|
|
67
80
|
|
|
68
81
|
/**
|
|
69
|
-
*
|
|
70
|
-
* @
|
|
71
|
-
* @returns {number}
|
|
82
|
+
* The size function returns the number of elements in an array.
|
|
83
|
+
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
72
84
|
*/
|
|
73
85
|
size(): number {
|
|
74
86
|
return this._nodes.length - this._offset;
|
|
75
87
|
}
|
|
76
88
|
|
|
77
89
|
/**
|
|
78
|
-
*
|
|
79
|
-
* @
|
|
80
|
-
* @returns {boolean}
|
|
90
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
91
|
+
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
|
|
81
92
|
*/
|
|
82
93
|
isEmpty(): boolean {
|
|
83
94
|
return this.size() === 0;
|
|
84
95
|
}
|
|
85
96
|
|
|
86
97
|
/**
|
|
87
|
-
*
|
|
88
|
-
* @
|
|
89
|
-
* @returns {array}
|
|
98
|
+
* The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
|
|
99
|
+
* @returns An array of type T is being returned.
|
|
90
100
|
*/
|
|
91
101
|
toArray(): T[] {
|
|
92
102
|
return this._nodes.slice(this._offset);
|
|
93
103
|
}
|
|
94
104
|
|
|
95
105
|
/**
|
|
96
|
-
*
|
|
97
|
-
* @public
|
|
106
|
+
* The clear function resets the nodes array and offset to their initial values.
|
|
98
107
|
*/
|
|
99
108
|
clear(): void {
|
|
100
109
|
this._nodes = [];
|
|
@@ -102,22 +111,10 @@ export class Queue<T> {
|
|
|
102
111
|
}
|
|
103
112
|
|
|
104
113
|
/**
|
|
105
|
-
*
|
|
106
|
-
* @
|
|
107
|
-
* @return {Queue}
|
|
114
|
+
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
115
|
+
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
108
116
|
*/
|
|
109
117
|
clone(): Queue<T> {
|
|
110
118
|
return new Queue(this._nodes.slice(this._offset));
|
|
111
119
|
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Creates a queue from an existing array.
|
|
115
|
-
* @public
|
|
116
|
-
* @static
|
|
117
|
-
* @param {array} elements
|
|
118
|
-
* @return {Queue}
|
|
119
|
-
*/
|
|
120
|
-
static fromArray<T>(elements: T[]): Queue<T> {
|
|
121
|
-
return new Queue(elements);
|
|
122
|
-
}
|
|
123
120
|
}
|