difference method

Set<V> difference (Set<Object> other)
override

See Set.difference

Implementation

Set<V> difference(Set<Object> other) {
  TreeSet<V> set = new TreeSet(comparator: comparator);

  if (other is TreeSet) {
    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) {
        hasMore1 = i1.moveNext();
        hasMore2 = i2.moveNext();
      } else if (c < 0) {
        set.add(i1.current);
        hasMore1 = i1.moveNext();
      } else {
        hasMore2 = i2.moveNext();
      }
    }
    if (hasMore1) {
      do {
        set.add(i1.current);
      } while (i1.moveNext());
    }
    return set;
  }

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