Compare commits

..
Author SHA1 Message Date
Nate Cook eb93e564d4 Add Sequence.randomElement()
The stdlib's randomElement() method extends Collection, since it
operates by selecting a random index. This adds randomElement() as
a Sequence method, specializing the reservoir sampling algorithm
from randomSample(count:) for a count of 1.
2022-03-07 18:17:41 -06:00
Nate Cook 11fda519ef Various and sundry random test improvements 2022-03-07 18:14:08 -06:00
Matt ZanchelliandMatt Zanchelli 195e0316d7 Update comments in Chunked.swift (#179)
* Use back ticks around code references

* Add missing spaces before parenthesis

* Use “passed” instead of “past”
Add missing comma
Add back ticks around code references
Add missing space before parenthesis

* Consistently use spaces around `%` operator

Co-authored-by: Matt Zanchelli <mattt@apple.com>
2022-02-23 13:07:19 -06:00
Matt Zanchelli da475ea9af Clarify endIndex behavior of partitioningIndex(where:) in documentation (Resolves #170) (#171)
The returned index isn’t necessarily an index of an element. In the case where it returns `endIndex`, it’s not valid for subscripting the collection.
2021-12-16 07:10:17 -06:00
Matt Zanchelli 19c1d00177 Remove the last element before determining the insertion index (#173)
This could result in fewer comparisons.

Additionally, it ensures that the insertion index is valid for insertion. The `removeLast()` documentation states:
> Calling this method may invalidate all saved indices of this collection. Do not rely on a previously stored index value after altering a collection with any operation that can change its length.
2021-12-04 00:25:02 -06:00
e2fa131d62 Add partitioned(by:) (#152)
* Add `partitioned(_:)`

`partitioned(_:)` works like `filter(_:)`, but also returns the excluded elements by returning a tuple of two `Array`s

* For collections with fewer than 8 elements, use the `Sequence`-based implementation

This constant was determined using benchmarking. More information: https://github.com/apple/swift-algorithms/pull/152#issuecomment-887130149

* Remove check for collections fewer than 8 elements

* Make `_partitioned` `internal`

* Prefer `Array` over `ContiguousArray`

* Document `partitioned(_:)` on `Collection`

* Remove `partitioned(upTo:)`

* Remove `_tupleMap`

* Remove `_partitioned` and use it inline (since it’s no longer used by the `Collection` implementation

* Remove unnecessary conversation of `Array` to `Array`

* Correct indentation

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Consistent syntax

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Add an external `by:` label to `partitioned`

* Add labels to returned tuple `falseElements`, `trueElements`

* Correct function signature

* Rename `belongsInSecondCollection` parameter name to simply `predicate`

The parameter name was potentially confusing. Unlike the other `partition` functions, this function can rely on its named tuple to clarify its behavior.

* Update copyright information

Co-authored-by: Nate Cook <natecook@apple.com>

* Update documentation

Co-authored-by: Nate Cook <natecook@apple.com>

* Update comment

* Add a precondition to ensure that the count matches up with the number of actual elements found while iterating

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>
Co-authored-by: Nate Cook <natecook@apple.com>
2021-10-20 22:21:39 +02:00
Ole Begemann aeead5ab20 Fix typo in readme
Looks like a copy/paste oversight.
2021-09-13 14:22:39 -07:00
Tim Vermeulen c5ea9e3e36 Prepare for Swift Algorithms 1.0 (#167)
* Rename types to include a Sequence or Collection suffix, and clean up some guides

* Various refactors

* Replace `validateIndexTraversals` test method with `IndexValidator` type

* Add LazySequenceProtocol to StridingSequence, StridingCollection

* Update the Numerics dependency to 1.0

* Set `IndexedCollection.Indices` to `Base.Indices`

* Remove deprecations

* Update the changelog/README for the 1.0.0 release

* Fix compiler error on Linux

* Rename `WindowsCollection` to `WindowsOfCountCollection`
2021-09-09 01:01:36 +02:00
Stephen Canon e195266e1f Make Algorithms depend on Numerics product instead of RealModule. (#164)
The RealModule _library product_ will be removed from Numerics at some future point (the _module_ will continue to exist). Update Package.swift to depend on the Numerics library instead.
2021-08-26 18:51:34 +02:00
9 changed files with 358 additions and 39 deletions

No files matched your search

+24 -1
View File
@@ -42,6 +42,20 @@ 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`.
@@ -69,11 +83,17 @@ 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_) operations, where _n_ is the length of the The existing partition is an O(_n_) operation, 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
@@ -82,6 +102,9 @@ 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
+2 -1
View File
@@ -27,6 +27,7 @@ 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.
@@ -72,7 +73,7 @@ Finally, add `import Algorithms` to your source code.
## Source Stability ## Source Stability
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 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 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.
+13 -13
View File
@@ -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 past the bounds while advancing forward and the limit is the // If we passed the bounds while advancing forward, and the limit is the
// `endIndex`, since the computation on base don't take into account the // `endIndex`, since the computation on `base` don't take into account
// remainder, we have to make sure that passing the bound was because of // the remainder, we have to make sure that passing the bound was
// the distance not just because of a remainder. Special casing is less // because of the distance not just because of a remainder. Special
// expensive than always use count(which could be O(n) for non-random // casing is less expensive than always using `count` (which could be
// access collection base) to compute the base distance taking remainder // O(n) for non-random access collection base) to compute the base
// into account. // distance taking remainder 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.
+1 -3
View File
@@ -28,11 +28,9 @@ 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)
} }
+139 -4
View File
@@ -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 Apple Inc. and the Swift project authors // Copyright (c) 2020-2021 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 index of the first element in the collection that matches /// Returns the start index of the partition of a collection that matches
/// the predicate. /// the given 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,7 +179,8 @@ 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`. /// `predicate` returns `true`, or `endIndex` if there are no elements
/// 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*).
@@ -204,3 +205,137 @@ 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)
)
}
}
+58
View File
@@ -235,3 +235,61 @@ 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,6 +92,26 @@ 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)
@@ -133,4 +153,40 @@ 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,13 +12,17 @@
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 = ((expectedValue / 3) * 2) ... ((expectedValue / 3) * 4) let expectedRange = expectedRange(for: expectedValue)
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 {
@@ -28,41 +32,44 @@ func validateRandomSamples<S: Sequence>(
private let n = 100 private let n = 100
private let k = 12 private let k = 12
private let iterations = 10_000 private let iterations = 1000
/// 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())
for x in sample { result.merge(sample.frequencies, uniquingKeysWith: +)
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
for x in c.randomSample(count: k) { result.merge(c.randomSample(count: k).frequencies, uniquingKeysWith: +)
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
for x in s.randomSample(count: k) { result.merge(s.randomSample(count: k).frequencies, uniquingKeysWith: +)
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)
@@ -77,21 +84,23 @@ final class RandomSampleTests: XCTestCase {
} }
func testRandomSampleRepeatable() { func testRandomSampleRepeatable() {
var generator = SplitMix64(seed: 0) let seed = UInt64.random(in: 0 ... .max)
var generator = SplitMix64(seed: seed)
let sample1a = c.randomSample(count: k, using: &generator) let sample1a = c.randomSample(count: k, using: &generator)
generator = SplitMix64(seed: 0) generator = SplitMix64(seed: seed)
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: 0) generator = SplitMix64(seed: seed)
let sample1b = s.randomSample(count: k, using: &generator) let sample1b = s.randomSample(count: k, using: &generator)
generator = SplitMix64(seed: 0) generator = SplitMix64(seed: seed)
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: 0) generator = SplitMix64(seed: seed)
let sample1c = c.randomStableSample(count: k, using: &generator) let sample1c = c.randomStableSample(count: k, using: &generator)
generator = SplitMix64(seed: 0) generator = SplitMix64(seed: seed)
let sample2c = c.randomStableSample(count: k, using: &generator) let sample2c = c.randomStableSample(count: k, using: &generator)
XCTAssertEqual(sample1c, sample2c) XCTAssertEqual(sample1c, sample2c)
} }
@@ -123,4 +132,37 @@ 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,6 +23,12 @@ 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 }