old_contents: import { max } from '../../utility/kit';
  
  export class Matrix {
      constructor(isDataRow) {
          this.isDataRow = isDataRow;
      }
  
      build(table) {
          const isDataRow = this.isDataRow;
          const rows = table.rows;
          const matrix = [];
  
          const cursor = new Array(rows.length);
          cursor.fill(0);
  
          let currentRow = 0;
          for (let i = 0, rowsLength = rows.length; i < rowsLength; i++) {
              const tr = rows[i];
              if (!isDataRow(tr)) {
                  continue;
              }
  
              const cells = tr.cells;
              for (let j = 0, cellsLength = cells.length; j < cellsLength; j++) {
                  const td = cells[j];
  
                  const colSpan = td.colSpan;
                  const rowSpan = td.rowSpan;
  
                  for (let k = 0; k < rowSpan; k++) {
                      const rowIndex = currentRow + k;
                      const colIndex = cursor[rowIndex];
                      let cursorRow = matrix[rowIndex];
                      if (!cursorRow) {
                          cursorRow = [];
                          matrix[rowIndex] = cursorRow;
                      }
  
                      for (let m = 0; m < colSpan; m++) {
                          cursorRow[colIndex + m] = td;
                      }
  
                      cursor[rowIndex] = colIndex + colSpan;
                  }
              }
  
              currentRow++;
          }
  
          return matrix;
      }
  }
new_contents: import { max } from '../../utility/kit';
  import { AppError } from '../../infrastructure/error';
  
  export class Matrix {
      constructor(isDataRow) {
          this.isDataRow = isDataRow;
      }
  
      build(table) {
          const isDataRow = this.isDataRow;
          const rows = table.rows;
          const matrix = [];
  
          const offset = new Array(rows.length);
          offset.fill(0);
  
          let cursor = 0;
          for (let i = 0, height = rows.length; i < height; i++) {
              const tr = rows[i];
              if (!isDataRow(tr)) {
                  continue;
              }
  
              const cells = tr.cells;
              for (let j = 0, width = cells.length; j < width; j++) {
                  const td = cells[j];
                  const { colSpan, rowSpan } = td;
  
                  for (let y = 0; y < rowSpan; y++) {
                      const yi = cursor + y;
                      const xi = offset[yi];
                      let row = matrix[yi];
                      if (!row) {
                          row = [];
                          matrix[yi] = row;
                      }
  
                      for (let x = 0; x < colSpan; x++) {
                          row[xi + x] = td;
                      }
  
                      offset[yi] = xi + colSpan;
                  }
              }
  
              cursor++;
          }
  
          this.assertFlatness(matrix);
          return matrix;
      }
  
      assertFlatness(matrix) {
          if (matrix.length) {
              const height = matrix.length;
              const width = matrix[0].length;
              for (let i = 1; i < height; i++) {
                  if (matrix[i].length !== width) {
                      throw new AppError('matrix', `Matrix is not flat, expect width ${width}, actual ${matrix[i].length}`);
                  }
              }
          }
      }
  }
subject: add flat assert