Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public bool SetEquals(IEnumerable<T> other)
return true;
}

return SetEquals(other, this.Origin);
return SetEquals(other, this.Origin);
}

/// <summary>
Expand Down Expand Up @@ -743,19 +743,84 @@ private static bool SetEquals(IEnumerable<T> other, MutationInput origin)
{
Requires.NotNull(other, nameof(other));

if (other is ICollection<T> otherAsICollectionGeneric)
{
//We check for < instead of != because other is not guaranteed to be a set, it could be a collection with duplicates.
if (otherAsICollectionGeneric.Count < origin.Count)
{
return false;
}

if (other is ImmutableHashSet<T> otherAsImmutableHashSet)
{
if (otherAsImmutableHashSet.KeyComparer == origin.EqualityComparer)
{
return SetEqualsWithImmutableHashset(otherAsImmutableHashSet, origin);
}
}

else if (other is HashSet<T> otherAsHashset)
{
if (otherAsHashset.Comparer == origin.EqualityComparer)
{
return SetEqualsWithHashset(otherAsHashset, origin);
}
}
}

else if (other is ICollection otherAsICollection && otherAsICollection.Count < origin.Count)
{
return false;
}

var otherSet = new HashSet<T>(other, origin.EqualityComparer);
if (origin.Count != otherSet.Count)
return SetEqualsWithHashset(otherSet, origin);
}

/// <summary>
/// Performs the set operation on a given data structure.
/// </summary>
private static bool SetEqualsWithImmutableHashset(ImmutableHashSet<T> other, MutationInput origin)
{
Requires.NotNull(other, nameof(other));

if (other.Count != origin.Count)
{
return false;
}

foreach (T item in otherSet)
foreach (T item in other)
{
if (!Contains(item, origin))
{
return false;
}
}

return true;
}

/// <summary>
/// Performs the set operation on a given data structure.
/// </summary>
private static bool SetEqualsWithHashset(HashSet<T> other, MutationInput origin)
{
Requires.NotNull(other, nameof(other));

if (origin.Count != other.Count)
{
return false;
}

using var e = new ImmutableHashSet<T>.Enumerator(origin.Root);
while (e.MoveNext())
{
if (!other.Contains(e.Current))
{
return false;
}
}

return true;
}

Expand Down
Loading