From fcac09d86a96d5c68e32478d14a9e2adaa691f4f Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 25 Aug 2022 20:26:19 -0700 Subject: [PATCH] [BitSet, BitArray] Add random factories --- .../BitArray/BitArray+Random.swift | 29 +++++++++++++++++++ .../BitCollections/BitSet/BitSet+Random.swift | 29 +++++++++++++++++++ .../AllOnesRandomNumberGenerator.swift | 21 ++++++++++++++ Tests/BitCollectionsTests/BitArrayTests.swift | 12 ++++++++ Tests/BitCollectionsTests/BitSetTests.swift | 12 ++++++++ 5 files changed, 103 insertions(+) create mode 100644 Sources/BitCollections/BitArray/BitArray+Random.swift create mode 100644 Sources/BitCollections/BitSet/BitSet+Random.swift create mode 100644 Sources/_CollectionsTestSupport/Utilities/AllOnesRandomNumberGenerator.swift diff --git a/Sources/BitCollections/BitArray/BitArray+Random.swift b/Sources/BitCollections/BitArray/BitArray+Random.swift new file mode 100644 index 00000000..42a13351 --- /dev/null +++ b/Sources/BitCollections/BitArray/BitArray+Random.swift @@ -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( + 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) + } +} diff --git a/Sources/BitCollections/BitSet/BitSet+Random.swift b/Sources/BitCollections/BitSet/BitSet+Random.swift new file mode 100644 index 00000000..4b98e3d9 --- /dev/null +++ b/Sources/BitCollections/BitSet/BitSet+Random.swift @@ -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( + 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) + } +} diff --git a/Sources/_CollectionsTestSupport/Utilities/AllOnesRandomNumberGenerator.swift b/Sources/_CollectionsTestSupport/Utilities/AllOnesRandomNumberGenerator.swift new file mode 100644 index 00000000..8402b052 --- /dev/null +++ b/Sources/_CollectionsTestSupport/Utilities/AllOnesRandomNumberGenerator.swift @@ -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 + } +} diff --git a/Tests/BitCollectionsTests/BitArrayTests.swift b/Tests/BitCollectionsTests/BitArrayTests.swift index 7ba8f403..ee8296ee 100644 --- a/Tests/BitCollectionsTests/BitArrayTests.swift +++ b/Tests/BitCollectionsTests/BitArrayTests.swift @@ -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) + } } diff --git a/Tests/BitCollectionsTests/BitSetTests.swift b/Tests/BitCollectionsTests/BitSetTests.swift index ae5ec4d3..b5cd7c93 100644 --- a/Tests/BitCollectionsTests/BitSetTests.swift +++ b/Tests/BitCollectionsTests/BitSetTests.swift @@ -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) + } }