fix: keep elements that compare equal in insertion order in OrderedCollection [patch] - #91
Merged
Conversation
…llection [patch] Add inserted at whatever index BinarySearch returned, so among elements that compare equal the order depended on where the search happened to probe. Clone, GetRange and the enumerable constructors rebuild by re-adding, so each rebuild shuffled equal elements again and a copy was not sequence-equal to its source. Add now inserts at the upper bound, after the last element that compares equal. Equal elements keep insertion order, and rebuilding a collection from its own sequence reproduces it. Fixes #80 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EdrcmpMTNHSnACRgnNGs8v
|
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 #80
What was wrong
OrderedCollection<T>.Addinserted at whatever indexBinarySearchreturned. When other elements already compared equal, that index could be any of them, so where a new equal element landed depended on where the search happened to probe.Clone(),GetRange()and theIEnumerableconstructors all rebuild by re-adding each item. Each rebuild shuffled equal elements again, so with a key-only comparer a copy was not sequence-equal to its source.Change
Addnow inserts at the upper bound, just after the last element that compares equal. A new privateFindUpperBounddoes this, mirroring the existingFindFirst. Equal elements keep insertion order, so rebuilding a collection from its own sequence gives back the same sequence.CloneandGetRangeare unchanged. They now preserve order becauseAddis stable, and since each item goes in at the end, a rebuild from already-sorted input no longer shifts elements.Tests
Four new tests use a
(int Key, string Name)element with a key-only comparer:Add_KeyComparer_KeepsEqualElementsInInsertionOrderClone_KeyComparer_IsSequenceEqualToSourceGetRange_KeyComparer_IsSequenceEqualToSourceRangeConstructor_FromEnumerableWithKeyComparer_PreservesInputOrderOfEqualElementsWith the fix reverted, all four fail. With it in place, the whole suite passes (
dotnet test: 422 passed, 0 failed), andContainers.csprojbuilds with 0 warnings.🤖 Generated with Claude Code
https://claude.ai/code/session_01EdrcmpMTNHSnACRgnNGs8v
Generated by Claude Code