data-structure-typed 1.19.3 → 1.19.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
- package/dist/data-structures/binary-tree/avl-tree.js +46 -90
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-tree.js +9 -31
- package/dist/data-structures/binary-tree/bst.js +96 -139
- package/dist/data-structures/binary-tree/rb-tree.js +32 -56
- package/dist/data-structures/binary-tree/segment-tree.js +78 -120
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.js +340 -574
- package/dist/data-structures/graph/directed-graph.js +146 -276
- package/dist/data-structures/graph/undirected-graph.js +87 -176
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/hash-table.js +2 -5
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/heap/heap.js +53 -77
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
- package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix2d.js +76 -93
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/vector2d.js +80 -101
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/queue.js +24 -25
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/tree/tree.js +32 -45
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/utils/utils.js +22 -107
- package/dist/utils/validate-type.js +2 -2
- package/package.json +1 -1
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/assets/logo.png +0 -0
- package/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/src/data-structures/binary-tree/aa-tree.ts +3 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
- package/src/data-structures/binary-tree/avl-tree.ts +297 -0
- package/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
- package/src/data-structures/binary-tree/binary-tree.ts +40 -0
- package/src/data-structures/binary-tree/bst.ts +435 -0
- 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/index.ts +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +102 -0
- package/src/data-structures/binary-tree/segment-tree.ts +243 -0
- package/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +1032 -0
- 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 +472 -0
- package/src/data-structures/graph/index.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +270 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-table.ts +3 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/pair.ts +3 -0
- package/src/data-structures/hash/tree-map.ts +3 -0
- package/src/data-structures/hash/tree-set.ts +3 -0
- package/src/data-structures/heap/heap.ts +183 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +34 -0
- package/src/data-structures/index.ts +15 -0
- package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
- package/src/data-structures/interfaces/abstract-graph.ts +40 -0
- package/src/data-structures/interfaces/avl-tree.ts +28 -0
- package/src/data-structures/interfaces/binary-tree.ts +8 -0
- package/src/data-structures/interfaces/bst.ts +32 -0
- package/src/data-structures/interfaces/directed-graph.ts +20 -0
- package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/heap.ts +1 -0
- package/src/data-structures/interfaces/index.ts +15 -0
- package/src/data-structures/interfaces/navigator.ts +1 -0
- package/src/data-structures/interfaces/priority-queue.ts +1 -0
- package/src/data-structures/interfaces/rb-tree.ts +11 -0
- package/src/data-structures/interfaces/segment-tree.ts +1 -0
- package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/tree-multiset.ts +12 -0
- package/src/data-structures/interfaces/undirected-graph.ts +6 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +208 -0
- package/src/data-structures/matrix/navigator.ts +122 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
- package/src/data-structures/priority-queue/priority-queue.ts +354 -0
- package/src/data-structures/queue/deque.ts +251 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +120 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +227 -0
- package/src/data-structures/types/abstract-binary-tree.ts +42 -0
- package/src/data-structures/types/abstract-graph.ts +5 -0
- package/src/data-structures/types/avl-tree.ts +5 -0
- package/src/data-structures/types/binary-tree.ts +9 -0
- package/src/data-structures/types/bst.ts +12 -0
- package/src/data-structures/types/directed-graph.ts +8 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +5 -0
- package/src/data-structures/types/helpers.ts +1 -0
- package/src/data-structures/types/index.ts +15 -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/rb-tree.ts +8 -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 +8 -0
- package/src/index.ts +2 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/types/index.ts +2 -0
- package/src/utils/types/utils.ts +6 -0
- package/src/utils/types/validate-type.ts +25 -0
- package/src/utils/utils.ts +78 -0
- package/src/utils/validate-type.ts +69 -0
- package/tsconfig.json +1 -1
|
@@ -11,15 +11,15 @@ exports.Matrix2D = void 0;
|
|
|
11
11
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
12
12
|
* @license MIT License
|
|
13
13
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
const vector2d_1 = __importDefault(require("./vector2d"));
|
|
15
|
+
class Matrix2D {
|
|
16
16
|
/**
|
|
17
17
|
* The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
|
|
18
18
|
* or Vector2D object.
|
|
19
19
|
* @param {number[][] | Vector2D} [value] - The `value` parameter can be either a 2D array of numbers (`number[][]`) or
|
|
20
20
|
* an instance of the `Vector2D` class.
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
constructor(value) {
|
|
23
23
|
if (typeof value === 'undefined') {
|
|
24
24
|
this._matrix = Matrix2D.identity;
|
|
25
25
|
}
|
|
@@ -33,72 +33,56 @@ var Matrix2D = /** @class */ (function () {
|
|
|
33
33
|
this._matrix = value;
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
enumerable: false,
|
|
72
|
-
configurable: true
|
|
73
|
-
});
|
|
74
|
-
Object.defineProperty(Matrix2D.prototype, "toVector", {
|
|
75
|
-
/**
|
|
76
|
-
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
77
|
-
* _matrix array.
|
|
78
|
-
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
79
|
-
* the first column of the matrix.
|
|
80
|
-
*/
|
|
81
|
-
get: function () {
|
|
82
|
-
return new vector2d_1.default(this._matrix[0][0], this._matrix[1][0]);
|
|
83
|
-
},
|
|
84
|
-
enumerable: false,
|
|
85
|
-
configurable: true
|
|
86
|
-
});
|
|
36
|
+
/**
|
|
37
|
+
* The function returns a 2D array with three empty arrays.
|
|
38
|
+
* @returns An empty 2-dimensional array with 3 empty arrays inside.
|
|
39
|
+
*/
|
|
40
|
+
static get empty() {
|
|
41
|
+
return [[], [], []];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The above function returns a 3x3 identity matrix.
|
|
45
|
+
* @returns The method is returning a 2-dimensional array of numbers representing the identity matrix.
|
|
46
|
+
*/
|
|
47
|
+
static get identity() {
|
|
48
|
+
return [
|
|
49
|
+
[1, 0, 0],
|
|
50
|
+
[0, 1, 0],
|
|
51
|
+
[0, 0, 1]
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The function returns a two-dimensional array of numbers.
|
|
56
|
+
* @returns The getter method is returning the value of the private variable `_matrix`, which is a two-dimensional
|
|
57
|
+
* array of numbers.
|
|
58
|
+
*/
|
|
59
|
+
get m() {
|
|
60
|
+
return this._matrix;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
64
|
+
* _matrix array.
|
|
65
|
+
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
66
|
+
* the first column of the matrix.
|
|
67
|
+
*/
|
|
68
|
+
get toVector() {
|
|
69
|
+
return new vector2d_1.default(this._matrix[0][0], this._matrix[1][0]);
|
|
70
|
+
}
|
|
87
71
|
/**
|
|
88
72
|
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
89
73
|
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
90
74
|
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
|
|
91
75
|
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
92
76
|
*/
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
for (
|
|
96
|
-
for (
|
|
77
|
+
static add(matrix1, matrix2) {
|
|
78
|
+
const result = Matrix2D.empty;
|
|
79
|
+
for (let i = 0; i < 3; i++) {
|
|
80
|
+
for (let j = 0; j < 3; j++) {
|
|
97
81
|
result[i][j] = matrix1.m[i][j] + matrix2.m[i][j];
|
|
98
82
|
}
|
|
99
83
|
}
|
|
100
84
|
return new Matrix2D(result);
|
|
101
|
-
}
|
|
85
|
+
}
|
|
102
86
|
/**
|
|
103
87
|
* The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
|
|
104
88
|
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
|
|
@@ -106,33 +90,33 @@ var Matrix2D = /** @class */ (function () {
|
|
|
106
90
|
* representing the matrix elements.
|
|
107
91
|
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
108
92
|
*/
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
for (
|
|
112
|
-
for (
|
|
93
|
+
static subtract(matrix1, matrix2) {
|
|
94
|
+
const result = Matrix2D.empty;
|
|
95
|
+
for (let i = 0; i < 3; i++) {
|
|
96
|
+
for (let j = 0; j < 3; j++) {
|
|
113
97
|
result[i][j] = matrix1.m[i][j] - matrix2.m[i][j];
|
|
114
98
|
}
|
|
115
99
|
}
|
|
116
100
|
return new Matrix2D(result);
|
|
117
|
-
}
|
|
101
|
+
}
|
|
118
102
|
/**
|
|
119
103
|
* The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
|
|
120
104
|
* @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
|
|
121
105
|
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
|
|
122
106
|
* @returns a new instance of the Matrix2D class, created using the result array.
|
|
123
107
|
*/
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
for (
|
|
127
|
-
for (
|
|
108
|
+
static multiply(matrix1, matrix2) {
|
|
109
|
+
const result = Matrix2D.empty;
|
|
110
|
+
for (let i = 0; i < 3; i++) {
|
|
111
|
+
for (let j = 0; j < 3; j++) {
|
|
128
112
|
result[i][j] = 0;
|
|
129
|
-
for (
|
|
113
|
+
for (let k = 0; k < 3; k++) {
|
|
130
114
|
result[i][j] += matrix1.m[i][k] * matrix2.m[k][j];
|
|
131
115
|
}
|
|
132
116
|
}
|
|
133
117
|
}
|
|
134
118
|
return new Matrix2D(result);
|
|
135
|
-
}
|
|
119
|
+
}
|
|
136
120
|
/**
|
|
137
121
|
* The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
|
|
138
122
|
* @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
|
|
@@ -140,24 +124,24 @@ var Matrix2D = /** @class */ (function () {
|
|
|
140
124
|
* @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
|
|
141
125
|
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
142
126
|
*/
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
for (
|
|
146
|
-
for (
|
|
127
|
+
static multiplyByValue(matrix, value) {
|
|
128
|
+
const result = Matrix2D.empty;
|
|
129
|
+
for (let i = 0; i < 3; i++) {
|
|
130
|
+
for (let j = 0; j < 3; j++) {
|
|
147
131
|
result[i][j] = matrix.m[i][j] * value;
|
|
148
132
|
}
|
|
149
133
|
}
|
|
150
134
|
return new Matrix2D(result);
|
|
151
|
-
}
|
|
135
|
+
}
|
|
152
136
|
/**
|
|
153
137
|
* The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
|
|
154
138
|
* @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
|
|
155
139
|
* @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
|
|
156
140
|
* @returns a Vector2D.
|
|
157
141
|
*/
|
|
158
|
-
|
|
142
|
+
static multiplyByVector(matrix, vector) {
|
|
159
143
|
return Matrix2D.multiply(matrix, new Matrix2D(vector)).toVector;
|
|
160
|
-
}
|
|
144
|
+
}
|
|
161
145
|
/**
|
|
162
146
|
* The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
|
|
163
147
|
* @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
|
|
@@ -166,54 +150,53 @@ var Matrix2D = /** @class */ (function () {
|
|
|
166
150
|
* calculate the centerY value, which is the vertical center of the view.
|
|
167
151
|
* @returns a Matrix2D object.
|
|
168
152
|
*/
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
153
|
+
static view(width, height) {
|
|
154
|
+
const scaleStep = 1; // Scale every vector * scaleStep
|
|
155
|
+
const centerX = width / 2;
|
|
156
|
+
const centerY = height / 2;
|
|
157
|
+
const flipX = Math.cos(Math.PI); // rotate 180deg / 3.14radian around X-axis
|
|
174
158
|
return new Matrix2D([
|
|
175
159
|
[scaleStep, 0, centerX],
|
|
176
160
|
[0, flipX * scaleStep, centerY],
|
|
177
161
|
[0, 0, 1]
|
|
178
162
|
]);
|
|
179
|
-
}
|
|
163
|
+
}
|
|
180
164
|
/**
|
|
181
165
|
* The function scales a matrix by a given factor.
|
|
182
166
|
* @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
|
|
183
167
|
* should be scaled.
|
|
184
168
|
* @returns the result of multiplying a new instance of Matrix2D by the given factor.
|
|
185
169
|
*/
|
|
186
|
-
|
|
170
|
+
static scale(factor) {
|
|
187
171
|
return Matrix2D.multiplyByValue(new Matrix2D(), factor);
|
|
188
|
-
}
|
|
172
|
+
}
|
|
189
173
|
/**
|
|
190
174
|
* The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
|
|
191
175
|
* @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
|
|
192
176
|
* @returns The code is returning a new instance of a Matrix2D object.
|
|
193
177
|
*/
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
178
|
+
static rotate(radians) {
|
|
179
|
+
const cos = Math.cos(radians);
|
|
180
|
+
const sin = Math.sin(radians);
|
|
197
181
|
return new Matrix2D([
|
|
198
182
|
[cos, -sin, 0],
|
|
199
183
|
[sin, cos, 0],
|
|
200
184
|
[0, 0, 1]
|
|
201
185
|
]);
|
|
202
|
-
}
|
|
186
|
+
}
|
|
203
187
|
/**
|
|
204
188
|
* The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
|
|
205
189
|
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
|
|
206
190
|
* and y, and an optional w component.
|
|
207
191
|
* @returns The method is returning a new instance of the Matrix2D class.
|
|
208
192
|
*/
|
|
209
|
-
|
|
193
|
+
static translate(vector) {
|
|
210
194
|
return new Matrix2D([
|
|
211
195
|
[1, 0, vector.x],
|
|
212
196
|
[0, 1, vector.y],
|
|
213
197
|
[0, 0, vector.w]
|
|
214
198
|
]);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
}());
|
|
199
|
+
}
|
|
200
|
+
}
|
|
218
201
|
exports.Matrix2D = Matrix2D;
|
|
219
202
|
exports.default = Matrix2D;
|
|
@@ -1,23 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
-
if (!m) return o;
|
|
5
|
-
var i = m.call(o), r, ar = [], e;
|
|
6
|
-
try {
|
|
7
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
-
}
|
|
9
|
-
catch (error) { e = { error: error }; }
|
|
10
|
-
finally {
|
|
11
|
-
try {
|
|
12
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
-
}
|
|
14
|
-
finally { if (e) throw e.error; }
|
|
15
|
-
}
|
|
16
|
-
return ar;
|
|
17
|
-
};
|
|
18
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
3
|
exports.Navigator = exports.Character = void 0;
|
|
20
|
-
|
|
4
|
+
class Character {
|
|
21
5
|
/**
|
|
22
6
|
* The constructor function takes in a direction and turning object and sets the direction and turn properties of the
|
|
23
7
|
* Character class.
|
|
@@ -26,21 +10,19 @@ var Character = /** @class */ (function () {
|
|
|
26
10
|
* @param {Turning} turning - The `turning` parameter is an object that maps each direction to the corresponding
|
|
27
11
|
* turning direction. It is used to determine the new direction when the character turns.
|
|
28
12
|
*/
|
|
29
|
-
|
|
13
|
+
constructor(direction, turning) {
|
|
30
14
|
this.direction = direction;
|
|
31
|
-
this.turn =
|
|
15
|
+
this.turn = () => new Character(turning[direction], turning);
|
|
32
16
|
}
|
|
33
|
-
|
|
34
|
-
}());
|
|
17
|
+
}
|
|
35
18
|
exports.Character = Character;
|
|
36
|
-
|
|
19
|
+
class Navigator {
|
|
37
20
|
/**
|
|
38
21
|
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
|
|
39
22
|
* in the matrix.
|
|
40
23
|
* @param - - `matrix`: a 2D array representing the grid or map
|
|
41
24
|
*/
|
|
42
|
-
|
|
43
|
-
var matrix = _a.matrix, turning = _a.turning, onMove = _a.onMove, _b = _a.init, cur = _b.cur, charDir = _b.charDir, VISITED = _b.VISITED;
|
|
25
|
+
constructor({ matrix, turning, onMove, init: { cur, charDir, VISITED } }) {
|
|
44
26
|
this._matrix = matrix;
|
|
45
27
|
this._cur = cur;
|
|
46
28
|
this._character = new Character(charDir, turning);
|
|
@@ -53,9 +35,9 @@ var Navigator = /** @class */ (function () {
|
|
|
53
35
|
* The "start" function moves the character in its current direction until it encounters an obstacle, then it turns the
|
|
54
36
|
* character and repeats the process.
|
|
55
37
|
*/
|
|
56
|
-
|
|
38
|
+
start() {
|
|
57
39
|
while (this.check(this._character.direction) || this.check(this._character.turn().direction)) {
|
|
58
|
-
|
|
40
|
+
const { direction } = this._character;
|
|
59
41
|
if (this.check(direction)) {
|
|
60
42
|
this.move(direction);
|
|
61
43
|
}
|
|
@@ -63,17 +45,17 @@ var Navigator = /** @class */ (function () {
|
|
|
63
45
|
this._character = this._character.turn();
|
|
64
46
|
}
|
|
65
47
|
}
|
|
66
|
-
}
|
|
48
|
+
}
|
|
67
49
|
/**
|
|
68
50
|
* The function checks if there is a valid move in the specified direction in a matrix.
|
|
69
51
|
* @param {Direction} direction - The direction parameter is a string that represents the direction in which to check.
|
|
70
52
|
* It can be one of the following values: 'up', 'right', 'down', or 'left'.
|
|
71
53
|
* @returns a boolean value.
|
|
72
54
|
*/
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
55
|
+
check(direction) {
|
|
56
|
+
let forward, row;
|
|
57
|
+
const matrix = this._matrix;
|
|
58
|
+
const [i, j] = this._cur;
|
|
77
59
|
switch (direction) {
|
|
78
60
|
case 'up':
|
|
79
61
|
row = matrix[i - 1];
|
|
@@ -95,13 +77,13 @@ var Navigator = /** @class */ (function () {
|
|
|
95
77
|
break;
|
|
96
78
|
}
|
|
97
79
|
return forward !== undefined && forward !== this._VISITED;
|
|
98
|
-
}
|
|
80
|
+
}
|
|
99
81
|
/**
|
|
100
82
|
* The `move` function updates the current position based on the given direction and updates the matrix accordingly.
|
|
101
83
|
* @param {Direction} direction - The `direction` parameter is a string that represents the direction in which to move.
|
|
102
84
|
* It can have one of the following values: 'up', 'right', 'down', or 'left'.
|
|
103
85
|
*/
|
|
104
|
-
|
|
86
|
+
move(direction) {
|
|
105
87
|
switch (direction) {
|
|
106
88
|
case 'up':
|
|
107
89
|
this._cur[0]--;
|
|
@@ -116,10 +98,9 @@ var Navigator = /** @class */ (function () {
|
|
|
116
98
|
this._cur[1]--;
|
|
117
99
|
break;
|
|
118
100
|
}
|
|
119
|
-
|
|
101
|
+
const [i, j] = this._cur;
|
|
120
102
|
this._matrix[i][j] = this._VISITED;
|
|
121
103
|
this.onMove && this.onMove(this._cur);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
}());
|
|
104
|
+
}
|
|
105
|
+
}
|
|
125
106
|
exports.Navigator = Navigator;
|