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,34 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
import type {Direction, NavigatorParams, Turning} from '../types';
|
|
3
6
|
|
|
4
7
|
export class Character {
|
|
5
8
|
direction: Direction;
|
|
6
9
|
turn: () => Character;
|
|
7
10
|
|
|
11
|
+
/**
|
|
12
|
+
* The constructor function takes in a direction and turning object and sets the direction and turn properties of the
|
|
13
|
+
* Character class.
|
|
14
|
+
* @param {Direction} direction - The direction parameter is used to specify the current direction of the character. It
|
|
15
|
+
* can be any value that represents a direction, such as "north", "south", "east", or "west".
|
|
16
|
+
* @param {Turning} turning - The `turning` parameter is an object that maps each direction to the corresponding
|
|
17
|
+
* turning direction. It is used to determine the new direction when the character turns.
|
|
18
|
+
*/
|
|
8
19
|
constructor(direction: Direction, turning: Turning) {
|
|
9
20
|
this.direction = direction;
|
|
10
21
|
this.turn = () => new Character(turning[direction], turning);
|
|
11
22
|
}
|
|
12
23
|
}
|
|
13
24
|
|
|
14
|
-
interface NavigatorParams<T> {
|
|
15
|
-
matrix: T[][],
|
|
16
|
-
turning: Turning,
|
|
17
|
-
onMove: (cur: [number, number]) => void
|
|
18
|
-
init: {
|
|
19
|
-
cur: [number, number],
|
|
20
|
-
charDir: Direction,
|
|
21
|
-
VISITED: T,
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
25
|
export class Navigator<T = number> {
|
|
26
|
+
onMove: (cur: [number, number]) => void;
|
|
26
27
|
private readonly _matrix: T[][];
|
|
27
28
|
private readonly _cur: [number, number];
|
|
28
29
|
private _character: Character;
|
|
29
30
|
private readonly _VISITED: T;
|
|
30
|
-
onMove: (cur: [number, number]) => void;
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
|
|
34
|
+
* in the matrix.
|
|
35
|
+
* @param - - `matrix`: a 2D array representing the grid or map
|
|
36
|
+
*/
|
|
32
37
|
constructor({matrix, turning, onMove, init: {cur, charDir, VISITED}}: NavigatorParams<T>) {
|
|
33
38
|
this._matrix = matrix;
|
|
34
39
|
this._cur = cur;
|
|
@@ -39,6 +44,10 @@ export class Navigator<T = number> {
|
|
|
39
44
|
this._matrix[this._cur[0]][this._cur[1]] = this._VISITED;
|
|
40
45
|
}
|
|
41
46
|
|
|
47
|
+
/**
|
|
48
|
+
* The "start" function moves the character in its current direction until it encounters an obstacle, then it turns the
|
|
49
|
+
* character and repeats the process.
|
|
50
|
+
*/
|
|
42
51
|
start() {
|
|
43
52
|
while (this.check(this._character.direction) || this.check(this._character.turn().direction)) {
|
|
44
53
|
const {direction} = this._character;
|
|
@@ -50,6 +59,12 @@ export class Navigator<T = number> {
|
|
|
50
59
|
}
|
|
51
60
|
}
|
|
52
61
|
|
|
62
|
+
/**
|
|
63
|
+
* The function checks if there is a valid move in the specified direction in a matrix.
|
|
64
|
+
* @param {Direction} direction - The direction parameter is a string that represents the direction in which to check.
|
|
65
|
+
* It can be one of the following values: 'up', 'right', 'down', or 'left'.
|
|
66
|
+
* @returns a boolean value.
|
|
67
|
+
*/
|
|
53
68
|
check(direction: Direction) {
|
|
54
69
|
let forward: T | undefined, row: T[] | undefined;
|
|
55
70
|
const matrix = this._matrix;
|
|
@@ -75,6 +90,11 @@ export class Navigator<T = number> {
|
|
|
75
90
|
return forward !== undefined && forward !== this._VISITED;
|
|
76
91
|
}
|
|
77
92
|
|
|
93
|
+
/**
|
|
94
|
+
* The `move` function updates the current position based on the given direction and updates the matrix accordingly.
|
|
95
|
+
* @param {Direction} direction - The `direction` parameter is a string that represents the direction in which to move.
|
|
96
|
+
* It can have one of the following values: 'up', 'right', 'down', or 'left'.
|
|
97
|
+
*/
|
|
78
98
|
move(direction: Direction) {
|
|
79
99
|
switch (direction) {
|
|
80
100
|
case 'up':
|
|
@@ -1,28 +1,135 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
export class Vector2D {
|
|
6
|
+
constructor(
|
|
7
|
+
public x: number = 0,
|
|
8
|
+
public y: number = 0,
|
|
9
|
+
public w: number = 1 // needed for matrix multiplication
|
|
10
|
+
) {
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The function checks if the x and y values of a point are both zero.
|
|
15
|
+
* @returns A boolean value indicating whether both the x and y properties of the object are equal to 0.
|
|
16
|
+
*/
|
|
17
|
+
public get isZero(): boolean {
|
|
18
|
+
return this.x === 0 && this.y === 0
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The above function calculates the length of a vector using the Pythagorean theorem.
|
|
23
|
+
* @returns The length of a vector, calculated using the Pythagorean theorem.
|
|
24
|
+
*/
|
|
25
|
+
public get length(): number {
|
|
26
|
+
return Math.sqrt((this.x * this.x) + (this.y * this.y))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The function calculates the square of the length of a vector.
|
|
31
|
+
* @returns The method is returning the sum of the squares of the x and y values.
|
|
32
|
+
*/
|
|
33
|
+
public get lengthSq(): number {
|
|
34
|
+
return (this.x * this.x) + (this.y * this.y)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The "rounded" function returns a new Vector2D object with the x and y values rounded to the nearest whole number.
|
|
39
|
+
* @returns The method is returning a new instance of the Vector2D class with the x and y values rounded to the nearest
|
|
40
|
+
* whole number.
|
|
41
|
+
*/
|
|
42
|
+
public get rounded(): Vector2D {
|
|
43
|
+
return new Vector2D(Math.round(this.x), Math.round(this.y))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The function "add" takes two Vector2D objects as parameters and returns a new Vector2D object with the sum of their
|
|
48
|
+
* x and y components.
|
|
49
|
+
* @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class. It represents a
|
|
50
|
+
* 2-dimensional vector with an `x` and `y` component.
|
|
51
|
+
* @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D. It represents a 2-dimensional vector with
|
|
52
|
+
* an x and y component.
|
|
53
|
+
* @returns The method is returning a new instance of the Vector2D class with the x and y components of the two input
|
|
54
|
+
* vectors added together.
|
|
55
|
+
*/
|
|
2
56
|
public static add(vector1: Vector2D, vector2: Vector2D): Vector2D {
|
|
3
57
|
return new Vector2D(vector1.x + vector2.x, vector1.y + vector2.y)
|
|
4
58
|
}
|
|
5
59
|
|
|
60
|
+
/**
|
|
61
|
+
* The subtract function takes two Vector2D objects as parameters and returns a new Vector2D object with the x and y
|
|
62
|
+
* components subtracted.
|
|
63
|
+
* @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class, representing a
|
|
64
|
+
* 2-dimensional vector. It has properties `x` and `y` which represent the x and y components of the vector
|
|
65
|
+
* respectively.
|
|
66
|
+
* @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object. It represents the second vector that you
|
|
67
|
+
* want to subtract from the first vector.
|
|
68
|
+
* @returns The method is returning a new Vector2D object with the x and y components subtracted from vector1 and
|
|
69
|
+
* vector2.
|
|
70
|
+
*/
|
|
6
71
|
public static subtract(vector1: Vector2D, vector2: Vector2D): Vector2D {
|
|
7
72
|
return new Vector2D(vector1.x - vector2.x, vector1.y - vector2.y)
|
|
8
73
|
}
|
|
9
74
|
|
|
75
|
+
/**
|
|
76
|
+
* The function subtracts a given value from the x and y components of a Vector2D object and returns a new Vector2D
|
|
77
|
+
* object.
|
|
78
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
|
|
79
|
+
* x and y components.
|
|
80
|
+
* @param {number} value - The "value" parameter is a number that will be subtracted from both the x and y components
|
|
81
|
+
* of the "vector" parameter.
|
|
82
|
+
* @returns A new Vector2D object with the x and y values subtracted by the given value.
|
|
83
|
+
*/
|
|
10
84
|
public static subtractValue(vector: Vector2D, value: number): Vector2D {
|
|
11
85
|
return new Vector2D(vector.x - value, vector.y - value)
|
|
12
86
|
}
|
|
13
87
|
|
|
88
|
+
/**
|
|
89
|
+
* The function multiplies a Vector2D object by a given value.
|
|
90
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
|
|
91
|
+
* x and y components.
|
|
92
|
+
* @param {number} value - The "value" parameter is a number that represents the value by which the x and y components
|
|
93
|
+
* of the vector will be multiplied.
|
|
94
|
+
* @returns A new Vector2D object with the x and y values multiplied by the given value.
|
|
95
|
+
*/
|
|
14
96
|
public static multiply(vector: Vector2D, value: number): Vector2D {
|
|
15
97
|
return new Vector2D(vector.x * value, vector.y * value)
|
|
16
98
|
}
|
|
17
99
|
|
|
100
|
+
/**
|
|
101
|
+
* The function divides the x and y components of a Vector2D by a given value and returns a new Vector2D.
|
|
102
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
|
|
103
|
+
* x and y components.
|
|
104
|
+
* @param {number} value - The value parameter is a number that will be used to divide the x and y components of the
|
|
105
|
+
* vector.
|
|
106
|
+
* @returns A new instance of the Vector2D class with the x and y values divided by the given value.
|
|
107
|
+
*/
|
|
18
108
|
public static divide(vector: Vector2D, value: number): Vector2D {
|
|
19
109
|
return new Vector2D(vector.x / value, vector.y / value)
|
|
20
110
|
}
|
|
21
111
|
|
|
112
|
+
/**
|
|
113
|
+
* The function checks if two Vector2D objects are equal by comparing their x and y values.
|
|
114
|
+
* @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
|
|
115
|
+
* It has two properties: `x` and `y`, which represent the x and y components of the vector, respectively.
|
|
116
|
+
* @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D.
|
|
117
|
+
* @returns a boolean value, which indicates whether the two input vectors are equal or not.
|
|
118
|
+
*/
|
|
22
119
|
public static equals(vector1: Vector2D, vector2: Vector2D): boolean {
|
|
23
120
|
return vector1.x === vector2.x && vector1.y === vector2.y
|
|
24
121
|
}
|
|
25
122
|
|
|
123
|
+
/**
|
|
124
|
+
* The function checks if two Vector2D objects are equal within a specified rounding factor.
|
|
125
|
+
* @param {Vector2D} vector1 - The first vector to compare.
|
|
126
|
+
* @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object, which represents a 2-dimensional vector.
|
|
127
|
+
* It is used as one of the inputs for the "equalsRounded" function.
|
|
128
|
+
* @param [roundingFactor=12] - The roundingFactor parameter is used to determine the threshold for considering two
|
|
129
|
+
* vectors as equal. If the absolute difference in the x and y components of the vectors is less than the
|
|
130
|
+
* roundingFactor, the vectors are considered equal.
|
|
131
|
+
* @returns a boolean value.
|
|
132
|
+
*/
|
|
26
133
|
public static equalsRounded(vector1: Vector2D, vector2: Vector2D, roundingFactor = 12): boolean {
|
|
27
134
|
const vector = Vector2D.abs(Vector2D.subtract(vector1, vector2))
|
|
28
135
|
if (vector.x < roundingFactor && vector.y < roundingFactor) {
|
|
@@ -33,7 +140,10 @@ class Vector2D {
|
|
|
33
140
|
}
|
|
34
141
|
|
|
35
142
|
/**
|
|
36
|
-
* Normalizes the vector if it matches a certain condition
|
|
143
|
+
* The normalize function takes a vector as input and returns a normalized version of the vector.Normalizes the vector if it matches a certain condition
|
|
144
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
|
|
145
|
+
* @returns the normalized vector if its length is greater than a very small value (epsilon), otherwise it returns the
|
|
146
|
+
* original vector.
|
|
37
147
|
*/
|
|
38
148
|
public static normalize(vector: Vector2D): Vector2D {
|
|
39
149
|
const length = vector.length
|
|
@@ -45,7 +155,12 @@ class Vector2D {
|
|
|
45
155
|
}
|
|
46
156
|
|
|
47
157
|
/**
|
|
48
|
-
* Adjusts x and y so that the length of the vector does not exceed max
|
|
158
|
+
* The function truncates a vector to a maximum length if it exceeds that length.Adjusts x and y so that the length of the vector does not exceed max
|
|
159
|
+
* @param {Vector2D} vector - A 2D vector represented by the Vector2D class.
|
|
160
|
+
* @param {number} max - The `max` parameter is a number that represents the maximum length that the `vector` should
|
|
161
|
+
* have.
|
|
162
|
+
* @returns either the original vector or a truncated version of the vector, depending on whether the length of the
|
|
163
|
+
* vector is greater than the maximum value specified.
|
|
49
164
|
*/
|
|
50
165
|
public static truncate(vector: Vector2D, max: number): Vector2D {
|
|
51
166
|
if (vector.length > max) {
|
|
@@ -56,32 +171,72 @@ class Vector2D {
|
|
|
56
171
|
}
|
|
57
172
|
|
|
58
173
|
/**
|
|
59
|
-
* The vector that is perpendicular to this one
|
|
174
|
+
* The function returns a new Vector2D object that is perpendicular to the input vector.The vector that is perpendicular to this one
|
|
175
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
|
|
176
|
+
* @returns A new Vector2D object is being returned.
|
|
60
177
|
*/
|
|
61
178
|
public static perp(vector: Vector2D): Vector2D {
|
|
62
179
|
return new Vector2D(-vector.y, vector.x)
|
|
63
180
|
}
|
|
64
181
|
|
|
65
182
|
/**
|
|
66
|
-
* returns
|
|
183
|
+
* The reverse function takes a Vector2D object and returns a new Vector2D object with the negated x and y values.
|
|
184
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
|
|
185
|
+
* has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
|
|
186
|
+
* @returns A new Vector2D object with the negated x and y values of the input vector. Returns the vector that is the reverse of this vector
|
|
67
187
|
*/
|
|
68
188
|
public static reverse(vector: Vector2D): Vector2D {
|
|
69
189
|
return new Vector2D(-vector.x, -vector.y)
|
|
70
190
|
}
|
|
71
191
|
|
|
192
|
+
/**
|
|
193
|
+
* The function takes a Vector2D object as input and returns a new Vector2D object with the absolute values of its x
|
|
194
|
+
* and y components.
|
|
195
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
|
|
196
|
+
* has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
|
|
197
|
+
* @returns The method is returning a new Vector2D object with the absolute values of the x and y components of the
|
|
198
|
+
* input vector.
|
|
199
|
+
*/
|
|
72
200
|
public static abs(vector: Vector2D): Vector2D {
|
|
73
201
|
return new Vector2D(Math.abs(vector.x), Math.abs(vector.y))
|
|
74
202
|
}
|
|
75
203
|
|
|
76
204
|
/**
|
|
77
|
-
* The dot product of v1 and v2
|
|
205
|
+
* The dot function calculates the dot product of two 2D vectors.The dot product of v1 and v2
|
|
206
|
+
* @param {Vector2D} vector1 - The parameter `vector1` represents a 2D vector with its x and y components.
|
|
207
|
+
* @param {Vector2D} vector2 - The "vector2" parameter is a Vector2D object. It represents a two-dimensional vector
|
|
208
|
+
* with an x and y component.
|
|
209
|
+
* @returns The dot product of the two input vectors.
|
|
78
210
|
*/
|
|
79
211
|
public static dot(vector1: Vector2D, vector2: Vector2D): number {
|
|
80
212
|
return (vector1.x * vector2.x) + (vector1.y * vector2.y)
|
|
81
213
|
}
|
|
82
214
|
|
|
215
|
+
// /**
|
|
216
|
+
// * Transform vectors based on the current tranformation matrices: translation, rotation and scale
|
|
217
|
+
// * @param vectors The vectors to transform
|
|
218
|
+
// */
|
|
219
|
+
// public static transform(vector: Vector2D, transformation: Matrix2D): Vector2D {
|
|
220
|
+
// return Matrix2D.multiplyByVector(transformation, vector)
|
|
221
|
+
// }
|
|
222
|
+
|
|
223
|
+
// /**
|
|
224
|
+
// * Transform vectors based on the current tranformation matrices: translation, rotation and scale
|
|
225
|
+
// * @param vectors The vectors to transform
|
|
226
|
+
// */
|
|
227
|
+
// public static transformList(vectors: Vector2D[], transformation: Matrix2D): Vector2D[] {
|
|
228
|
+
// return vectors.map(vector => Matrix2D.multiplyByVector(transformation, vector))
|
|
229
|
+
// }
|
|
230
|
+
|
|
83
231
|
/**
|
|
84
|
-
* The distance between
|
|
232
|
+
* The function calculates the distance between two points in a two-dimensional space.
|
|
233
|
+
* @param {Vector2D} vector1 - The parameter `vector1` represents the first vector in 2D space, while `vector2`
|
|
234
|
+
* represents the second vector. Each vector has an `x` and `y` component, which represent their respective coordinates
|
|
235
|
+
* in the 2D space.
|
|
236
|
+
* @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in the calculation of distance. It
|
|
237
|
+
* is an instance of the `Vector2D` class, which typically has properties `x` and `y` representing the coordinates of
|
|
238
|
+
* the vector in a 2D space.
|
|
239
|
+
* @returns The distance between vector1 and vector2.
|
|
85
240
|
*/
|
|
86
241
|
public static distance(vector1: Vector2D, vector2: Vector2D): number {
|
|
87
242
|
const ySeparation = vector2.y - vector1.y
|
|
@@ -90,7 +245,12 @@ class Vector2D {
|
|
|
90
245
|
}
|
|
91
246
|
|
|
92
247
|
/**
|
|
93
|
-
* The distance between
|
|
248
|
+
* The function calculates the squared distance between two 2D vectors.
|
|
249
|
+
* @param {Vector2D} vector1 - The parameter `vector1` represents the first vector, which is an instance of the
|
|
250
|
+
* `Vector2D` class. It contains the x and y coordinates of the vector.
|
|
251
|
+
* @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in a two-dimensional space. It has
|
|
252
|
+
* properties `x` and `y` which represent the coordinates of the vector.
|
|
253
|
+
* @returns the square of the distance between the two input vectors.
|
|
94
254
|
*/
|
|
95
255
|
public static distanceSq(vector1: Vector2D, vector2: Vector2D): number {
|
|
96
256
|
const ySeparation = vector2.y - vector1.y
|
|
@@ -99,8 +259,13 @@ class Vector2D {
|
|
|
99
259
|
}
|
|
100
260
|
|
|
101
261
|
/**
|
|
102
|
-
*
|
|
262
|
+
* The sign function determines the sign of the cross product between two 2D vectors.
|
|
103
263
|
* (assuming the Y axis is pointing down, X axis to right like a Window app)
|
|
264
|
+
* @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
|
|
265
|
+
* It likely has properties `x` and `y` representing the x and y components of the vector, respectively.
|
|
266
|
+
* @param {Vector2D} vector2 - The above code defines a function called "sign" that takes two parameters: vector1 and
|
|
267
|
+
* vector2. Both vector1 and vector2 are of type Vector2D.
|
|
268
|
+
* @returns either -1 or 1. Returns positive if v2 is clockwise of this vector, negative if counterclockwise
|
|
104
269
|
*/
|
|
105
270
|
public static sign(vector1: Vector2D, vector2: Vector2D): number {
|
|
106
271
|
if (vector1.y * vector2.x > vector1.x * vector2.y) {
|
|
@@ -111,8 +276,11 @@ class Vector2D {
|
|
|
111
276
|
}
|
|
112
277
|
|
|
113
278
|
/**
|
|
114
|
-
*
|
|
115
|
-
* @param vector
|
|
279
|
+
* The function calculates the angle between a given vector and the negative y-axis.
|
|
280
|
+
* @param {Vector2D} vector - The "vector" parameter is an instance of the Vector2D class, which represents a
|
|
281
|
+
* 2-dimensional vector. It has two properties: "x" and "y", which represent the x and y components of the vector,
|
|
282
|
+
* respectively.
|
|
283
|
+
* @returns the angle between the given vector and the vector (0, -1) in radians.Returns the angle between origin and the given vector in radians
|
|
116
284
|
*/
|
|
117
285
|
public static angle(vector: Vector2D): number {
|
|
118
286
|
const origin = new Vector2D(0, -1)
|
|
@@ -120,70 +288,26 @@ class Vector2D {
|
|
|
120
288
|
return Vector2D.sign(vector, origin) === 1 ? ((Math.PI * 2) - radian) : radian
|
|
121
289
|
}
|
|
122
290
|
|
|
291
|
+
/**
|
|
292
|
+
* The function "random" generates a random Vector2D object with x and y values within the specified range.
|
|
293
|
+
* @param {number} maxX - The maxX parameter represents the maximum value for the x-coordinate of the random vector.
|
|
294
|
+
* @param {number} maxY - The `maxY` parameter represents the maximum value for the y-coordinate of the generated
|
|
295
|
+
* random vector.
|
|
296
|
+
* @returns a new instance of the Vector2D class with random x and y values.
|
|
297
|
+
*/
|
|
123
298
|
public static random(maxX: number, maxY: number): Vector2D {
|
|
124
299
|
const randX = Math.floor(Math.random() * maxX - (maxX / 2))
|
|
125
300
|
const randY = Math.floor(Math.random() * maxY - (maxY / 2))
|
|
126
301
|
return new Vector2D(randX, randY)
|
|
127
302
|
}
|
|
128
303
|
|
|
129
|
-
// /**
|
|
130
|
-
// * Transform vectors based on the current tranformation matrices: translation, rotation and scale
|
|
131
|
-
// * @param vectors The vectors to transform
|
|
132
|
-
// */
|
|
133
|
-
// public static transform(vector: Vector2D, transformation: Matrix2D): Vector2D {
|
|
134
|
-
// return Matrix2D.multiplyByVector(transformation, vector)
|
|
135
|
-
// }
|
|
136
|
-
|
|
137
|
-
// /**
|
|
138
|
-
// * Transform vectors based on the current tranformation matrices: translation, rotation and scale
|
|
139
|
-
// * @param vectors The vectors to transform
|
|
140
|
-
// */
|
|
141
|
-
// public static transformList(vectors: Vector2D[], transformation: Matrix2D): Vector2D[] {
|
|
142
|
-
// return vectors.map(vector => Matrix2D.multiplyByVector(transformation, vector))
|
|
143
|
-
// }
|
|
144
|
-
|
|
145
|
-
constructor(
|
|
146
|
-
public x: number = 0,
|
|
147
|
-
public y: number = 0,
|
|
148
|
-
public w: number = 1 // needed for matrix multiplication
|
|
149
|
-
) {
|
|
150
|
-
}
|
|
151
|
-
|
|
152
304
|
/**
|
|
153
|
-
*
|
|
305
|
+
* The function sets the values of x and y to zero.
|
|
154
306
|
*/
|
|
155
307
|
public zero(): void {
|
|
156
308
|
this.x = 0
|
|
157
309
|
this.y = 0
|
|
158
310
|
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Set x and y both to zero
|
|
162
|
-
*/
|
|
163
|
-
public get isZero(): boolean {
|
|
164
|
-
return this.x === 0 && this.y === 0
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* The length / magnitude of the vector
|
|
169
|
-
*/
|
|
170
|
-
public get length(): number {
|
|
171
|
-
return Math.sqrt((this.x * this.x) + (this.y * this.y))
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* The squared length of the vector
|
|
176
|
-
*/
|
|
177
|
-
public get lengthSq(): number {
|
|
178
|
-
return (this.x * this.x) + (this.y * this.y)
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Return the vector with rounded values
|
|
183
|
-
*/
|
|
184
|
-
public get rounded(): Vector2D {
|
|
185
|
-
return new Vector2D(Math.round(this.x), Math.round(this.y))
|
|
186
|
-
}
|
|
187
311
|
}
|
|
188
312
|
|
|
189
313
|
export default Vector2D
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
import {PriorityQueue} from './priority-queue';
|
|
6
|
+
import type {PriorityQueueOptions} from '../types';
|
|
2
7
|
|
|
3
8
|
export class MaxPriorityQueue<T = number> extends PriorityQueue<T> {
|
|
4
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The constructor initializes a PriorityQueue with optional nodes and a comparator function.
|
|
11
|
+
* @param [options] - An optional object that contains the following properties:
|
|
12
|
+
*/
|
|
13
|
+
constructor(options?: PriorityQueueOptions<T>) {
|
|
5
14
|
super({
|
|
6
|
-
nodes: options
|
|
15
|
+
nodes: options?.nodes, comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
|
|
7
16
|
const aKey = a as unknown as number, bKey = b as unknown as number;
|
|
8
17
|
return bKey - aKey;
|
|
9
18
|
}
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
import {PriorityQueue} from './priority-queue';
|
|
6
|
+
import type {PriorityQueueOptions} from '../types';
|
|
2
7
|
|
|
3
8
|
export class MinPriorityQueue<T = number> extends PriorityQueue<T> {
|
|
4
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The constructor initializes a PriorityQueue with optional nodes and a comparator function.
|
|
11
|
+
* @param [options] - An optional object that contains the following properties:
|
|
12
|
+
*/
|
|
13
|
+
constructor(options?: PriorityQueueOptions<T>) {
|
|
5
14
|
super({
|
|
6
|
-
nodes: options
|
|
15
|
+
nodes: options?.nodes, comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
|
|
7
16
|
const aKey = a as unknown as number, bKey = b as unknown as number;
|
|
8
17
|
return aKey - bKey;
|
|
9
18
|
}
|