intersection method

  1. @override
Set<V> intersection(
  1. Set<Object?> other
)
override

Implementation

@override
Set<V> intersection(Set<Object?> other) {
  TreeSet<V> set = TreeSet(comparator: comparator);

  // Optimized for sorted sets
  if (other is TreeSet<V>) {
    var i1 = iterator;
    var i2 = other.iterator;
    var hasMore1 = i1.moveNext();
    var hasMore2 = i2.moveNext();
    while (hasMore1 && hasMore2) {
      var c = comparator(i1.current, i2.current);
      if (c == 0) {
        set.add(i1.current);
        hasMore1 = i1.moveNext();
        hasMore2 = i2.moveNext();
      } else if (c < 0) {
        hasMore1 = i1.moveNext();
      } else {
        hasMore2 = i2.moveNext();
      }
    }
    return set;
  }

  // Non-optimized version.
  for (final target in this) {
    if (other.contains(target)) {
      set.add(target);
    }
  }
  return set;
}