setGrid method

void setGrid(
  1. List<List<bool>> grid
)

Sets the grid of the Matrix object.

This method takes a 2D list of boolean values representing the grid of the Matrix. It ensures that all rows have the same length, and creates a deep copy of the grid to store in the Matrix's internal _data field.

If the input grid is empty or has no rows, the Matrix's rows and cols fields are set to 0, and the _data field is set to an empty list.

Parameters: grid (List<List

Implementation

void setGrid(final List<List<bool>> grid) {
  if (grid.isEmpty || grid[0].isEmpty) {
    rows = 0;
    cols = 0;
    _data = [];
    return;
  }
  // Ensure all rows have the same length
  assert(
    _data.every((row) => row.length == cols),
    'All rows in the grid must have the same length',
  );

  rows = grid.length;
  cols = grid[0].length;

  // Create a deep copy of the grid
  // _data = grid;
  _data = List.generate(
    rows,
    (i) => List<bool>.from(grid[i]),
  );
}