SplayTreeGrid<E>.from constructor

SplayTreeGrid<E>.from(
  1. Grid<E> other, {
  2. E? empty,
})

Creates a new splay tree grid from an existing grid.

The new grid is a shallow copy of the existing grid, with the same size, position, and elements. The empty element is used as the default value, which defaults to other.empty if omitted.

Example

final grid = SplayTreeGrid.filled(2, 2, empty: 0);
grid.set(Pos(0, 0), 1);

final clone = SplayTreeGrid.from(grid);
print(clone.get(Pos(0, 0))); // 1

Implementation

factory SplayTreeGrid.from(Grid<E> other, {E? empty}) {
  final clone = _SplayTreeGrid<E>(
    other.width,
    other.height,
    empty ?? other.empty,
  );
  if (other is _SplayTreeGrid<E>) {
    clone._nodes.addAll(other._nodes);
  } else {
    for (var y = 0; y < other.height; y++) {
      for (var x = 0; x < other.width; x++) {
        final pos = Pos(x, y);
        clone.setUnsafe(pos, other.getUnsafe(pos));
      }
    }
  }
  return clone;
}