data-structure-typed 2.4.5 → 2.5.0
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/CHANGELOG.md +1 -1
- package/README.md +15 -5
- package/dist/cjs/index.cjs +10240 -2079
- package/dist/cjs-legacy/index.cjs +10305 -2135
- package/dist/esm/index.mjs +10241 -2078
- package/dist/esm-legacy/index.mjs +10306 -2134
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/data-structure-typed.js +10308 -2133
- package/dist/umd/data-structure-typed.min.js +4 -4
- package/package.json +5 -4
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +146 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +316 -247
- package/src/data-structures/binary-tree/binary-tree.ts +454 -79
- package/src/data-structures/binary-tree/bst.ts +359 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -97
- package/src/data-structures/binary-tree/segment-tree.ts +378 -248
- package/src/data-structures/binary-tree/tree-map.ts +1403 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1214 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +954 -65
- package/src/data-structures/binary-tree/tree-set.ts +1250 -9
- package/src/data-structures/graph/directed-graph.ts +229 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +213 -59
- package/src/data-structures/hash/hash-map.ts +241 -77
- package/src/data-structures/heap/heap.ts +301 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
- package/src/data-structures/matrix/matrix.ts +424 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +287 -65
- package/src/data-structures/queue/queue.ts +223 -42
- package/src/data-structures/stack/stack.ts +184 -32
- package/src/data-structures/trie/trie.ts +225 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -11,6 +11,89 @@ import { ERR } from '../../common';
|
|
|
11
11
|
/**
|
|
12
12
|
*
|
|
13
13
|
*/
|
|
14
|
+
/**
|
|
15
|
+
* Matrix — a numeric matrix with standard linear algebra operations.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Basic matrix arithmetic
|
|
19
|
+
* const a = new Matrix([
|
|
20
|
+
* [1, 2],
|
|
21
|
+
* [3, 4]
|
|
22
|
+
* ]);
|
|
23
|
+
* const b = new Matrix([
|
|
24
|
+
* [5, 6],
|
|
25
|
+
* [7, 8]
|
|
26
|
+
* ]);
|
|
27
|
+
*
|
|
28
|
+
* const sum = a.add(b);
|
|
29
|
+
* console.log(sum?.data); // [
|
|
30
|
+
* // [6, 8],
|
|
31
|
+
* // [10, 12]
|
|
32
|
+
* // ];
|
|
33
|
+
*
|
|
34
|
+
* const diff = b.subtract(a);
|
|
35
|
+
* console.log(diff?.data); // [
|
|
36
|
+
* // [4, 4],
|
|
37
|
+
* // [4, 4]
|
|
38
|
+
* // ];
|
|
39
|
+
* @example
|
|
40
|
+
* // Matrix multiplication for transformations
|
|
41
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
42
|
+
* const a = new Matrix([
|
|
43
|
+
* [1, 2, 3],
|
|
44
|
+
* [4, 5, 6]
|
|
45
|
+
* ]);
|
|
46
|
+
* const b = new Matrix([
|
|
47
|
+
* [7, 8],
|
|
48
|
+
* [9, 10],
|
|
49
|
+
* [11, 12]
|
|
50
|
+
* ]);
|
|
51
|
+
*
|
|
52
|
+
* const product = a.multiply(b);
|
|
53
|
+
* console.log(product?.rows); // 2;
|
|
54
|
+
* console.log(product?.cols); // 2;
|
|
55
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
56
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
57
|
+
* console.log(product?.data); // [
|
|
58
|
+
* // [58, 64],
|
|
59
|
+
* // [139, 154]
|
|
60
|
+
* // ];
|
|
61
|
+
* @example
|
|
62
|
+
* // Matrix transpose (square matrix)
|
|
63
|
+
* const m = new Matrix([
|
|
64
|
+
* [1, 2, 3],
|
|
65
|
+
* [4, 5, 6],
|
|
66
|
+
* [7, 8, 9]
|
|
67
|
+
* ]);
|
|
68
|
+
*
|
|
69
|
+
* const transposed = m.transpose();
|
|
70
|
+
* console.log(transposed.rows); // 3;
|
|
71
|
+
* console.log(transposed.cols); // 3;
|
|
72
|
+
* console.log(transposed.data); // [
|
|
73
|
+
* // [1, 4, 7],
|
|
74
|
+
* // [2, 5, 8],
|
|
75
|
+
* // [3, 6, 9]
|
|
76
|
+
* // ];
|
|
77
|
+
*
|
|
78
|
+
* // Transpose of transpose = original
|
|
79
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
80
|
+
* @example
|
|
81
|
+
* // Get and set individual cells
|
|
82
|
+
* const m = new Matrix([
|
|
83
|
+
* [0, 0, 0],
|
|
84
|
+
* [0, 0, 0]
|
|
85
|
+
* ]);
|
|
86
|
+
*
|
|
87
|
+
* m.set(0, 1, 42);
|
|
88
|
+
* m.set(1, 2, 99);
|
|
89
|
+
*
|
|
90
|
+
* console.log(m.get(0, 1)); // 42;
|
|
91
|
+
* console.log(m.get(1, 2)); // 99;
|
|
92
|
+
* console.log(m.get(0, 0)); // 0;
|
|
93
|
+
*
|
|
94
|
+
* // Out of bounds returns undefined
|
|
95
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
96
|
+
*/
|
|
14
97
|
export class Matrix {
|
|
15
98
|
/**
|
|
16
99
|
* The constructor function initializes a matrix object with the provided data and options, or with
|
|
@@ -106,6 +189,34 @@ export class Matrix {
|
|
|
106
189
|
* retrieve from the data array.
|
|
107
190
|
* @returns The `get` function returns a number if the provided row and column indices are valid.
|
|
108
191
|
* Otherwise, it returns `undefined`.
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
* @example
|
|
205
|
+
* // Get and set individual cells
|
|
206
|
+
* const m = new Matrix([
|
|
207
|
+
* [0, 0, 0],
|
|
208
|
+
* [0, 0, 0]
|
|
209
|
+
* ]);
|
|
210
|
+
*
|
|
211
|
+
* m.set(0, 1, 42);
|
|
212
|
+
* m.set(1, 2, 99);
|
|
213
|
+
*
|
|
214
|
+
* console.log(m.get(0, 1)); // 42;
|
|
215
|
+
* console.log(m.get(1, 2)); // 99;
|
|
216
|
+
* console.log(m.get(0, 0)); // 0;
|
|
217
|
+
*
|
|
218
|
+
* // Out of bounds returns undefined
|
|
219
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
109
220
|
*/
|
|
110
221
|
get(row: number, col: number): number | undefined {
|
|
111
222
|
if (this.isValidIndex(row, col)) {
|
|
@@ -124,6 +235,25 @@ export class Matrix {
|
|
|
124
235
|
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
125
236
|
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
126
237
|
* set.
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
* @example
|
|
251
|
+
* // Modify individual cells
|
|
252
|
+
* const m = Matrix.zeros(2, 2);
|
|
253
|
+
* console.log(m.set(0, 0, 5)); // true;
|
|
254
|
+
* console.log(m.set(1, 1, 10)); // true;
|
|
255
|
+
* console.log(m.get(0, 0)); // 5;
|
|
256
|
+
* console.log(m.get(1, 1)); // 10;
|
|
127
257
|
*/
|
|
128
258
|
set(row: number, col: number, value: number): boolean {
|
|
129
259
|
if (this.isValidIndex(row, col)) {
|
|
@@ -148,6 +278,40 @@ export class Matrix {
|
|
|
148
278
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
149
279
|
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
150
280
|
* current matrix with the provided `matrix` parameter.
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
* @example
|
|
294
|
+
* // Basic matrix arithmetic
|
|
295
|
+
* const a = new Matrix([
|
|
296
|
+
* [1, 2],
|
|
297
|
+
* [3, 4]
|
|
298
|
+
* ]);
|
|
299
|
+
* const b = new Matrix([
|
|
300
|
+
* [5, 6],
|
|
301
|
+
* [7, 8]
|
|
302
|
+
* ]);
|
|
303
|
+
*
|
|
304
|
+
* const sum = a.add(b);
|
|
305
|
+
* console.log(sum?.data); // [
|
|
306
|
+
* // [6, 8],
|
|
307
|
+
* // [10, 12]
|
|
308
|
+
* // ];
|
|
309
|
+
*
|
|
310
|
+
* const diff = b.subtract(a);
|
|
311
|
+
* console.log(diff?.data); // [
|
|
312
|
+
* // [4, 4],
|
|
313
|
+
* // [4, 4]
|
|
314
|
+
* // ];
|
|
151
315
|
*/
|
|
152
316
|
add(matrix: Matrix): Matrix | undefined {
|
|
153
317
|
if (!this.isMatchForCalculate(matrix)) {
|
|
@@ -161,10 +325,7 @@ export class Matrix {
|
|
|
161
325
|
const a = this.get(i, j),
|
|
162
326
|
b = matrix.get(i, j);
|
|
163
327
|
if (a !== undefined && b !== undefined) {
|
|
164
|
-
|
|
165
|
-
if (added) {
|
|
166
|
-
resultData[i][j] = added;
|
|
167
|
-
}
|
|
328
|
+
resultData[i][j] = this._addFn(a, b) ?? 0;
|
|
168
329
|
}
|
|
169
330
|
}
|
|
170
331
|
}
|
|
@@ -184,6 +345,24 @@ export class Matrix {
|
|
|
184
345
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
185
346
|
* represents the matrix that you want to subtract from the current matrix.
|
|
186
347
|
* @returns a new Matrix object with the result of the subtraction operation.
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
* @example
|
|
361
|
+
* // Element-wise subtraction
|
|
362
|
+
* const a = Matrix.from([[5, 6], [7, 8]]);
|
|
363
|
+
* const b = Matrix.from([[1, 2], [3, 4]]);
|
|
364
|
+
* const result = a.subtract(b);
|
|
365
|
+
* console.log(result?.toArray()); // [[4, 4], [4, 4]];
|
|
187
366
|
*/
|
|
188
367
|
subtract(matrix: Matrix): Matrix | undefined {
|
|
189
368
|
if (!this.isMatchForCalculate(matrix)) {
|
|
@@ -197,10 +376,7 @@ export class Matrix {
|
|
|
197
376
|
const a = this.get(i, j),
|
|
198
377
|
b = matrix.get(i, j);
|
|
199
378
|
if (a !== undefined && b !== undefined) {
|
|
200
|
-
|
|
201
|
-
if (subtracted) {
|
|
202
|
-
resultData[i][j] = subtracted;
|
|
203
|
-
}
|
|
379
|
+
resultData[i][j] = this._subtractFn(a, b) ?? 0;
|
|
204
380
|
}
|
|
205
381
|
}
|
|
206
382
|
}
|
|
@@ -219,6 +395,40 @@ export class Matrix {
|
|
|
219
395
|
* as a new matrix.
|
|
220
396
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
221
397
|
* @returns a new Matrix object.
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
* @example
|
|
411
|
+
* // Matrix multiplication for transformations
|
|
412
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
413
|
+
* const a = new Matrix([
|
|
414
|
+
* [1, 2, 3],
|
|
415
|
+
* [4, 5, 6]
|
|
416
|
+
* ]);
|
|
417
|
+
* const b = new Matrix([
|
|
418
|
+
* [7, 8],
|
|
419
|
+
* [9, 10],
|
|
420
|
+
* [11, 12]
|
|
421
|
+
* ]);
|
|
422
|
+
*
|
|
423
|
+
* const product = a.multiply(b);
|
|
424
|
+
* console.log(product?.rows); // 2;
|
|
425
|
+
* console.log(product?.cols); // 2;
|
|
426
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
427
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
428
|
+
* console.log(product?.data); // [
|
|
429
|
+
* // [58, 64],
|
|
430
|
+
* // [139, 154]
|
|
431
|
+
* // ];
|
|
222
432
|
*/
|
|
223
433
|
multiply(matrix: Matrix): Matrix | undefined {
|
|
224
434
|
if (this.cols !== matrix.rows) {
|
|
@@ -257,9 +467,40 @@ export class Matrix {
|
|
|
257
467
|
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
258
468
|
* original matrix.
|
|
259
469
|
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
* @example
|
|
483
|
+
* // Matrix transpose (square matrix)
|
|
484
|
+
* const m = new Matrix([
|
|
485
|
+
* [1, 2, 3],
|
|
486
|
+
* [4, 5, 6],
|
|
487
|
+
* [7, 8, 9]
|
|
488
|
+
* ]);
|
|
489
|
+
*
|
|
490
|
+
* const transposed = m.transpose();
|
|
491
|
+
* console.log(transposed.rows); // 3;
|
|
492
|
+
* console.log(transposed.cols); // 3;
|
|
493
|
+
* console.log(transposed.data); // [
|
|
494
|
+
* // [1, 4, 7],
|
|
495
|
+
* // [2, 5, 8],
|
|
496
|
+
* // [3, 6, 9]
|
|
497
|
+
* // ];
|
|
498
|
+
*
|
|
499
|
+
* // Transpose of transpose = original
|
|
500
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
260
501
|
*/
|
|
261
502
|
transpose(): Matrix {
|
|
262
|
-
if (this.data.some(row => row.length !== this.
|
|
503
|
+
if (this.data.some(row => row.length !== this.cols)) {
|
|
263
504
|
throw new Error(ERR.matrixNotRectangular());
|
|
264
505
|
}
|
|
265
506
|
|
|
@@ -285,6 +526,29 @@ export class Matrix {
|
|
|
285
526
|
/**
|
|
286
527
|
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
287
528
|
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
* @example
|
|
542
|
+
* // Compute the inverse of a 2x2 matrix
|
|
543
|
+
* const m = Matrix.from([[4, 7], [2, 6]]);
|
|
544
|
+
* const inv = m.inverse();
|
|
545
|
+
* console.log(inv); // defined;
|
|
546
|
+
* // A * A^-1 should ≈ Identity
|
|
547
|
+
* const product = m.multiply(inv!);
|
|
548
|
+
* console.log(product?.get(0, 0)); // toBeCloseTo;
|
|
549
|
+
* console.log(product?.get(0, 1)); // toBeCloseTo;
|
|
550
|
+
* console.log(product?.get(1, 0)); // toBeCloseTo;
|
|
551
|
+
* console.log(product?.get(1, 1)); // toBeCloseTo;
|
|
288
552
|
*/
|
|
289
553
|
inverse(): Matrix | undefined {
|
|
290
554
|
// Check if the matrix is square
|
|
@@ -365,6 +629,24 @@ export class Matrix {
|
|
|
365
629
|
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
366
630
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
367
631
|
* @returns a new Matrix object.
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
* @example
|
|
645
|
+
* // Dot product of two matrices
|
|
646
|
+
* const a = Matrix.from([[1, 2], [3, 4]]);
|
|
647
|
+
* const b = Matrix.from([[5, 6], [7, 8]]);
|
|
648
|
+
* const result = a.dot(b);
|
|
649
|
+
* console.log(result?.toArray()); // [[19, 22], [43, 50]];
|
|
368
650
|
*/
|
|
369
651
|
dot(matrix: Matrix): Matrix | undefined {
|
|
370
652
|
if (this.cols !== matrix.rows) {
|
|
@@ -418,15 +700,145 @@ export class Matrix {
|
|
|
418
700
|
* and properties as the current instance.
|
|
419
701
|
*/
|
|
420
702
|
clone(): Matrix {
|
|
421
|
-
return new Matrix(
|
|
422
|
-
|
|
423
|
-
|
|
703
|
+
return new Matrix(
|
|
704
|
+
this._data.map(row => [...row]),
|
|
705
|
+
{
|
|
706
|
+
rows: this.rows,
|
|
707
|
+
cols: this.cols,
|
|
708
|
+
addFn: this.addFn,
|
|
709
|
+
subtractFn: this.subtractFn,
|
|
710
|
+
multiplyFn: this.multiplyFn
|
|
711
|
+
}
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// ─── Standard interface ─────────────────────────────────────
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* Returns [rows, cols] dimensions tuple.
|
|
719
|
+
*/
|
|
720
|
+
get size(): [number, number] {
|
|
721
|
+
return [this._rows, this._cols];
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
isEmpty(): boolean {
|
|
725
|
+
return this._rows === 0 || this._cols === 0;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* Returns a deep copy of the data as a plain 2D array.
|
|
730
|
+
*/
|
|
731
|
+
toArray(): number[][] {
|
|
732
|
+
return this._data.map(row => [...row]);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* Returns a flat row-major array.
|
|
737
|
+
*/
|
|
738
|
+
flatten(): number[] {
|
|
739
|
+
const result: number[] = [];
|
|
740
|
+
for (const row of this._data) {
|
|
741
|
+
for (const v of row) result.push(v);
|
|
742
|
+
}
|
|
743
|
+
return result;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* Iterates over rows.
|
|
748
|
+
*/
|
|
749
|
+
[Symbol.iterator](): IterableIterator<number[]> {
|
|
750
|
+
const data = this._data;
|
|
751
|
+
let i = 0;
|
|
752
|
+
return {
|
|
753
|
+
[Symbol.iterator]() {
|
|
754
|
+
return this;
|
|
755
|
+
},
|
|
756
|
+
next(): IteratorResult<number[]> {
|
|
757
|
+
if (i < data.length) {
|
|
758
|
+
return { value: [...data[i++]], done: false };
|
|
759
|
+
}
|
|
760
|
+
return { value: undefined as any, done: true };
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
/**
|
|
766
|
+
* Visits each element with its row and column index.
|
|
767
|
+
*/
|
|
768
|
+
forEach(callback: (value: number, row: number, col: number) => void): void {
|
|
769
|
+
for (let i = 0; i < this._rows; i++) {
|
|
770
|
+
for (let j = 0; j < this._cols; j++) {
|
|
771
|
+
callback(this._data[i][j], i, j);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* Maps each element (number → number) and returns a new Matrix.
|
|
778
|
+
*/
|
|
779
|
+
map(callback: (value: number, row: number, col: number) => number): Matrix {
|
|
780
|
+
const resultData: number[][] = [];
|
|
781
|
+
for (let i = 0; i < this._rows; i++) {
|
|
782
|
+
resultData[i] = [];
|
|
783
|
+
for (let j = 0; j < this._cols; j++) {
|
|
784
|
+
resultData[i][j] = callback(this._data[i][j], i, j);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
return new Matrix(resultData, {
|
|
788
|
+
rows: this._rows,
|
|
789
|
+
cols: this._cols,
|
|
424
790
|
addFn: this.addFn,
|
|
425
791
|
subtractFn: this.subtractFn,
|
|
426
792
|
multiplyFn: this.multiplyFn
|
|
427
793
|
});
|
|
428
794
|
}
|
|
429
795
|
|
|
796
|
+
print(): void {
|
|
797
|
+
for (const row of this._data) {
|
|
798
|
+
console.log(row.join('\t'));
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
// ─── Factory methods ────────────────────────────────────────
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* Creates a rows×cols zero matrix.
|
|
806
|
+
* @example
|
|
807
|
+
* ```ts
|
|
808
|
+
* const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
|
|
809
|
+
* ```
|
|
810
|
+
*/
|
|
811
|
+
static zeros(rows: number, cols: number): Matrix {
|
|
812
|
+
const data: number[][] = Array.from({ length: rows }, () => new Array(cols).fill(0));
|
|
813
|
+
return new Matrix(data);
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* Creates an n×n identity matrix.
|
|
818
|
+
* @example
|
|
819
|
+
* ```ts
|
|
820
|
+
* const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
|
|
821
|
+
* ```
|
|
822
|
+
*/
|
|
823
|
+
static identity(n: number): Matrix {
|
|
824
|
+
const data: number[][] = Array.from({ length: n }, (_, i) =>
|
|
825
|
+
Array.from({ length: n }, (_, j) => (i === j ? 1 : 0))
|
|
826
|
+
);
|
|
827
|
+
return new Matrix(data);
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/**
|
|
831
|
+
* Creates a Matrix from a plain 2D array (deep copy).
|
|
832
|
+
* @example
|
|
833
|
+
* ```ts
|
|
834
|
+
* const m = Matrix.from([[1, 2], [3, 4]]);
|
|
835
|
+
* m.get(0, 1); // 2
|
|
836
|
+
* ```
|
|
837
|
+
*/
|
|
838
|
+
static from(data: number[][]): Matrix {
|
|
839
|
+
return new Matrix(data.map(row => [...row]));
|
|
840
|
+
}
|
|
841
|
+
|
|
430
842
|
protected _addFn(a: number | undefined, b: number): number | undefined {
|
|
431
843
|
if (a === undefined) return b;
|
|
432
844
|
return a + b;
|
|
@@ -16,6 +16,63 @@ import { ERR } from '../../common';
|
|
|
16
16
|
* @template E Element type stored in the queue.
|
|
17
17
|
* @template R Extra record/metadata associated with each element.
|
|
18
18
|
* @example
|
|
19
|
+
* // Job scheduling by priority
|
|
20
|
+
* const jobs = new MaxPriorityQueue<number>();
|
|
21
|
+
*
|
|
22
|
+
* jobs.add(3); // low priority
|
|
23
|
+
* jobs.add(7); // high priority
|
|
24
|
+
* jobs.add(5); // medium priority
|
|
25
|
+
* jobs.add(10); // critical
|
|
26
|
+
*
|
|
27
|
+
* // Highest priority job first
|
|
28
|
+
* console.log(jobs.poll()); // 10;
|
|
29
|
+
* console.log(jobs.poll()); // 7;
|
|
30
|
+
* console.log(jobs.poll()); // 5;
|
|
31
|
+
* console.log(jobs.poll()); // 3;
|
|
32
|
+
* @example
|
|
33
|
+
* // Auction system with highest bid tracking
|
|
34
|
+
* interface Bid {
|
|
35
|
+
* bidder: string;
|
|
36
|
+
* amount: number;
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* const auction = new MaxPriorityQueue<Bid>([], {
|
|
40
|
+
* comparator: (a, b) => b.amount - a.amount
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* auction.add({ bidder: 'Alice', amount: 100 });
|
|
44
|
+
* auction.add({ bidder: 'Bob', amount: 250 });
|
|
45
|
+
* auction.add({ bidder: 'Charlie', amount: 175 });
|
|
46
|
+
*
|
|
47
|
+
* // Current highest bid
|
|
48
|
+
* console.log(auction.peek()?.bidder); // 'Bob';
|
|
49
|
+
* console.log(auction.peek()?.amount); // 250;
|
|
50
|
+
*
|
|
51
|
+
* // Process winning bid
|
|
52
|
+
* const winner = auction.poll()!;
|
|
53
|
+
* console.log(winner.bidder); // 'Bob';
|
|
54
|
+
* console.log(auction.peek()?.bidder); // 'Charlie';
|
|
55
|
+
* @example
|
|
56
|
+
* // CPU process scheduling
|
|
57
|
+
* const cpuQueue = new MaxPriorityQueue<[number, string]>([], {
|
|
58
|
+
* comparator: (a, b) => b[0] - a[0]
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* cpuQueue.add([5, 'System process']);
|
|
62
|
+
* cpuQueue.add([1, 'Background task']);
|
|
63
|
+
* cpuQueue.add([8, 'User interaction']);
|
|
64
|
+
* cpuQueue.add([3, 'Network sync']);
|
|
65
|
+
*
|
|
66
|
+
* const order = [];
|
|
67
|
+
* while (cpuQueue.size > 0) {
|
|
68
|
+
* order.push(cpuQueue.poll()![1]);
|
|
69
|
+
* }
|
|
70
|
+
* console.log(order); // [
|
|
71
|
+
* // 'User interaction',
|
|
72
|
+
* // 'System process',
|
|
73
|
+
* // 'Network sync',
|
|
74
|
+
* // 'Background task'
|
|
75
|
+
* // ];
|
|
19
76
|
*/
|
|
20
77
|
export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
21
78
|
/**
|
|
@@ -15,6 +15,66 @@ import { PriorityQueue } from './priority-queue';
|
|
|
15
15
|
* @template E Element type stored in the queue.
|
|
16
16
|
* @template R Extra record/metadata associated with each element.
|
|
17
17
|
* @example
|
|
18
|
+
* // Shortest job first scheduling
|
|
19
|
+
* const jobs = new MinPriorityQueue<number>();
|
|
20
|
+
*
|
|
21
|
+
* jobs.add(8); // 8 seconds
|
|
22
|
+
* jobs.add(2); // 2 seconds
|
|
23
|
+
* jobs.add(5); // 5 seconds
|
|
24
|
+
* jobs.add(1); // 1 second
|
|
25
|
+
*
|
|
26
|
+
* // Shortest job first
|
|
27
|
+
* console.log(jobs.poll()); // 1;
|
|
28
|
+
* console.log(jobs.poll()); // 2;
|
|
29
|
+
* console.log(jobs.poll()); // 5;
|
|
30
|
+
* console.log(jobs.poll()); // 8;
|
|
31
|
+
* @example
|
|
32
|
+
* // Event-driven simulation with timestamps
|
|
33
|
+
* interface Event {
|
|
34
|
+
* time: number;
|
|
35
|
+
* action: string;
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* const timeline = new MinPriorityQueue<Event>([], {
|
|
39
|
+
* comparator: (a, b) => a.time - b.time
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* timeline.add({ time: 300, action: 'Timeout' });
|
|
43
|
+
* timeline.add({ time: 100, action: 'Request received' });
|
|
44
|
+
* timeline.add({ time: 200, action: 'Processing done' });
|
|
45
|
+
* timeline.add({ time: 150, action: 'Cache hit' });
|
|
46
|
+
*
|
|
47
|
+
* const order = [];
|
|
48
|
+
* while (timeline.size > 0) {
|
|
49
|
+
* order.push(timeline.poll()!.action);
|
|
50
|
+
* }
|
|
51
|
+
* console.log(order); // [
|
|
52
|
+
* // 'Request received',
|
|
53
|
+
* // 'Cache hit',
|
|
54
|
+
* // 'Processing done',
|
|
55
|
+
* // 'Timeout'
|
|
56
|
+
* // ];
|
|
57
|
+
* @example
|
|
58
|
+
* // Huffman coding frequency selection
|
|
59
|
+
* // Character frequencies for Huffman tree building
|
|
60
|
+
* const freq = new MinPriorityQueue<[number, string]>([], {
|
|
61
|
+
* comparator: (a, b) => a[0] - b[0]
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* freq.add([5, 'a']);
|
|
65
|
+
* freq.add([9, 'b']);
|
|
66
|
+
* freq.add([12, 'c']);
|
|
67
|
+
* freq.add([2, 'd']);
|
|
68
|
+
*
|
|
69
|
+
* // Always pick two lowest frequencies
|
|
70
|
+
* const first = freq.poll()!;
|
|
71
|
+
* const second = freq.poll()!;
|
|
72
|
+
* console.log(first[1]); // 'd'; // freq 2
|
|
73
|
+
* console.log(second[1]); // 'a'; // freq 5
|
|
74
|
+
*
|
|
75
|
+
* // Combined node goes back
|
|
76
|
+
* freq.add([first[0] + second[0], first[1] + second[1]]);
|
|
77
|
+
* console.log(freq.peek()![0]); // 7;
|
|
18
78
|
*/
|
|
19
79
|
export class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
20
80
|
/**
|
|
@@ -11,6 +11,66 @@ import { Heap } from '../heap';
|
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* @example
|
|
14
|
+
* // Hospital emergency room triage
|
|
15
|
+
* interface Patient {
|
|
16
|
+
* name: string;
|
|
17
|
+
* severity: number; // 1 = critical, 5 = minor
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* const er = new PriorityQueue<Patient>([], {
|
|
21
|
+
* comparator: (a, b) => a.severity - b.severity
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* er.add({ name: 'Flu symptoms', severity: 4 });
|
|
25
|
+
* er.add({ name: 'Heart attack', severity: 1 });
|
|
26
|
+
* er.add({ name: 'Broken arm', severity: 3 });
|
|
27
|
+
* er.add({ name: 'Stroke', severity: 1 });
|
|
28
|
+
*
|
|
29
|
+
* // Most critical patients first
|
|
30
|
+
* console.log(er.poll()?.severity); // 1;
|
|
31
|
+
* console.log(er.poll()?.severity); // 1;
|
|
32
|
+
* console.log(er.poll()?.severity); // 3;
|
|
33
|
+
* console.log(er.poll()?.severity); // 4;
|
|
34
|
+
* @example
|
|
35
|
+
* // Task scheduler with deadlines
|
|
36
|
+
* interface Task {
|
|
37
|
+
* name: string;
|
|
38
|
+
* deadline: number; // hours until due
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* const scheduler = new PriorityQueue<Task>([], {
|
|
42
|
+
* comparator: (a, b) => a.deadline - b.deadline
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* scheduler.add({ name: 'Report', deadline: 24 });
|
|
46
|
+
* scheduler.add({ name: 'Email', deadline: 2 });
|
|
47
|
+
* scheduler.add({ name: 'Meeting prep', deadline: 4 });
|
|
48
|
+
* scheduler.add({ name: 'Code review', deadline: 8 });
|
|
49
|
+
*
|
|
50
|
+
* // Process most urgent first
|
|
51
|
+
* console.log(scheduler.peek()?.name); // 'Email';
|
|
52
|
+
* console.log(scheduler.size); // 4;
|
|
53
|
+
*
|
|
54
|
+
* const order = [];
|
|
55
|
+
* while (scheduler.size > 0) {
|
|
56
|
+
* order.push(scheduler.poll()!.name);
|
|
57
|
+
* }
|
|
58
|
+
* console.log(order); // ['Email', 'Meeting prep', 'Code review', 'Report'];
|
|
59
|
+
* @example
|
|
60
|
+
* // Bandwidth allocation with priorities
|
|
61
|
+
* const bandwidth = new PriorityQueue<[number, string]>([], {
|
|
62
|
+
* comparator: (a, b) => a[0] - b[0]
|
|
63
|
+
* });
|
|
64
|
+
*
|
|
65
|
+
* bandwidth.add([1, 'Video call']); // highest priority
|
|
66
|
+
* bandwidth.add([3, 'File download']);
|
|
67
|
+
* bandwidth.add([2, 'Web browsing']);
|
|
68
|
+
* bandwidth.add([1, 'Voice call']);
|
|
69
|
+
*
|
|
70
|
+
* // Allocate bandwidth to highest priority first
|
|
71
|
+
* console.log(bandwidth.poll()?.[1]); // 'Video call';
|
|
72
|
+
* console.log(bandwidth.poll()?.[1]); // 'Voice call';
|
|
73
|
+
* console.log(bandwidth.size); // 2;
|
|
14
74
|
*/
|
|
15
75
|
export class PriorityQueue<E = any, R = any> extends Heap<E, R> {
|
|
16
76
|
constructor(elements: Iterable<E> | Iterable<R> = [], options?: PriorityQueueOptions<E, R>) {
|