Repository navigation
Make IndexOf return the element Remove removes [patch] - #117
Merged
Merged
Conversation
With a key-based comparer and duplicate keys, IndexOf returned the first element that merely compared equal, while Remove picked the one that also Equals the argument. RemoveAt(IndexOf(x)) therefore removed a different element than Remove(x). Both now share FindMatchIndex, which keeps the fall-back to the first compare-equal element so Contains(x) still implies IndexOf(x) >= 0. Fixes #107 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SWgHqHW7R9hBKkZ6Zzx5Lg
|
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 #107
What changed
OrderedCollection<T>.IndexOfandRemovenow share a privateFindMatchIndexhelper. Among the run of elements that compare equal to the argument, it returns the first one that alsoEqualsit. If none does, it falls back to the first element that compares equal.Before this change, with a key-based comparer and duplicate keys,
IndexOf(b)returned the index of a different elementathat had the same key.RemoveAt(IndexOf(b))then removedaand leftbin the collection.Remove(b)already picked the right element.Because of the fallback,
Contains(x)being true still meansIndexOf(x) >= 0.Tests
IndexOf_KeyComparerWithDuplicateKeys_ReturnsIndexOfTheElementPassedInIndexOf_KeyComparerWithNoExactMatch_ReturnsFirstOccurrenceRemoveAtIndexOf_KeyComparerWithDuplicateKeys_RemovesSameElementAsRemoveI reverted the source change and reran the tests: the first and third fail without the fix and pass with it. The full suite passes locally (483 tests).
🤖 Generated with Claude Code
https://claude.ai/code/session_01SWgHqHW7R9hBKkZ6Zzx5Lg
Generated by Claude Code