diffAndIntersect<G> method

DiffAndIntersectResult<T, G> diffAndIntersect<G>(
  1. Set<G> other, {
  2. bool diffThisMinusOther = true,
  3. bool diffOtherMinusThis = true,
  4. bool intersectThisWithOther = true,
  5. bool intersectOtherWithThis = true,
})

Given this set and other, returns:

  1. Items of this set which are NOT in other (difference this - other), in this set"s order.
  2. Items of other which are NOT in this set (difference other - this), in other"s order.
  3. Items of this set which are also in other, in this set"s order.
  4. Items of this set which are also in other, in other"s order.

Implementation

DiffAndIntersectResult<T, G> diffAndIntersect<G>(
  Set<G> other, {
  bool diffThisMinusOther = true,
  bool diffOtherMinusThis = true,
  bool intersectThisWithOther = true,
  bool intersectOtherWithThis = true,
}) {
  List<T>? _diffThisMinusOther = diffThisMinusOther ? [] : null;
  List<G>? _diffOtherMinusThis = diffOtherMinusThis ? [] : null;
  List<T>? _intersectThisWithOther = intersectThisWithOther ? [] : null;
  List<T>? _intersectOtherWithThis = intersectOtherWithThis ? [] : null;

  if (diffThisMinusOther || intersectThisWithOther)
    for (var element in this) {
      if (other.contains(element)) {
        _intersectThisWithOther?.add(element);
      } else
        _diffThisMinusOther?.add(element);
    }

  if (diffOtherMinusThis || intersectOtherWithThis)
    for (var element in other) {
      if (contains(element))
        _intersectOtherWithThis?.add(element as T);
      else
        _diffOtherMinusThis?.add(element);
    }

  return DiffAndIntersectResult(
    diffThisMinusOther: _diffThisMinusOther,
    diffOtherMinusThis: _diffOtherMinusThis,
    intersectThisWithOther: _intersectThisWithOther,
    intersectOtherWithThis: _intersectOtherWithThis,
  );
}