es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
43 lines (42 loc) • 1.21 kB
JavaScript
/**
* Represents a matrix data structure.
* @template T The type of elements in the matrix.
*/
export class Matrix {
data;
constructor(rows, cols) {
this.data = Array.from({ length: rows }, () => Array(cols).fill(null));
}
/**
* Sets a value at a specific position in the matrix.
* @param {number} row - The row index.
* @param {number} col - The column index.
* @param {T} value - The value to set.
*/
setValue(row, col, value) {
this.data[row][col] = value;
}
/**
* Gets a value from a specific position in the matrix.
* @param {number} row - The row index.
* @param {number} col - The column index.
* @returns {T | null} The value at the specified position, or null if out of bounds.
*/
getValue(row, col) {
return this.data[row]?.[col] ?? null;
}
/**
* Returns the number of rows in the matrix.
* @returns {number} The number of rows.
*/
getRows() {
return this.data.length;
}
/**
* Returns the number of columns in the matrix.
* @returns {number} The number of columns.
*/
getCols() {
return this.data[0]?.length ?? 0;
}
}