difference method

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

Implementation

@override
Set<V> difference(Set<Object?> other) {
  TreeSet<V> set = 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 (final target in this) {
    if (!other.contains(target)) {
      set.add(target);
    }
  }
  return set;
}