[OrderedCollections] Add OrderedDictionary.replaceElement(at:withKey:…#616
Open
inju2403 wants to merge 1 commit intoapple:mainfrom
Open
[OrderedCollections] Add OrderedDictionary.replaceElement(at:withKey:…#616inju2403 wants to merge 1 commit intoapple:mainfrom
inju2403 wants to merge 1 commit intoapple:mainfrom
Conversation
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.
Summary
Add
replaceElement(at:withKey:value:)toOrderedDictionaryandOrderedDictionary.Elements.This method replaces the key-value pair at a given index with a new pair in expected amortized O(1) time.
Motivation
OrderedDictionaryprovides O(1) primitives for updating a value by key (updateValue(_:forKey:)), replacing a value at an index (values[index]), and swapping elements (swapAt). However, there is no single operation to replace a key-value pair at a given index when the new key has a different hash than the old one.dict[newKey] = valueremove(at:)+ reinsertioncount) due to element shiftingupdateValue(_:forKey:)Callers can compose
append→swapAt→removeLastto work around this, but the pattern is non-obvious and requires careful duplicate-key validation and bounds checking that is easy to omit.replaceElement(at:withKey:value:)fills this gap as a first-class operation, consistent with howswapAtalready exposes an index-based mutation primitive.Detailed design
Signature:
OrderedDictionaryandOrderedDictionary.Elements, matching the existing pattern for index-based mutations.@discardableResult).indexis out of bounds orkeyalready exists.Keyimplements high-quality hashing.Implementation: Appends the new pair, swaps it into the target position, and removes the old element from the end — each step is O(1).
Testing
Tests added in both
OrderedDictionary Tests.swiftandOrderedDictionary+Elements Tests.swift, covering:withHiddenCopiesdict[newKey] == newValue)dict[oldKey] == nil)_checkInvariants()Checklist