mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-30 04:48:43 +00:00
Compare commits
12
Commits
1.2.0
...
nate/pdqsort
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e84988b833 | ||
|
|
34eec0dd27 | ||
|
|
09dc5e4354 | ||
|
|
0a01f21eae | ||
|
|
34b1705efd | ||
|
|
ae92f2b33f | ||
|
|
33abb69428 | ||
|
|
82ee335a48 | ||
|
|
9e3900be49 | ||
|
|
9dd88e5eaf | ||
|
|
c3b3acbca3 | ||
|
|
9a8cbc61e0 |
No files matched your search
+1
-1
@@ -71,7 +71,7 @@ Calling `keyed(by:)` is an O(_n_) operation.
|
|||||||
|
|
||||||
#### Alternative names
|
#### Alternative names
|
||||||
|
|
||||||
Kotlin's `associatedBy` naming is a good alterative, and matches the past tense of [Swift's API Design Guidelines](https://www.swift.org/documentation/api-design-guidelines/), though perhaps we'd spell it `associated(by:)`.
|
Kotlin's `associatedBy` naming is a good alternative, and matches the past tense of [Swift's API Design Guidelines](https://www.swift.org/documentation/api-design-guidelines/), though perhaps we'd spell it `associated(by:)`.
|
||||||
|
|
||||||
#### Customization points
|
#### Customization points
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@ collection until predicate returns `false` and skipping the remaining elements.
|
|||||||
This example uses `suffix(while: )` to iterate through collection of integers
|
This example uses `suffix(while: )` to iterate through collection of integers
|
||||||
from the end until the predicate returns false, in this case when `$0 <= 5`
|
from the end until the predicate returns false, in this case when `$0 <= 5`
|
||||||
```swift
|
```swift
|
||||||
(0...10).suffix(while: { $0 > 5 } // == [6,7,8,9,10]
|
(0...10).suffix(while: { $0 > 5 }) // == [6,7,8,9,10]
|
||||||
```
|
```
|
||||||
|
|
||||||
## Detailed Design
|
## Detailed Design
|
||||||
|
|||||||
+7
-2
@@ -1,4 +1,4 @@
|
|||||||
// swift-tools-version:5.4
|
// swift-tools-version:5.7
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
//
|
//
|
||||||
// This source file is part of the Swift Algorithms open source project
|
// This source file is part of the Swift Algorithms open source project
|
||||||
@@ -27,7 +27,12 @@ let package = Package(
|
|||||||
name: "Algorithms",
|
name: "Algorithms",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.product(name: "RealModule", package: "swift-numerics"),
|
.product(name: "RealModule", package: "swift-numerics"),
|
||||||
]),
|
],
|
||||||
|
swiftSettings: [
|
||||||
|
.define("INSERTION_TEMP"),
|
||||||
|
// .define("INSERTION_MAX_32"),
|
||||||
|
]
|
||||||
|
),
|
||||||
.testTarget(
|
.testTarget(
|
||||||
name: "SwiftAlgorithmsTests",
|
name: "SwiftAlgorithmsTests",
|
||||||
dependencies: ["Algorithms"]),
|
dependencies: ["Algorithms"]),
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ Explore more chunking methods and the remainder of the `Algorithms` package in t
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
For API documentation, see the library's official documentation in Xocde or on the Web.
|
For API documentation, see the library's official documentation in Xcode or on the Web.
|
||||||
|
|
||||||
- [API documentation][docs]
|
- [API documentation][docs]
|
||||||
- [Swift.org announcement][announcement]
|
- [Swift.org announcement][announcement]
|
||||||
|
|||||||
@@ -78,6 +78,10 @@ public struct CombinationsSequence<Base: Collection> {
|
|||||||
binomial(n: n, k: $0)
|
binomial(n: n, k: $0)
|
||||||
}.reduce(0, +)
|
}.reduce(0, +)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The total number of combinations.
|
||||||
|
@inlinable
|
||||||
|
public var underestimatedCount: Int { count }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension CombinationsSequence: Sequence {
|
extension CombinationsSequence: Sequence {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ or a specific number of elements at the minimum and maximum.
|
|||||||
- ``Swift/Sequence/max(count:sortedBy:)``
|
- ``Swift/Sequence/max(count:sortedBy:)``
|
||||||
- ``Swift/Collection/max(count:sortedBy:)``
|
- ``Swift/Collection/max(count:sortedBy:)``
|
||||||
|
|
||||||
### Finding the Minimum and Maximum Elements Simulataneously
|
### Finding the Minimum and Maximum Elements Simultaneously
|
||||||
|
|
||||||
- ``Swift/Sequence/minAndMax()``
|
- ``Swift/Sequence/minAndMax()``
|
||||||
- ``Swift/Sequence/minAndMax(by:)``
|
- ``Swift/Sequence/minAndMax(by:)``
|
||||||
|
|||||||
@@ -0,0 +1,539 @@
|
|||||||
|
|
||||||
|
extension Int {
|
||||||
|
@inlinable
|
||||||
|
internal var _log2: Int {
|
||||||
|
Self.bitWidth - leadingZeroBitCount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable internal var _partialInsertionSortSwapLimit: Int { 8 }
|
||||||
|
|
||||||
|
@inlinable internal var _insertionSortMaxCount: Int {
|
||||||
|
#if INSERTION_MAX_32
|
||||||
|
32
|
||||||
|
#else
|
||||||
|
20
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable internal var _medianOfMediansMinCount: Int { 200 }
|
||||||
|
|
||||||
|
extension MutableCollection where Self: BidirectionalCollection {
|
||||||
|
@inlinable
|
||||||
|
internal mutating func _partialInsertionSort(
|
||||||
|
within range: Range<Index>,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows -> Bool {
|
||||||
|
if range.isEmpty {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var sortedEnd = index(after: range.lowerBound)
|
||||||
|
var swaps = 0
|
||||||
|
|
||||||
|
// Continue sorting until the sorted elements cover the whole sequence.
|
||||||
|
while sortedEnd != range.upperBound {
|
||||||
|
var i = sortedEnd
|
||||||
|
// Look backwards for `self[i]`'s position in the sorted sequence,
|
||||||
|
// moving each element forward to make room.
|
||||||
|
repeat {
|
||||||
|
let j = index(before: i)
|
||||||
|
|
||||||
|
// If `self[i]` doesn't belong before `self[j]`, we've found
|
||||||
|
// its position.
|
||||||
|
if try !areInIncreasingOrder(self[i], self[j]) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
swapAt(i, j)
|
||||||
|
i = j
|
||||||
|
|
||||||
|
// Quit if we've completed more than the swap limit.
|
||||||
|
swaps += 1
|
||||||
|
if swaps > _partialInsertionSortSwapLimit { return false }
|
||||||
|
} while i != range.lowerBound
|
||||||
|
|
||||||
|
formIndex(after: &sortedEnd)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sorts `self[range]` according to `areInIncreasingOrder`. Stable.
|
||||||
|
///
|
||||||
|
/// - Precondition: `sortedEnd != range.lowerBound`
|
||||||
|
/// - Precondition: `elements[..<sortedEnd]` are already in order.
|
||||||
|
@inlinable
|
||||||
|
public
|
||||||
|
mutating func _insertionSort(
|
||||||
|
within range: Range<Index>,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows {
|
||||||
|
if range.isEmpty {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var sortedEnd = index(after: range.lowerBound)
|
||||||
|
|
||||||
|
// Continue sorting until the sorted elements cover the whole sequence.
|
||||||
|
while sortedEnd != range.upperBound {
|
||||||
|
var i = sortedEnd
|
||||||
|
#if INSERTION_TEMP
|
||||||
|
let temp = self[i]
|
||||||
|
#endif
|
||||||
|
// Look backwards for `self[i]`'s position in the sorted sequence,
|
||||||
|
// moving each element forward to make room.
|
||||||
|
repeat {
|
||||||
|
let j = index(before: i)
|
||||||
|
|
||||||
|
// If `self[i]` doesn't belong before `self[j]`, we've found
|
||||||
|
// its position.
|
||||||
|
#if INSERTION_TEMP
|
||||||
|
if try !areInIncreasingOrder(temp, self[j]) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
self[j] = self[i]
|
||||||
|
#else
|
||||||
|
if try !areInIncreasingOrder(self[i], self[j]) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
swapAt(i, j)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
i = j
|
||||||
|
} while i != range.lowerBound
|
||||||
|
#if INSERTION_TEMP
|
||||||
|
self[i] = temp
|
||||||
|
#endif
|
||||||
|
|
||||||
|
formIndex(after: &sortedEnd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
internal mutating func _sort2(
|
||||||
|
_ a: Index, _ b: Index,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows {
|
||||||
|
if try areInIncreasingOrder(self[b], self[a]) {
|
||||||
|
swapAt(a, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
internal mutating func _sort3a(
|
||||||
|
_ a: Index, _ b: Index, _ c: Index,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows {
|
||||||
|
try _sort2(a, b, by: areInIncreasingOrder)
|
||||||
|
try _sort2(b, c, by: areInIncreasingOrder)
|
||||||
|
try _sort2(a, b, by: areInIncreasingOrder)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sorts the elements at `elements[a]`, `elements[b]`, and `elements[c]` to
|
||||||
|
/// match the relative order of `a`, `b`, and `c`.
|
||||||
|
@inlinable
|
||||||
|
internal mutating func _sort3(
|
||||||
|
_ a: Index, _ b: Index, _ c: Index,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows {
|
||||||
|
// There are thirteen possible permutations for the original ordering of
|
||||||
|
// the elements at indices `a`, `b`, and `c`. The comments in the code below
|
||||||
|
// show the relative ordering of the three elements using a three-digit
|
||||||
|
// number as shorthand for the position and comparative relationship of
|
||||||
|
// each element. For example, "312" indicates that the element at `a` is the
|
||||||
|
// largest of the three, the element at `b` is the smallest, and the element
|
||||||
|
// at `c` is the median. This hypothetical input array has a 312 ordering for
|
||||||
|
// `a`, `b`, and `c`:
|
||||||
|
//
|
||||||
|
// [ 7, 4, 3, 9, 2, 0, 3, 7, 6, 5 ]
|
||||||
|
// ^ ^ ^
|
||||||
|
// a b c
|
||||||
|
//
|
||||||
|
// - If each of the three elements is distinct, they could be ordered as any
|
||||||
|
// of the permutations of 1, 2, and 3: 123, 132, 213, 231, 312, or 321.
|
||||||
|
// - If two elements are equivalent and one is distinct, they could be
|
||||||
|
// ordered as any permutation of 1, 1, and 2 or 1, 2, and 2: 112, 121, 211,
|
||||||
|
// 122, 212, or 221.
|
||||||
|
// - If all three elements are equivalent, they are already in order: 111.
|
||||||
|
|
||||||
|
switch try (areInIncreasingOrder(self[b], self[a]),
|
||||||
|
areInIncreasingOrder(self[c], self[b])) {
|
||||||
|
case (false, false):
|
||||||
|
// 0 swaps: 123, 112, 122, 111
|
||||||
|
break
|
||||||
|
|
||||||
|
case (true, true):
|
||||||
|
// 1 swap: 321
|
||||||
|
// swap(a, c): 312->123
|
||||||
|
swapAt(a, c)
|
||||||
|
|
||||||
|
case (true, false):
|
||||||
|
// 1 swap: 213, 212 --- 2 swaps: 312, 211
|
||||||
|
// swap(a, b): 213->123, 212->122, 312->132, 211->121
|
||||||
|
swapAt(a, b)
|
||||||
|
|
||||||
|
if try areInIncreasingOrder(self[c], self[b]) {
|
||||||
|
// 132 (started as 312), 121 (started as 211)
|
||||||
|
// swap(b, c): 132->123, 121->112
|
||||||
|
swapAt(b, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
case (false, true):
|
||||||
|
// 1 swap: 132, 121 --- 2 swaps: 231, 221
|
||||||
|
// swap(b, c): 132->123, 121->112, 231->213, 221->212
|
||||||
|
swapAt(b, c)
|
||||||
|
|
||||||
|
if try areInIncreasingOrder(self[b], self[a]) {
|
||||||
|
// 213 (started as 231), 212 (started as 221)
|
||||||
|
// swap(a, b): 213->123, 212->122
|
||||||
|
swapAt(a, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension MutableCollection where Self: RandomAccessCollection {
|
||||||
|
@inlinable
|
||||||
|
internal mutating func _siftDown(
|
||||||
|
_ idx: Index,
|
||||||
|
within range: Range<Index>,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows {
|
||||||
|
var idx = idx
|
||||||
|
var countToIndex = distance(from: range.lowerBound, to: idx)
|
||||||
|
var countFromIndex = distance(from: idx, to: range.upperBound)
|
||||||
|
// Check if left child is within bounds. If not, stop iterating, because
|
||||||
|
// there are no children of the given node in the heap.
|
||||||
|
while countToIndex + 1 < countFromIndex {
|
||||||
|
let left = index(idx, offsetBy: countToIndex + 1)
|
||||||
|
var largest = idx
|
||||||
|
if try areInIncreasingOrder(self[largest], self[left]) {
|
||||||
|
largest = left
|
||||||
|
}
|
||||||
|
// Check if right child is also within bounds before trying to examine it.
|
||||||
|
if countToIndex + 2 < countFromIndex {
|
||||||
|
let right = index(after: left)
|
||||||
|
if try areInIncreasingOrder(self[largest], self[right]) {
|
||||||
|
largest = right
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If a child is bigger than the current node, swap them and continue
|
||||||
|
// sifting down.
|
||||||
|
if largest != idx {
|
||||||
|
swapAt(idx, largest)
|
||||||
|
idx = largest
|
||||||
|
countToIndex = distance(from: range.lowerBound, to: idx)
|
||||||
|
countFromIndex = distance(from: idx, to: range.upperBound)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
internal mutating func _heapify(
|
||||||
|
within range: Range<Index>,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows {
|
||||||
|
// Here we build a heap starting from the lowest nodes and moving to the
|
||||||
|
// root. On every step we sift down the current node to obey the max-heap
|
||||||
|
// property:
|
||||||
|
// parent >= max(leftChild, rightChild)
|
||||||
|
//
|
||||||
|
// We skip the rightmost half of the array, because these nodes don't have
|
||||||
|
// any children.
|
||||||
|
let root = range.lowerBound
|
||||||
|
let half = distance(from: range.lowerBound, to: range.upperBound) / 2
|
||||||
|
var node = index(root, offsetBy: half)
|
||||||
|
|
||||||
|
while node != root {
|
||||||
|
formIndex(before: &node)
|
||||||
|
try _siftDown(node, within: range, by: areInIncreasingOrder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
internal mutating func _heapSort(
|
||||||
|
within range: Range<Index>,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows {
|
||||||
|
var hi = range.upperBound
|
||||||
|
let lo = range.lowerBound
|
||||||
|
try _heapify(within: range, by: areInIncreasingOrder)
|
||||||
|
formIndex(before: &hi)
|
||||||
|
while hi != lo {
|
||||||
|
swapAt(lo, hi)
|
||||||
|
try _siftDown(lo, within: lo..<hi, by: areInIncreasingOrder)
|
||||||
|
formIndex(before: &hi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension MutableCollection where Self: BidirectionalCollection {
|
||||||
|
@inlinable
|
||||||
|
public // testing
|
||||||
|
mutating func _partitionLeft(
|
||||||
|
subrange: Range<Index>,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows -> Index {
|
||||||
|
assert(distance(from: subrange.lowerBound, to: subrange.upperBound) >= 2)
|
||||||
|
|
||||||
|
var (low, high) = (subrange.lowerBound, subrange.upperBound)
|
||||||
|
let pivot = self[subrange.lowerBound]
|
||||||
|
|
||||||
|
// Search from end for last element equal to or less than pivot.
|
||||||
|
repeat {
|
||||||
|
formIndex(before: &high)
|
||||||
|
// Don't need a separate `high > low` check, since self[low] == pivot,
|
||||||
|
// which is a terminating condition based on comparison above.
|
||||||
|
} while try areInIncreasingOrder(pivot, self[high])
|
||||||
|
|
||||||
|
// Search from beginning for first element greater than pivot.
|
||||||
|
if high == index(before: subrange.upperBound) {
|
||||||
|
while low < high {
|
||||||
|
formIndex(after: &low)
|
||||||
|
guard try !areInIncreasingOrder(pivot, self[low]) else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
repeat {
|
||||||
|
formIndex(after: &low)
|
||||||
|
} while try !areInIncreasingOrder(pivot, self[low])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Swap remaining elements as needed.
|
||||||
|
while low < high {
|
||||||
|
swapAt(low, high)
|
||||||
|
repeat {
|
||||||
|
formIndex(before: &high)
|
||||||
|
} while try areInIncreasingOrder(pivot, self[high])
|
||||||
|
repeat {
|
||||||
|
formIndex(after: &low)
|
||||||
|
} while try !areInIncreasingOrder(pivot, self[low])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move pivot to end of lower partition and return its position.
|
||||||
|
swapAt(subrange.lowerBound, high)
|
||||||
|
return high
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
public // testing
|
||||||
|
mutating func _partitionRight(
|
||||||
|
subrange: Range<Index>,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows -> (Index, alreadyPartitioned: Bool) {
|
||||||
|
assert(distance(from: subrange.lowerBound, to: subrange.upperBound) >= 2)
|
||||||
|
|
||||||
|
var (low, high) = (subrange.lowerBound, subrange.upperBound)
|
||||||
|
let pivot = self[subrange.lowerBound]
|
||||||
|
|
||||||
|
// Search from start for first element greater than or equal to pivot.
|
||||||
|
repeat {
|
||||||
|
formIndex(after: &low)
|
||||||
|
} while try areInIncreasingOrder(self[low], pivot)
|
||||||
|
|
||||||
|
// Search from end for last element less than pivot.
|
||||||
|
if low == index(after: subrange.lowerBound) {
|
||||||
|
while low < high {
|
||||||
|
formIndex(before: &high)
|
||||||
|
guard try !areInIncreasingOrder(self[high], pivot) else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
repeat {
|
||||||
|
formIndex(before: &high)
|
||||||
|
} while try !areInIncreasingOrder(self[high], pivot)
|
||||||
|
}
|
||||||
|
|
||||||
|
let noSwapsRequired = low >= high
|
||||||
|
|
||||||
|
// Swap remaining elements as needed.
|
||||||
|
while low < high {
|
||||||
|
swapAt(low, high)
|
||||||
|
repeat {
|
||||||
|
formIndex(after: &low)
|
||||||
|
} while try areInIncreasingOrder(self[low], pivot)
|
||||||
|
repeat {
|
||||||
|
formIndex(before: &high)
|
||||||
|
} while try !areInIncreasingOrder(self[high], pivot)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move pivot to start of upper partition and return its position.
|
||||||
|
let result = index(before: low)
|
||||||
|
swapAt(subrange.lowerBound, result)
|
||||||
|
return (result, noSwapsRequired)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension MutableCollection where Self: RandomAccessCollection {
|
||||||
|
@inlinable
|
||||||
|
public // testing
|
||||||
|
mutating func _preparePivot(
|
||||||
|
subrange: Range<Index>,
|
||||||
|
count: Int,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows {
|
||||||
|
let low = subrange.lowerBound
|
||||||
|
let high = index(before: subrange.upperBound)
|
||||||
|
let mid = index(low, offsetBy: count / 2)
|
||||||
|
|
||||||
|
if count > _medianOfMediansMinCount {
|
||||||
|
// Sort three "letter groups" of elements:
|
||||||
|
// [A1, B1, C1, ..., A2, B2, C2, ..., C3, B3, A3]
|
||||||
|
let low2 = index(after: low)
|
||||||
|
let high2 = index(before: high)
|
||||||
|
let midLo = index(before: mid)
|
||||||
|
let midHi = index(after: mid)
|
||||||
|
try _sort3(low, midLo, high, by: areInIncreasingOrder)
|
||||||
|
try _sort3(low2, mid, high2, by: areInIncreasingOrder)
|
||||||
|
try _sort3(
|
||||||
|
index(after: low2), midHi, index(before: high2),
|
||||||
|
by: areInIncreasingOrder)
|
||||||
|
// After sorting, each lettered group of elements is sorted, so the
|
||||||
|
// middle three elements are the three medians. Sort those three.
|
||||||
|
try _sort3(midLo, mid, midHi, by: areInIncreasingOrder)
|
||||||
|
|
||||||
|
// The median of medians is the pivot; move it to the front.
|
||||||
|
swapAt(low, mid)
|
||||||
|
} else {
|
||||||
|
// Note the order of index parameters; the smallest element will be at `mid`,
|
||||||
|
// the middle element at `low`, and the largest element at `high`:
|
||||||
|
try _sort3(mid, low, high, by: areInIncreasingOrder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
internal mutating func _shuffleElements(subrange: Range<Index>, count: Int) {
|
||||||
|
guard count > _insertionSortMaxCount else { return }
|
||||||
|
|
||||||
|
var low = subrange.lowerBound
|
||||||
|
var lowQuarter = index(low, offsetBy: count / 4)
|
||||||
|
var high = index(before: subrange.upperBound)
|
||||||
|
var highQuarter = index(high, offsetBy: -count / 4)
|
||||||
|
swapAt(low, lowQuarter)
|
||||||
|
swapAt(high, highQuarter)
|
||||||
|
|
||||||
|
if count > _insertionSortMaxCount {
|
||||||
|
for _ in 0..<2 {
|
||||||
|
formIndex(after: &low)
|
||||||
|
formIndex(before: &lowQuarter)
|
||||||
|
formIndex(before: &high)
|
||||||
|
formIndex(after: &highQuarter)
|
||||||
|
swapAt(low, lowQuarter)
|
||||||
|
swapAt(high, highQuarter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
internal mutating func _pdqSortImpl(
|
||||||
|
subrange: Range<Index>,
|
||||||
|
count: Int,
|
||||||
|
isLeftmost: Bool,
|
||||||
|
remainingBadPartitions: Int,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows {
|
||||||
|
guard count > _insertionSortMaxCount else {
|
||||||
|
try _insertionSort(within: subrange, by: areInIncreasingOrder)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try _preparePivot(subrange: subrange, count: count, by: areInIncreasingOrder)
|
||||||
|
|
||||||
|
if !isLeftmost {
|
||||||
|
let predecessor = index(before: subrange.lowerBound)
|
||||||
|
if try !areInIncreasingOrder(self[predecessor], self[subrange.lowerBound]) {
|
||||||
|
let newLow = try _partitionLeft(subrange: subrange, by: areInIncreasingOrder)
|
||||||
|
try _pdqSortImpl(
|
||||||
|
subrange: newLow..<subrange.upperBound,
|
||||||
|
count: distance(from: newLow, to: subrange.upperBound),
|
||||||
|
isLeftmost: false,
|
||||||
|
remainingBadPartitions: remainingBadPartitions,
|
||||||
|
by: areInIncreasingOrder)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let (pivot, alreadyPartitioned) = try _partitionRight(
|
||||||
|
subrange: subrange, by: areInIncreasingOrder)
|
||||||
|
|
||||||
|
let startOfUpper = index(after: pivot)
|
||||||
|
let lowerCount = distance(from: subrange.lowerBound, to: pivot)
|
||||||
|
let upperCount = count - lowerCount - 1
|
||||||
|
let minPartitionSize = count / 8
|
||||||
|
let isBadPartition = lowerCount < minPartitionSize || upperCount < minPartitionSize
|
||||||
|
|
||||||
|
var remainingBadPartitions = remainingBadPartitions
|
||||||
|
if isBadPartition {
|
||||||
|
// If we've had too many bad partitions, fall back to heap sort.
|
||||||
|
remainingBadPartitions -= 1
|
||||||
|
guard remainingBadPartitions > 0 else {
|
||||||
|
try _heapSort(within: subrange, by: areInIncreasingOrder)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try shuffling elements within the partition to decrease chance of poor
|
||||||
|
// pivot selection.
|
||||||
|
_shuffleElements(subrange: subrange.lowerBound..<pivot, count: lowerCount)
|
||||||
|
_shuffleElements(subrange: startOfUpper..<subrange.upperBound, count: upperCount)
|
||||||
|
} else {
|
||||||
|
// If there were no swaps required, use insertion sort to scan for already
|
||||||
|
// sorted partitions, including some minor fix-ups.
|
||||||
|
if alreadyPartitioned {
|
||||||
|
if try _partialInsertionSort(within: subrange.lowerBound..<pivot, by: areInIncreasingOrder)
|
||||||
|
&& _partialInsertionSort(within: startOfUpper..<subrange.upperBound, by: areInIncreasingOrder) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try _pdqSortImpl(
|
||||||
|
subrange: subrange.lowerBound..<pivot,
|
||||||
|
count: lowerCount,
|
||||||
|
isLeftmost: isLeftmost,
|
||||||
|
remainingBadPartitions: remainingBadPartitions,
|
||||||
|
by: areInIncreasingOrder)
|
||||||
|
try _pdqSortImpl(
|
||||||
|
subrange: startOfUpper..<subrange.upperBound,
|
||||||
|
count: upperCount,
|
||||||
|
isLeftmost: false,
|
||||||
|
remainingBadPartitions: remainingBadPartitions,
|
||||||
|
by: areInIncreasingOrder)
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
public mutating func sortUnstable(
|
||||||
|
subrange: Range<Index>,
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows {
|
||||||
|
let count = distance(from: subrange.lowerBound, to: subrange.upperBound)
|
||||||
|
try _pdqSortImpl(
|
||||||
|
subrange: subrange,
|
||||||
|
count: count,
|
||||||
|
isLeftmost: true,
|
||||||
|
remainingBadPartitions: count._log2,
|
||||||
|
by: areInIncreasingOrder)
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
public mutating func sortUnstable(
|
||||||
|
by areInIncreasingOrder: (Element, Element) throws -> Bool
|
||||||
|
) rethrows {
|
||||||
|
try sortUnstable(subrange: startIndex..<endIndex, by: areInIncreasingOrder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension MutableCollection where Self: RandomAccessCollection, Element: Comparable {
|
||||||
|
@inlinable
|
||||||
|
public mutating func sortUnstable(subrange: Range<Index>) {
|
||||||
|
sortUnstable(subrange: subrange, by: <)
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
public mutating func sortUnstable() {
|
||||||
|
sortUnstable(by: <)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -210,8 +210,8 @@ extension Collection {
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
extension Sequence {
|
extension Sequence {
|
||||||
/// Returns two arrays containing, in order, the elements of the sequence that
|
/// Returns two arrays containing the elements of the sequence that
|
||||||
/// do and don’t satisfy the given predicate.
|
/// don’t and do satisfy the given predicate, respectively.
|
||||||
///
|
///
|
||||||
/// In this example, `partitioned(by:)` is used to separate the input based on
|
/// In this example, `partitioned(by:)` is used to separate the input based on
|
||||||
/// whether a name is shorter than five characters:
|
/// whether a name is shorter than five characters:
|
||||||
@@ -230,7 +230,9 @@ extension Sequence {
|
|||||||
///
|
///
|
||||||
/// - Returns: Two arrays with all of the elements of the receiver. The
|
/// - Returns: Two arrays with all of the elements of the receiver. The
|
||||||
/// first array contains all the elements that `predicate` didn’t allow, and
|
/// first array contains all the elements that `predicate` didn’t allow, and
|
||||||
/// the second array contains all the elements that `predicate` allowed.
|
/// the second array contains all the elements that `predicate` allowed. The
|
||||||
|
/// order of the elements in the arrays matches the order of the elements in
|
||||||
|
/// the original sequence.
|
||||||
///
|
///
|
||||||
/// - Complexity: O(*n*), where *n* is the length of the sequence.
|
/// - Complexity: O(*n*), where *n* is the length of the sequence.
|
||||||
@inlinable
|
@inlinable
|
||||||
@@ -253,8 +255,8 @@ extension Sequence {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension Collection {
|
extension Collection {
|
||||||
/// Returns two arrays containing, in order, the elements of the collection
|
/// Returns two arrays containing the elements of the collection that
|
||||||
/// that do and don’t satisfy the given predicate.
|
/// don’t and do satisfy the given predicate, respectively.
|
||||||
///
|
///
|
||||||
/// In this example, `partitioned(by:)` is used to separate the input based on
|
/// In this example, `partitioned(by:)` is used to separate the input based on
|
||||||
/// whether a name is shorter than five characters.
|
/// whether a name is shorter than five characters.
|
||||||
@@ -273,7 +275,9 @@ extension Collection {
|
|||||||
///
|
///
|
||||||
/// - Returns: Two arrays with all of the elements of the receiver. The
|
/// - Returns: Two arrays with all of the elements of the receiver. The
|
||||||
/// first array contains all the elements that `predicate` didn’t allow, and
|
/// first array contains all the elements that `predicate` didn’t allow, and
|
||||||
/// the second array contains all the elements that `predicate` allowed.
|
/// the second array contains all the elements that `predicate` allowed. The
|
||||||
|
/// order of the elements in the arrays matches the order of the elements in
|
||||||
|
/// the original collection.
|
||||||
///
|
///
|
||||||
/// - Complexity: O(*n*), where *n* is the length of the collection.
|
/// - Complexity: O(*n*), where *n* is the length of the collection.
|
||||||
@inlinable
|
@inlinable
|
||||||
|
|||||||
@@ -10,8 +10,14 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
// For log(_:) and root(_:_:)
|
// For log(_:) and root(_:_:)
|
||||||
|
#if swift(>=5.11)
|
||||||
|
internal import RealModule
|
||||||
|
#elseif swift(>=5.10)
|
||||||
|
import RealModule
|
||||||
|
#else
|
||||||
@_implementationOnly
|
@_implementationOnly
|
||||||
import RealModule
|
import RealModule
|
||||||
|
#endif
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
// randomStableSample(count:)
|
// randomStableSample(count:)
|
||||||
|
|||||||
@@ -512,7 +512,7 @@ extension SplitCollection: Collection {
|
|||||||
var subsequenceStart = i.baseRange.upperBound
|
var subsequenceStart = i.baseRange.upperBound
|
||||||
if subsequenceStart < base.endIndex {
|
if subsequenceStart < base.endIndex {
|
||||||
// If we're not already at the end of the base collection, the previous
|
// If we're not already at the end of the base collection, the previous
|
||||||
// susequence ended with a separator. Start searching for the next
|
// subsequence ended with a separator. Start searching for the next
|
||||||
// subsequence at the following element.
|
// subsequence at the following element.
|
||||||
subsequenceStart = base.index(after: i.baseRange.upperBound)
|
subsequenceStart = base.index(after: i.baseRange.upperBound)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,57 +15,63 @@ import Algorithms
|
|||||||
final class CombinationsTests: XCTestCase {
|
final class CombinationsTests: XCTestCase {
|
||||||
func testCount() {
|
func testCount() {
|
||||||
let c = "ABCD"
|
let c = "ABCD"
|
||||||
|
|
||||||
let c0 = c.combinations(ofCount: 0).count
|
/// XCTAsserts that `x`'s `count` and `underestimatedCount` are both `l` at
|
||||||
XCTAssertEqual(c0, 1)
|
/// the given `file` and `line`.
|
||||||
|
func check(
|
||||||
let c1 = c.combinations(ofCount: 1).count
|
_ x: CombinationsSequence<String>, countsAre l: Int,
|
||||||
XCTAssertEqual(c1, 4)
|
file: StaticString, line: UInt)
|
||||||
|
{
|
||||||
let c2 = c.combinations(ofCount: 2).count
|
XCTAssertEqual(x.count, l, "unexpected count", file: file, line: line)
|
||||||
XCTAssertEqual(c2, 6)
|
XCTAssertEqual(
|
||||||
|
x.underestimatedCount, l, "unexpected underestimatedCount",
|
||||||
let c3 = c.combinations(ofCount: 3).count
|
file: file, line: line)
|
||||||
XCTAssertEqual(c3, 4)
|
}
|
||||||
|
|
||||||
let c4 = c.combinations(ofCount: 4).count
|
/// XCTAsserts that the `count` and `underestimatedCount` of
|
||||||
XCTAssertEqual(c4, 1)
|
/// `c.combinations(ofCount: l)` are both `n` at the given `file` and
|
||||||
|
/// `line`.
|
||||||
let c5 = c.combinations(ofCount: 0...0).count
|
func check(
|
||||||
XCTAssertEqual(c5, 1)
|
cHas n: Int,
|
||||||
|
combinationsOfLength l: Int,
|
||||||
let c6 = c.combinations(ofCount: 1...1).count
|
file: StaticString = #filePath, line: UInt = #line)
|
||||||
XCTAssertEqual(c6, 4)
|
{
|
||||||
|
check(c.combinations(ofCount: l), countsAre: n, file: file, line: line)
|
||||||
let c7 = c.combinations(ofCount: 1...2).count
|
}
|
||||||
XCTAssertEqual(c7, 10)
|
|
||||||
|
/// XCTAsserts that the `count` and `underestimatedCount` of
|
||||||
let c8 = c.combinations(ofCount: 1...3).count
|
/// `c.combinations(ofCount: l)` are both `n` at the given `file` and
|
||||||
XCTAssertEqual(c8, 14)
|
/// `line`.
|
||||||
|
func check<R: RangeExpression>(
|
||||||
let c9 = c.combinations(ofCount: 2...4).count
|
cHas n: Int,
|
||||||
XCTAssertEqual(c9, 11)
|
combinationsOfLengths l: R,
|
||||||
|
file: StaticString = #filePath, line: UInt = #line) where R.Bound == Int
|
||||||
|
{
|
||||||
|
check(c.combinations(ofCount: l), countsAre: n, file: file, line: line)
|
||||||
|
}
|
||||||
|
|
||||||
|
check(cHas: 1, combinationsOfLength: 0)
|
||||||
|
check(cHas: 4, combinationsOfLength: 1)
|
||||||
|
check(cHas: 6, combinationsOfLength: 2)
|
||||||
|
check(cHas: 1, combinationsOfLength: 4)
|
||||||
|
|
||||||
|
check(cHas: 1, combinationsOfLengths: 0...0)
|
||||||
|
check(cHas: 4, combinationsOfLengths: 1...1)
|
||||||
|
check(cHas: 10, combinationsOfLengths: 1...2)
|
||||||
|
check(cHas: 14, combinationsOfLengths: 1...3)
|
||||||
|
check(cHas: 11, combinationsOfLengths: 2...4)
|
||||||
|
|
||||||
|
// `k` greater than element count results in same number of combinations
|
||||||
|
check(cHas: 5, combinationsOfLengths: 3...10)
|
||||||
|
|
||||||
// `k` greater than element count results in same number of combinations
|
// `k` greater than element count results in same number of combinations
|
||||||
let c10 = c.combinations(ofCount: 3...10).count
|
check(cHas: 1, combinationsOfLengths: 4...10)
|
||||||
XCTAssertEqual(c10, 5)
|
|
||||||
|
|
||||||
// `k` greater than element count results in same number of combinations
|
|
||||||
let c11 = c.combinations(ofCount: 4...10).count
|
|
||||||
XCTAssertEqual(c11, 1)
|
|
||||||
|
|
||||||
// `k` entirely greater than element count results in no combinations
|
// `k` entirely greater than element count results in no combinations
|
||||||
let c12 = c.combinations(ofCount: 5...10).count
|
check(cHas: 0, combinationsOfLengths: 5...10)
|
||||||
XCTAssertEqual(c12, 0)
|
|
||||||
|
|
||||||
let c13 = c.combinations(ofCount: 0...).count
|
check(cHas: 16, combinationsOfLengths: 0...)
|
||||||
XCTAssertEqual(c13, 16)
|
check(cHas: 15, combinationsOfLengths: ...3)
|
||||||
|
|
||||||
let c14 = c.combinations(ofCount: ...3).count
|
|
||||||
XCTAssertEqual(c14, 15)
|
|
||||||
|
|
||||||
let c15 = c.combinations(ofCount: 0...).count
|
|
||||||
XCTAssertEqual(c15, 16)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testCombinations() {
|
func testCombinations() {
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This source file is part of the Swift Algorithms open source project
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 Apple Inc. and the Swift project authors
|
||||||
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||||
|
//
|
||||||
|
// See https://swift.org/LICENSE.txt for license information
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
import XCTest
|
||||||
|
import Algorithms
|
||||||
|
|
||||||
|
final class PDQSortTests: XCTestCase {
|
||||||
|
func testPartitionLeft() {
|
||||||
|
for _ in 1...1000 {
|
||||||
|
var a = (0...20).map { _ in Int.random(in: 1...5) }
|
||||||
|
a._preparePivot(subrange: a.indices, count: a.indices.count, by: <)
|
||||||
|
let pivot = a._partitionLeft(subrange: a.indices, by: <)
|
||||||
|
for x in a[..<pivot] {
|
||||||
|
XCTAssertLessThanOrEqual(x, a[pivot])
|
||||||
|
}
|
||||||
|
for x in a[(pivot + 1)...] {
|
||||||
|
XCTAssertGreaterThan(x, a[pivot])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testPartitionRight() {
|
||||||
|
for _ in 1...1000 {
|
||||||
|
var a = (0...20).map { _ in Int.random(in: 1...5) }
|
||||||
|
a._preparePivot(subrange: a.indices, count: a.indices.count, by: <)
|
||||||
|
let (pivot, _) = a._partitionRight(subrange: a.indices, by: <)
|
||||||
|
for x in a[..<pivot] {
|
||||||
|
XCTAssertLessThan(x, a[pivot])
|
||||||
|
}
|
||||||
|
for x in a[(pivot + 1)...] {
|
||||||
|
XCTAssertGreaterThanOrEqual(x, a[pivot])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testSort() {
|
||||||
|
var empty: [Int] = []
|
||||||
|
empty.sortUnstable()
|
||||||
|
|
||||||
|
var inOrder = Array(1...1000)
|
||||||
|
inOrder.sortUnstable()
|
||||||
|
XCTAssert(inOrder.isSorted())
|
||||||
|
|
||||||
|
var reversed = Array((1...1000).reversed())
|
||||||
|
reversed.sortUnstable()
|
||||||
|
XCTAssert(reversed.isSorted())
|
||||||
|
|
||||||
|
for _ in 1...1000 {
|
||||||
|
var a = (0...Int.random(in: 100...1000)).map { _ in Int.random(in: 1...50) }
|
||||||
|
a.sortUnstable()
|
||||||
|
XCTAssert(a.isSorted())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testSpecialCase() {
|
||||||
|
var a = (1...10).shuffled() + [20] + (31...40).shuffled()
|
||||||
|
a.sortUnstable()
|
||||||
|
XCTAssert(a.isSorted())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -216,7 +216,7 @@ extension IndexValidator {
|
|||||||
/// - expectedCount:
|
/// - expectedCount:
|
||||||
///
|
///
|
||||||
/// - Complexity: O(*n*^3) where *n* is the length of the collection if the
|
/// - Complexity: O(*n*^3) where *n* is the length of the collection if the
|
||||||
/// collection conforms to `RandomAccesCollection`, otherwise O(*n*^4).
|
/// collection conforms to `RandomAccessCollection`, otherwise O(*n*^4).
|
||||||
func validate(
|
func validate(
|
||||||
_ collection: C,
|
_ collection: C,
|
||||||
expectedCount: Int? = nil,
|
expectedCount: Int? = nil,
|
||||||
|
|||||||
Reference in new issue
Block a user