public bool IsSubset(ArraySet <T> anotherSet) { if (anotherSet == null) { throw new ArgumentNullException(nameof(anotherSet)); } if (this.Empty) { return(true); } if (this.Count > anotherSet.Count) { return(false); } var startIndex = 0; foreach (var item in this) { var index = Array.BinarySearch(anotherSet.items, startIndex, anotherSet.Count - startIndex, item, comparer); if (index >= 0) { startIndex = index; } else { return(false); } } return(true); }
public ArraySet <T> Difference(ArraySet <T> anotherSet) { if (anotherSet == null) { throw new ArgumentNullException(nameof(anotherSet)); } if (anotherSet.Empty) { return(new ArraySet <T>(this)); } var differenceList = new List <T>(); var startIndex = 0; foreach (var item in this) { var index = Array.BinarySearch(anotherSet.items, startIndex, anotherSet.Count - startIndex, item, comparer); if (index >= 0) { startIndex = index; } else { differenceList.Add(item); startIndex = -index - 1; } } var difference = new ArraySet <T>(differenceList.ToArray(), differenceList.Count, comparer); return(difference); }
public ArraySet(ArraySet <T> set) { count = set.count; items = new T[set.Capacity]; this.comparer = set.comparer; Array.Copy(set.items, items, set.Count); }
private T[] GenerateUnionArray(ArraySet <T> anotherSet, out int count) { var unionArray = new T[this.Count + anotherSet.Count]; var nextIndex = 0; var thisSetIndex = 0; var anotherSetIndex = 0; while (thisSetIndex < Count && anotherSetIndex < anotherSet.Count) { var compareResult = comparer.Compare(items[thisSetIndex], anotherSet.items[anotherSetIndex]); if (compareResult < 0) { unionArray[nextIndex++] = items[thisSetIndex++]; } else if (compareResult > 0) { unionArray[nextIndex++] = anotherSet.items[anotherSetIndex++]; } else { unionArray[nextIndex++] = items[thisSetIndex]; thisSetIndex++; anotherSetIndex++; } } if (thisSetIndex < Count) { var remainItems = Count - thisSetIndex; Array.Copy(items, thisSetIndex, unionArray, nextIndex, remainItems); count = nextIndex + remainItems; return(unionArray); } if (anotherSetIndex < anotherSet.Count) { var remainItems = anotherSet.Count - anotherSetIndex; Array.Copy(anotherSet.items, anotherSetIndex, unionArray, nextIndex, remainItems); count = nextIndex + remainItems; return(unionArray); } count = nextIndex; return(unionArray); }
public ArraySet <T> Union(ArraySet <T> anotherSet) { if (anotherSet == null) { throw new ArgumentNullException(nameof(anotherSet)); } if (this.Empty) { return(new ArraySet <T>(anotherSet)); } if (anotherSet.Empty) { return(new ArraySet <T>(this)); } int unionCount; var unionArray = GenerateUnionArray(anotherSet, out unionCount); var union = new ArraySet <T>(unionArray, unionCount, comparer); return(union); }