directed-graph-typed 1.49.4 → 1.49.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +154 -143
- package/dist/data-structures/binary-tree/binary-tree.js +211 -198
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +113 -89
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -55
- package/dist/data-structures/binary-tree/tree-multimap.js +59 -94
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/data-structures/graph/abstract-graph.js +3 -2
- package/dist/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/data-structures/hash/hash-map.js +2 -2
- package/dist/data-structures/heap/heap.js +2 -3
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/index.d.ts +0 -2
- package/dist/data-structures/matrix/index.js +0 -2
- package/dist/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/data-structures/matrix/matrix.js +400 -15
- package/dist/data-structures/queue/deque.d.ts +2 -2
- package/dist/data-structures/queue/deque.js +5 -7
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/utils/utils.d.ts +1 -0
- package/dist/utils/utils.js +6 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -1
- package/src/data-structures/base/iterable-base.ts +7 -10
- package/src/data-structures/binary-tree/avl-tree.ts +73 -61
- package/src/data-structures/binary-tree/binary-tree.ts +301 -270
- package/src/data-structures/binary-tree/bst.ts +139 -115
- package/src/data-structures/binary-tree/rb-tree.ts +81 -73
- package/src/data-structures/binary-tree/tree-multimap.ts +72 -103
- package/src/data-structures/graph/abstract-graph.ts +13 -11
- package/src/data-structures/graph/directed-graph.ts +1 -3
- package/src/data-structures/graph/map-graph.ts +6 -1
- package/src/data-structures/graph/undirected-graph.ts +3 -6
- package/src/data-structures/hash/hash-map.ts +18 -16
- package/src/data-structures/heap/heap.ts +7 -10
- package/src/data-structures/heap/max-heap.ts +2 -1
- package/src/data-structures/heap/min-heap.ts +2 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/matrix/index.ts +0 -2
- package/src/data-structures/matrix/matrix.ts +442 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
- package/src/data-structures/queue/deque.ts +18 -39
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +9 -4
- package/src/types/common.ts +5 -5
- package/src/types/data-structures/base/base.ts +14 -3
- package/src/types/data-structures/base/index.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +4 -2
- package/src/types/data-structures/hash/hash-map.ts +3 -3
- package/src/types/data-structures/heap/heap.ts +2 -2
- package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/utils/utils.ts +7 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/data-structures/matrix/matrix2d.js +0 -199
- package/dist/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/data-structures/matrix/vector2d.js +0 -290
- package/src/data-structures/matrix/matrix2d.ts +0 -211
- package/src/data-structures/matrix/vector2d.ts +0 -315
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MatrixNTI2D = void 0;
|
|
4
2
|
/**
|
|
5
3
|
* data-structure-typed
|
|
6
4
|
*
|
|
@@ -8,21 +6,408 @@ exports.MatrixNTI2D = void 0;
|
|
|
8
6
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
7
|
* @license MIT License
|
|
10
8
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.Matrix = void 0;
|
|
11
|
+
class Matrix {
|
|
13
12
|
/**
|
|
14
|
-
* The constructor
|
|
15
|
-
*
|
|
16
|
-
* @param
|
|
13
|
+
* The constructor function initializes a matrix object with the provided data and options, or with
|
|
14
|
+
* default values if no options are provided.
|
|
15
|
+
* @param {number[][]} data - A 2D array of numbers representing the data for the matrix.
|
|
16
|
+
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
17
|
+
* properties:
|
|
17
18
|
*/
|
|
18
|
-
constructor(options) {
|
|
19
|
-
|
|
20
|
-
this.
|
|
19
|
+
constructor(data, options) {
|
|
20
|
+
var _a, _b, _c;
|
|
21
|
+
this._rows = 0;
|
|
22
|
+
this._cols = 0;
|
|
23
|
+
if (options) {
|
|
24
|
+
const { rows, cols, addFn, subtractFn, multiplyFn } = options;
|
|
25
|
+
if (typeof rows === 'number' && rows > 0)
|
|
26
|
+
this._rows = rows;
|
|
27
|
+
else
|
|
28
|
+
this._rows = data.length;
|
|
29
|
+
if (typeof cols === 'number' && cols > 0)
|
|
30
|
+
this._cols = cols;
|
|
31
|
+
else
|
|
32
|
+
this._cols = ((_a = data[0]) === null || _a === void 0 ? void 0 : _a.length) || 0;
|
|
33
|
+
if (addFn)
|
|
34
|
+
this._addFn = addFn;
|
|
35
|
+
if (subtractFn)
|
|
36
|
+
this._subtractFn = subtractFn;
|
|
37
|
+
if (multiplyFn)
|
|
38
|
+
this._multiplyFn = multiplyFn;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this._rows = data.length;
|
|
42
|
+
this._cols = (_c = (_b = data[0]) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0;
|
|
43
|
+
}
|
|
44
|
+
if (data.length > 0) {
|
|
45
|
+
this._data = data;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this._data = [];
|
|
49
|
+
for (let i = 0; i < this.rows; i++) {
|
|
50
|
+
this._data[i] = new Array(this.cols).fill(0);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
get rows() {
|
|
55
|
+
return this._rows;
|
|
56
|
+
}
|
|
57
|
+
get cols() {
|
|
58
|
+
return this._cols;
|
|
59
|
+
}
|
|
60
|
+
get data() {
|
|
61
|
+
return this._data;
|
|
62
|
+
}
|
|
63
|
+
get addFn() {
|
|
64
|
+
return this._addFn;
|
|
65
|
+
}
|
|
66
|
+
get subtractFn() {
|
|
67
|
+
return this._subtractFn;
|
|
21
68
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
69
|
+
get multiplyFn() {
|
|
70
|
+
return this._multiplyFn;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The `get` function returns the value at the specified row and column index if it is a valid index.
|
|
74
|
+
* @param {number} row - The `row` parameter represents the row index of the element you want to
|
|
75
|
+
* retrieve from the data array.
|
|
76
|
+
* @param {number} col - The parameter "col" represents the column number of the element you want to
|
|
77
|
+
* retrieve from the data array.
|
|
78
|
+
* @returns The `get` function returns a number if the provided row and column indices are valid.
|
|
79
|
+
* Otherwise, it returns `undefined`.
|
|
80
|
+
*/
|
|
81
|
+
get(row, col) {
|
|
82
|
+
if (this.isValidIndex(row, col)) {
|
|
83
|
+
return this.data[row][col];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The set function updates the value at a specified row and column in a two-dimensional array.
|
|
88
|
+
* @param {number} row - The "row" parameter represents the row index of the element in a
|
|
89
|
+
* two-dimensional array or matrix. It specifies the row where the value will be set.
|
|
90
|
+
* @param {number} col - The "col" parameter represents the column index of the element in a
|
|
91
|
+
* two-dimensional array.
|
|
92
|
+
* @param {number} value - The value parameter represents the number that you want to set at the
|
|
93
|
+
* specified row and column in the data array.
|
|
94
|
+
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
95
|
+
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
96
|
+
* set.
|
|
97
|
+
*/
|
|
98
|
+
set(row, col, value) {
|
|
99
|
+
if (this.isValidIndex(row, col)) {
|
|
100
|
+
this.data[row][col] = value;
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The function checks if the dimensions of the given matrix match the dimensions of the current
|
|
107
|
+
* matrix.
|
|
108
|
+
* @param {Matrix} matrix - The parameter `matrix` is of type `Matrix`.
|
|
109
|
+
* @returns a boolean value.
|
|
110
|
+
*/
|
|
111
|
+
isMatchForCalculate(matrix) {
|
|
112
|
+
return this.rows === matrix.rows && this.cols === matrix.cols;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The `add` function adds two matrices together, returning a new matrix with the result.
|
|
116
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
117
|
+
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
118
|
+
* current matrix with the provided `matrix` parameter.
|
|
119
|
+
*/
|
|
120
|
+
add(matrix) {
|
|
121
|
+
if (!this.isMatchForCalculate(matrix)) {
|
|
122
|
+
throw new Error('Matrix dimensions must match for addition.');
|
|
123
|
+
}
|
|
124
|
+
const resultData = [];
|
|
125
|
+
for (let i = 0; i < this.rows; i++) {
|
|
126
|
+
resultData[i] = [];
|
|
127
|
+
for (let j = 0; j < this.cols; j++) {
|
|
128
|
+
const a = this.get(i, j), b = matrix.get(i, j);
|
|
129
|
+
if (a !== undefined && b !== undefined) {
|
|
130
|
+
const added = this._addFn(a, b);
|
|
131
|
+
if (added) {
|
|
132
|
+
resultData[i][j] = added;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return new Matrix(resultData, {
|
|
138
|
+
rows: this.rows,
|
|
139
|
+
cols: this.cols,
|
|
140
|
+
addFn: this.addFn,
|
|
141
|
+
subtractFn: this.subtractFn,
|
|
142
|
+
multiplyFn: this.multiplyFn
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The `subtract` function performs element-wise subtraction between two matrices and returns a new
|
|
147
|
+
* matrix with the result.
|
|
148
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
149
|
+
* represents the matrix that you want to subtract from the current matrix.
|
|
150
|
+
* @returns a new Matrix object with the result of the subtraction operation.
|
|
151
|
+
*/
|
|
152
|
+
subtract(matrix) {
|
|
153
|
+
if (!this.isMatchForCalculate(matrix)) {
|
|
154
|
+
throw new Error('Matrix dimensions must match for subtraction.');
|
|
155
|
+
}
|
|
156
|
+
const resultData = [];
|
|
157
|
+
for (let i = 0; i < this.rows; i++) {
|
|
158
|
+
resultData[i] = [];
|
|
159
|
+
for (let j = 0; j < this.cols; j++) {
|
|
160
|
+
const a = this.get(i, j), b = matrix.get(i, j);
|
|
161
|
+
if (a !== undefined && b !== undefined) {
|
|
162
|
+
const subtracted = this._subtractFn(a, b);
|
|
163
|
+
if (subtracted) {
|
|
164
|
+
resultData[i][j] = subtracted;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return new Matrix(resultData, {
|
|
170
|
+
rows: this.rows,
|
|
171
|
+
cols: this.cols,
|
|
172
|
+
addFn: this.addFn,
|
|
173
|
+
subtractFn: this.subtractFn,
|
|
174
|
+
multiplyFn: this.multiplyFn
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The `multiply` function performs matrix multiplication between two matrices and returns the result
|
|
179
|
+
* as a new matrix.
|
|
180
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
181
|
+
* @returns a new Matrix object.
|
|
182
|
+
*/
|
|
183
|
+
multiply(matrix) {
|
|
184
|
+
if (this.cols !== matrix.rows) {
|
|
185
|
+
throw new Error('Matrix dimensions must be compatible for multiplication (A.cols = B.rows).');
|
|
186
|
+
}
|
|
187
|
+
const resultData = [];
|
|
188
|
+
for (let i = 0; i < this.rows; i++) {
|
|
189
|
+
resultData[i] = [];
|
|
190
|
+
for (let j = 0; j < matrix.cols; j++) {
|
|
191
|
+
let sum;
|
|
192
|
+
for (let k = 0; k < this.cols; k++) {
|
|
193
|
+
const a = this.get(i, k), b = matrix.get(k, j);
|
|
194
|
+
if (a !== undefined && b !== undefined) {
|
|
195
|
+
const multiplied = this.multiplyFn(a, b);
|
|
196
|
+
if (multiplied !== undefined) {
|
|
197
|
+
sum = this.addFn(sum, multiplied);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (sum !== undefined)
|
|
202
|
+
resultData[i][j] = sum;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return new Matrix(resultData, {
|
|
206
|
+
rows: this.rows,
|
|
207
|
+
cols: matrix.cols,
|
|
208
|
+
addFn: this.addFn,
|
|
209
|
+
subtractFn: this.subtractFn,
|
|
210
|
+
multiplyFn: this.multiplyFn
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
215
|
+
* original matrix.
|
|
216
|
+
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
217
|
+
*/
|
|
218
|
+
transpose() {
|
|
219
|
+
if (this.data.some(row => row.length !== this.rows)) {
|
|
220
|
+
throw new Error('Matrix must be rectangular for transposition.');
|
|
221
|
+
}
|
|
222
|
+
const resultData = [];
|
|
223
|
+
for (let j = 0; j < this.cols; j++) {
|
|
224
|
+
resultData[j] = [];
|
|
225
|
+
for (let i = 0; i < this.rows; i++) {
|
|
226
|
+
const trans = this.get(i, j);
|
|
227
|
+
if (trans !== undefined)
|
|
228
|
+
resultData[j][i] = trans;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return new Matrix(resultData, {
|
|
232
|
+
rows: this.cols,
|
|
233
|
+
cols: this.rows,
|
|
234
|
+
addFn: this.addFn,
|
|
235
|
+
subtractFn: this.subtractFn,
|
|
236
|
+
multiplyFn: this.multiplyFn
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
241
|
+
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
242
|
+
*/
|
|
243
|
+
inverse() {
|
|
244
|
+
var _a;
|
|
245
|
+
// Check if the matrix is square
|
|
246
|
+
if (this.rows !== this.cols) {
|
|
247
|
+
throw new Error('Matrix must be square for inversion.');
|
|
248
|
+
}
|
|
249
|
+
// Create an augmented matrix [this | I]
|
|
250
|
+
const augmentedMatrixData = [];
|
|
251
|
+
for (let i = 0; i < this.rows; i++) {
|
|
252
|
+
augmentedMatrixData[i] = this.data[i].slice(); // Copy the original matrix
|
|
253
|
+
for (let j = 0; j < this.cols; j++) {
|
|
254
|
+
augmentedMatrixData[i][this.cols + j] = i === j ? 1 : 0; // Append the identity matrix
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const augmentedMatrix = new Matrix(augmentedMatrixData, {
|
|
258
|
+
rows: this.rows,
|
|
259
|
+
cols: this.cols * 2,
|
|
260
|
+
addFn: this.addFn,
|
|
261
|
+
subtractFn: this.subtractFn,
|
|
262
|
+
multiplyFn: this.multiplyFn
|
|
263
|
+
});
|
|
264
|
+
// Apply Gaussian elimination to transform the left half into the identity matrix
|
|
265
|
+
for (let i = 0; i < this.rows; i++) {
|
|
266
|
+
// Find pivot
|
|
267
|
+
let pivotRow = i;
|
|
268
|
+
while (pivotRow < this.rows && augmentedMatrix.get(pivotRow, i) === 0) {
|
|
269
|
+
pivotRow++;
|
|
270
|
+
}
|
|
271
|
+
if (pivotRow === this.rows) {
|
|
272
|
+
// Matrix is singular, and its inverse does not exist
|
|
273
|
+
throw new Error('Matrix is singular, and its inverse does not exist.');
|
|
274
|
+
}
|
|
275
|
+
// Swap rows to make the pivot the current row
|
|
276
|
+
augmentedMatrix._swapRows(i, pivotRow);
|
|
277
|
+
// Scale the pivot row to make the pivot element 1
|
|
278
|
+
const pivotElement = (_a = augmentedMatrix.get(i, i)) !== null && _a !== void 0 ? _a : 1;
|
|
279
|
+
if (pivotElement === 0) {
|
|
280
|
+
// Handle division by zero
|
|
281
|
+
throw new Error('Matrix is singular, and its inverse does not exist (division by zero).');
|
|
282
|
+
}
|
|
283
|
+
augmentedMatrix._scaleRow(i, 1 / pivotElement);
|
|
284
|
+
// Eliminate other rows to make elements in the current column zero
|
|
285
|
+
for (let j = 0; j < this.rows; j++) {
|
|
286
|
+
if (j !== i) {
|
|
287
|
+
let factor = augmentedMatrix.get(j, i);
|
|
288
|
+
if (factor === undefined)
|
|
289
|
+
factor = 0;
|
|
290
|
+
augmentedMatrix._addScaledRow(j, i, -factor);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
// Extract the right half of the augmented matrix as the inverse
|
|
295
|
+
const inverseData = [];
|
|
296
|
+
for (let i = 0; i < this.rows; i++) {
|
|
297
|
+
inverseData[i] = augmentedMatrix.data[i].slice(this.cols);
|
|
298
|
+
}
|
|
299
|
+
return new Matrix(inverseData, {
|
|
300
|
+
rows: this.rows,
|
|
301
|
+
cols: this.cols,
|
|
302
|
+
addFn: this.addFn,
|
|
303
|
+
subtractFn: this.subtractFn,
|
|
304
|
+
multiplyFn: this.multiplyFn
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
309
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
310
|
+
* @returns a new Matrix object.
|
|
311
|
+
*/
|
|
312
|
+
dot(matrix) {
|
|
313
|
+
if (this.cols !== matrix.rows) {
|
|
314
|
+
throw new Error('Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product.');
|
|
315
|
+
}
|
|
316
|
+
const resultData = [];
|
|
317
|
+
for (let i = 0; i < this.rows; i++) {
|
|
318
|
+
resultData[i] = [];
|
|
319
|
+
for (let j = 0; j < matrix.cols; j++) {
|
|
320
|
+
let sum;
|
|
321
|
+
for (let k = 0; k < this.cols; k++) {
|
|
322
|
+
const a = this.get(i, k), b = matrix.get(k, j);
|
|
323
|
+
if (a !== undefined && b !== undefined) {
|
|
324
|
+
const multiplied = this.multiplyFn(a, b);
|
|
325
|
+
if (multiplied !== undefined) {
|
|
326
|
+
sum = this.addFn(sum, multiplied);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (sum !== undefined)
|
|
331
|
+
resultData[i][j] = sum;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return new Matrix(resultData, {
|
|
335
|
+
rows: this.rows,
|
|
336
|
+
cols: matrix.cols,
|
|
337
|
+
addFn: this.addFn,
|
|
338
|
+
subtractFn: this.subtractFn,
|
|
339
|
+
multiplyFn: this.multiplyFn
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
_addFn(a, b) {
|
|
343
|
+
if (a === undefined)
|
|
344
|
+
return b;
|
|
345
|
+
return a + b;
|
|
346
|
+
}
|
|
347
|
+
_subtractFn(a, b) {
|
|
348
|
+
return a - b;
|
|
349
|
+
}
|
|
350
|
+
_multiplyFn(a, b) {
|
|
351
|
+
return a * b;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* The function checks if a given row and column index is valid within a specified range.
|
|
355
|
+
* @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
|
|
356
|
+
* matrix. It is a number that indicates the specific row in the matrix.
|
|
357
|
+
* @param {number} col - The "col" parameter represents the column index in a two-dimensional array
|
|
358
|
+
* or grid. It is used to check if the given column index is valid within the bounds of the grid.
|
|
359
|
+
* @returns A boolean value is being returned.
|
|
360
|
+
*/
|
|
361
|
+
isValidIndex(row, col) {
|
|
362
|
+
return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* The function `_swapRows` swaps the positions of two rows in an array.
|
|
366
|
+
* @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
|
|
367
|
+
* @param {number} row2 - The `row2` parameter is the index of the second row that you want to swap
|
|
368
|
+
* with the first row.
|
|
369
|
+
*/
|
|
370
|
+
_swapRows(row1, row2) {
|
|
371
|
+
const temp = this.data[row1];
|
|
372
|
+
this.data[row1] = this.data[row2];
|
|
373
|
+
this.data[row2] = temp;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* The function scales a specific row in a matrix by a given scalar value.
|
|
377
|
+
* @param {number} row - The `row` parameter represents the index of the row in the matrix that you
|
|
378
|
+
* want to scale. It is a number that indicates the position of the row within the matrix.
|
|
379
|
+
* @param {number} scalar - The scalar parameter is a number that is used to multiply each element in
|
|
380
|
+
* a specific row of a matrix.
|
|
381
|
+
*/
|
|
382
|
+
_scaleRow(row, scalar) {
|
|
383
|
+
for (let j = 0; j < this.cols; j++) {
|
|
384
|
+
let multiplied = this.multiplyFn(this.data[row][j], scalar);
|
|
385
|
+
if (multiplied === undefined)
|
|
386
|
+
multiplied = 0;
|
|
387
|
+
this.data[row][j] = multiplied;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* The function `_addScaledRow` multiplies a row in a matrix by a scalar value and adds it to another
|
|
392
|
+
* row.
|
|
393
|
+
* @param {number} targetRow - The targetRow parameter represents the index of the row in which the
|
|
394
|
+
* scaled values will be added.
|
|
395
|
+
* @param {number} sourceRow - The sourceRow parameter represents the index of the row from which the
|
|
396
|
+
* values will be scaled and added to the targetRow.
|
|
397
|
+
* @param {number} scalar - The scalar parameter is a number that is used to scale the values in the
|
|
398
|
+
* source row before adding them to the target row.
|
|
399
|
+
*/
|
|
400
|
+
_addScaledRow(targetRow, sourceRow, scalar) {
|
|
401
|
+
for (let j = 0; j < this.cols; j++) {
|
|
402
|
+
let multiplied = this.multiplyFn(this.data[sourceRow][j], scalar);
|
|
403
|
+
if (multiplied === undefined)
|
|
404
|
+
multiplied = 0;
|
|
405
|
+
const scaledValue = multiplied;
|
|
406
|
+
let added = this.addFn(this.data[targetRow][j], scaledValue);
|
|
407
|
+
if (added === undefined)
|
|
408
|
+
added = 0;
|
|
409
|
+
this.data[targetRow][j] = added;
|
|
410
|
+
}
|
|
26
411
|
}
|
|
27
412
|
}
|
|
28
|
-
exports.
|
|
413
|
+
exports.Matrix = Matrix;
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { ElementCallback, IterableWithSizeOrLength } from
|
|
9
|
-
import { IterableElementBase } from
|
|
8
|
+
import type { ElementCallback, IterableWithSizeOrLength } from '../../types';
|
|
9
|
+
import { IterableElementBase } from '../base';
|
|
10
10
|
/**
|
|
11
11
|
* 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
|
|
12
12
|
* 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
|
|
@@ -20,7 +20,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
20
20
|
* @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be
|
|
21
21
|
* stored in each bucket. It determines the size of each bucket in the data structure.
|
|
22
22
|
*/
|
|
23
|
-
constructor(elements = [], bucketSize =
|
|
23
|
+
constructor(elements = [], bucketSize = 1 << 12) {
|
|
24
24
|
super();
|
|
25
25
|
this._bucketFirst = 0;
|
|
26
26
|
this._firstInBucket = 0;
|
|
@@ -49,7 +49,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
49
49
|
}
|
|
50
50
|
const needBucketNum = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize);
|
|
51
51
|
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
|
52
|
-
this._firstInBucket = this._lastInBucket = (this._bucketSize - _size % this._bucketSize) >> 1;
|
|
52
|
+
this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
|
|
53
53
|
for (const element of elements) {
|
|
54
54
|
this.push(element);
|
|
55
55
|
}
|
|
@@ -101,8 +101,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
101
101
|
this._bucketLast = 0;
|
|
102
102
|
this._lastInBucket = 0;
|
|
103
103
|
}
|
|
104
|
-
if (this._bucketLast === this._bucketFirst &&
|
|
105
|
-
this._lastInBucket === this._firstInBucket)
|
|
104
|
+
if (this._bucketLast === this._bucketFirst && this._lastInBucket === this._firstInBucket)
|
|
106
105
|
this._reallocate();
|
|
107
106
|
}
|
|
108
107
|
this._size += 1;
|
|
@@ -168,8 +167,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
168
167
|
this._bucketFirst = this._bucketCount - 1;
|
|
169
168
|
this._firstInBucket = this._bucketSize - 1;
|
|
170
169
|
}
|
|
171
|
-
if (this._bucketFirst === this._bucketLast &&
|
|
172
|
-
this._firstInBucket === this._lastInBucket)
|
|
170
|
+
if (this._bucketFirst === this._bucketLast && this._firstInBucket === this._lastInBucket)
|
|
173
171
|
this._reallocate();
|
|
174
172
|
}
|
|
175
173
|
this._size += 1;
|
|
@@ -768,7 +766,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
768
766
|
if (bucketIndex >= this._bucketCount) {
|
|
769
767
|
bucketIndex -= this._bucketCount;
|
|
770
768
|
}
|
|
771
|
-
indexInBucket = (overallIndex + 1) % this._bucketSize - 1;
|
|
769
|
+
indexInBucket = ((overallIndex + 1) % this._bucketSize) - 1;
|
|
772
770
|
if (indexInBucket < 0) {
|
|
773
771
|
indexInBucket = this._bucketSize - 1;
|
|
774
772
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
import type { ElementCallback } from '../../types';
|
|
7
|
-
import { IterableElementBase } from
|
|
7
|
+
import { IterableElementBase } from '../base';
|
|
8
8
|
import { SinglyLinkedList } from '../linked-list';
|
|
9
9
|
/**
|
|
10
10
|
* 1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback,
|
|
2
|
+
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, KeyOrNodeOrEntry } from '../types';
|
|
3
3
|
export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>> {
|
|
4
4
|
createNode(key: K, value?: N['value']): N;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
|
|
6
|
-
add(keyOrNodeOrEntry:
|
|
7
|
-
addMany(nodes: Iterable<
|
|
6
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
|
|
7
|
+
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[];
|
|
8
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[];
|
|
9
9
|
}
|
package/dist/types/common.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare enum BSTVariant {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
STANDARD = "STANDARD",
|
|
3
|
+
INVERSE = "INVERSE"
|
|
4
4
|
}
|
|
5
5
|
export declare enum CP {
|
|
6
6
|
lt = "lt",
|
|
@@ -44,7 +44,7 @@ export type BinaryTreePrintOptions = {
|
|
|
44
44
|
};
|
|
45
45
|
export type BTNEntry<K, V> = [K | null | undefined, V | undefined];
|
|
46
46
|
export type BTNKeyOrNode<K, N> = K | null | undefined | N;
|
|
47
|
-
export type
|
|
47
|
+
export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
|
|
48
48
|
export type BTNodePureKeyOrNode<K, N> = K | N;
|
|
49
49
|
export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
|
|
50
50
|
export type BSTNKeyOrNode<K, N> = K | undefined | N;
|
package/dist/types/common.js
CHANGED
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.FamilyPosition = exports.IterationType = exports.CP = exports.BSTVariant = void 0;
|
|
4
4
|
var BSTVariant;
|
|
5
5
|
(function (BSTVariant) {
|
|
6
|
-
BSTVariant["
|
|
7
|
-
BSTVariant["
|
|
6
|
+
BSTVariant["STANDARD"] = "STANDARD";
|
|
7
|
+
BSTVariant["INVERSE"] = "INVERSE";
|
|
8
8
|
})(BSTVariant = exports.BSTVariant || (exports.BSTVariant = {}));
|
|
9
9
|
var CP;
|
|
10
10
|
(function (CP) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IterableElementBase, IterableEntryBase } from
|
|
1
|
+
import { IterableElementBase, IterableEntryBase } from '../../../data-structures';
|
|
2
2
|
export type EntryCallback<K, V, R> = (value: V, key: K, index: number, container: IterableEntryBase<K, V>) => R;
|
|
3
3
|
export type ElementCallback<V, R> = (element: V, index: number, container: IterableElementBase<V>) => R;
|
|
4
4
|
export type ReduceEntryCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, container: IterableEntryBase<K, V>) => R;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { HeapOptions } from
|
|
1
|
+
import { HeapOptions } from '../heap';
|
|
2
2
|
export type PriorityQueueOptions<T> = HeapOptions<T> & {};
|
package/dist/utils/utils.d.ts
CHANGED
|
@@ -22,3 +22,4 @@ export declare const rangeCheck: (index: number, min: number, max: number, messa
|
|
|
22
22
|
export declare const throwRangeError: (message?: string) => void;
|
|
23
23
|
export declare const isWeakKey: (input: unknown) => input is object;
|
|
24
24
|
export declare const calcMinUnitsRequired: (totalQuantity: number, unitSize: number) => number;
|
|
25
|
+
export declare const roundFixed: (num: number, digit?: number) => number;
|
package/dist/utils/utils.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.calcMinUnitsRequired = exports.isWeakKey = exports.throwRangeError = exports.rangeCheck = exports.getMSB = exports.trampolineAsync = exports.trampoline = exports.toThunk = exports.isThunk = exports.THUNK_SYMBOL = exports.arrayRemove = exports.uuidV4 = void 0;
|
|
12
|
+
exports.roundFixed = exports.calcMinUnitsRequired = exports.isWeakKey = exports.throwRangeError = exports.rangeCheck = exports.getMSB = exports.trampolineAsync = exports.trampoline = exports.toThunk = exports.isThunk = exports.THUNK_SYMBOL = exports.arrayRemove = exports.uuidV4 = void 0;
|
|
13
13
|
const uuidV4 = function () {
|
|
14
14
|
return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
|
|
15
15
|
const r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
@@ -87,3 +87,8 @@ const isWeakKey = (input) => {
|
|
|
87
87
|
exports.isWeakKey = isWeakKey;
|
|
88
88
|
const calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
|
|
89
89
|
exports.calcMinUnitsRequired = calcMinUnitsRequired;
|
|
90
|
+
const roundFixed = (num, digit = 10) => {
|
|
91
|
+
const multiplier = Math.pow(10, digit);
|
|
92
|
+
return Math.round(num * multiplier) / multiplier;
|
|
93
|
+
};
|
|
94
|
+
exports.roundFixed = roundFixed;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "directed-graph-typed",
|
|
3
|
-
"version": "1.49.
|
|
3
|
+
"version": "1.49.6",
|
|
4
4
|
"description": "Directed Graph. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -147,6 +147,6 @@
|
|
|
147
147
|
"typescript": "^4.9.5"
|
|
148
148
|
},
|
|
149
149
|
"dependencies": {
|
|
150
|
-
"data-structure-typed": "^1.49.
|
|
150
|
+
"data-structure-typed": "^1.49.5"
|
|
151
151
|
}
|
|
152
152
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './iterable-base';
|
|
1
|
+
export * from './iterable-base';
|