mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
stdlib: change sort() and sorted() into protocol extensions
Swift SVN r28736
This commit is contained in:
@@ -105,13 +105,13 @@ partitionDocComment = """\
|
||||
orderingRequirementForPredicate = """\
|
||||
/// - Requires: `isOrderedBefore` is a
|
||||
/// [strict weak ordering](http://en.wikipedia.org/wiki/Strict_weak_order#Strict_weak_orderings)
|
||||
/// over `self`."""
|
||||
/// over the elements in `self`."""
|
||||
|
||||
orderingRequirementForComparable = """\
|
||||
/// - Requires: The less-than operator (`func <`) defined in
|
||||
/// the `Comparable` conformance is a
|
||||
/// [strict weak ordering](http://en.wikipedia.org/wiki/Strict_weak_order#Strict_weak_orderings)
|
||||
/// over `self`."""
|
||||
/// over the elements in `self`."""
|
||||
|
||||
}%
|
||||
|
||||
@@ -139,3 +139,108 @@ ${orderingRequirementForPredicate}
|
||||
}
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// sort()
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
%{
|
||||
|
||||
sortDocCommentForPredicate = """\
|
||||
/// Return an `Array` containing the sorted elements of `source`
|
||||
/// according to `isOrderedBefore`."""
|
||||
|
||||
sortDocCommentForComparable = """\
|
||||
/// Return an `Array` containing the sorted elements of `source`."""
|
||||
|
||||
sortInPlaceDocCommentForPredicate = """\
|
||||
/// Sort `self` in-place according to `isOrderedBefore`."""
|
||||
|
||||
sortInPlaceDocCommentForComparable = """\
|
||||
/// Sort `self` in-place."""
|
||||
|
||||
sortIsUnstableForPredicate = """\
|
||||
/// The sorting algorithm is not stable (can change the relative order of
|
||||
/// elements for which `isOrderedBefore` does not establish an order)."""
|
||||
|
||||
sortIsUnstableForComparable = """\
|
||||
/// The sorting algorithm is not stable (can change the relative order of
|
||||
/// elements that compare equal)."""
|
||||
|
||||
}%
|
||||
|
||||
extension SequenceType where Self.Generator.Element : Comparable {
|
||||
${sortDocCommentForComparable}
|
||||
///
|
||||
${sortIsUnstableForComparable}
|
||||
///
|
||||
${orderingRequirementForComparable}
|
||||
final public func _prext_sort() -> [Generator.Element] {
|
||||
var result = ContiguousArray(self)
|
||||
result._prext_sortInPlace()
|
||||
return Array(result)
|
||||
}
|
||||
}
|
||||
|
||||
extension SequenceType {
|
||||
${sortDocCommentForPredicate}
|
||||
///
|
||||
${sortIsUnstableForPredicate}
|
||||
///
|
||||
${orderingRequirementForPredicate}
|
||||
final public func _prext_sort(
|
||||
@noescape isOrderedBefore: (Generator.Element, Generator.Element) -> Bool
|
||||
) -> [Generator.Element] {
|
||||
var result = ContiguousArray(self)
|
||||
result._prext_sortInPlace(isOrderedBefore)
|
||||
return Array(result)
|
||||
}
|
||||
}
|
||||
|
||||
extension MutableCollectionType
|
||||
where
|
||||
Self.Index : RandomAccessIndexType,
|
||||
Self.Generator.Element : Comparable {
|
||||
|
||||
${sortInPlaceDocCommentForComparable}
|
||||
///
|
||||
${sortIsUnstableForComparable}
|
||||
///
|
||||
${orderingRequirementForComparable}
|
||||
final public mutating func _prext_sortInPlace() {
|
||||
let didSortUnsafeBuffer: Void? =
|
||||
_withUnsafeMutableBufferPointerIfSupported {
|
||||
(bufferPointer) -> () in
|
||||
bufferPointer._prext_sortInPlace()
|
||||
return ()
|
||||
}
|
||||
if didSortUnsafeBuffer == nil {
|
||||
_introSort(&self, self.indices)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension MutableCollectionType where Self.Index : RandomAccessIndexType {
|
||||
${sortInPlaceDocCommentForPredicate}
|
||||
///
|
||||
${sortIsUnstableForPredicate}
|
||||
///
|
||||
${orderingRequirementForPredicate}
|
||||
final public mutating func _prext_sortInPlace(
|
||||
@noescape isOrderedBefore: (Generator.Element, Generator.Element) -> Bool
|
||||
) {
|
||||
let didSortUnsafeBuffer: Void? =
|
||||
_withUnsafeMutableBufferPointerIfSupported {
|
||||
(bufferPointer) -> () in
|
||||
bufferPointer._prext_sortInPlace(isOrderedBefore)
|
||||
return ()
|
||||
}
|
||||
if didSortUnsafeBuffer == nil {
|
||||
typealias IsOrderedBefore =
|
||||
(Generator.Element, Generator.Element) -> Bool
|
||||
let escapableIsOrderedBefore =
|
||||
unsafeBitCast(isOrderedBefore, IsOrderedBefore.self)
|
||||
_introSort(&self, self.indices, escapableIsOrderedBefore)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user