From 0dba0e5b87b137905d72a97f3284b173b7a0cab0 Mon Sep 17 00:00:00 2001 From: Meler Paine Date: Wed, 21 Oct 2020 02:16:12 +0800 Subject: [PATCH] fix stablePartition(subrange:by:) using subrange count instead of entire collection count. (#30) --- Sources/Algorithms/Partition.swift | 2 +- Tests/SwiftAlgorithmsTests/PartitionTests.swift | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Sources/Algorithms/Partition.swift b/Sources/Algorithms/Partition.swift index 0454c5d..0bccb3a 100644 --- a/Sources/Algorithms/Partition.swift +++ b/Sources/Algorithms/Partition.swift @@ -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) } diff --git a/Tests/SwiftAlgorithmsTests/PartitionTests.swift b/Tests/SwiftAlgorithmsTests/PartitionTests.swift index a65bcc3..ad8ba8e 100644 --- a/Tests/SwiftAlgorithmsTests/PartitionTests.swift +++ b/Tests/SwiftAlgorithmsTests/PartitionTests.swift @@ -61,6 +61,21 @@ final class PartitionTests: XCTestCase { } } + func testStablePartitionWithSubrange() { + for length in 10...20 { + let a = Array(0.. 0 ? 1 : 0) + XCTAssertEqualSequences(b[partitionRange.lowerBound..