iterableEquals<T> method

bool iterableEquals<T>(
  1. Iterable<T> a,
  2. Iterable<T> b, {
  3. bool unordered = false,
  4. bool equals(
    1. T a,
    2. T b
    )?,
})
inherited

Implementation

bool iterableEquals<T>(
  Iterable<T> a,
  Iterable<T> b, {
  bool unordered = false,
  bool Function(T a, T b)? equals,
}) {
  equals ??= (x, y) => x == y;

  if (a.isEmpty && b.isEmpty) return true;

  if (a.length != b.length) return false;

  if (!unordered) {
    final itA = a.iterator;
    final itB = b.iterator;

    while (itA.moveNext() && itB.moveNext()) {
      if (!equals(itA.current, itB.current)) return false;
    }
    return true;
  }

  // unordered comparison
  final used = List<bool>.filled(b.length, false);
  final listB = b.toList();

  for (final itemA in a) {
    bool found = false;

    for (int i = 0; i < listB.length; i++) {
      if (!used[i] && equals(itemA, listB[i])) {
        used[i] = true;
        found = true;
        break;
      }
    }

    if (!found) return false;
  }

  return true;
}