[stdlib] De-gyb Sort (#17954)

* [stdlib] De-gyb Sort
This commit is contained in:
Ben Cohen
2018-07-15 14:23:06 -07:00
committed by GitHub
parent adf79f92bc
commit bd7171bedf
7 changed files with 621 additions and 696 deletions

View File

@@ -239,4 +239,32 @@ extension MutableCollection {
}
}
// the legacy swap free function
//
/// Exchanges the values of the two arguments.
///
/// The two arguments must not alias each other. To swap two elements of a
/// mutable collection, use the `swapAt(_:_:)` method of that collection
/// instead of this function.
///
/// - Parameters:
/// - a: The first value to swap.
/// - b: The second value to swap.
@inlinable
public func swap<T>(_ a: inout T, _ b: inout T) {
// Semantically equivalent to (a, b) = (b, a).
// Microoptimized to avoid retain/release traffic.
let p1 = Builtin.addressof(&a)
let p2 = Builtin.addressof(&b)
_debugPrecondition(
p1 != p2,
"swapping a location with itself is not supported")
// Take from P1.
let tmp: T = Builtin.take(p1)
// Transfer P2 into P1.
Builtin.initialize(Builtin.take(p2) as T, p1)
// Initialize P2.
Builtin.initialize(tmp, p2)
}