Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] Switch to a stable sort algorithm #19717

Merged
merged 16 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions stdlib/public/core/CollectionAlgorithms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,21 @@ extension MutableCollection where Self : BidirectionalCollection {
) rethrows -> Index {
let maybeOffset = try _withUnsafeMutableBufferPointerIfSupported {
(bufferPointer) -> Int in
let unsafeBufferPivot = try bufferPointer.partition(
let unsafeBufferPivot = try bufferPointer._partitionImpl(
by: belongsInSecondPartition)
return unsafeBufferPivot - bufferPointer.startIndex
}
if let offset = maybeOffset {
return index(startIndex, offsetBy: numericCast(offset))
return index(startIndex, offsetBy: offset)
} else {
return try _partitionImpl(by: belongsInSecondPartition)
}

}

@usableFromInline
internal mutating func _partitionImpl(
by belongsInSecondPartition: (Element) throws -> Bool
) rethrows -> Index {
var lo = startIndex
var hi = endIndex

Expand Down
735 changes: 439 additions & 296 deletions stdlib/public/core/Sort.swift

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions stdlib/public/core/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ extension Unsafe${Mutable}BufferPointer {
count = other.count
}

@inlinable
public mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R? {
return try body(&self)
}

% else:

/// Creates an immutable typed buffer pointer referencing the same memory as the
Expand Down
Loading