/// <summary> /// Do the intersection of two sets /// </summary> /// <param name="set1"></param> /// <param name="set2"></param> /// <returns></returns> public Set Intersect(Set set1, Set set2) { if (set1 == null && set2 == null) { return new Set(); } else if (set1 == null) { return (Set)set2.GetClone(); } else if (set2 == null) { return (Set)set1.GetClone(); } else { Set newSet = new Set(); foreach(int element in set1.list) { if (set2.Contains(element)) { newSet.Add(element); } } return newSet; } }
/// <summary> /// Substract set2 from set1 /// </summary> /// <param name="set1"></param> /// <param name="set2"></param> /// <returns></returns> public Set Substract(Set set1, Set set2) { if (set1 == null) { return new Set(); } if (set2 == null) { return (Set)set1.GetClone(); } Set newSet = (Set)set1.GetClone(); foreach (int element in set2.list) { if (newSet.list.Contains(element)) { newSet.list.Remove(element); } } return newSet; }
/// <summary> /// Union two sets /// </summary> /// <param name="set1"></param> /// <param name="set2"></param> /// <returns></returns> public Set Union(Set set1, Set set2) { if (set1 == null && set2 == null) { return new Set(); } else if (set1 == null) { return (Set)set2.GetClone(); } else if (set2 == null) { return (Set)set1.GetClone(); } else { Set newSet = new Set(); foreach (int element in set1.list) { newSet.Add(element); } foreach (int element in set2.list) { newSet.Add(element); } return newSet; } }