mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-22 04:10:39 +00:00
fix stablePartition(subrange:by:) using subrange count instead of entire collection count. (#30)
This commit is contained in:
1 parent
a0b7954f80
commit
0dba0e5b87
2 files changed
+16
-1
No files matched your search
@@ -61,7 +61,7 @@ extension MutableCollection {
|
||||
by belongsInSecondPartition: (Element) throws-> Bool
|
||||
) rethrows -> Index {
|
||||
try stablePartition(
|
||||
count: count,
|
||||
count: self[subrange].count,
|
||||
subrange: subrange,
|
||||
by: belongsInSecondPartition)
|
||||
}
|
||||
|
||||
@@ -61,6 +61,21 @@ final class PartitionTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testStablePartitionWithSubrange() {
|
||||
for length in 10...20 {
|
||||
let a = Array(0..<length)
|
||||
for j in 0..<length {
|
||||
var b = a
|
||||
let partitionRange = 0..<j
|
||||
let condition = { $0 < j - 1 }
|
||||
let p = b.stablePartition(subrange: partitionRange, by: condition)
|
||||
XCTAssertEqual(p, partitionRange.count > 0 ? 1 : 0)
|
||||
XCTAssertEqualSequences(b[partitionRange.lowerBound..<p], a[partitionRange].filter { !condition($0) })
|
||||
XCTAssertEqualSequences(b[p..<partitionRange.upperBound], a[partitionRange].filter(condition))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testPartitioningIndex() {
|
||||
for i in 0..<7 {
|
||||
for j in i..<11 {
|
||||
|
||||
Reference in new issue
Block a user