union method

Set<V> union (Set<V> other)
override

See Set.union

Implementation

Set<V> union(Set<V> 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) {
        set.add(i1.current);
        hasMore1 = i1.moveNext();
        hasMore2 = i2.moveNext();
      } else if (c < 0) {
        set.add(i1.current);
        hasMore1 = i1.moveNext();
      } else {
        set.add(i2.current);
        hasMore2 = i2.moveNext();
      }
    }
    if (hasMore1 || hasMore2) {
      i1 = hasMore1 ? i1 : i2;
      do {
        set.add(i1.current);
      } while (i1.moveNext());
    }
    return set;
  }

  // Non-optimized version.
  return set..addAll(this)..addAll(other);
}