Files
swift-collections/Sources/OrderedCollections
inju2403 95d8afe88f [OrderedCollections] Add OrderedSet.replace(_:at:)
`OrderedSet` already offers index-based mutations -- `update(_:at:)`, `insert(_:at:)`, `swapAt`, `remove(at:)` -- but no way to replace the member at an index with a *different* element. `update(_:at:)` looks like it should, but it requires the replacement to compare equal to the existing member.

Add `replace(_:at:)`, which replaces the member at a given index with a new element in expected amortized O(1) time. If the new element compares equal to the member at `index`, it is replaced in place via `update(_:at:)`; otherwise it is appended, swapped into position, and the old member is removed from the end -- each step O(1). Replacing a member with an element already present at a different index traps, as that would introduce a duplicate.

This is the OrderedSet counterpart of `OrderedDictionary.replaceElement(at:withKey:value:)` (#616), which already implements this dance on its keys' OrderedSet internally.
2026-06-14 19:38:01 +09:00
..