diffSync<Item> function

List<Operation<Item>> diffSync<Item>(
  1. List<Item> oldList,
  2. List<Item> newList, {
  3. EqualityChecker<Item>? areEqual,
})

Calculates a minimal list of Operations that convert the oldList into the newList.

Unlike diff, this function works synchronously (i.e., without using Futures).

See also:

  • diff, for a detailed explanation or if you have very long lists.

Implementation

List<Operation<Item>> diffSync<Item>(
  List<Item> oldList,
  List<Item> newList, {
  EqualityChecker<Item>? areEqual,
}) {
  // Use == operator and item hash code as default comparison functions
  final areEqualCheck = areEqual ?? (a, b) => a == b;

  final trimResult = trim(oldList, newList, areEqualCheck);

  // Those are sublists that reduce the problem to a smaller problem domain.
  List<Operation<Item>> operations = calculateDiffSync(
    trimResult.shortenedOldList,
    trimResult.shortenedNewList,
    areEqualCheck,
  );

  // Shift operations back.
  return operations.map((op) => op.shift(trimResult.start)).toList();
}