containsExactly method

bool containsExactly(
  1. Iterable<T> other, {
  2. bool equals(
    1. Object?,
    2. Object?
    ) = _equalityTest,
})

Returns whether other has the same elements in the same positions.

Equality of elements is implemented by == test by default for equals.

Implementation

bool containsExactly(
  Iterable<T> other, {
  bool Function(Object?, Object?) equals = _equalityTest,
}) {
  if (identical(this, other)) {
    return true;
  }
  final self = this;
  if (self is List<T> && other is List<T>) {
    // Optimization for <List>.containsExactly(<List>).
    // (Avoids allocating two iterators)
    if (length != other.length) {
      return false;
    }
    for (var i = 0; i < length; i++) {
      if (!equals(self[i], other[i])) {
        return false;
      }
    }
  } else {
    final a = iterator;
    final b = other.iterator;
    while (a.moveNext()) {
      if (!b.moveNext() || !equals(a.current, b.current)) {
        return false;
      }
    }
    if (b.moveNext()) {
      return false;
    }
  }
  return true;
}