updateWithMin method

void updateWithMin({
  1. required Row<E> previousRow,
  2. required int indexInNew,
  3. E? newItem,
  4. int? indexInOld,
  5. E? oldItem,
})

Choose the min

Implementation

void updateWithMin(
    {required Row<E> previousRow,
    required int indexInNew,
    E? newItem,
    int? indexInOld,
    E? oldItem}) {
  final slotIndex = indexInNew + 1;
  final topSlot = previousRow.slots[slotIndex];
  final leftSlot = slots[slotIndex - 1];
  final topLeftSlot = previousRow.slots[slotIndex - 1];

  final minCount =
      min(min(topSlot.length, leftSlot.length), topLeftSlot.length);

  // Order of cases does not matter

  if (minCount == topSlot.length) {
    slots[slotIndex] = combine(
      slot: topSlot,
      change: DeleteDiff<E>(args, indexInOld, 1),
    );
  } else if (minCount == leftSlot.length) {
    slots[slotIndex] = combine(
      slot: leftSlot,
      change:
          InsertDiff<E>(args, indexInNew, 1, [if (newItem != null) newItem]),
    );
  } else if (minCount == topLeftSlot.length) {
    slots[slotIndex] = combine(
      slot: topLeftSlot,
      change:
          ReplaceDiff(args, indexInNew, 1, [if (newItem != null) newItem]),
    );
  } else {
    throw "Bad algorithm.  Please try again";
  }
}