mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[stdlib][NFC] Make partition(by:)’s implementation easier to follow
Also, fix a case in the description of the loop invariant
This commit is contained in:
@@ -401,35 +401,30 @@ extension MutableCollection where Self: BidirectionalCollection {
|
|||||||
) rethrows -> Index {
|
) rethrows -> Index {
|
||||||
var lo = startIndex
|
var lo = startIndex
|
||||||
var hi = endIndex
|
var hi = endIndex
|
||||||
|
while true {
|
||||||
|
// Invariants at this point:
|
||||||
|
//
|
||||||
|
// * `lo <= hi`
|
||||||
|
// * all elements in `startIndex ..< lo` belong in the first partition
|
||||||
|
// * all elements in `hi ..< endIndex` belong in the second partition
|
||||||
|
|
||||||
// 'Loop' invariants (at start of Loop, all are true):
|
// Find next element from `lo` that may not be in the right place.
|
||||||
// * lo < hi
|
while true {
|
||||||
// * predicate(self[i]) == false, for i in startIndex ..< lo
|
guard lo < hi else { return lo }
|
||||||
// * predicate(self[i]) == true, for i in hi ..< endIndex
|
if try belongsInSecondPartition(self[lo]) { break }
|
||||||
|
formIndex(after: &lo)
|
||||||
|
}
|
||||||
|
|
||||||
Loop: while true {
|
// Find next element down from `hi` that we can swap `lo` with.
|
||||||
FindLo: repeat {
|
while true {
|
||||||
while lo < hi {
|
|
||||||
if try belongsInSecondPartition(self[lo]) { break FindLo }
|
|
||||||
formIndex(after: &lo)
|
|
||||||
}
|
|
||||||
break Loop
|
|
||||||
} while false
|
|
||||||
|
|
||||||
FindHi: repeat {
|
|
||||||
formIndex(before: &hi)
|
formIndex(before: &hi)
|
||||||
while lo < hi {
|
guard lo < hi else { return lo }
|
||||||
if try !belongsInSecondPartition(self[hi]) { break FindHi }
|
if try !belongsInSecondPartition(self[hi]) { break }
|
||||||
formIndex(before: &hi)
|
}
|
||||||
}
|
|
||||||
break Loop
|
|
||||||
} while false
|
|
||||||
|
|
||||||
swapAt(lo, hi)
|
swapAt(lo, hi)
|
||||||
formIndex(after: &lo)
|
formIndex(after: &lo)
|
||||||
}
|
}
|
||||||
|
|
||||||
return lo
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user