calculateDiffSync<Item> function

List<Operation<Item>> calculateDiffSync<Item>(
  1. List<Item> oldList,
  2. List<Item> newList,
  3. bool areEqual(
    1. Item a,
    2. Item b
    )
)

A synchronous variant of _calculateDiff.

Implementation

List<Operation<Item>> calculateDiffSync<Item>(
  List<Item> oldList,
  List<Item> newList,
  bool Function(Item a, Item b) areEqual,
) {
  assert(Item is! ReferenceToItemOnOtherIsolate);

  var row = _getInitialRow(oldList);

  for (var y = 0; y < newList.length; y++) {
    final nextRow = <_Sequence<Item>>[];

    for (var x = 0; x <= oldList.length; x++) {
      if (x == 0) {
        nextRow.add(_Sequence.insert(row[0], newList[y]));
      } else if (areEqual(newList[y], oldList[x - 1])) {
        nextRow.add(_Sequence.unchanged(row[x - 1]));
      } else if (row[x].isShorterThan(nextRow[x - 1])) {
        nextRow.add(_Sequence.insert(row[x], newList[y]));
      } else {
        nextRow.add(_Sequence.delete(nextRow[x - 1], oldList[x - 1]));
      }
    }

    row = nextRow;
  }

  return row.last.toOperations();
}