SplayTreeGrid<E>.generate constructor

SplayTreeGrid<E>.generate(
  1. int width,
  2. int height,
  3. E generator(
    1. Pos
    ), {
  4. required E empty,
})

Creates a new splay tree grid with the given width and height.

Each element in the grid is initialized by calling generator with the position of the element. The empty element is used as the default value.

The width and height must be positive.

Example

final grid = SplayTreeGrid.generate(2, 2, (pos) => pos.x + pos.y, empty: 0);

print(grid.get(Pos(0, 0))); // 0
print(grid.get(Pos(1, 0))); // 1

Implementation

factory SplayTreeGrid.generate(
  int width,
  int height,
  E Function(Pos) generator, {
  required E empty,
}) {
  checkPositive(width, 'width');
  checkPositive(height, 'height');
  final grid = SplayTreeGrid<E>.filled(width, height, empty: empty);
  for (var y = 0; y < height; y++) {
    for (var x = 0; x < width; x++) {
      grid.setUnsafe(Pos(x, y), generator(Pos(x, y)));
    }
  }
  return grid;
}