Skip to content
Merged
Show file tree
Hide file tree
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 @@ -375,24 +375,56 @@ public bool SetEquals(IEnumerable<T> other)
return true;
}

var otherSet = new SortedSet<T>(other, this.KeyComparer);
if (this.Count != otherSet.Count)
switch (other)
{
return false;
case ImmutableSortedSet<T> otherAsImmutableSortedSet:
if (EqualityComparer<IComparer<T>>.Default.Equals(this.KeyComparer, otherAsImmutableSortedSet.KeyComparer))
{
if (otherAsImmutableSortedSet.Count != this.Count)
{
return false;
}
return SetEqualsWithImmutableSortedSet(otherAsImmutableSortedSet, this);
}

if (otherAsImmutableSortedSet.Count < this.Count)
{
return false;
}
break;

case SortedSet<T> otherAsSortedSet:
if (EqualityComparer<IComparer<T>>.Default.Equals(this.KeyComparer, otherAsSortedSet.Comparer))
{
if (otherAsSortedSet.Count != this.Count)
{
return false;
}
return SetEqualsWithSortedSet(otherAsSortedSet, this);
}

if (otherAsSortedSet.Count < this.Count)
{
return false;
}
break;

case 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 < this.Count)
{
return false;
}
break;
}

int matches = 0;
foreach (T item in otherSet)
var otherSet = new SortedSet<T>(other, this.KeyComparer);
if (otherSet.Count != this.Count)
{
if (!this.Contains(item))
{
return false;
}

matches++;
return false;
}

return matches == this.Count;
return SetEqualsWithSortedSet(otherSet, this);
}

/// <summary>
Expand Down Expand Up @@ -1079,6 +1111,42 @@ private ImmutableSortedSet<T> UnionIncremental(ReadOnlySpan<T> items)
return this.Wrap(result);
}

private static bool SetEqualsWithImmutableSortedSet(ImmutableSortedSet<T> other, ImmutableSortedSet<T> source)
{
// We can use a linear scan because both sets are sorted using the same comparer.
using var e = other.GetEnumerator();
foreach (T item in source)
{
bool eHasMore = e.MoveNext();
Debug.Assert(eHasMore);

if (source.KeyComparer.Compare(item, e.Current) != 0)
{
return false;
}
}

return true;
}

private static bool SetEqualsWithSortedSet(SortedSet<T> other, ImmutableSortedSet<T> source)
{
// We can use a linear scan because both sets are sorted using the same comparer.
using var e = other.GetEnumerator();
foreach (T item in source)
{
bool eHasMore = e.MoveNext();
Debug.Assert(eHasMore);

if (source.KeyComparer.Compare(item, e.Current) != 0)
{
return false;
}
}

return true;
}

/// <summary>
/// Creates a wrapping collection type around a root node.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,96 @@ public void RandomOperationsTest()
}
}

[Fact]
public void SetEqualsMismatchedComparersOriginInsensitiveOtherSensitive()
{
var ignoreCaseSet = ImmutableSortedSet.Create(StringComparer.OrdinalIgnoreCase, "a");
var sensitiveSet = ImmutableSortedSet.Create(StringComparer.Ordinal, "a", "A");

Assert.True(ignoreCaseSet.SetEquals(sensitiveSet));
}

[Fact]
public void SetEqualsMismatchedComparersOriginSensitiveOtherInsensitive()
{
var sensitiveSetMain = ImmutableSortedSet.Create(StringComparer.Ordinal, "a");
var insensitiveMutable = new SortedSet<string>(StringComparer.OrdinalIgnoreCase) { "a", "A" };

Assert.True(sensitiveSetMain.SetEquals(insensitiveMutable));
}

[Fact]
public void SetEqualsICollectionWithDuplicatesValidatesCorrectness()
{
var ignoreCaseSet = ImmutableSortedSet.Create(StringComparer.OrdinalIgnoreCase, "a");
var listWithDupes = new List<string> { "a", "a", "a", "a" };

Assert.True(ignoreCaseSet.SetEquals(listWithDupes));
}

[Fact]
public void SetEqualsDifferentContent()
{
var ignoreCaseSet = ImmutableSortedSet.Create(StringComparer.OrdinalIgnoreCase, "a");
var setB = ImmutableSortedSet.Create(StringComparer.Ordinal, "b");

Assert.False(ignoreCaseSet.SetEquals(setB));
}

[Fact]
public void SetEqualsMismatchedComparersOtherCountSmaller()
{
var originTwoElements = ImmutableSortedSet.Create(StringComparer.OrdinalIgnoreCase, "a", "b");
var otherOneElement = ImmutableSortedSet.Create(StringComparer.Ordinal, "a");

Assert.False(originTwoElements.SetEquals(otherOneElement));
}

[Fact]
public void SetEqualsMatchedComparersDifferentCounts()
{
var matchedSet1 = ImmutableSortedSet.Create(StringComparer.Ordinal, "a", "b");
var matchedSet2 = ImmutableSortedSet.Create(StringComparer.Ordinal, "a");

Assert.False(matchedSet1.SetEquals(matchedSet2));
}

[Fact]
public void SetEqualsMatchedComparersSameContent()
{
var matchedSet1 = ImmutableSortedSet.Create(StringComparer.Ordinal, "a", "b");
var matchedSet2 = ImmutableSortedSet.Create(StringComparer.Ordinal, "a", "b");

Assert.True(matchedSet1.SetEquals(matchedSet2));
}

[Fact]
public void SetEqualsEmptySetsDifferentComparers()
{
var empty1 = ImmutableSortedSet<string>.Empty.WithComparer(StringComparer.Ordinal);
var empty2 = ImmutableSortedSet<string>.Empty.WithComparer(StringComparer.OrdinalIgnoreCase);

Assert.True(empty1.SetEquals(empty2));
}

[Fact]
public void SetEqualsMismatchedComparersOriginSensitiveOtherInsensitiveSameCount()
{
var sensitiveSet = ImmutableSortedSet.Create(StringComparer.Ordinal, "a", "A");
var insensitiveSet = ImmutableSortedSet.Create(StringComparer.OrdinalIgnoreCase, "a", "b");

Assert.False(sensitiveSet.SetEquals(insensitiveSet));
}

[Fact]
public void SetEqualsMismatchedComparersOtherIsLarger()
{
var origin = ImmutableSortedSet.Create(StringComparer.OrdinalIgnoreCase, "a");
var other = ImmutableSortedSet.Create(StringComparer.Ordinal, "a", "b");

Assert.False(origin.SetEquals(other));
}

[Fact]
public void CustomSort()
{
Expand Down
Loading