Skip to content
Merged
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 @@ -706,7 +706,15 @@ public bool IsSupersetOf(IEnumerable<T> other)
}
}

return ContainsAllElements(other);
foreach (T element in other)
{
if (!Contains(element))
{
return false;
}
}

return true;
}

/// <summary>Determines whether a <see cref="HashSet{T}"/> object is a proper superset of the specified collection.</summary>
Expand Down Expand Up @@ -743,7 +751,7 @@ public bool IsProperSupersetOf(IEnumerable<T> other)
}

// Now perform element check.
return ContainsAllElements(otherAsSet);
return otherAsSet.IsSubsetOfHashSetWithSameComparer(this);
}
}

Expand Down Expand Up @@ -811,8 +819,8 @@ public bool SetEquals(IEnumerable<T> other)
}

// Already confirmed that the sets have the same number of distinct elements, so if
// one is a superset of the other then they must be equal.
return ContainsAllElements(otherAsSet);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After these changes, there is now only one caller for ContainsAllElements(IEnumerable<T>) (IsSupersetOf). Should the helper method just be inlined there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

// one is a subset of the other then they must be equal.
return IsSubsetOfHashSetWithSameComparer(otherAsSet);
}
else
{
Expand Down Expand Up @@ -1178,24 +1186,6 @@ private bool AddIfNotPresent(T value, out int location)
return true;
}

/// <summary>
/// Checks if this contains of other's elements. Iterates over other's elements and
/// returns false as soon as it finds an element in other that's not in this.
/// Used by SupersetOf, ProperSupersetOf, and SetEquals.
/// </summary>
private bool ContainsAllElements(IEnumerable<T> other)
{
foreach (T element in other)
{
if (!Contains(element))
{
return false;
}
}

return true;
}

/// <summary>
/// Implementation Notes:
/// If other is a hashset and is using same equality comparer, then checking subset is
Expand Down Expand Up @@ -1391,7 +1381,7 @@ private unsafe void SymmetricExceptWithEnumerable(IEnumerable<T> other)
/// <param name="other"></param>
/// <param name="returnIfUnfound">Allows us to finish faster for equals and proper superset
/// because unfoundCount must be 0.</param>
private unsafe (int UniqueCount, int UnfoundCount) CheckUniqueAndUnfoundElements(IEnumerable<T> other, bool returnIfUnfound)
private (int UniqueCount, int UnfoundCount) CheckUniqueAndUnfoundElements(IEnumerable<T> other, bool returnIfUnfound)
{
// Need special case in case this has no elements.
if (_count == 0)
Expand Down