It would take too much effort to try to mitigate stdlib differences.
Fix low-hanging warnings, but turn off `.strictMemorySafety` on older toolchains by splitting the package manifest.
As of 2026-09-01, conformance checks are now diagnosed as errors:
```
70 | // Fast paths for when we know elements are all unique
71 | if elements is _UniqueCollection {
| `- error: cannot perform a dynamic cast to a type involving protocol '_UniqueCollection' in Embedded Swift
```
#688 factored `replaceElement`'s general case onto `OrderedSet._replaceNew(at:with:in:)` and, per review feedback, switched the value overwrite there to `exchange(_:with:)`. The equal-key path above it was outside that diff, so it still reads the old value into a local and then assigns over it.
Switch it to `exchange(_:with:)` as well, so both paths move the old value out instead of copying it. No behavior change: the returned element, the preconditions, and the expected amortized O(1) complexity are all unchanged.
Twenty-five doc comments in the shipping modules name an argument that the
declaration below them does not have, so DocC has nothing to bind the
description to and drops it. Nothing warns about this.
Most of them are renames that the comment did not follow, and the file itself
usually holds the proof: RigidDeque.prepend says - maxCount while its own
Complexity line already says newItemCount, and append(copying:) has three
overloads where the oldest still takes newElements and the two newer ones take
items with the older one's wording.
Three entries name an argument that no longer exists at all and are removed
rather than renamed.
#669 left `OrderedDictionary.replaceElement` untouched to keep that PR purely additive, noting it could later be reimplemented on top of `replace(at:with:)`. Both it and `OrderedSet.replace(at:with:)` performed the same "append the new member, swap it into position, then remove the old one from the end" step inline.
Factor that step into an internal `OrderedSet._replaceNew(at:with:in:)` primitive and call it from both, matching the existing `_appendNew` / `_removeExistingMember` primitives that `OrderedDictionary` already delegates its key-side mutations to. In `replaceElement` only the keys go through the primitive; the value at `index` is overwritten in place.
Sharing the mechanical step this way — rather than having `replaceElement` call `replace` directly — keeps each type's diagnostics intact: `OrderedDictionary` still traps with "Duplicate key: '\(key)'" and "Index out of range", not `OrderedSet`'s "Duplicate element" / "Index out of bounds". The shared step is already verified by the caller, so there is no behavior change.
Swap the parameter order and add the `with` label so the API matches the shape of `replaceSubrange(_:with:)` on RangeReplaceableCollection, per review feedback.
When the BorrowAndMutateAccessors feature is enabled, this code appears to be parsed as if it declared a `read` accessor, rather than a getter passing a closure to the `read` function:
```swift
@inlinable
internal var collisionHash: _Hash {
read { $0.collisionHash }
}
```
Avoid this by adding a `self` prefix, as in `self.read { $0.collisionHash }`.
`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.
Reversing an ordered set is a deterministic permutation: the element at offset `i` moves to offset `count - 1 - i`. Hash values are unaffected, so every element stays in its current bucket and only the stored offset needs to be flipped. `reverse()` was instead regenerating the whole hash table from scratch, rehashing every element -- exactly what the existing `// FIXME: Update hash table contents in place.` called out.
Replace `_regenerateHashTable()` with an in-place offset flip (`v -> count - 1 - v`) over the table's buckets, reusing the same bucket-walk idiom as `adjustContents` and `swapBucketValues`. This drops all hash recomputation and the table reallocation.
Reworks the relocation API and extends it to `OrderedDictionary`:
* Renames the index-range move to `moveSubrange(_:to:)` (with a
`RangeExpression` overload) and the by-value move to `move(contentsOf:to:)`,
matching the conventions of `removeSubrange`/`insert(_:at:)`. Adds
`move(fromIndices:to:)`, which takes the source indices directly.
* Factors the element rearrangement into generic
`UnsafeMutableBufferPointer` operations (rotation / rotation+permutation /
scatter) so `OrderedDictionary` relocates its keys and values in lockstep
through the same code, adding only the parallel value move; the hash-table
maintenance stays entirely in `OrderedSet`.
* The hot path runs in O(*d* + *k*) and falls back to O(`count`) for large
moves; bucket maintenance chooses targeted updates or a full rebuild by the
same threshold.
* `move(contentsOf:to:)` ignores elements/keys that aren't present; duplicate
members are detected and trap.
Adds tests (exhaustive hash-table integrity, value alignment under lifetime
tracking) and move benchmarks for both types.
Adds two overloads for relocating elements within an `OrderedSet`: one
taking a contiguous `Range<Int>`, and one taking a `Sequence<Element>`.
The moved elements occupy consecutive positions starting at the
destination, in the order they appear in the input.
The hot path runs in O(*d* + *k*) time, where *d* is the distance moved
and *k* is the number of elements moved; the implementation falls back
to O(`count`) for large moves.
Per review feedback, relax the precondition so that the new key may compare equal to the key currently stored at index. This enables replacing an existing key with an equal-but-not-identical instance — for example, swapping a decomposed Unicode string with its precomposed equivalent — without an inefficient remove/insert workaround.
When the new key matches the one being replaced, the pair is updated in place via OrderedSet.update(_:at:) and the hash table is left untouched, preserving the expected amortized O(1) complexity.
Add a test_replaceElement_sameKey test on both OrderedDictionary and its Elements view that exercises the new path with LifetimeTracked keys carrying equal payloads but distinct instances.
DocC keeps changing its link syntax. The latest iteration appears to have trouble dealing with `Self`-taking or `Self`-returning methods, so change our API to replace `Self` with a direct mention of the concrete type. ¯\\\_(ツ)\_/¯
That said, the new compiler also found a large number of mismatching parameter names, and that was really helpful.
The indexes passed to span(after:) and friends on UniqueArray
and RigidArray are modified before return. This adds notes
about the updated values to the method documentation, along
with a few other minor formatting changes (dropping smart
quotes, single-sentence abstracts).