private static void Copy <T>( ISet <T> source, ISet <T> target, MemberSettings settings, ReferencePairCollection referencePairs) { if (Is.IsFixedSize(source, target) && source.Count != target.Count) { throw State.Copy.Throw.CannotCopyFixesSizeCollections(source, target, settings); } if (settings.IsImmutable(typeof(T))) { using (var borrow = HashSetPool <T> .Borrow(EqualityComparer <T> .Default)) { borrow.Value.UnionWith(source); target.IntersectWith(borrow.Value); target.UnionWith(borrow.Value); return; } } switch (settings.ReferenceHandling) { case ReferenceHandling.Throw: break; case ReferenceHandling.References: using (var borrow = HashSetPool <T> .Borrow((x, y) => ReferenceEquals(x, y), x => RuntimeHelpers.GetHashCode(x))) { borrow.Value.UnionWith(source); target.IntersectWith(borrow.Value); target.UnionWith(borrow.Value); } break; case ReferenceHandling.Structural: IEqualityComparer <T> comparer; if (!Set.TryGetComparer(source, out comparer)) { comparer = EqualityComparer <T> .Default; } var copyIngComparer = new CopyingComparer <T>(comparer, settings, referencePairs); using (var borrow = HashSetPool <T> .Borrow(copyIngComparer)) { borrow.Value.UnionWith(source); target.IntersectWith(borrow.Value); copyIngComparer.StartCopying(); target.UnionWith(borrow.Value); } return; default: throw new ArgumentOutOfRangeException(); } }
private bool ItemsEquals(ISet <T> x, ISet <T> y, Func <T, T, bool> compare, Func <T, int> getHashCode) { using (var borrow = HashSetPool <T> .Borrow(compare, getHashCode)) { borrow.Value.UnionWith(x); var result = borrow.Value.SetEquals(y); return(result); } }
private void AddDiffs( DiffBuilder collectionBuilder, IDictionary <TKey, TValue> x, IDictionary <TKey, TValue> y, MemberSettings settings) { using (var borrow = HashSetPool <TKey> .Borrow(EqualityComparer <TKey> .Default.Equals, EqualityComparer <TKey> .Default.GetHashCode)) { borrow.Value.UnionWith(x.Keys); borrow.Value.UnionWith(y.Keys); foreach (var key in borrow.Value) { var xv = x.ElementAtOrMissing(key); var yv = y.ElementAtOrMissing(key); collectionBuilder.UpdateCollectionItemDiff(xv, yv, key, settings); } } }
// ReSharper disable once UnusedParameter.Local private void AddDiffs( DiffBuilder collectionBuilder, ISet <T> x, ISet <T> y, MemberSettings settings) { if (typeof(T).Implements <IEquatable <T> >()) { using (var borrow = HashSetPool <T> .Borrow(EqualityComparer <T> .Default.Equals, EqualityComparer <T> .Default.GetHashCode)) { AddItemDiffs(collectionBuilder, x, y, borrow.Value); return; } } switch (settings.ReferenceHandling) { case ReferenceHandling.Throw: throw Throw.ShouldNeverGetHereException("ReferenceHandling should be checked before"); case ReferenceHandling.References: using (var borrow = HashSetPool <T> .Borrow((xi, yi) => ReferenceEquals(xi, yi), item => RuntimeHelpers.GetHashCode(item))) { AddItemDiffs(collectionBuilder, x, y, borrow.Value); return; } case ReferenceHandling.Structural: using (var borrow = HashSetPool <T> .Borrow((xi, yi) => EqualBy.MemberValues(xi, yi, settings), xi => 0)) { AddItemDiffs(collectionBuilder, x, y, borrow.Value); return; } default: throw new ArgumentOutOfRangeException(); } }