/// <summary> /// Union /// </summary> public static MultiHashSet <T> Union <T>(this MultiHashSet <T> left, MultiHashSet <T> right) { if (null == left) { throw new ArgumentNullException(nameof(left)); } else if (null == right) { throw new ArgumentNullException(nameof(right)); } MultiHashSet <T> result = new MultiHashSet <T>(left.Comparer); foreach (var leftPair in left.Items) { long count = leftPair.Value + right[leftPair.Key]; result.Add(leftPair.Key, Math.Min(leftPair.Value, count)); } foreach (var rightPair in right.Items) { if (!left.Contains(rightPair.Key)) { result.Add(rightPair.Key, rightPair.Value); } } return(result); }
/// <summary> /// Intersect /// </summary> public static MultiHashSet <T> Intersect <T>(this MultiHashSet <T> left, MultiHashSet <T> right) { if (null == left) { throw new ArgumentNullException(nameof(left)); } else if (null == right) { throw new ArgumentNullException(nameof(right)); } MultiHashSet <T> result = new MultiHashSet <T>(left.Comparer); foreach (var leftPair in left.Items) { long rightCount = right[leftPair.Key]; if (rightCount > 0) { result.Add(leftPair.Key, Math.Min(leftPair.Value, rightCount)); } } return(result); }