Skip to content

Commit 0dba0e5

Browse files
authored
fix stablePartition(subrange:by:) using subrange count instead of entire collection count. (apple#30)
1 parent a0b7954 commit 0dba0e5

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Sources/Algorithms/Partition.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extension MutableCollection {
6161
by belongsInSecondPartition: (Element) throws-> Bool
6262
) rethrows -> Index {
6363
try stablePartition(
64-
count: count,
64+
count: self[subrange].count,
6565
subrange: subrange,
6666
by: belongsInSecondPartition)
6767
}

Tests/SwiftAlgorithmsTests/PartitionTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ final class PartitionTests: XCTestCase {
6161
}
6262
}
6363

64+
func testStablePartitionWithSubrange() {
65+
for length in 10...20 {
66+
let a = Array(0..<length)
67+
for j in 0..<length {
68+
var b = a
69+
let partitionRange = 0..<j
70+
let condition = { $0 < j - 1 }
71+
let p = b.stablePartition(subrange: partitionRange, by: condition)
72+
XCTAssertEqual(p, partitionRange.count > 0 ? 1 : 0)
73+
XCTAssertEqualSequences(b[partitionRange.lowerBound..<p], a[partitionRange].filter { !condition($0) })
74+
XCTAssertEqualSequences(b[p..<partitionRange.upperBound], a[partitionRange].filter(condition))
75+
}
76+
}
77+
}
78+
6479
func testPartitioningIndex() {
6580
for i in 0..<7 {
6681
for j in i..<11 {

0 commit comments

Comments
 (0)