mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-26 04:27:48 +00:00
Rename sortedPrefix methods (#77)
This renames sortedPrefix to min(count:), adds max(count:), and moves both methods down to Sequence. In order to still provide the optimization when `count` is close to a collection's length, we include a Collection-specific overload of all methods. These won't be accessed when in a Sequence-generic context, but it's the best we can do without adding these methods as protocol requirements.
This commit is contained in:
1 parent
32837b9ad6
commit
bd64bef3d2
7 files changed
+631
-449
No files matched your search
@@ -0,0 +1,187 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 SortedPrefixTests: XCTestCase {
|
||||
func testMinCount() {
|
||||
// Replacement at start and end
|
||||
let input = [10, 11, 12, 1, 13, 2, 14, 3, 4, 5, 6, 0, 7, 8, 9]
|
||||
let min = input.min(count: 5)
|
||||
XCTAssertEqualSequences(min, 0..<5)
|
||||
|
||||
// Stability with all equal values
|
||||
let maxZeroes = Array(repeating: 0, count: 100)
|
||||
.enumerated()
|
||||
.max(count: 5, sortedBy: { $0.element < $1.element })
|
||||
XCTAssertEqualSequences(maxZeroes.map { $0.offset }, 95..<100)
|
||||
}
|
||||
|
||||
func testMaxCount() {
|
||||
// Replacement at start and end
|
||||
let input = [0, 11, 12, 1, 13, 2, 14, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
let max = input.max(count: 5)
|
||||
XCTAssertEqualSequences(max, 10..<15)
|
||||
|
||||
// Stability with all equal values
|
||||
let maxZeroes = Array(repeating: 0, count: 100)
|
||||
.enumerated()
|
||||
.max(count: 5, sortedBy: { $0.element < $1.element })
|
||||
XCTAssertEqualSequences(maxZeroes.map { $0.offset }, 95..<100)
|
||||
}
|
||||
|
||||
func testEmpty() {
|
||||
let array = [Int]()
|
||||
XCTAssertEqual(array.min(count: 0), [])
|
||||
XCTAssertEqual(array.min(count: 100), [])
|
||||
|
||||
XCTAssertEqual(array.max(count: 0), [])
|
||||
XCTAssertEqual(array.max(count: 100), [])
|
||||
}
|
||||
|
||||
func testMinMaxSequences() {
|
||||
let input = (1...100).shuffled().interspersed(with: 50)
|
||||
let seq = AnySequence(input)
|
||||
|
||||
XCTAssertEqualSequences(seq.min(count: 0), [])
|
||||
XCTAssertEqualSequences(seq.min(count: 5), 1...5)
|
||||
|
||||
XCTAssertEqualSequences(seq.max(count: 0), [])
|
||||
XCTAssertEqualSequences(seq.max(count: 5), 96...100)
|
||||
}
|
||||
|
||||
func testSortedPrefixComparable() {
|
||||
let array: [Int] = [20, 1, 4, 70, 100, 2, 3, 7, 90]
|
||||
|
||||
XCTAssertEqual(array.min(count: 0), [])
|
||||
XCTAssertEqual(array.min(count: 1), [1])
|
||||
XCTAssertEqualSequences(array.min(count: 5), array.sorted().prefix(5))
|
||||
XCTAssertEqual(array.min(count: 10), array.sorted())
|
||||
}
|
||||
|
||||
func testMinMaxWithHugeCount() {
|
||||
XCTAssertEqual([4, 2, 1, 3].min(count: .max), [1, 2, 3, 4])
|
||||
XCTAssertEqual([4, 2, 1, 3].max(count: .max), [1, 2, 3, 4])
|
||||
}
|
||||
|
||||
func testMinCountWithHugeInput() {
|
||||
let range = 1...1000
|
||||
let input = range.shuffled()
|
||||
|
||||
XCTAssertEqual(input.min(count: 0, sortedBy: <), [])
|
||||
XCTAssertEqual(input.min(count: 0, sortedBy: >), [])
|
||||
|
||||
XCTAssertEqual(input.min(count: 1, sortedBy: <), [range.lowerBound])
|
||||
XCTAssertEqual(input.min(count: 1, sortedBy: >), [range.upperBound])
|
||||
|
||||
XCTAssertEqualSequences(input.min(count: 5, sortedBy: <), range.prefix(5))
|
||||
XCTAssertEqualSequences(
|
||||
input.min(count: 5, sortedBy: >),
|
||||
range.suffix(5).reversed())
|
||||
|
||||
XCTAssertEqualSequences(
|
||||
input.min(count: 500, sortedBy: <),
|
||||
range.prefix(500))
|
||||
XCTAssertEqualSequences(
|
||||
input.min(count: 500, sortedBy: >),
|
||||
range.suffix(500).reversed())
|
||||
|
||||
XCTAssertEqualSequences(input.min(count: 1000, sortedBy: <), range)
|
||||
XCTAssertEqualSequences(
|
||||
input.min(count: 1000, sortedBy: >),
|
||||
range.reversed())
|
||||
}
|
||||
|
||||
func testMaxCountWithHugeInput() {
|
||||
let range = 1...1000
|
||||
let input = range.shuffled()
|
||||
|
||||
XCTAssertEqual(input.max(count: 0, sortedBy: <), [])
|
||||
XCTAssertEqual(input.max(count: 0, sortedBy: >), [])
|
||||
|
||||
XCTAssertEqual(input.max(count: 1, sortedBy: <), [range.upperBound])
|
||||
XCTAssertEqual(input.max(count: 1, sortedBy: >), [range.lowerBound])
|
||||
|
||||
XCTAssertEqualSequences(input.max(count: 5, sortedBy: <), range.suffix(5))
|
||||
XCTAssertEqualSequences(
|
||||
input.max(count: 5, sortedBy: >),
|
||||
range.prefix(5).reversed())
|
||||
|
||||
XCTAssertEqualSequences(
|
||||
input.max(count: 500, sortedBy: <),
|
||||
range.suffix(500))
|
||||
XCTAssertEqualSequences(
|
||||
input.max(count: 500, sortedBy: >),
|
||||
range.prefix(500).reversed())
|
||||
|
||||
XCTAssertEqualSequences(input.max(count: 1000, sortedBy: <), range)
|
||||
XCTAssertEqualSequences(
|
||||
input.max(count: 1000, sortedBy: >),
|
||||
range.reversed())
|
||||
}
|
||||
|
||||
func testStability() {
|
||||
assertStability([1,1,1,2,5,7,3,6,2,5,7,3,6], withCount: 3)
|
||||
assertStability([1,1,1,2,5,7,3,6,2,5,7,3,6], withCount: 6)
|
||||
assertStability([1,1,1,2,5,7,3,6,2,5,7,3,6], withCount: 20)
|
||||
assertStability([1,1,1,2,5,7,3,6,2,5,7,3,6], withCount: 1000)
|
||||
assertStability(Array(repeating: 0, count: 100), withCount: 0)
|
||||
assertStability(Array(repeating: 0, count: 100), withCount: 1)
|
||||
assertStability(Array(repeating: 0, count: 100), withCount: 2)
|
||||
assertStability(Array(repeating: 0, count: 100), withCount: 5)
|
||||
assertStability(Array(repeating: 0, count: 100), withCount: 20)
|
||||
assertStability(Array(repeating: 0, count: 100), withCount: 100)
|
||||
assertStability(Array(repeating: 1, count: 50) + Array(repeating: 0, count: 50), withCount: 2)
|
||||
assertStability(Array(repeating: 1, count: 50) + Array(repeating: 0, count: 50), withCount: 5)
|
||||
assertStability(Array(repeating: 1, count: 50) + Array(repeating: 0, count: 50), withCount: 20)
|
||||
assertStability(Array(repeating: 1, count: 50) + Array(repeating: 0, count: 50), withCount: 50)
|
||||
assertStability([0,0], withCount: 1)
|
||||
assertStability([0,0], withCount: 2)
|
||||
assertStability([0,1,0,1,0,1], withCount: 2)
|
||||
assertStability([0,1,0,1,0,1], withCount: 6)
|
||||
assertStability([0,0,0,1,1,1], withCount: 1)
|
||||
assertStability([0,0,0,1,1,1], withCount: 3)
|
||||
assertStability([0,0,0,1,1,1], withCount: 4)
|
||||
assertStability([0,0,0,1,1,1], withCount: 6)
|
||||
assertStability([1,1,1,0,0,0], withCount: 1)
|
||||
assertStability([1,1,1,0,0,0], withCount: 3)
|
||||
assertStability([1,1,1,0,0,0], withCount: 4)
|
||||
assertStability([1,1,1,0,0,0], withCount: 6)
|
||||
assertStability([1,1,1,0,0,0], withCount: 5)
|
||||
}
|
||||
|
||||
func assertStability(
|
||||
_ actual: [Int],
|
||||
withCount count: Int
|
||||
) {
|
||||
func stableOrder(
|
||||
a: (offset: Int, element: Int),
|
||||
b: (offset: Int, element: Int)) -> Bool
|
||||
{
|
||||
a.element == b.element
|
||||
? a.offset < b.offset
|
||||
: a.element < b.element
|
||||
}
|
||||
|
||||
do {
|
||||
let sorted = actual.enumerated()
|
||||
.min(count: count) { $0.element < $1.element }
|
||||
XCTAssert(sorted.isSorted(by: stableOrder))
|
||||
}
|
||||
|
||||
do {
|
||||
let sorted = actual.enumerated()
|
||||
.max(count: count) { $0.element < $1.element }
|
||||
XCTAssert(sorted.isSorted(by: stableOrder))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,301 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 SortedPrefixTests: XCTestCase {
|
||||
func testEmpty() {
|
||||
let array = [Int]()
|
||||
XCTAssertEqual(array.sortedPrefix(0), [])
|
||||
}
|
||||
|
||||
func testSortedPrefixWithOrdering() {
|
||||
let array: [Int] = [20, 1, 4, 70, 100, 2, 3, 7, 90]
|
||||
|
||||
XCTAssertEqual(array.sortedPrefix(0, by: >), [])
|
||||
XCTAssertEqual(
|
||||
array.sortedPrefix(1, by: >),
|
||||
[100]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
array.sortedPrefix(5, by: >),
|
||||
[100, 90, 70, 20, 7]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
array.sortedPrefix(9, by: >),
|
||||
[100, 90, 70, 20, 7, 4, 3, 2, 1]
|
||||
)
|
||||
|
||||
XCTAssertEqual([1].sortedPrefix(0, by: <), [])
|
||||
XCTAssertEqual([1].sortedPrefix(0, by: >), [])
|
||||
XCTAssertEqual([1].sortedPrefix(1, by: <), [1])
|
||||
XCTAssertEqual([1].sortedPrefix(1, by: >), [1])
|
||||
XCTAssertEqual([0, 1].sortedPrefix(1, by: <), [0])
|
||||
XCTAssertEqual([1, 0].sortedPrefix(1, by: <), [0])
|
||||
XCTAssertEqual([1, 0].sortedPrefix(2, by: <), [0, 1])
|
||||
XCTAssertEqual([0, 1].sortedPrefix(1, by: >), [1])
|
||||
XCTAssertEqual([1, 0].sortedPrefix(1, by: >), [1])
|
||||
XCTAssertEqual([1, 0].sortedPrefix(2, by: >), [1, 0])
|
||||
|
||||
XCTAssertEqual(
|
||||
[1, 2, 3, 4, 7, 20, 70, 90, 100].sortedPrefix(5, by: <),
|
||||
[1, 2, 3, 4, 7]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
[1, 2, 3, 4, 7, 20, 70, 90, 100].sortedPrefix(5, by: >),
|
||||
[100, 90, 70, 20, 7]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
[1, 2, 3, 4, 7, 20, 70, 90, 100].sortedPrefix(5, by: >),
|
||||
[100, 90, 70, 20, 7]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
[1, 2, 3, 4, 7, 20, 70, 90, 100].sortedPrefix(5, by: <),
|
||||
[1, 2, 3, 4, 7]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
[4, 5, 6, 1, 2, 3].sortedPrefix(3, by: <),
|
||||
[1, 2, 3]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
[4, 5, 9, 8, 7, 6].sortedPrefix(3, by: <),
|
||||
[4, 5, 6]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
[4, 3, 2, 1].sortedPrefix(1, by: <),
|
||||
[1]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
[4, 2, 1, 3].sortedPrefix(3, by: >),
|
||||
[4, 3, 2]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
[4, 2, 1, 3].sortedPrefix(3, by: <),
|
||||
[1, 2, 3]
|
||||
)
|
||||
}
|
||||
|
||||
func testSortedPrefixComparable() {
|
||||
let array: [Int] = [20, 1, 4, 70, 100, 2, 3, 7, 90]
|
||||
|
||||
XCTAssertEqual(array.sortedPrefix(0), [])
|
||||
|
||||
XCTAssertEqual(
|
||||
array.sortedPrefix(1),
|
||||
[1]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
array.sortedPrefix(5),
|
||||
[1, 2, 3, 4, 7]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
array.sortedPrefix(9),
|
||||
[1, 2, 3, 4, 7, 20, 70, 90, 100]
|
||||
)
|
||||
}
|
||||
|
||||
func testSortedPrefixWithHugePrefix() {
|
||||
XCTAssertEqual(
|
||||
[4, 2, 1, 3].sortedPrefix(.max),
|
||||
[1, 2, 3, 4]
|
||||
)
|
||||
}
|
||||
|
||||
func testSortedPrefixWithHugeInput() {
|
||||
let input = (1...1000).shuffled()
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(0, by: <),
|
||||
[]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(0, by: >),
|
||||
[]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(1, by: <),
|
||||
[1]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(1, by: >),
|
||||
[1000]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(5, by: <),
|
||||
[1, 2, 3, 4, 5]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(5, by: >),
|
||||
[1000, 999, 998, 997, 996]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(10, by: <),
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(10, by: >),
|
||||
[1000, 999, 998, 997, 996, 995, 994, 993, 992, 991]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(50, by: <),
|
||||
Array((1...50))
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(50, by: >),
|
||||
Array((1...1000).reversed().prefix(50))
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(250, by: <),
|
||||
Array((1...250))
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(250, by: >),
|
||||
Array((1...1000).reversed().prefix(250))
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(500, by: <),
|
||||
Array((1...500))
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(500, by: >),
|
||||
Array((1...1000).reversed().prefix(500))
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(750, by: <),
|
||||
Array((1...750))
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(750, by: >),
|
||||
Array((1...1000).reversed().prefix(750))
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(1000, by: <),
|
||||
Array((1...1000))
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
input.sortedPrefix(1000, by: >),
|
||||
(1...1000).reversed()
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
([0] + Array(repeating: 1, count: 100)).sortedPrefix(1, by: <),
|
||||
[0]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
([1] + Array(repeating: 0, count: 100)).sortedPrefix(1, by: <),
|
||||
[0]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
([0] + Array(repeating: 1, count: 100)).sortedPrefix(2, by: <),
|
||||
[0, 1]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
([1] + Array(repeating: 0, count: 100)).sortedPrefix(2, by: <),
|
||||
[0, 0]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
([1] + Array(repeating: 1, count: 100)).sortedPrefix(1, by: >),
|
||||
[1]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
([0] + Array(repeating: 1, count: 100)).sortedPrefix(1, by: >),
|
||||
[1]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
([1] + Array(repeating: 0, count: 100)).sortedPrefix(2, by: >),
|
||||
[1, 0]
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
([0] + Array(repeating: 1, count: 100)).sortedPrefix(2, by: >),
|
||||
[1, 1]
|
||||
)
|
||||
}
|
||||
|
||||
func testStability() {
|
||||
assertStability([1,1,1,2,5,7,3,6,2,5,7,3,6], withPrefix: 3)
|
||||
assertStability([1,1,1,2,5,7,3,6,2,5,7,3,6], withPrefix: 6)
|
||||
assertStability([1,1,1,2,5,7,3,6,2,5,7,3,6], withPrefix: 20)
|
||||
assertStability([1,1,1,2,5,7,3,6,2,5,7,3,6], withPrefix: 1000)
|
||||
assertStability(Array(repeating: 0, count: 100), withPrefix: 0)
|
||||
assertStability(Array(repeating: 0, count: 100), withPrefix: 1)
|
||||
assertStability(Array(repeating: 0, count: 100), withPrefix: 2)
|
||||
assertStability(Array(repeating: 0, count: 100), withPrefix: 5)
|
||||
assertStability(Array(repeating: 0, count: 100), withPrefix: 20)
|
||||
assertStability(Array(repeating: 0, count: 100), withPrefix: 100)
|
||||
assertStability(Array(repeating: 1, count: 50) + Array(repeating: 0, count: 50), withPrefix: 2)
|
||||
assertStability(Array(repeating: 1, count: 50) + Array(repeating: 0, count: 50), withPrefix: 5)
|
||||
assertStability(Array(repeating: 1, count: 50) + Array(repeating: 0, count: 50), withPrefix: 20)
|
||||
assertStability(Array(repeating: 1, count: 50) + Array(repeating: 0, count: 50), withPrefix: 50)
|
||||
assertStability([0,0], withPrefix: 1)
|
||||
assertStability([0,0], withPrefix: 2)
|
||||
assertStability([0,1,0,1,0,1], withPrefix: 2)
|
||||
assertStability([0,1,0,1,0,1], withPrefix: 6)
|
||||
assertStability([0,0,0,1,1,1], withPrefix: 1)
|
||||
assertStability([0,0,0,1,1,1], withPrefix: 3)
|
||||
assertStability([0,0,0,1,1,1], withPrefix: 4)
|
||||
assertStability([0,0,0,1,1,1], withPrefix: 6)
|
||||
assertStability([1,1,1,0,0,0], withPrefix: 1)
|
||||
assertStability([1,1,1,0,0,0], withPrefix: 3)
|
||||
assertStability([1,1,1,0,0,0], withPrefix: 4)
|
||||
assertStability([1,1,1,0,0,0], withPrefix: 6)
|
||||
assertStability([1,1,1,0,0,0], withPrefix: 5)
|
||||
}
|
||||
|
||||
func assertStability(
|
||||
_ actual: [Int],
|
||||
withPrefix prefixCount: Int
|
||||
) {
|
||||
let indexed = actual.enumerated()
|
||||
let sorted = indexed.map { $0 }.sortedPrefix(prefixCount) { $0.element < $1.element }
|
||||
|
||||
for element in Set(actual) {
|
||||
let filtered = sorted.filter { $0.element == element }.map(\.offset)
|
||||
XCTAssertEqual(filtered, filtered.sorted())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user