Skip to content

Instantly share code, notes, and snippets.

@joao-r-reis
Last active October 31, 2020 20:59
Show Gist options
  • Save joao-r-reis/d90155867e39c6b610bc3de555d75943 to your computer and use it in GitHub Desktop.
Save joao-r-reis/d90155867e39c6b610bc3de555d75943 to your computer and use it in GitHub Desktop.
SetEquals.cs
public bool SetEquals(IEnumerable<T> other)
{
SortedSet<T> asSorted = other as SortedSet<T>;
if (asSorted != null && AreComparersEqual(this, asSorted))
{
IEnumerator<T> mine = this.GetEnumerator();
IEnumerator<T> theirs = asSorted.GetEnumerator();
bool mineEnded = !mine.MoveNext();
bool theirsEnded = !theirs.MoveNext();
while (!mineEnded && !theirsEnded)
{
if (Comparer.Compare(mine.Current, theirs.Current) != 0)
{
return false;
}
mineEnded = !mine.MoveNext();
theirsEnded = !theirs.MoveNext();
}
return mineEnded && theirsEnded;
}
//worst case: mark every element in my set and see if i've counted all
//O(N) by size of other
ElementCount result = CheckUniqueAndUnfoundElements(other, true);
return (result.uniqueCount == Count && result.unfoundCount == 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment