merge method

void merge(
  1. Buffer other
)

Merge other buffer into this one. Overlapping cells from other win.

Implementation

void merge(Buffer other) {
  final merged = area.union(other.area);
  final oldContent = List<Cell>.from(content);
  final newSize = merged.area;
  if (content.length < newSize) {
    content.addAll(List.generate(newSize - content.length, (_) => Cell()));
  } else if (content.length > newSize) {
    content.length = newSize;
  }
  for (var i = 0; i < content.length; i++) {
    content[i] = Cell();
  }

  for (var i = 0; i < oldContent.length; i++) {
    final pos = posOf(i);
    final newIndex = (pos.y - merged.y) * merged.width + (pos.x - merged.x);
    if (newIndex < content.length) {
      content[newIndex] = oldContent[i];
    }
  }

  for (var i = 0; i < other.content.length; i++) {
    final pos = other.posOf(i);
    final newIndex = (pos.y - merged.y) * merged.width + (pos.x - merged.x);
    if (newIndex < content.length) {
      content[newIndex] = other.content[i];
    }
  }
  area = merged;
}