Last active
October 31, 2020 20:59
-
-
Save joao-r-reis/d90155867e39c6b610bc3de555d75943 to your computer and use it in GitHub Desktop.
SetEquals.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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