intersection method

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

See Set.intersection

Implementation

Set<V> intersection(Set<Object> other) {
  TreeSet<V> set = new 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 (var target in this) {
    if (other.contains(target)) {
      set.add(target);
    }
  }
  return set;
}