[BitSet, BitArray] Add random factories

This commit is contained in:
Karoy Lorentey committed 2022-08-25 22:21:34 -07:00
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)
}
}