mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-24 04:17:48 +00:00
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b14b7f4c52 | ||
|
|
9650a85d9f | ||
|
|
9c81fc619a | ||
|
|
d500cf718e | ||
|
|
4708b34399 | ||
|
|
69f29d4d16 | ||
|
|
b6c1da00bb | ||
|
|
859e4bbab2 | ||
|
|
5289974477 | ||
|
|
88be07a691 |
No files matched your search
+1
-24
@@ -42,20 +42,6 @@ let p = numbers.partitioningIndex(where: { $0.isMultiple(of: 20) })
|
|||||||
// numbers[p...] = [20, 40, 60]
|
// numbers[p...] = [20, 40, 60]
|
||||||
```
|
```
|
||||||
|
|
||||||
The standard library’s existing `filter(_:)` method provides functionality to
|
|
||||||
get the elements that do match a given predicate. `partitioned(by:)` returns
|
|
||||||
both the elements that match the predicate as well as those that don’t, as a
|
|
||||||
tuple.
|
|
||||||
|
|
||||||
```swift
|
|
||||||
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
|
|
||||||
let (longNames, shortNames) = cast.partitioned(by: { $0.count < 5 })
|
|
||||||
print(longNames)
|
|
||||||
// Prints "["Vivien", "Marlon"]"
|
|
||||||
print(shortNames)
|
|
||||||
// Prints "["Kim", "Karl"]"
|
|
||||||
```
|
|
||||||
|
|
||||||
## Detailed Design
|
## Detailed Design
|
||||||
|
|
||||||
All mutating methods are declared as extensions to `MutableCollection`.
|
All mutating methods are declared as extensions to `MutableCollection`.
|
||||||
@@ -83,17 +69,11 @@ extension Collection {
|
|||||||
where belongsInSecondPartition: (Element) throws -> Bool
|
where belongsInSecondPartition: (Element) throws -> Bool
|
||||||
) rethrows -> Index
|
) rethrows -> Index
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Sequence {
|
|
||||||
public func partitioned(
|
|
||||||
by predicate: (Element) throws -> Bool
|
|
||||||
) rethrows -> (falseElements: [Element], trueElements: [Element])
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Complexity
|
### Complexity
|
||||||
|
|
||||||
The existing partition is an O(_n_) operation, where _n_ is the length of the
|
The existing partition is an O(_n_) operations, where _n_ is the length of the
|
||||||
range to be partitioned, while the stable partition is O(_n_ log _n_). Both
|
range to be partitioned, while the stable partition is O(_n_ log _n_). Both
|
||||||
partitions have algorithms with improved performance for bidirectional
|
partitions have algorithms with improved performance for bidirectional
|
||||||
collections, so it would be ideal for those to be customization points were they
|
collections, so it would be ideal for those to be customization points were they
|
||||||
@@ -102,9 +82,6 @@ to eventually land in the standard library.
|
|||||||
`partitioningIndex(where:)` is a slight generalization of a binary search, and
|
`partitioningIndex(where:)` is a slight generalization of a binary search, and
|
||||||
is an O(log _n_) operation for random-access collections; O(_n_) otherwise.
|
is an O(log _n_) operation for random-access collections; O(_n_) otherwise.
|
||||||
|
|
||||||
`partitioned(by:)` is an O(_n_) operation, where _n_ is the number of elements
|
|
||||||
in the original sequence.
|
|
||||||
|
|
||||||
### Comparison with other languages
|
### Comparison with other languages
|
||||||
|
|
||||||
**C++:** The `<algorithm>` library defines `partition`, `stable_partition`, and
|
**C++:** The `<algorithm>` library defines `partition`, `stable_partition`, and
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ Read more about the package, and the intent behind it, in the [announcement on s
|
|||||||
#### Subsetting operations
|
#### Subsetting operations
|
||||||
|
|
||||||
- [`compacted()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Compacted.md): Drops the `nil`s from a sequence or collection, unwrapping the remaining elements.
|
- [`compacted()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Compacted.md): Drops the `nil`s from a sequence or collection, unwrapping the remaining elements.
|
||||||
- [`partitioned(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Partition.md): Returns the elements in a sequence or collection that do and do not match a given predicate.
|
|
||||||
- [`randomSample(count:)`, `randomSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection.
|
- [`randomSample(count:)`, `randomSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection.
|
||||||
- [`randomStableSample(count:)`, `randomStableSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection, preserving their original relative order.
|
- [`randomStableSample(count:)`, `randomStableSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection, preserving their original relative order.
|
||||||
- [`striding(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Stride.md): Returns every nth element of a collection.
|
- [`striding(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Stride.md): Returns every nth element of a collection.
|
||||||
@@ -73,7 +72,7 @@ Finally, add `import Algorithms` to your source code.
|
|||||||
|
|
||||||
## Source Stability
|
## Source Stability
|
||||||
|
|
||||||
The Swift Algorithms package is source stable; version numbers follow [Semantic Versioning](https://semver.org/). Source breaking changes to public API can only land in a new major version.
|
The Swift Numerics package is source stable; version numbers follow [Semantic Versioning](https://semver.org/). Source breaking changes to public API can only land in a new major version.
|
||||||
|
|
||||||
The public API of version 1.0 of the `swift-algorithms` package consists of non-underscored declarations that are marked `public` in the `Algorithms` module. Interfaces that aren't part of the public API may continue to change in any release, including patch releases.
|
The public API of version 1.0 of the `swift-algorithms` package consists of non-underscored declarations that are marked `public` in the `Algorithms` module. Interfaces that aren't part of the public API may continue to change in any release, including patch releases.
|
||||||
|
|
||||||
|
|||||||
@@ -303,7 +303,7 @@ extension Collection {
|
|||||||
/// A collection that presents the elements of its base collection in
|
/// A collection that presents the elements of its base collection in
|
||||||
/// `SubSequence` chunks of any given count.
|
/// `SubSequence` chunks of any given count.
|
||||||
///
|
///
|
||||||
/// A `ChunksOfCountCollection` is a lazy view on the base `Collection`, but it
|
/// A `ChunksOfCountCollection` is a lazy view on the base Collection, but it
|
||||||
/// does not implicitly confer laziness on algorithms applied to its result. In
|
/// does not implicitly confer laziness on algorithms applied to its result. In
|
||||||
/// other words, for ordinary collections `c`:
|
/// other words, for ordinary collections `c`:
|
||||||
///
|
///
|
||||||
@@ -486,11 +486,11 @@ extension ChunksOfCountCollection {
|
|||||||
_ i: Index, _ distance: Int
|
_ i: Index, _ distance: Int
|
||||||
) -> Int {
|
) -> Int {
|
||||||
if i == endIndex {
|
if i == endIndex {
|
||||||
let remainder = base.count % chunkCount
|
let remainder = base.count%chunkCount
|
||||||
// We have to take it into account when calculating offsets.
|
// We have to take it into account when calculating offsets.
|
||||||
if remainder != 0 {
|
if remainder != 0 {
|
||||||
// Distance "minus" one (at this point distance is negative) because we
|
// Distance "minus" one(at this point distance is negative) because we
|
||||||
// need to adjust for the last position that have a variadic (remainder)
|
// need to adjust for the last position that have a variadic(remainder)
|
||||||
// number of elements.
|
// number of elements.
|
||||||
return ((distance + 1) * chunkCount) - remainder
|
return ((distance + 1) * chunkCount) - remainder
|
||||||
}
|
}
|
||||||
@@ -512,7 +512,7 @@ extension ChunksOfCountCollection {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to compute `index(offsetBy:)` index.
|
// Helper to compute index(offsetBy:) index.
|
||||||
@inlinable
|
@inlinable
|
||||||
internal func makeOffsetIndex(
|
internal func makeOffsetIndex(
|
||||||
from i: Index, baseBound: Base.Index, distance: Int, baseDistance: Int,
|
from i: Index, baseBound: Base.Index, distance: Int, baseDistance: Int,
|
||||||
@@ -525,13 +525,13 @@ extension ChunksOfCountCollection {
|
|||||||
|
|
||||||
if let limit = limit {
|
if let limit = limit {
|
||||||
if baseIdx == nil {
|
if baseIdx == nil {
|
||||||
// If we passed the bounds while advancing forward, and the limit is the
|
// If we past the bounds while advancing forward and the limit is the
|
||||||
// `endIndex`, since the computation on `base` don't take into account
|
// `endIndex`, since the computation on base don't take into account the
|
||||||
// the remainder, we have to make sure that passing the bound was
|
// remainder, we have to make sure that passing the bound was because of
|
||||||
// because of the distance not just because of a remainder. Special
|
// the distance not just because of a remainder. Special casing is less
|
||||||
// casing is less expensive than always using `count` (which could be
|
// expensive than always use count(which could be O(n) for non-random
|
||||||
// O(n) for non-random access collection base) to compute the base
|
// access collection base) to compute the base distance taking remainder
|
||||||
// distance taking remainder into account.
|
// into account.
|
||||||
if baseDistance > 0 && limit == endIndex {
|
if baseDistance > 0 && limit == endIndex {
|
||||||
if self.distance(from: i, to: limit) < distance {
|
if self.distance(from: i, to: limit) < distance {
|
||||||
return nil
|
return nil
|
||||||
@@ -561,7 +561,7 @@ extension Collection {
|
|||||||
/// Returns a `ChunksOfCountCollection<Self>` view presenting the elements in
|
/// Returns a `ChunksOfCountCollection<Self>` view presenting the elements in
|
||||||
/// chunks with count of the given count parameter.
|
/// chunks with count of the given count parameter.
|
||||||
///
|
///
|
||||||
/// - Parameter count: The size of the chunks. If the `count` parameter is
|
/// - Parameter count: The size of the chunks. If the count parameter is
|
||||||
/// evenly divided by the count of the base `Collection` all the chunks will
|
/// evenly divided by the count of the base `Collection` all the chunks will
|
||||||
/// have the count equals to size. Otherwise, the last chunk will contain
|
/// have the count equals to size. Otherwise, the last chunk will contain
|
||||||
/// the remaining elements.
|
/// the remaining elements.
|
||||||
|
|||||||
@@ -28,9 +28,11 @@ extension Sequence {
|
|||||||
while let e = iterator.next() {
|
while let e = iterator.next() {
|
||||||
// To be part of `result`, `e` must be strictly less than `result.last`.
|
// To be part of `result`, `e` must be strictly less than `result.last`.
|
||||||
guard try areInIncreasingOrder(e, result.last!) else { continue }
|
guard try areInIncreasingOrder(e, result.last!) else { continue }
|
||||||
result.removeLast()
|
|
||||||
let insertionIndex =
|
let insertionIndex =
|
||||||
try result.partitioningIndex { try areInIncreasingOrder(e, $0) }
|
try result.partitioningIndex { try areInIncreasingOrder(e, $0) }
|
||||||
|
|
||||||
|
assert(insertionIndex != result.endIndex)
|
||||||
|
result.removeLast()
|
||||||
result.insert(e, at: insertionIndex)
|
result.insert(e, at: insertionIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
//
|
//
|
||||||
// This source file is part of the Swift Algorithms open source project
|
// This source file is part of the Swift Algorithms open source project
|
||||||
//
|
//
|
||||||
// Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
|
// Copyright (c) 2020 Apple Inc. and the Swift project authors
|
||||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||||
//
|
//
|
||||||
// See https://swift.org/LICENSE.txt for license information
|
// See https://swift.org/LICENSE.txt for license information
|
||||||
@@ -168,8 +168,8 @@ extension MutableCollection where Self: BidirectionalCollection {
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
extension Collection {
|
extension Collection {
|
||||||
/// Returns the start index of the partition of a collection that matches
|
/// Returns the index of the first element in the collection that matches
|
||||||
/// the given predicate.
|
/// the predicate.
|
||||||
///
|
///
|
||||||
/// The collection must already be partitioned according to the predicate.
|
/// The collection must already be partitioned according to the predicate.
|
||||||
/// That is, there should be an index `i` where for every element in
|
/// That is, there should be an index `i` where for every element in
|
||||||
@@ -179,8 +179,7 @@ extension Collection {
|
|||||||
/// - Parameter belongsInSecondPartition: A predicate that partitions the
|
/// - Parameter belongsInSecondPartition: A predicate that partitions the
|
||||||
/// collection.
|
/// collection.
|
||||||
/// - Returns: The index of the first element in the collection for which
|
/// - Returns: The index of the first element in the collection for which
|
||||||
/// `predicate` returns `true`, or `endIndex` if there are no elements
|
/// `predicate` returns `true`.
|
||||||
/// for which `predicate` returns `true`.
|
|
||||||
///
|
///
|
||||||
/// - Complexity: O(log *n*), where *n* is the length of this collection if
|
/// - Complexity: O(log *n*), where *n* is the length of this collection if
|
||||||
/// the collection conforms to `RandomAccessCollection`, otherwise O(*n*).
|
/// the collection conforms to `RandomAccessCollection`, otherwise O(*n*).
|
||||||
@@ -205,137 +204,3 @@ extension Collection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
// partitioned(by:)
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
extension Sequence {
|
|
||||||
/// Returns two arrays containing, in order, the elements of the sequence that
|
|
||||||
/// do and don’t satisfy the given predicate.
|
|
||||||
///
|
|
||||||
/// In this example, `partitioned(by:)` is used to separate the input based on
|
|
||||||
/// whether a name is shorter than five characters:
|
|
||||||
///
|
|
||||||
/// let cast = ["Vivien", "Marlon", "Kim", "Karl"]
|
|
||||||
/// let (longNames, shortNames) = cast.partitioned(by: { $0.count < 5 })
|
|
||||||
/// print(longNames)
|
|
||||||
/// // Prints "["Vivien", "Marlon"]"
|
|
||||||
/// print(shortNames)
|
|
||||||
/// // Prints "["Kim", "Karl"]"
|
|
||||||
///
|
|
||||||
/// - Parameter predicate: A closure that takes an element of the sequence as
|
|
||||||
/// its argument and returns a Boolean value indicating whether the element
|
|
||||||
/// should be included in the second returned array. Otherwise, the element
|
|
||||||
/// will appear in the first returned array.
|
|
||||||
///
|
|
||||||
/// - Returns: Two arrays with all of the elements of the receiver. The
|
|
||||||
/// first array contains all the elements that `predicate` didn’t allow, and
|
|
||||||
/// the second array contains all the elements that `predicate` allowed.
|
|
||||||
///
|
|
||||||
/// - Complexity: O(*n*), where *n* is the length of the sequence.
|
|
||||||
@inlinable
|
|
||||||
public func partitioned(
|
|
||||||
by predicate: (Element) throws -> Bool
|
|
||||||
) rethrows -> (falseElements: [Element], trueElements: [Element]) {
|
|
||||||
var lhs = [Element]()
|
|
||||||
var rhs = [Element]()
|
|
||||||
|
|
||||||
for element in self {
|
|
||||||
if try predicate(element) {
|
|
||||||
rhs.append(element)
|
|
||||||
} else {
|
|
||||||
lhs.append(element)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (lhs, rhs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Collection {
|
|
||||||
/// Returns two arrays containing, in order, the elements of the collection
|
|
||||||
/// that do and don’t satisfy the given predicate.
|
|
||||||
///
|
|
||||||
/// In this example, `partitioned(by:)` is used to separate the input based on
|
|
||||||
/// whether a name is shorter than five characters.
|
|
||||||
///
|
|
||||||
/// let cast = ["Vivien", "Marlon", "Kim", "Karl"]
|
|
||||||
/// let (longNames, shortNames) = cast.partitioned(by: { $0.count < 5 })
|
|
||||||
/// print(longNames)
|
|
||||||
/// // Prints "["Vivien", "Marlon"]"
|
|
||||||
/// print(shortNames)
|
|
||||||
/// // Prints "["Kim", "Karl"]"
|
|
||||||
///
|
|
||||||
/// - Parameter predicate: A closure that takes an element of the collection
|
|
||||||
/// as its argument and returns a Boolean value indicating whether the element
|
|
||||||
/// should be included in the second returned array. Otherwise, the element
|
|
||||||
/// will appear in the first returned array.
|
|
||||||
///
|
|
||||||
/// - Returns: Two arrays with all of the elements of the receiver. The
|
|
||||||
/// first array contains all the elements that `predicate` didn’t allow, and
|
|
||||||
/// the second array contains all the elements that `predicate` allowed.
|
|
||||||
///
|
|
||||||
/// - Complexity: O(*n*), where *n* is the length of the collection.
|
|
||||||
@inlinable
|
|
||||||
public func partitioned(
|
|
||||||
by predicate: (Element) throws -> Bool
|
|
||||||
) rethrows -> (falseElements: [Element], trueElements: [Element]) {
|
|
||||||
guard !self.isEmpty else {
|
|
||||||
return ([], [])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Since collections have known sizes, we can allocate one array of size
|
|
||||||
// `self.count`, then insert items at the beginning or end of that contiguous
|
|
||||||
// block. This way, we don’t have to do any dynamic array resizing. Since we
|
|
||||||
// insert the right elements on the right side in reverse order, we need to
|
|
||||||
// reverse them back to the original order at the end.
|
|
||||||
|
|
||||||
let count = self.count
|
|
||||||
|
|
||||||
// Inside of the `initializer` closure, we set what the actual mid-point is.
|
|
||||||
// We will use this to partition the single array into two.
|
|
||||||
var midPoint: Int = 0
|
|
||||||
|
|
||||||
let elements = try [Element](
|
|
||||||
unsafeUninitializedCapacity: count,
|
|
||||||
initializingWith: { buffer, initializedCount in
|
|
||||||
var lhs = buffer.baseAddress!
|
|
||||||
var rhs = lhs + buffer.count
|
|
||||||
do {
|
|
||||||
for element in self {
|
|
||||||
if try predicate(element) {
|
|
||||||
rhs -= 1
|
|
||||||
rhs.initialize(to: element)
|
|
||||||
} else {
|
|
||||||
lhs.initialize(to: element)
|
|
||||||
lhs += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
precondition(lhs == rhs, """
|
|
||||||
Collection's `count` differed from the number of elements iterated.
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
let rhsIndex = rhs - buffer.baseAddress!
|
|
||||||
buffer[rhsIndex...].reverse()
|
|
||||||
initializedCount = buffer.count
|
|
||||||
|
|
||||||
midPoint = rhsIndex
|
|
||||||
} catch {
|
|
||||||
let lhsCount = lhs - buffer.baseAddress!
|
|
||||||
let rhsCount = (buffer.baseAddress! + buffer.count) - rhs
|
|
||||||
buffer.baseAddress!.deinitialize(count: lhsCount)
|
|
||||||
rhs.deinitialize(count: rhsCount)
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
let lhs = elements[..<midPoint]
|
|
||||||
let rhs = elements[midPoint...]
|
|
||||||
return (
|
|
||||||
Array(lhs),
|
|
||||||
Array(rhs)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -235,61 +235,3 @@ extension Sequence {
|
|||||||
return randomSample(count: k, using: &g)
|
return randomSample(count: k, using: &g)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
// randomElement()
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
// This method is a single-element specialization of `randomSample(count:)`,
|
|
||||||
// and extends the stdlib's `randomElement()` functionality (which is available
|
|
||||||
// for collections) down to sequences.
|
|
||||||
|
|
||||||
extension Sequence {
|
|
||||||
/// Randomly selects an element from this sequence, using the given generator
|
|
||||||
/// as the source of randomness.
|
|
||||||
///
|
|
||||||
/// - Parameter rng: The random number generator to use for sampling.
|
|
||||||
/// - Returns: A random element. If the sequence has no elements, this method
|
|
||||||
/// returns `nil`.
|
|
||||||
///
|
|
||||||
/// - Complexity: O(*n*), where *n* is the length of the sequence.
|
|
||||||
@inlinable
|
|
||||||
public func randomElement<G: RandomNumberGenerator>(
|
|
||||||
using rng: inout G
|
|
||||||
) -> Element? {
|
|
||||||
var iterator = makeIterator()
|
|
||||||
guard var result = iterator.next() else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var w = 1.0
|
|
||||||
while true {
|
|
||||||
w *= nextW(k: 1, using: &rng)
|
|
||||||
var offset = nextOffset(w: w, using: &rng)
|
|
||||||
while offset > 0, let _ = iterator.next() {
|
|
||||||
offset -= 1
|
|
||||||
}
|
|
||||||
guard let nextElement = iterator.next()
|
|
||||||
else { break }
|
|
||||||
|
|
||||||
result = nextElement
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Randomly selects an element from this sequence.
|
|
||||||
///
|
|
||||||
/// This method is equivalent to calling `randomSample(using:)`, passing in
|
|
||||||
/// the system's default random generator.
|
|
||||||
///
|
|
||||||
/// - Returns: A random element. If the sequence has no elements, this method
|
|
||||||
/// returns `nil`.
|
|
||||||
///
|
|
||||||
/// - Complexity: O(*n*), where *n* is the length of the sequence.
|
|
||||||
@inlinable
|
|
||||||
public func randomElement() -> Element? {
|
|
||||||
var g = SystemRandomNumberGenerator()
|
|
||||||
return randomElement(using: &g)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -92,26 +92,6 @@ final class PartitionTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPartitioningIndexWithEmptyInput() {
|
|
||||||
let input: [Int] = []
|
|
||||||
|
|
||||||
let a = input.partitioningIndex(where: { _ in return true })
|
|
||||||
XCTAssertEqual(a, input.startIndex)
|
|
||||||
|
|
||||||
let b = input.partitioningIndex(where: { _ in return false })
|
|
||||||
XCTAssertEqual(b, input.endIndex)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testPartitioningIndexWithOneEmptyPartition() {
|
|
||||||
let input: Range<Int> = (0 ..< 10)
|
|
||||||
|
|
||||||
let a = input.partitioningIndex(where: { $0 > 10 })
|
|
||||||
XCTAssertEqual(a, input.endIndex)
|
|
||||||
|
|
||||||
let b = input.partitioningIndex(where: { $0 >= 0 })
|
|
||||||
XCTAssertEqual(b, input.startIndex)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testPartitionWithSubrangeBidirectionalCollection() {
|
func testPartitionWithSubrangeBidirectionalCollection() {
|
||||||
for length in 10...20 {
|
for length in 10...20 {
|
||||||
let a = Array(0..<length)
|
let a = Array(0..<length)
|
||||||
@@ -153,40 +133,4 @@ final class PartitionTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPartitionedWithEmptyInput() {
|
|
||||||
let input: [Int] = []
|
|
||||||
|
|
||||||
let s0 = input.partitioned(by: { _ in return true })
|
|
||||||
|
|
||||||
XCTAssertTrue(s0.0.isEmpty)
|
|
||||||
XCTAssertTrue(s0.1.isEmpty)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Test the example given in the `partitioned(by:)` documentation
|
|
||||||
func testPartitionedExample() throws {
|
|
||||||
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
|
|
||||||
let (longNames, shortNames) = cast.partitioned(by: { $0.count < 5 })
|
|
||||||
XCTAssertEqual(longNames, ["Vivien", "Marlon"])
|
|
||||||
XCTAssertEqual(shortNames, ["Kim", "Karl"])
|
|
||||||
}
|
|
||||||
|
|
||||||
func testPartitionedWithPredicate() throws {
|
|
||||||
let s0 = ["A", "B", "C", "D"].partitioned(by: { $0 == $0.lowercased() })
|
|
||||||
let s1 = ["a", "B", "C", "D"].partitioned(by: { $0 == $0.lowercased() })
|
|
||||||
let s2 = ["a", "B", "c", "D"].partitioned(by: { $0 == $0.lowercased() })
|
|
||||||
let s3 = ["a", "B", "c", "d"].partitioned(by: { $0 == $0.lowercased() })
|
|
||||||
|
|
||||||
XCTAssertEqual(s0.0, ["A", "B", "C", "D"])
|
|
||||||
XCTAssertEqual(s0.1, [])
|
|
||||||
|
|
||||||
XCTAssertEqual(s1.0, ["B", "C", "D"])
|
|
||||||
XCTAssertEqual(s1.1, ["a"])
|
|
||||||
|
|
||||||
XCTAssertEqual(s2.0, ["B", "D"])
|
|
||||||
XCTAssertEqual(s2.1, ["a", "c"])
|
|
||||||
|
|
||||||
XCTAssertEqual(s3.0, ["B"])
|
|
||||||
XCTAssertEqual(s3.1, ["a", "c", "d"])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -12,17 +12,13 @@
|
|||||||
import XCTest
|
import XCTest
|
||||||
@testable import Algorithms
|
@testable import Algorithms
|
||||||
|
|
||||||
func expectedRange(for value: Int) -> ClosedRange<Int> {
|
|
||||||
((value / 3) * 2) ... ((value / 3) * 4)
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateRandomSamples<S: Sequence>(
|
func validateRandomSamples<S: Sequence>(
|
||||||
_ samples: [Int: Int],
|
_ samples: [Int: Int],
|
||||||
elements: S,
|
elements: S,
|
||||||
expectedValue: Int,
|
expectedValue: Int,
|
||||||
file: StaticString = #file, line: UInt = #line
|
file: StaticString = #file, line: UInt = #line
|
||||||
) where S.Element == Int {
|
) where S.Element == Int {
|
||||||
let expectedRange = expectedRange(for: expectedValue)
|
let expectedRange = ((expectedValue / 3) * 2) ... ((expectedValue / 3) * 4)
|
||||||
XCTAssertEqualSequences(samples.keys.sorted(), elements,
|
XCTAssertEqualSequences(samples.keys.sorted(), elements,
|
||||||
file: file, line: line)
|
file: file, line: line)
|
||||||
for v in samples.values {
|
for v in samples.values {
|
||||||
@@ -32,44 +28,41 @@ func validateRandomSamples<S: Sequence>(
|
|||||||
|
|
||||||
private let n = 100
|
private let n = 100
|
||||||
private let k = 12
|
private let k = 12
|
||||||
private let iterations = 1000
|
private let iterations = 10_000
|
||||||
|
|
||||||
/// A collection to sample from.
|
|
||||||
private let c = 0..<n
|
private let c = 0..<n
|
||||||
/// A sequence to sample from.
|
|
||||||
private let s = sequence(first: 0, next: { $0 == n - 1 ? nil : $0 + 1 })
|
private let s = sequence(first: 0, next: { $0 == n - 1 ? nil : $0 + 1 })
|
||||||
/// An empty sequence
|
|
||||||
private let emptySequence = sequence(state: 0, next: { (_) -> Int? in nil })
|
|
||||||
|
|
||||||
final class RandomSampleTests: XCTestCase {
|
final class RandomSampleTests: XCTestCase {
|
||||||
func testRandomStableSampleCollection() {
|
func testRandomStableSampleCollection() {
|
||||||
let samples: [Int: Int] = (0..<iterations).reduce(into: [:]) { result, _ in
|
let samples: [Int: Int] = (0..<iterations).reduce(into: [:]) { result, _ in
|
||||||
let sample = c.randomStableSample(count: k)
|
let sample = c.randomStableSample(count: k)
|
||||||
XCTAssert(sample.isSorted())
|
XCTAssert(sample.isSorted())
|
||||||
result.merge(sample.frequencies, uniquingKeysWith: +)
|
for x in sample {
|
||||||
|
result[x, default: 0] += 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
validateRandomSamples(samples, elements: c, expectedValue: (k * iterations) / n)
|
validateRandomSamples(samples, elements: c, expectedValue: (k * iterations) / n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testRandomSampleCollection() {
|
func testRandomSampleCollection() {
|
||||||
let samples: [Int: Int] = (0..<iterations).reduce(into: [:]) { result, _ in
|
let samples: [Int: Int] = (0..<iterations).reduce(into: [:]) { result, _ in
|
||||||
result.merge(c.randomSample(count: k).frequencies, uniquingKeysWith: +)
|
for x in c.randomSample(count: k) {
|
||||||
|
result[x, default: 0] += 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
validateRandomSamples(samples, elements: c, expectedValue: (k * iterations) / n)
|
validateRandomSamples(samples, elements: c, expectedValue: (k * iterations) / n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testRandomSampleSequence() {
|
func testRandomSampleSequence() {
|
||||||
let samples: [Int: Int] = (0..<iterations).reduce(into: [:]) { result, _ in
|
let samples: [Int: Int] = (0..<iterations).reduce(into: [:]) { result, _ in
|
||||||
result.merge(s.randomSample(count: k).frequencies, uniquingKeysWith: +)
|
for x in s.randomSample(count: k) {
|
||||||
|
result[x, default: 0] += 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
validateRandomSamples(samples, elements: s, expectedValue: (k * iterations) / n)
|
validateRandomSamples(samples, elements: s, expectedValue: (k * iterations) / n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testRandomSampleEdgeCases() {
|
func testRandomSampleEdgeCases() {
|
||||||
XCTAssert(emptySequence.randomSample(count: 10).isEmpty)
|
|
||||||
XCTAssert(([] as [Int]).randomSample(count: 10).isEmpty)
|
|
||||||
XCTAssert(([] as [Int]).randomStableSample(count: 10).isEmpty)
|
|
||||||
|
|
||||||
XCTAssert(c.randomStableSample(count: 0).isEmpty)
|
XCTAssert(c.randomStableSample(count: 0).isEmpty)
|
||||||
XCTAssertEqualSequences(c.randomStableSample(count: n), c)
|
XCTAssertEqualSequences(c.randomStableSample(count: n), c)
|
||||||
XCTAssertEqualSequences(c.randomStableSample(count: n * 2), c)
|
XCTAssertEqualSequences(c.randomStableSample(count: n * 2), c)
|
||||||
@@ -84,23 +77,21 @@ final class RandomSampleTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testRandomSampleRepeatable() {
|
func testRandomSampleRepeatable() {
|
||||||
let seed = UInt64.random(in: 0 ... .max)
|
var generator = SplitMix64(seed: 0)
|
||||||
|
|
||||||
var generator = SplitMix64(seed: seed)
|
|
||||||
let sample1a = c.randomSample(count: k, using: &generator)
|
let sample1a = c.randomSample(count: k, using: &generator)
|
||||||
generator = SplitMix64(seed: seed)
|
generator = SplitMix64(seed: 0)
|
||||||
let sample2a = c.randomSample(count: k, using: &generator)
|
let sample2a = c.randomSample(count: k, using: &generator)
|
||||||
XCTAssertEqual(sample1a, sample2a)
|
XCTAssertEqual(sample1a, sample2a)
|
||||||
|
|
||||||
generator = SplitMix64(seed: seed)
|
generator = SplitMix64(seed: 0)
|
||||||
let sample1b = s.randomSample(count: k, using: &generator)
|
let sample1b = s.randomSample(count: k, using: &generator)
|
||||||
generator = SplitMix64(seed: seed)
|
generator = SplitMix64(seed: 0)
|
||||||
let sample2b = s.randomSample(count: k, using: &generator)
|
let sample2b = s.randomSample(count: k, using: &generator)
|
||||||
XCTAssertEqual(sample1b, sample2b)
|
XCTAssertEqual(sample1b, sample2b)
|
||||||
|
|
||||||
generator = SplitMix64(seed: seed)
|
generator = SplitMix64(seed: 0)
|
||||||
let sample1c = c.randomStableSample(count: k, using: &generator)
|
let sample1c = c.randomStableSample(count: k, using: &generator)
|
||||||
generator = SplitMix64(seed: seed)
|
generator = SplitMix64(seed: 0)
|
||||||
let sample2c = c.randomStableSample(count: k, using: &generator)
|
let sample2c = c.randomStableSample(count: k, using: &generator)
|
||||||
XCTAssertEqual(sample1c, sample2c)
|
XCTAssertEqual(sample1c, sample2c)
|
||||||
}
|
}
|
||||||
@@ -132,37 +123,4 @@ final class RandomSampleTests: XCTestCase {
|
|||||||
almostAllZero = AlmostAllZeroGenerator(seed: 0)
|
almostAllZero = AlmostAllZeroGenerator(seed: 0)
|
||||||
_ = c.randomSample(count: k, using: &almostAllZero) // must not crash
|
_ = c.randomSample(count: k, using: &almostAllZero) // must not crash
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSequenceRandomElement() {
|
|
||||||
XCTAssertNil(emptySequence.randomElement())
|
|
||||||
|
|
||||||
let expectedRange = expectedRange(for: iterations)
|
|
||||||
let randomElements = (0..<(n*iterations)).map { _ in
|
|
||||||
s.randomElement()!
|
|
||||||
}.frequencies
|
|
||||||
XCTAssertEqual(randomElements.count, n)
|
|
||||||
XCTAssert(randomElements.values.allSatisfy { expectedRange.contains($0) })
|
|
||||||
|
|
||||||
let oneElementSequence = sequence(first: 0, next: { _ in nil })
|
|
||||||
let randomOneElement = (0..<iterations).map { _ in
|
|
||||||
oneElementSequence.randomElement()!
|
|
||||||
}
|
|
||||||
XCTAssert(randomOneElement.allSatisfy { $0 == 0 })
|
|
||||||
|
|
||||||
let twoElementSequence = sequence(state: [1, 2].makeIterator(), next: { $0.next() })
|
|
||||||
let randomTwoElement = (0..<(2*iterations)).map { _ in
|
|
||||||
twoElementSequence.randomElement()!
|
|
||||||
}.frequencies
|
|
||||||
XCTAssertEqual(randomTwoElement.count, 2)
|
|
||||||
XCTAssert(randomElements.values.allSatisfy { expectedRange.contains($0) })
|
|
||||||
}
|
|
||||||
|
|
||||||
func testSequenceRandomElementRepeatable() {
|
|
||||||
let seed = UInt64.random(in: 0 ... .max)
|
|
||||||
var generator = SplitMix64(seed: seed)
|
|
||||||
let elements1 = (0..<500).map { _ in s.randomElement(using: &generator)! }
|
|
||||||
generator = SplitMix64(seed: seed)
|
|
||||||
let elements2 = (0..<500).map { _ in s.randomElement(using: &generator)! }
|
|
||||||
XCTAssertEqual(elements1, elements2)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -23,12 +23,6 @@ extension Sequence where Element: Comparable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Sequence where Element: Hashable {
|
|
||||||
var frequencies: [Element: Int] {
|
|
||||||
reduce(into: [:]) { $0[$1, default: 0] += 1 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Numeric {
|
extension Numeric {
|
||||||
func factorial() -> Self {
|
func factorial() -> Self {
|
||||||
guard self != 0 else { return 1 }
|
guard self != 0 else { return 1 }
|
||||||
|
|||||||
Reference in new issue
Block a user