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,30 +1,18 @@
|
|
|
1
|
-
import {PriorityQueue} from '../priority-queue';
|
|
2
|
-
|
|
3
|
-
export interface HeapOptions<T> {
|
|
4
|
-
priority?: (element: T) => number;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface HeapItem<T> {
|
|
8
|
-
priority: number;
|
|
9
|
-
element: T | null;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
1
|
/**
|
|
14
|
-
* @copyright
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
15
3
|
* @license MIT
|
|
16
|
-
*
|
|
17
|
-
* @abstract
|
|
18
|
-
* @class Heap
|
|
19
4
|
*/
|
|
5
|
+
import {PriorityQueue} from '../priority-queue';
|
|
6
|
+
import type {HeapItem, HeapOptions} from '../types';
|
|
7
|
+
|
|
20
8
|
export abstract class Heap<T> {
|
|
21
9
|
protected abstract _pq: PriorityQueue<HeapItem<T>>;
|
|
22
10
|
protected _priorityCb: (element: T) => number;
|
|
23
11
|
|
|
24
12
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* @
|
|
13
|
+
* The function is a constructor for a class that initializes a priority callback function based on the
|
|
14
|
+
* options provided.
|
|
15
|
+
* @param [options] - An optional object that contains configuration options for the Heap.
|
|
28
16
|
*/
|
|
29
17
|
protected constructor(options?: HeapOptions<T>) {
|
|
30
18
|
if (options) {
|
|
@@ -39,44 +27,45 @@ export abstract class Heap<T> {
|
|
|
39
27
|
}
|
|
40
28
|
|
|
41
29
|
/**
|
|
42
|
-
*
|
|
43
|
-
* @returns
|
|
30
|
+
* The function returns the size of a priority queue.
|
|
31
|
+
* @returns The size of the priority queue.
|
|
44
32
|
*/
|
|
45
33
|
get size(): number {
|
|
46
34
|
return this._pq.size;
|
|
47
35
|
}
|
|
48
36
|
|
|
49
37
|
/**
|
|
50
|
-
*
|
|
51
|
-
* @returns {boolean}
|
|
38
|
+
* The function checks if a priority queue is empty.
|
|
39
|
+
* @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
|
|
52
40
|
*/
|
|
53
41
|
isEmpty(): boolean {
|
|
54
42
|
return this._pq.size < 1;
|
|
55
43
|
}
|
|
56
44
|
|
|
57
45
|
/**
|
|
58
|
-
*
|
|
59
|
-
* @
|
|
60
|
-
* @returns {object}
|
|
46
|
+
* The `peek` function returns the top item in the priority queue without removing it.
|
|
47
|
+
* @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an element with the highest priority in the queue
|
|
61
48
|
*/
|
|
62
49
|
peek(): HeapItem<T> | null {
|
|
63
50
|
return this._pq.peek();
|
|
64
51
|
}
|
|
65
52
|
|
|
66
53
|
/**
|
|
67
|
-
*
|
|
68
|
-
* @
|
|
69
|
-
* @returns {object}
|
|
54
|
+
* The `peekLast` function returns the last item in the heap.
|
|
55
|
+
* @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an element with the lowest priority in the queue
|
|
70
56
|
*/
|
|
71
57
|
peekLast(): HeapItem<T> | null {
|
|
72
58
|
return this._pq.leaf();
|
|
73
59
|
}
|
|
74
60
|
|
|
75
61
|
/**
|
|
76
|
-
*
|
|
77
|
-
* @
|
|
78
|
-
*
|
|
79
|
-
* @param priority
|
|
62
|
+
* The `offer` function adds an element to a priority queue with an optional priority value.
|
|
63
|
+
* @param {T} element - The `element` parameter represents the value that you want to add to the heap. It can be of any
|
|
64
|
+
* type.
|
|
65
|
+
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
66
|
+
* element being offered to the heap. If the `element` parameter is a number, then the `priority` parameter is set to
|
|
67
|
+
* the value of `element`. If the `element` parameter is not a number, then the
|
|
68
|
+
* @returns The `offer` method returns the instance of the `Heap` class.
|
|
80
69
|
* @throws {Error} if priority is not a valid number
|
|
81
70
|
*/
|
|
82
71
|
offer(element: T, priority?: number): Heap<T> {
|
|
@@ -105,9 +94,8 @@ export abstract class Heap<T> {
|
|
|
105
94
|
}
|
|
106
95
|
|
|
107
96
|
/**
|
|
108
|
-
* Removes and returns an element with highest priority in the queue
|
|
109
|
-
* @
|
|
110
|
-
* @returns {object}
|
|
97
|
+
* The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an element with the highest priority in the queue
|
|
98
|
+
* @returns either a HeapItem<T> object or null.
|
|
111
99
|
*/
|
|
112
100
|
poll(): HeapItem<T> | null {
|
|
113
101
|
const top = this._pq.poll();
|
|
@@ -118,17 +106,15 @@ export abstract class Heap<T> {
|
|
|
118
106
|
}
|
|
119
107
|
|
|
120
108
|
/**
|
|
121
|
-
*
|
|
122
|
-
* @
|
|
123
|
-
* @returns {array}
|
|
109
|
+
* The `toArray` function returns an array of `HeapItem<T>` objects.
|
|
110
|
+
* @returns An array of HeapItem<T> objects.Returns a sorted list of elements
|
|
124
111
|
*/
|
|
125
112
|
toArray(): HeapItem<T>[] {
|
|
126
113
|
return this._pq.toArray();
|
|
127
114
|
}
|
|
128
115
|
|
|
129
116
|
/**
|
|
130
|
-
*
|
|
131
|
-
* @public
|
|
117
|
+
* The clear function clears the priority queue.
|
|
132
118
|
*/
|
|
133
119
|
clear(): void {
|
|
134
120
|
this._pq.clear();
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @copyright
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
3
|
* @license MIT
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {Heap
|
|
6
|
+
import {Heap} from './heap';
|
|
7
7
|
import {PriorityQueue} from '../priority-queue';
|
|
8
|
+
import type {HeapItem, HeapOptions} from '../types';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* @class MaxHeap
|
|
@@ -13,6 +14,11 @@ import {PriorityQueue} from '../priority-queue';
|
|
|
13
14
|
export class MaxHeap<T> extends Heap<T> {
|
|
14
15
|
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
15
16
|
|
|
17
|
+
/**
|
|
18
|
+
* The constructor initializes a PriorityQueue with a custom comparator function.
|
|
19
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
20
|
+
* type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
|
|
21
|
+
*/
|
|
16
22
|
constructor(options?: HeapOptions<T>) {
|
|
17
23
|
super(options);
|
|
18
24
|
this._pq = new PriorityQueue<HeapItem<T>>({
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @copyright
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
3
|
* @license MIT
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {Heap
|
|
6
|
+
import {Heap} from './heap';
|
|
7
7
|
import {PriorityQueue} from '../priority-queue';
|
|
8
|
+
import type {HeapItem, HeapOptions} from '../types';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* @class MinHeap
|
|
@@ -13,6 +14,12 @@ import {PriorityQueue} from '../priority-queue';
|
|
|
13
14
|
export class MinHeap<T> extends Heap<T> {
|
|
14
15
|
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
15
16
|
|
|
17
|
+
/**
|
|
18
|
+
* The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
|
|
19
|
+
* objects.
|
|
20
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
21
|
+
* type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
|
|
22
|
+
*/
|
|
16
23
|
constructor(options?: HeapOptions<T>) {
|
|
17
24
|
super(options);
|
|
18
25
|
this._pq = new PriorityQueue<HeapItem<T>>({
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
// 查看尾部 Last_Element peekLast [length - 1] back [-1] $array[-1] end last
|
|
7
|
-
// 查看头部 First_Element peekFirst [0] front [0] $array[0] reset first
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
import type {DoublyLinkedListGetBy} from '../types';
|
|
8
6
|
|
|
9
7
|
export class DoublyLinkedListNode<T> {
|
|
10
8
|
val: T;
|
|
@@ -18,8 +16,6 @@ export class DoublyLinkedListNode<T> {
|
|
|
18
16
|
}
|
|
19
17
|
}
|
|
20
18
|
|
|
21
|
-
export type DoublyLinkedListGetBy = 'node' | 'val';
|
|
22
|
-
|
|
23
19
|
export class DoublyLinkedList<T> {
|
|
24
20
|
private _first: DoublyLinkedListNode<T> | null = null;
|
|
25
21
|
private _last: DoublyLinkedListNode<T> | null = null;
|
|
@@ -33,8 +29,10 @@ export class DoublyLinkedList<T> {
|
|
|
33
29
|
}
|
|
34
30
|
|
|
35
31
|
/**
|
|
36
|
-
*
|
|
37
|
-
* @param val
|
|
32
|
+
* The function adds a new node with a given value to the beginning of a doubly linked list.
|
|
33
|
+
* @param {T} val - The `val` parameter represents the value of the element that you want to add to the beginning of
|
|
34
|
+
* the doubly linked list.
|
|
35
|
+
* @returns A boolean value is being returned.
|
|
38
36
|
*/
|
|
39
37
|
offerFirst(val: T): boolean {
|
|
40
38
|
const newNode = new DoublyLinkedListNode(val);
|
|
@@ -51,8 +49,10 @@ export class DoublyLinkedList<T> {
|
|
|
51
49
|
}
|
|
52
50
|
|
|
53
51
|
/**
|
|
54
|
-
*
|
|
55
|
-
* @param val
|
|
52
|
+
* The function adds a new node with a given value to the end of a doubly linked list.
|
|
53
|
+
* @param {T} val - The `val` parameter represents the value of the element that you want to add to the end of the
|
|
54
|
+
* doubly linked list.
|
|
55
|
+
* @returns a boolean value, which is always true.
|
|
56
56
|
*/
|
|
57
57
|
offerLast(val: T): boolean {
|
|
58
58
|
const newNode = new DoublyLinkedListNode(val);
|
|
@@ -71,6 +71,14 @@ export class DoublyLinkedList<T> {
|
|
|
71
71
|
peekFirst(): T | null;
|
|
72
72
|
peekFirst(by: 'val'): T | null;
|
|
73
73
|
peekFirst(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
74
|
+
/**
|
|
75
|
+
* The `peekFirst` function returns the first node or value in a doubly linked list, depending on the specified
|
|
76
|
+
* parameter.
|
|
77
|
+
* @param {DoublyLinkedListGetBy} [by] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy. It
|
|
78
|
+
* is used to specify whether to return the first node, the value of the first node, or the first node itself.
|
|
79
|
+
* @returns The method `peekFirst` returns either the first node of the doubly linked list (`DoublyLinkedListNode<T>`),
|
|
80
|
+
* the value of the first node (`T`), or `null` depending on the value of the `by` parameter.
|
|
81
|
+
*/
|
|
74
82
|
peekFirst(by?: DoublyLinkedListGetBy): T | DoublyLinkedListNode<T> | null {
|
|
75
83
|
switch (by) {
|
|
76
84
|
case 'node':
|
|
@@ -85,6 +93,13 @@ export class DoublyLinkedList<T> {
|
|
|
85
93
|
peekLast(): T | null;
|
|
86
94
|
peekLast(by: 'val'): T | null;
|
|
87
95
|
peekLast(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
96
|
+
/**
|
|
97
|
+
* The `peekLast` function returns the last node or value in a doubly linked list.
|
|
98
|
+
* @param {DoublyLinkedListGetBy} [by=val] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy.
|
|
99
|
+
* It specifies whether to return the last node, the value of the last node, or both. The default value is 'val', which
|
|
100
|
+
* means that if no value is provided for the "by" parameter, the method
|
|
101
|
+
* @returns The method `peekLast` returns the last node, value, or null based on the specified `by` parameter.
|
|
102
|
+
*/
|
|
88
103
|
peekLast(by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
|
|
89
104
|
switch (by) {
|
|
90
105
|
case 'node':
|
|
@@ -100,7 +115,14 @@ export class DoublyLinkedList<T> {
|
|
|
100
115
|
pollFirst(by: 'val'): T | null;
|
|
101
116
|
pollFirst(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
102
117
|
/**
|
|
103
|
-
*
|
|
118
|
+
* The function `pollFirst` removes and returns the first element of a doubly linked list, either as a node or its
|
|
119
|
+
* value, depending on the specified parameter.
|
|
120
|
+
* @param {DoublyLinkedListGetBy} [by=val] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy.
|
|
121
|
+
* It specifies the criteria by which the first element should be retrieved from the doubly linked list. The default
|
|
122
|
+
* value is 'val', which means the first element will be retrieved by its value. Other possible values for "by
|
|
123
|
+
* @returns The method `pollFirst` returns either the value of the first node in the doubly linked list, the first node
|
|
124
|
+
* itself, or null if the list is empty. The specific return type depends on the value of the `by` parameter. If `by`
|
|
125
|
+
* is set to 'node', the method returns the first node. If `by` is set to 'val', the method returns the value
|
|
104
126
|
*/
|
|
105
127
|
pollFirst(by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
|
|
106
128
|
if (this._size === 0) return null;
|
|
@@ -128,7 +150,14 @@ export class DoublyLinkedList<T> {
|
|
|
128
150
|
pollLast(by: 'val'): T | null;
|
|
129
151
|
pollLast(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
130
152
|
/**
|
|
131
|
-
*
|
|
153
|
+
* The function `pollLast` removes and returns the last element in a doubly linked list, either as a node or its value,
|
|
154
|
+
* depending on the specified parameter.
|
|
155
|
+
* @param {DoublyLinkedListGetBy} [by=val] - The parameter "by" is of type DoublyLinkedListGetBy, which is an enum that
|
|
156
|
+
* can have two possible values: 'node' or 'val'. It determines the type of value that will be returned by the pollLast
|
|
157
|
+
* method. If 'node' is specified, the method will return the
|
|
158
|
+
* @returns The method `pollLast` returns either a `DoublyLinkedListNode<T>`, the value of the node (`T`), or `null`.
|
|
159
|
+
* The specific type that is returned depends on the value of the `by` parameter. If `by` is set to `'node'`, then a
|
|
160
|
+
* `DoublyLinkedListNode<T>` is returned. If `by` is set to `'
|
|
132
161
|
*/
|
|
133
162
|
pollLast(by: DoublyLinkedListGetBy = 'val'): DoublyLinkedListNode<T> | T | null {
|
|
134
163
|
if (this._size === 0) return null;
|
|
@@ -234,8 +263,12 @@ export class DoublyLinkedList<T> {
|
|
|
234
263
|
}
|
|
235
264
|
|
|
236
265
|
/**
|
|
237
|
-
*
|
|
238
|
-
*
|
|
266
|
+
* The `remove` function removes an element at a specified index from a data structure, updating the links between
|
|
267
|
+
* nodes accordingly.
|
|
268
|
+
* @param {number} index - The index parameter represents the position of the element to be removed in the data
|
|
269
|
+
* structure. It is of type number.
|
|
270
|
+
* @returns The `remove` method returns the value of the removed element (`T`) if the removal is successful, or `null`
|
|
271
|
+
* if the index is out of bounds.
|
|
239
272
|
*/
|
|
240
273
|
remove(index: number): T | null {
|
|
241
274
|
if (index < 0 || index > this._size - 1) return null;
|
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
list: SinglyLinkedList<NodeData>,
|
|
6
|
-
) => boolean;
|
|
7
|
-
|
|
8
|
-
/** Type used for map and forEach methods, returning anything */
|
|
9
|
-
type TMapFunction<NodeData> = (
|
|
10
|
-
data: any,
|
|
11
|
-
index: number,
|
|
12
|
-
list: SinglyLinkedList<NodeData>,
|
|
13
|
-
) => any;
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
14
5
|
|
|
15
6
|
/**
|
|
16
7
|
* The class which represents one link or node in a linked list
|
|
@@ -104,6 +95,23 @@ export class SinglyLinkedListNode<NodeData = any> {
|
|
|
104
95
|
*/
|
|
105
96
|
export class SinglyLinkedList<NodeData = any> {
|
|
106
97
|
|
|
98
|
+
/** The head of the list, the first node */
|
|
99
|
+
public head: SinglyLinkedListNode<NodeData> | null;
|
|
100
|
+
/** The tail of the list, the last node */
|
|
101
|
+
public tail: SinglyLinkedListNode<NodeData> | null;
|
|
102
|
+
/** Internal size reference */
|
|
103
|
+
private size: number;
|
|
104
|
+
|
|
105
|
+
constructor(...args: NodeData[]) {
|
|
106
|
+
this.head = null;
|
|
107
|
+
this.tail = null;
|
|
108
|
+
this.size = 0;
|
|
109
|
+
|
|
110
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
111
|
+
this.append(args[i]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
107
115
|
/**
|
|
108
116
|
* The length of the list
|
|
109
117
|
*/
|
|
@@ -123,25 +131,6 @@ export class SinglyLinkedList<NodeData = any> {
|
|
|
123
131
|
return new SinglyLinkedList(...iterable);
|
|
124
132
|
}
|
|
125
133
|
|
|
126
|
-
/** The head of the list, the first node */
|
|
127
|
-
public head: SinglyLinkedListNode<NodeData> | null;
|
|
128
|
-
|
|
129
|
-
/** The tail of the list, the last node */
|
|
130
|
-
public tail: SinglyLinkedListNode<NodeData> | null;
|
|
131
|
-
|
|
132
|
-
/** Internal size reference */
|
|
133
|
-
private size: number;
|
|
134
|
-
|
|
135
|
-
constructor(...args: NodeData[]) {
|
|
136
|
-
this.head = null;
|
|
137
|
-
this.tail = null;
|
|
138
|
-
this.size = 0;
|
|
139
|
-
|
|
140
|
-
for (let i = 0; i < arguments.length; i++) {
|
|
141
|
-
this.append(args[i]);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
134
|
/**
|
|
146
135
|
* Get the node val at a specified index, zero based
|
|
147
136
|
* ```ts
|
|
@@ -187,7 +176,11 @@ export class SinglyLinkedList<NodeData = any> {
|
|
|
187
176
|
* ```
|
|
188
177
|
* @param f A function to be applied to the val of each node
|
|
189
178
|
*/
|
|
190
|
-
public findNodeIndex(f:
|
|
179
|
+
public findNodeIndex(f: (
|
|
180
|
+
data: NodeData,
|
|
181
|
+
index: number,
|
|
182
|
+
list: SinglyLinkedList<NodeData>,
|
|
183
|
+
) => boolean): ({
|
|
191
184
|
node: SinglyLinkedListNode<NodeData>,
|
|
192
185
|
index: number,
|
|
193
186
|
}) | undefined {
|
|
@@ -215,7 +208,11 @@ export class SinglyLinkedList<NodeData = any> {
|
|
|
215
208
|
* ```
|
|
216
209
|
* @param f Function to test val against
|
|
217
210
|
*/
|
|
218
|
-
public findNode(f:
|
|
211
|
+
public findNode(f: (
|
|
212
|
+
data: NodeData,
|
|
213
|
+
index: number,
|
|
214
|
+
list: SinglyLinkedList<NodeData>,
|
|
215
|
+
) => boolean): SinglyLinkedListNode<NodeData> | undefined {
|
|
219
216
|
const nodeIndex = this.findNodeIndex(f);
|
|
220
217
|
return nodeIndex !== undefined ? nodeIndex.node : undefined;
|
|
221
218
|
}
|
|
@@ -228,7 +225,11 @@ export class SinglyLinkedList<NodeData = any> {
|
|
|
228
225
|
* ```
|
|
229
226
|
* @param f Function to test val against
|
|
230
227
|
*/
|
|
231
|
-
public find(f:
|
|
228
|
+
public find(f: (
|
|
229
|
+
data: NodeData,
|
|
230
|
+
index: number,
|
|
231
|
+
list: SinglyLinkedList<NodeData>,
|
|
232
|
+
) => boolean): NodeData | undefined {
|
|
232
233
|
const nodeIndex = this.findNodeIndex(f);
|
|
233
234
|
return nodeIndex !== undefined ? nodeIndex.node.val : undefined;
|
|
234
235
|
}
|
|
@@ -241,7 +242,11 @@ export class SinglyLinkedList<NodeData = any> {
|
|
|
241
242
|
* ```
|
|
242
243
|
* @param f Function to test val against
|
|
243
244
|
*/
|
|
244
|
-
public findIndex(f:
|
|
245
|
+
public findIndex(f: (
|
|
246
|
+
data: NodeData,
|
|
247
|
+
index: number,
|
|
248
|
+
list: SinglyLinkedList<NodeData>,
|
|
249
|
+
) => boolean): number {
|
|
245
250
|
const nodeIndex = this.findNodeIndex(f);
|
|
246
251
|
return nodeIndex !== undefined ? nodeIndex.index : -1;
|
|
247
252
|
}
|
|
@@ -615,7 +620,11 @@ export class SinglyLinkedList<NodeData = any> {
|
|
|
615
620
|
* @param f Function to execute for each element, taking up to three arguments.
|
|
616
621
|
* @param reverse Indicates if the list should be walked in reverse order, default is false
|
|
617
622
|
*/
|
|
618
|
-
public forEach(f:
|
|
623
|
+
public forEach(f: (
|
|
624
|
+
data: any,
|
|
625
|
+
index: number,
|
|
626
|
+
list: SinglyLinkedList<NodeData>,
|
|
627
|
+
) => any, reverse = false): void {
|
|
619
628
|
let currentIndex = reverse ? this.length - 1 : 0;
|
|
620
629
|
let currentNode = reverse ? this.tail : this.head;
|
|
621
630
|
const modifier = reverse ? -1 : 1;
|
|
@@ -637,7 +646,11 @@ export class SinglyLinkedList<NodeData = any> {
|
|
|
637
646
|
* @param reverse Indicates if the list should be mapped in reverse order, default is false
|
|
638
647
|
*/
|
|
639
648
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
640
|
-
public map(f:
|
|
649
|
+
public map(f: (
|
|
650
|
+
data: any,
|
|
651
|
+
index: number,
|
|
652
|
+
list: SinglyLinkedList<NodeData>,
|
|
653
|
+
) => any, reverse = false): SinglyLinkedList<NodeData | {}> {
|
|
641
654
|
const list = new SinglyLinkedList();
|
|
642
655
|
this.forEach((val, index) => list.append(f(val, index, this)), reverse);
|
|
643
656
|
return list;
|
|
@@ -653,7 +666,11 @@ export class SinglyLinkedList<NodeData = any> {
|
|
|
653
666
|
* @param reverse Indicates if the list should be filtered in reverse order, default is false
|
|
654
667
|
*/
|
|
655
668
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
656
|
-
public filter(f:
|
|
669
|
+
public filter(f: (
|
|
670
|
+
data: NodeData,
|
|
671
|
+
index: number,
|
|
672
|
+
list: SinglyLinkedList<NodeData>,
|
|
673
|
+
) => boolean, reverse = false): SinglyLinkedList<NodeData | {}> {
|
|
657
674
|
const list = new SinglyLinkedList();
|
|
658
675
|
this.forEach((val, index) => {
|
|
659
676
|
if (f(val, index, this)) {
|
|
@@ -1,12 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
// todo need to be improved
|
|
2
6
|
export class MatrixNTI2D<T = number> {
|
|
3
7
|
private readonly _matrix: Array<Array<T>>;
|
|
4
8
|
|
|
9
|
+
/**
|
|
10
|
+
* The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
|
|
11
|
+
* given initial value or 0 if not provided.
|
|
12
|
+
* @param options - An object containing the following properties:
|
|
13
|
+
*/
|
|
5
14
|
constructor(options: { row: number, col: number, initialVal?: T }) {
|
|
6
15
|
const {row, col, initialVal} = options;
|
|
7
16
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
8
17
|
}
|
|
9
18
|
|
|
19
|
+
/* The `toArray` method returns the matrix as a two-dimensional array. It converts the internal representation of the
|
|
20
|
+
matrix, which is an array of arrays, into a format that is more commonly used in JavaScript. */
|
|
10
21
|
toArray(): Array<Array<T>> {
|
|
11
22
|
return this._matrix;
|
|
12
23
|
}
|
|
@@ -1,8 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
import Vector2D from './vector2d'
|
|
2
6
|
|
|
3
7
|
export class Matrix2D {
|
|
4
8
|
private readonly _matrix: number[][];
|
|
5
9
|
|
|
10
|
+
/**
|
|
11
|
+
* The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
|
|
12
|
+
* or Vector2D object.
|
|
13
|
+
* @param {number[][] | Vector2D} [value] - The `value` parameter can be either a 2D array of numbers (`number[][]`) or
|
|
14
|
+
* an instance of the `Vector2D` class.
|
|
15
|
+
*/
|
|
6
16
|
constructor(value?: number[][] | Vector2D) {
|
|
7
17
|
if (typeof value === 'undefined') {
|
|
8
18
|
this._matrix = Matrix2D.identity
|
|
@@ -17,22 +27,16 @@ export class Matrix2D {
|
|
|
17
27
|
}
|
|
18
28
|
|
|
19
29
|
/**
|
|
20
|
-
*
|
|
30
|
+
* The function returns a 2D array with three empty arrays.
|
|
31
|
+
* @returns An empty 2-dimensional array with 3 empty arrays inside.
|
|
21
32
|
*/
|
|
22
|
-
public get m(): number[][] {
|
|
23
|
-
return this._matrix
|
|
24
|
-
}
|
|
25
|
-
|
|
26
33
|
public static get empty(): number[][] {
|
|
27
34
|
return [[], [], []]
|
|
28
35
|
}
|
|
29
36
|
|
|
30
|
-
public get toVector(): Vector2D {
|
|
31
|
-
return new Vector2D(this._matrix[0][0], this._matrix[1][0])
|
|
32
|
-
}
|
|
33
|
-
|
|
34
37
|
/**
|
|
35
|
-
*
|
|
38
|
+
* The above function returns a 3x3 identity matrix.
|
|
39
|
+
* @returns The method is returning a 2-dimensional array of numbers representing the identity matrix.
|
|
36
40
|
*/
|
|
37
41
|
public static get identity(): number[][] {
|
|
38
42
|
return [
|
|
@@ -41,6 +45,31 @@ export class Matrix2D {
|
|
|
41
45
|
[0, 0, 1]]
|
|
42
46
|
}
|
|
43
47
|
|
|
48
|
+
/**
|
|
49
|
+
* The function returns a two-dimensional array of numbers.
|
|
50
|
+
* @returns The getter method is returning the value of the private variable `_matrix`, which is a two-dimensional
|
|
51
|
+
* array of numbers.
|
|
52
|
+
*/
|
|
53
|
+
public get m(): number[][] {
|
|
54
|
+
return this._matrix
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
59
|
+
* _matrix array.
|
|
60
|
+
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
61
|
+
* the first column of the matrix.
|
|
62
|
+
*/
|
|
63
|
+
public get toVector(): Vector2D {
|
|
64
|
+
return new Vector2D(this._matrix[0][0], this._matrix[1][0])
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
69
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
70
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
|
|
71
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
72
|
+
*/
|
|
44
73
|
public static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D {
|
|
45
74
|
const result = Matrix2D.empty
|
|
46
75
|
for (let i = 0; i < 3; i++) {
|
|
@@ -51,6 +80,13 @@ export class Matrix2D {
|
|
|
51
80
|
return new Matrix2D(result);
|
|
52
81
|
}
|
|
53
82
|
|
|
83
|
+
/**
|
|
84
|
+
* The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
|
|
85
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
|
|
86
|
+
* @param {Matrix2D} matrix2 - Matrix2D is a class representing a 2D matrix. It has a property `m` which is a 2D array
|
|
87
|
+
* representing the matrix elements.
|
|
88
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
89
|
+
*/
|
|
54
90
|
public static subtract(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D {
|
|
55
91
|
const result = Matrix2D.empty
|
|
56
92
|
for (let i = 0; i < 3; i++) {
|
|
@@ -61,6 +97,12 @@ export class Matrix2D {
|
|
|
61
97
|
return new Matrix2D(result);
|
|
62
98
|
}
|
|
63
99
|
|
|
100
|
+
/**
|
|
101
|
+
* The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
|
|
102
|
+
* @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
|
|
103
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
|
|
104
|
+
* @returns a new instance of the Matrix2D class, created using the result array.
|
|
105
|
+
*/
|
|
64
106
|
public static multiply(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D {
|
|
65
107
|
const result = Matrix2D.empty
|
|
66
108
|
for (let i = 0; i < 3; i++) {
|
|
@@ -74,6 +116,13 @@ export class Matrix2D {
|
|
|
74
116
|
return new Matrix2D(result);
|
|
75
117
|
}
|
|
76
118
|
|
|
119
|
+
/**
|
|
120
|
+
* The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
|
|
121
|
+
* @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
|
|
122
|
+
* matrix. It contains a property `m` that is a 2D array representing the matrix elements.
|
|
123
|
+
* @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
|
|
124
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
125
|
+
*/
|
|
77
126
|
public static multiplyByValue(matrix: Matrix2D, value: number): Matrix2D {
|
|
78
127
|
const result = Matrix2D.empty
|
|
79
128
|
for (let i = 0; i < 3; i++) {
|
|
@@ -84,10 +133,24 @@ export class Matrix2D {
|
|
|
84
133
|
return new Matrix2D(result);
|
|
85
134
|
}
|
|
86
135
|
|
|
136
|
+
/**
|
|
137
|
+
* The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
|
|
138
|
+
* @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
|
|
139
|
+
* @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
|
|
140
|
+
* @returns a Vector2D.
|
|
141
|
+
*/
|
|
87
142
|
public static multiplyByVector(matrix: Matrix2D, vector: Vector2D): Vector2D {
|
|
88
143
|
return Matrix2D.multiply(matrix, new Matrix2D(vector)).toVector
|
|
89
144
|
}
|
|
90
145
|
|
|
146
|
+
/**
|
|
147
|
+
* The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
|
|
148
|
+
* @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
|
|
149
|
+
* specifies the width in pixels or any other unit of measurement.
|
|
150
|
+
* @param {number} height - The height parameter represents the height of the view or the canvas. It is used to
|
|
151
|
+
* calculate the centerY value, which is the vertical center of the view.
|
|
152
|
+
* @returns a Matrix2D object.
|
|
153
|
+
*/
|
|
91
154
|
public static view(width: number, height: number): Matrix2D {
|
|
92
155
|
const scaleStep = 1 // Scale every vector * scaleStep
|
|
93
156
|
const centerX = width / 2
|
|
@@ -100,10 +163,21 @@ export class Matrix2D {
|
|
|
100
163
|
[0, 0, 1]])
|
|
101
164
|
}
|
|
102
165
|
|
|
166
|
+
/**
|
|
167
|
+
* The function scales a matrix by a given factor.
|
|
168
|
+
* @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
|
|
169
|
+
* should be scaled.
|
|
170
|
+
* @returns the result of multiplying a new instance of Matrix2D by the given factor.
|
|
171
|
+
*/
|
|
103
172
|
public static scale(factor: number) {
|
|
104
173
|
return Matrix2D.multiplyByValue(new Matrix2D(), factor)
|
|
105
174
|
}
|
|
106
175
|
|
|
176
|
+
/**
|
|
177
|
+
* The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
|
|
178
|
+
* @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
|
|
179
|
+
* @returns The code is returning a new instance of a Matrix2D object.
|
|
180
|
+
*/
|
|
107
181
|
public static rotate(radians: number) {
|
|
108
182
|
const cos = Math.cos(radians)
|
|
109
183
|
const sin = Math.sin(radians)
|
|
@@ -114,6 +188,12 @@ export class Matrix2D {
|
|
|
114
188
|
[0, 0, 1]])
|
|
115
189
|
}
|
|
116
190
|
|
|
191
|
+
/**
|
|
192
|
+
* The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
|
|
193
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
|
|
194
|
+
* and y, and an optional w component.
|
|
195
|
+
* @returns The method is returning a new instance of the Matrix2D class.
|
|
196
|
+
*/
|
|
117
197
|
public static translate(vector: Vector2D): Matrix2D {
|
|
118
198
|
return new Matrix2D([
|
|
119
199
|
[1, 0, vector.x],
|