/// <inheritdoc cref="ImmutableHashSet{T}.Builder.ExceptWith(IEnumerable{T})"/> public void ExceptWith(IEnumerable <T> other) { if (other is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.other); } if (_mutableSet is not null) { _mutableSet.ExceptWith(other); return; } if (other == this) { Clear(); return; } else if (other is ImmutableSegmentedHashSet <T> otherSet) { if (otherSet == _set) { Clear(); return; } else if (otherSet.IsEmpty) { // No action required return; } else { GetOrCreateMutableSet().ExceptWith(otherSet._set); return; } } else { // Manually enumerate to avoid changes to the builder if 'other' is empty or does not contain any // items present in the current set. SegmentedHashSet <T>?mutableSet = null; foreach (var item in other) { if (mutableSet is null) { if (!ReadOnlySet.Contains(item)) { continue; } mutableSet = GetOrCreateMutableSet(); } mutableSet.Remove(item); } return; } }
public void HashSet_Generic_Constructor_HashSet_SparselyFilled(int count) { SegmentedHashSet <T> source = (SegmentedHashSet <T>)CreateEnumerable(EnumerableType.SegmentedHashSet, null, count, 0, 0); List <T> sourceElements = source.ToList(); foreach (int i in NonSquares(count)) { source.Remove(sourceElements[i]);// Unevenly spaced survivors increases chance of catching any spacing-related bugs. } SegmentedHashSet <T> set = new SegmentedHashSet <T>(source, GetIEqualityComparer()); Assert.True(set.SetEquals(source)); }
public void HashSet_Generic_RemoveWhere_NewObject() // Regression Dev10_624201 { object[] array = new object[2]; object obj = new(); SegmentedHashSet <object> set = new SegmentedHashSet <object>(); set.Add(obj); set.Remove(obj); foreach (object o in set) { } set.CopyTo(array, 0, 2); set.RemoveWhere((element) => { return(false); }); }
public void EnsureCapacity_Generic_GrowCapacityWithFreeList(int setLength) { SegmentedHashSet <T> set = (SegmentedHashSet <T>)GenericISetFactory(setLength); // Remove the first element to ensure we have a free list. Assert.True(set.Remove(set.ElementAt(0))); int currentCapacity = set.EnsureCapacity(0); Assert.True(currentCapacity > 0); int newCapacity = set.EnsureCapacity(currentCapacity + 1); Assert.True(newCapacity > currentCapacity); }
public void HashSet_Generic_TrimExcess_AfterRemovingOneElement(int setLength) { if (setLength > 0) { SegmentedHashSet <T> set = (SegmentedHashSet <T>)GenericISetFactory(setLength); List <T> expected = set.ToList(); T elementToRemove = set.ElementAt(0); set.TrimExcess(); Assert.True(set.Remove(elementToRemove)); expected.Remove(elementToRemove); set.TrimExcess(); Assert.True(set.SetEquals(expected)); } }
public void Remove_NonDefaultComparer_ComparerUsed(int capacity) { var c = new TrackingEqualityComparer <T>(); var set = new SegmentedHashSet <T>(capacity, c); AddToCollection(set, capacity); T first = set.First(); c.EqualsCalls = 0; c.GetHashCodeCalls = 0; Assert.Equal(capacity, set.Count); set.Remove(first); Assert.Equal(capacity - 1, set.Count); Assert.InRange(c.EqualsCalls, 1, int.MaxValue); Assert.InRange(c.GetHashCodeCalls, 1, int.MaxValue); }