Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | 3x 3x 3x 58x 75x 75x 53x 38x 109x 4x 105x 105x 15x 6x 9x 54x 1x 1x 19x 19x 71x 71x 71x 352x 352x 352x 352x 352x 352x 71x 19x 71x 19x 82x 125x 1x 124x 54x 9x 3x 6x 6x 20x 1x 36x 19x 2x 17x 4x 1x 5x 3x 1x 2x 6x 1x 5x 1x 4x 4x 8x 1x 7x 5x 1x 4x 10x 10x 2x 7x 6x 6x 6x 14x 14x 64x 64x 8x 6x 56x 1x 55x 2x 53x 7x 1x 6x 1x 5x 1x 4x 1x 3x 3x 32x 1x 31x 1x 30x 30x 89x 30x 8x 1x 7x 5x 1x 4x 1x 3x 1x 2x 4x 4x 2x 1x 1x 2x 6x 1x 5x 1x 4x 4x 6x 1x 5x 1x 4x 17x 17x 4x 5x 1x 4x 2x 2x 2x 1x 1x 6x 1x 5x 1x 4x 2x 2x 1x 1x 6x 1x 1x 1x | /* eslint-disable security/detect-object-injection */ import _ from 'lodash'; import { DanMatrixConstructorType, DanMatrixRowsIterator, DanMatrixColumnsIterator } from '.'; /** * DanMatrix is a class to handle two-dimension vectors, or matrices * @typeParam T is the type of element storable in the matrix */ export class DanMatrix<T> { /** * The data structure containing the real data. * * It's defined as an Array of Array of T * @typeParam T is the type of element storable in the matrix */ private _2dvector: Array<Array<T>>; /** * The public constructor of DanMatrix * * @param props the optional input of type DanMatrixConstructorType */ constructor(props?: DanMatrixConstructorType<T>) { this.setupMatrix(props); } /** * The public method to setup the matrix * * @param props the optional input of type DanMatrixConstructorType * @throws Error if a wrong input is passed */ public setupMatrix(props?: DanMatrixConstructorType<T>) { this._2dvector = []; if (props) { if (Array.isArray(props)) { let prevRowLength: number | undefined; for (const row of props) { if (!Array.isArray(row) || row.length < 1 || (prevRowLength !== undefined && prevRowLength !== row.length)) { throw new Error('Wrong input'); } this._2dvector.push(_.cloneDeep(row)); prevRowLength = row.length; } } else { if (!_.isInteger(props.rows) || !_.isInteger(props.columns) || props.rows <= 0 || props.columns <= 0) { throw new Error('Wrong input'); } for (let i = 0; i < props.rows; i++) { this._2dvector.push(Array.from({ length: props.columns }, () => props.val)); } } } } /** * Clone the current DanMatrix instance * @returns a new DanMatrix instance equal to the current one */ public clone(): DanMatrix<T> { const clonedMatrix: DanMatrix<T> = new DanMatrix<T>(this._2dvector); return clonedMatrix; } /** * Get a string representation of the current DanMatrix instance * @param fixedSpacing an optional numeric value for the cell spacing (default is 15) * @returns a string representation of the current DanMatrix instance */ public getMatrixString(fixedSpacing: number = 15): string { let outStr = ''; for (let rowIndex = 0; rowIndex < this._2dvector.length; rowIndex++) { const row = this._2dvector[rowIndex]; let rowStr = '┃'; for (const value of row) { let valStr = ` ${value}`; if (_.isString(value) || _.isNumber(value)) { const missingSpaces = fixedSpacing - valStr.length; if (missingSpaces > 0) { valStr += `${' '.repeat(missingSpaces)}`; } } rowStr += `${valStr} ┃`; } if (rowIndex === 0) { outStr += `┏${'-'.repeat(rowStr.length - 2)}┓\n`; } outStr += `${rowStr}\n┣${'-'.repeat(rowStr.length - 2)}┫\n`; } return outStr; } /** * Get the number of rows of the matrix * @returns the number of rows of the matrix */ public rowsNum(): number { return this._2dvector.length; } /** * Get the number of columns of the matrix * @returns the number of columns of the matrix */ public colsNum(): number { if (this._2dvector.length < 1) { return 0; } return this._2dvector[0].length; } /** * Get the matrix value at (x, y) * @param x - the x coordinate (index of the rows) * @param y - the y coordinate (index of the columns) * @returns the value at (x, y) or undefined if the coordinates are wrong */ public get(x: number, y: number): T | undefined { return this._2dvector[x]?.[y]; } /** * Set a value at (x, y) * @param x - the x coordinate (index of the rows) * @param y - the y coordinate (index of the columns) * @param val - the value to set * @returns the new value set at (x, y) or undefined if the coordinates are wrong */ public set(x: number, y: number, val: T): T | undefined { if (x >= this._2dvector.length || y >= this._2dvector[x].length) { return undefined; } this._2dvector[x][y] = val; return this._2dvector[x][y]; } /** * Get the matrix value at `coord` * @param coord a string representation of the coordinates using the dash '-' as separator. * Example: "1-4" represents x:1 and y:4 * @returns the value at `coord` or undefined if the string coordinates are wrong */ public getCoord(coord: string): T | undefined { if (!_.isString(coord)) { return undefined; } const coords = coord.split('-').map((elem: string) => Number(elem.trim())); if (coords.length < 2) { return undefined; } return this.get(coords[0], coords[1]); } /** * Set a value at `coord` * @param coord a string representation of the coordinates using the dash '-' as separator. * Example: "1-4" represents x:1 and y:4 * @param val - the value to set * @returns the value at `coord` or undefined if the string coordinates are wrong */ public setCoord(coord: string, val: T): T | undefined { if (!_.isString(coord)) { return undefined; } const coords = coord.split('-').map((elem: string) => Number(elem.trim())); if (coords.length < 2) { return undefined; } return this.set(coords[0], coords[1], val); } /** * Add a row to the matrix * @param row - the row to be added * @returns true if the new row was correctly added, otherwise it returns false * @throws Error if a wrong input is passed */ public addRow(row: Array<T>): boolean { if (!Array.isArray(row) || row.length < 1) { throw new Error('Wrong input'); } if (this._2dvector.length > 0 && row.length !== this.colsNum()) { return false; } this._2dvector.push(_.cloneDeep(row)); return true; } /** * Add a column to the matrix * @param column - the column to be added * @returns true if the new column was correctly added, otherwise it returns false * @throws Error if a wrong input is passed */ public addColumn(column: Array<T>): boolean { if (!Array.isArray(column) || column.length < 1) { throw new Error('Wrong input'); } if (this._2dvector.length > 0) { if (column.length !== this._2dvector.length) { return false; } for (let rowIndex = 0; rowIndex < this._2dvector.length; rowIndex++) { const row = this._2dvector[rowIndex]; row.push(column[rowIndex]); } } else { for (const columnElement of column) { this._2dvector.push([columnElement]); } } return true; } /** * Look for a specific value inside the matrix * @param val - the value you're looking for * @returns - an array of string coordinates where the value was found */ public lookForValue(val: T): Array<string> { const coordsArray: Array<string> = []; for (let rowIndex = 0; rowIndex < this._2dvector.length; rowIndex++) { const row = this._2dvector[rowIndex]; for (let colIndex = 0; colIndex < row.length; colIndex++) { const element = row[colIndex]; if (element === val) { coordsArray.push(`${rowIndex}-${colIndex}`); } } } return coordsArray; } /** * Get matrix row at `rowIndex` * @param rowIndex the row index * @returns the requested row as array of values * @throws Error if a wrong input is passed */ public getRowAt(rowIndex: number): Array<T> | undefined { if (!_.isInteger(rowIndex) || rowIndex < 0) { throw new Error('Wrong input'); } if (rowIndex >= this._2dvector.length) { return undefined; } return _.cloneDeep(this._2dvector[rowIndex]); } /** * Insert a row at the specific `rowIndex` index * @param rowIndex the row index * @param row the row to insert as an array of values * @returns true if the new row was correctly inserted, otherwise it returns false * @throws Error if a wrong input is passed */ public insertRowAt(rowIndex: number, row: Array<T>): boolean { if (!_.isInteger(rowIndex) || rowIndex < 0 || !Array.isArray(row) || row.length < 1) { throw new Error('Wrong input'); } if (rowIndex > this._2dvector.length) { return false; } if (rowIndex === this._2dvector.length) { return this.addRow(row); } if (row.length !== this.colsNum()) { return false; } this._2dvector.splice(rowIndex, 0, _.cloneDeep(row)); return true; } /** * Get matrix column at `columnIndex` * @param columnIndex the column index * @returns the requested column as array of values * @throws Error if a wrong input is passed */ public getColumnAt(columnIndex: number): Array<T> | undefined { if (!_.isInteger(columnIndex) || columnIndex < 0) { throw new Error('Wrong input'); } if (columnIndex >= this.colsNum()) { return undefined; } const columnToReturn: Array<T> = []; for (let rowIndex = 0; rowIndex < this._2dvector.length; rowIndex++) { columnToReturn.push(this._2dvector[rowIndex][columnIndex]); } return columnToReturn; } /** * Insert a column at the specific `columnIndex` index * @param columnIndex the column index * @param column the column to insert as an array of values * @returns true if the new column was correctly inserted, otherwise it returns false * @throws Error if a wrong input is passed */ public insertColumnAt(columnIndex: number, column: Array<T>): boolean { if (!_.isInteger(columnIndex) || columnIndex < 0 || !Array.isArray(column) || column.length < 1) { throw new Error('Wrong input'); } if (this._2dvector.length > 0) { if (column.length !== this._2dvector.length) { return false; } if (columnIndex > this.colsNum()) { return false; } else if (columnIndex === this.colsNum()) { return this.addColumn(column); } else { for (let rowIndex = 0; rowIndex < this._2dvector.length; rowIndex++) { const row = this._2dvector[rowIndex]; row.splice(columnIndex, 0, column[rowIndex]); } } } else { if (columnIndex === 0) { return this.addColumn(column); } else { return false; } } return true; } /** * Remove matrix row at index 'rowIndex' * @param {number} rowIndex the index of the row to be removed. If it's not an * integer number or if it's less than zero, an exception "Error('Wrong input')" is thrown * @returns true if the row removal was successful, otherwise it returns false * @throws exception "Error('Wrong input')" when any parameter in input is wrong */ public removeRowAt(rowIndex: number): boolean { if (!_.isInteger(rowIndex) || rowIndex < 0) { throw new Error('Wrong input'); } if (rowIndex >= this._2dvector.length) { return false; } this._2dvector.splice(rowIndex, 1); return true; } /** * Remove matrix column at index 'columnIndex' * @param {number} columnIndex the index of the column to be removed. If it's not an * integer number or if it's less than zero, an exception "Error('Wrong input')" is thrown * @returns true if the column removal was successful, otherwise it returns false * @throws exception "Error('Wrong input')" when any parameter in input is wrong */ public removeColumnAt(columnIndex: number): boolean { if (!_.isInteger(columnIndex) || columnIndex < 0) { throw new Error('Wrong input'); } if (this._2dvector.length < 1 || columnIndex >= this.colsNum()) { return false; } for (let rowIndex = 0; rowIndex < this._2dvector.length; rowIndex++) { const row = this._2dvector[rowIndex]; row.splice(columnIndex, 1); } return true; } /** * Replace matrix row at index 'rowIndex' * @param {number} rowIndex the index of the row to be replaced. If it's not an * integer number or if it's less than zero, an exception "Error('Wrong input')" is thrown * @param {Array<T>} row the array of values which must replace the existing row. * If it's not an array or if the array is empty, an exception "Error('Wrong input')" is thrown * @returns true if the row replacement was successful, otherwise it returns false * @throws exception "Error('Wrong input')" when any parameter in input is wrong */ public replaceRowAt(rowIndex: number, row: Array<T>): boolean { if (!_.isInteger(rowIndex) || rowIndex < 0 || !Array.isArray(row) || row.length < 1) { throw new Error('Wrong input'); } if (rowIndex >= this._2dvector.length) { return false; } const currentRow = this._2dvector[rowIndex]; if (row.length !== currentRow.length) { return false; } for (let index = 0; index < row.length; index++) { currentRow[index] = row[index]; } return true; } /** * Replace matrix column at index 'columnIndex' * @param {number} columnIndex the index of the column to be replaced. If it's not an * integer number or if it's less than zero, an exception "Error('Wrong input')" is thrown * @param {Array<T>} column the array of values which must replace the existing column. * If it's not an array or if the array is empty, an exception "Error('Wrong input')" is thrown * @returns true if the column replacement was successful, otherwise it returns false * @throws exception "Error('Wrong input')" when any parameter in input is wrong */ public replaceColumnAt(columnIndex: number, column: Array<T>): boolean { if (!_.isInteger(columnIndex) || columnIndex < 0 || !Array.isArray(column) || column.length < 1) { throw new Error('Wrong input'); } if (columnIndex >= this.colsNum()) { return false; } if (column.length !== this._2dvector.length) { return false; } for (let rowIndex = 0; rowIndex < this._2dvector.length; rowIndex++) { this._2dvector[rowIndex][columnIndex] = column[rowIndex]; } return true; } /** * Get matrix rows iterator * @returns {DanMatrixRowsIterator<T>} the matrix rows iterator */ public getRowsIterator(): DanMatrixRowsIterator<T> { return new DanMatrixRowsIterator<T>(this); } /** * Get matrix columns iterator * @returns {DanMatrixColumnsIterator<T>} the matrix columns iterator */ public getColumnsIterator(): DanMatrixColumnsIterator<T> { return new DanMatrixColumnsIterator<T>(this); } } |