trim<Item> function

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

Check if the lists start or end with the same items to trim the problem down as much as possible.

Implementation

_TrimResult<Item> trim<Item>(
  List<Item> oldList,
  List<Item> newList,
  bool Function(Item a, Item b) areEqual,
) {
  final oldLen = oldList.length;
  final newLen = newList.length;
  var start = 0;
  while (start < oldLen &&
      start < newLen &&
      areEqual(oldList[start], newList[start])) {
    start++;
  }
  var end = 0;
  while (end < oldLen &&
      end < newLen &&
      areEqual(oldList[oldLen - 1 - end], newList[newLen - 1 - end])) {
    end++;
  }

  // We can now reduce the problem to two possibly smaller sublists.
  return _TrimResult(
    shortenedOldList:
        oldLen == end ? <Item>[] : oldList.sublist(start, oldLen - end),
    shortenedNewList:
        newLen == end ? <Item>[] : newList.sublist(start, newLen - end),
    start: start,
  );
}