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 @@ -130,6 +130,19 @@ private protected override bool OverlapsCore(IEnumerable<T> other)
{
Debug.Assert(_thisSet.Count != 0, "EmptyFrozenSet should have been used.");

// Fast path for Overlaps when other is IReadOnlySet with fast lookup and same equality comparer
if (other is IReadOnlySet<T> otherAsSet && ComparersAreCompatible(otherAsSet))
{
return otherAsSet.Count > Count
? SmallerFrozenSetOverlapsLargerCompatibleReadOnlySetImpl(this, otherAsSet)
: LargerFrozenSetOverlapsSmallerCompatibleReadOnlySetImpl(otherAsSet, this);
}

return OverlapsEnumerableImpl(other);
}

private bool OverlapsEnumerableImpl(IEnumerable<T> other)
{
foreach (T element in other)
{
if (_thisSet.FindItemIndex(element) >= 0)
Expand All @@ -141,6 +154,32 @@ private protected override bool OverlapsCore(IEnumerable<T> other)
return false;
}

private static bool SmallerFrozenSetOverlapsLargerCompatibleReadOnlySetImpl(FrozenSet<T> smaller, IReadOnlySet<T> larger)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not just have one method and switch arguments at call-site to keep it simpler?

@Advitalitum Advitalitum Sep 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it can be done for ISet as arguments, just need to duplicate method ComparersAreCompatible for ISet, but Immutable collections don't implement ISet

{
foreach (T element in smaller)
{
if (larger.Contains(element))
{
return true;
}
}

return false;
}

private static bool LargerFrozenSetOverlapsSmallerCompatibleReadOnlySetImpl(IReadOnlySet<T> smaller, FrozenSet<T> larger)
{
foreach (T element in smaller)
{
if (larger.Contains(element))
{
return true;
}
}

return false;
}

/// <inheritdoc />
private protected override bool SetEqualsCore(IEnumerable<T> other)
{
Expand Down
Loading