mirror of
https://github.com/apple/swift-collections
synced 2026-11-30 05:18:44 +00:00
[BitSet, BitArray] Add random factories
This commit is contained in:
1 parent
65976b8d85
commit
fcac09d86a
5 files changed
+103
No files matched your search
@@ -0,0 +1,29 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitArray {
|
||||
public static func random(count: Int) -> BitArray {
|
||||
var rng = SystemRandomNumberGenerator()
|
||||
return random(count: count, using: &rng)
|
||||
}
|
||||
|
||||
public static func random<R: RandomNumberGenerator>(
|
||||
count: Int,
|
||||
using rng: inout R
|
||||
) -> BitArray {
|
||||
precondition(count >= 0, "Invalid capacity value")
|
||||
guard count > 0 else { return BitArray() }
|
||||
let (w, b) = _BitPosition(count).endSplit
|
||||
var words = (0 ... w).map { _ in _Word(rng.next() as UInt) }
|
||||
words[w].formIntersection(_Word(upTo: b))
|
||||
return BitArray(_storage: words, count: count)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public static func random(upTo limit: Int) -> BitSet {
|
||||
var rng = SystemRandomNumberGenerator()
|
||||
return random(upTo: limit, using: &rng)
|
||||
}
|
||||
|
||||
public static func random<R: RandomNumberGenerator>(
|
||||
upTo limit: Int,
|
||||
using rng: inout R
|
||||
) -> BitSet {
|
||||
precondition(limit >= 0, "Invalid limit value")
|
||||
guard limit > 0 else { return BitSet() }
|
||||
let (w, b) = _BitPosition(limit).endSplit
|
||||
var words = (0 ... w).map { _ in _Word(rng.next() as UInt) }
|
||||
words[w].formIntersection(_Word(upTo: b))
|
||||
return BitSet(_words: words)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2021 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// A terrible random number generator that always returns values with all
|
||||
/// bits set to true.
|
||||
public struct AllOnesRandomNumberGenerator: RandomNumberGenerator {
|
||||
|
||||
public init() {}
|
||||
|
||||
public mutating func next() -> UInt64 {
|
||||
UInt64.max
|
||||
}
|
||||
}
|
||||
@@ -632,4 +632,16 @@ final class BitArrayTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_random() {
|
||||
var rng = AllOnesRandomNumberGenerator()
|
||||
for c in [0, 10, 64, 65, 77, 1200] {
|
||||
let array = BitArray.random(count: c, using: &rng)
|
||||
expectEqual(array.count, c)
|
||||
expectEqualElements(array, repeatElement(true, count: c))
|
||||
}
|
||||
|
||||
let a = Set((0..<10).map { _ in BitArray.random(count: 1000) })
|
||||
expectEqual(a.count, 10)
|
||||
}
|
||||
}
|
||||
@@ -1029,4 +1029,16 @@ final class BitSetTest: CollectionTestCase {
|
||||
expectEqualElements(s1, [0, 3, 5, 23, 283, 6362])
|
||||
expectEqualElements(s2, [0, 3, 5, 23, 283, 6362])
|
||||
}
|
||||
|
||||
func test_random() {
|
||||
var rng = AllOnesRandomNumberGenerator()
|
||||
for c in [0, 10, 64, 65, 77, 1200] {
|
||||
let set = BitSet.random(upTo: c, using: &rng)
|
||||
expectEqual(set.count, c)
|
||||
expectEqualElements(set, 0 ..< c)
|
||||
}
|
||||
|
||||
let a = Set((0..<10).map { _ in BitSet.random(upTo: 1000) })
|
||||
expectEqual(a.count, 10)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user