data-structure-typed 2.4.4 → 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.
Files changed (80) hide show
  1. package/CHANGELOG.md +22 -1
  2. package/README.md +34 -1
  3. package/dist/cjs/index.cjs +10639 -2151
  4. package/dist/cjs-legacy/index.cjs +10694 -2195
  5. package/dist/esm/index.mjs +10639 -2150
  6. package/dist/esm-legacy/index.mjs +10694 -2194
  7. package/dist/types/common/error.d.ts +23 -0
  8. package/dist/types/common/index.d.ts +1 -0
  9. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  11. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  15. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  16. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  18. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  19. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  20. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  22. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  23. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  24. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  25. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  26. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  27. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  28. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  29. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  30. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  31. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  32. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  33. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  34. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  35. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  36. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  37. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  38. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  39. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  40. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  41. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  42. package/dist/umd/data-structure-typed.js +10725 -2221
  43. package/dist/umd/data-structure-typed.min.js +4 -2
  44. package/package.json +5 -4
  45. package/src/common/error.ts +60 -0
  46. package/src/common/index.ts +2 -0
  47. package/src/data-structures/base/iterable-element-base.ts +2 -2
  48. package/src/data-structures/binary-tree/avl-tree.ts +146 -51
  49. package/src/data-structures/binary-tree/binary-indexed-tree.ts +317 -247
  50. package/src/data-structures/binary-tree/binary-tree.ts +567 -121
  51. package/src/data-structures/binary-tree/bst.ts +370 -37
  52. package/src/data-structures/binary-tree/red-black-tree.ts +328 -96
  53. package/src/data-structures/binary-tree/segment-tree.ts +378 -248
  54. package/src/data-structures/binary-tree/tree-map.ts +1411 -13
  55. package/src/data-structures/binary-tree/tree-multi-map.ts +1218 -215
  56. package/src/data-structures/binary-tree/tree-multi-set.ts +959 -69
  57. package/src/data-structures/binary-tree/tree-set.ts +1257 -15
  58. package/src/data-structures/graph/abstract-graph.ts +106 -1
  59. package/src/data-structures/graph/directed-graph.ts +233 -47
  60. package/src/data-structures/graph/map-graph.ts +59 -1
  61. package/src/data-structures/graph/undirected-graph.ts +308 -59
  62. package/src/data-structures/hash/hash-map.ts +254 -79
  63. package/src/data-structures/heap/heap.ts +305 -102
  64. package/src/data-structures/heap/max-heap.ts +48 -3
  65. package/src/data-structures/heap/min-heap.ts +59 -0
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
  67. package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
  68. package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
  69. package/src/data-structures/matrix/matrix.ts +433 -22
  70. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  71. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  72. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  73. package/src/data-structures/queue/deque.ts +358 -68
  74. package/src/data-structures/queue/queue.ts +223 -42
  75. package/src/data-structures/stack/stack.ts +184 -32
  76. package/src/data-structures/trie/trie.ts +227 -44
  77. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  78. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  79. package/src/types/data-structures/queue/deque.ts +7 -0
  80. package/src/utils/utils.ts +4 -2
@@ -6,10 +6,94 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { MatrixOptions } from '../../types';
9
+ import { ERR } from '../../common';
9
10
 
10
11
  /**
11
12
  *
12
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
+ */
13
97
  export class Matrix {
14
98
  /**
15
99
  * The constructor function initializes a matrix object with the provided data and options, or with
@@ -105,6 +189,34 @@ export class Matrix {
105
189
  * retrieve from the data array.
106
190
  * @returns The `get` function returns a number if the provided row and column indices are valid.
107
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;
108
220
  */
109
221
  get(row: number, col: number): number | undefined {
110
222
  if (this.isValidIndex(row, col)) {
@@ -123,6 +235,25 @@ export class Matrix {
123
235
  * @returns a boolean value. It returns true if the index (row, col) is valid and the value is
124
236
  * successfully set in the data array. It returns false if the index is invalid and the value is not
125
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;
126
257
  */
127
258
  set(row: number, col: number, value: number): boolean {
128
259
  if (this.isValidIndex(row, col)) {
@@ -147,10 +278,44 @@ export class Matrix {
147
278
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
148
279
  * @returns The `add` method returns a new `Matrix` object that represents the result of adding the
149
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
+ * // ];
150
315
  */
151
316
  add(matrix: Matrix): Matrix | undefined {
152
317
  if (!this.isMatchForCalculate(matrix)) {
153
- throw new Error('Matrix dimensions must match for addition.');
318
+ throw new Error(ERR.matrixDimensionMismatch('addition'));
154
319
  }
155
320
 
156
321
  const resultData: number[][] = [];
@@ -160,10 +325,7 @@ export class Matrix {
160
325
  const a = this.get(i, j),
161
326
  b = matrix.get(i, j);
162
327
  if (a !== undefined && b !== undefined) {
163
- const added = this._addFn(a, b);
164
- if (added) {
165
- resultData[i][j] = added;
166
- }
328
+ resultData[i][j] = this._addFn(a, b) ?? 0;
167
329
  }
168
330
  }
169
331
  }
@@ -183,10 +345,28 @@ export class Matrix {
183
345
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
184
346
  * represents the matrix that you want to subtract from the current matrix.
185
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]];
186
366
  */
187
367
  subtract(matrix: Matrix): Matrix | undefined {
188
368
  if (!this.isMatchForCalculate(matrix)) {
189
- throw new Error('Matrix dimensions must match for subtraction.');
369
+ throw new Error(ERR.matrixDimensionMismatch('subtraction'));
190
370
  }
191
371
 
192
372
  const resultData: number[][] = [];
@@ -196,10 +376,7 @@ export class Matrix {
196
376
  const a = this.get(i, j),
197
377
  b = matrix.get(i, j);
198
378
  if (a !== undefined && b !== undefined) {
199
- const subtracted = this._subtractFn(a, b);
200
- if (subtracted) {
201
- resultData[i][j] = subtracted;
202
- }
379
+ resultData[i][j] = this._subtractFn(a, b) ?? 0;
203
380
  }
204
381
  }
205
382
  }
@@ -218,10 +395,44 @@ export class Matrix {
218
395
  * as a new matrix.
219
396
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
220
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
+ * // ];
221
432
  */
222
433
  multiply(matrix: Matrix): Matrix | undefined {
223
434
  if (this.cols !== matrix.rows) {
224
- throw new Error('Matrix dimensions must be compatible for multiplication (A.cols = B.rows).');
435
+ throw new Error(ERR.matrixDimensionMismatch('multiplication (A.cols must equal B.rows)'));
225
436
  }
226
437
 
227
438
  const resultData: number[][] = [];
@@ -256,10 +467,41 @@ export class Matrix {
256
467
  * The transpose function takes a matrix and returns a new matrix that is the transpose of the
257
468
  * original matrix.
258
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;
259
501
  */
260
502
  transpose(): Matrix {
261
- if (this.data.some(row => row.length !== this.rows)) {
262
- throw new Error('Matrix must be rectangular for transposition.');
503
+ if (this.data.some(row => row.length !== this.cols)) {
504
+ throw new Error(ERR.matrixNotRectangular());
263
505
  }
264
506
 
265
507
  const resultData: number[][] = [];
@@ -284,11 +526,34 @@ export class Matrix {
284
526
  /**
285
527
  * The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
286
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;
287
552
  */
288
553
  inverse(): Matrix | undefined {
289
554
  // Check if the matrix is square
290
555
  if (this.rows !== this.cols) {
291
- throw new Error('Matrix must be square for inversion.');
556
+ throw new Error(ERR.matrixNotSquare());
292
557
  }
293
558
 
294
559
  // Create an augmented matrix [this | I]
@@ -318,7 +583,7 @@ export class Matrix {
318
583
 
319
584
  if (pivotRow === this.rows) {
320
585
  // Matrix is singular, and its inverse does not exist
321
- throw new Error('Matrix is singular, and its inverse does not exist.');
586
+ throw new Error(ERR.matrixSingular());
322
587
  }
323
588
 
324
589
  // Swap rows to make the pivot the current row
@@ -329,7 +594,7 @@ export class Matrix {
329
594
 
330
595
  if (pivotElement === 0) {
331
596
  // Handle division by zero
332
- throw new Error('Matrix is singular, and its inverse does not exist (division by zero).');
597
+ throw new Error(ERR.matrixSingular());
333
598
  }
334
599
 
335
600
  augmentedMatrix._scaleRow(i, 1 / pivotElement);
@@ -364,12 +629,28 @@ export class Matrix {
364
629
  * The dot function calculates the dot product of two matrices and returns a new matrix.
365
630
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
366
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]];
367
650
  */
368
651
  dot(matrix: Matrix): Matrix | undefined {
369
652
  if (this.cols !== matrix.rows) {
370
- throw new Error(
371
- 'Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product.'
372
- );
653
+ throw new Error(ERR.matrixDimensionMismatch('dot product (A.cols must equal B.rows)'));
373
654
  }
374
655
 
375
656
  const resultData: number[][] = [];
@@ -419,15 +700,145 @@ export class Matrix {
419
700
  * and properties as the current instance.
420
701
  */
421
702
  clone(): Matrix {
422
- return new Matrix(this.data, {
423
- rows: this.rows,
424
- cols: this.cols,
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,
425
790
  addFn: this.addFn,
426
791
  subtractFn: this.subtractFn,
427
792
  multiplyFn: this.multiplyFn
428
793
  });
429
794
  }
430
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
+
431
842
  protected _addFn(a: number | undefined, b: number): number | undefined {
432
843
  if (a === undefined) return b;
433
844
  return a + b;
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import type { PriorityQueueOptions } from '../../types';
9
9
  import { PriorityQueue } from './priority-queue';
10
+ import { ERR } from '../../common';
10
11
 
11
12
  /**
12
13
  * Max-oriented priority queue (max-heap) built on {@link PriorityQueue}.
@@ -15,6 +16,63 @@ import { PriorityQueue } from './priority-queue';
15
16
  * @template E Element type stored in the queue.
16
17
  * @template R Extra record/metadata associated with each element.
17
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
+ * // ];
18
76
  */
19
77
  export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
20
78
  /**
@@ -28,9 +86,7 @@ export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
28
86
  super(elements, {
29
87
  comparator: (a: E, b: E): number => {
30
88
  if (typeof a === 'object' || typeof b === 'object') {
31
- throw TypeError(
32
- `When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
33
- );
89
+ throw new TypeError(ERR.comparatorRequired('MaxPriorityQueue'));
34
90
  }
35
91
  if (a < b) return 1;
36
92
  if (a > b) return -1;