Honor OrderedSet's own comparer in the set operations - #52
Merged
Merged
Conversation
IntersectWith, SymmetricExceptWith, IsSubsetOf, IsProperSubsetOf,
IsProperSupersetOf and SetEquals each materialized the other collection
into a HashSet<T> built with the default constructor, so membership and
cardinality were decided by EqualityComparer<T>.Default while Add,
Remove and Contains route through BinarySearch and the set's own
IComparer<T>. A set built with StringComparer.OrdinalIgnoreCase would
report Contains("HELLO") as true and then have "Hello" evicted by
IntersectWith(["HELLO"]).
Build the temporary set with the set's own comparer instead, mirroring
what the sibling ContiguousSet<T> already does. IsProperSubsetOf and
SetEquals now test membership directly rather than round-tripping
through IsSubsetOf, which would have rebuilt the same temporary set.
Fixes #51
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K19wdK61qatQNZ7MvvVgvB
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #51
The problem
OrderedSet<T>decides membership two different ways.Add,RemoveandContainsroute throughBinarySearch, which uses theIComparer<T>the set was constructed with. But six of theISet<T>operations materialized the other collection into aHashSet<T>built with the default constructor, so both membership tests and cardinality were answered byEqualityComparer<T>.Defaultinstead:IntersectWithSymmetricExceptWithIsSubsetOfIsProperSubsetOfIsProperSupersetOfSetEqualsFor any set built with a comparer that defines a different equality notion than the default, the two disagree:
The fix
Build the temporary set with the set's own comparer, via a small private
ToComparerSethelper, so membership and distinct-element counts are decided by the same rule asContains. This mirrors the siblingContiguousSet<T>.IntersectWith, which already threads its comparer into the temporary set.A
HashSet<T>cannot be used here: anIEqualityComparer<T>adapter over anIComparer<T>has no way to produce hash codes consistent with that comparer's equality. Building anOrderedSet<T>with the same comparer sidesteps that and keeps membership at O(log n).IsProperSubsetOfandSetEqualsnow test membership directly rather than calling back intoIsSubsetOf, which would have rebuilt the same temporary set a second time.Nothing changes for a set using the default comparer, and no public API changed.
Tests
Seven tests added to
OrderedSetTests.cs, all usingStringComparer.OrdinalIgnoreCaseso the set's comparer and default equality disagree. They cover every affected operation, including the cases where the bug showed up in the count rather than the membership test (SetEquals(["HELLO", "hello", "WORLD"])on a two-element set).main: 357 passed.OrderedSet.csand re-running: 6 of the 7 new tests fail. The seventh (IsProperSubsetOf_CustomComparer_CountsDistinctElementsUnderThatComparer) asserts afalsethe old code also happened to return, and is kept as a regression guard.dotnet build Containers.slnis clean — 0 warnings, 0 errors.🤖 Generated with Claude Code
https://claude.ai/code/session_01K19wdK61qatQNZ7MvvVgvB
Generated by Claude Code