[BitSet] Make BitSet generic over its Element

This gets rid of the weird asymmetry between reading the contents of a BitSet (where we used to always get Int values) and mutating it (where we used to be able to give it values fo any fixed-width integer type).
This commit is contained in:
Károly Lőrentey committed 2022-07-30 23:25:53 -07:00
1 parent 55f3b58437
commit 1d1fbc82d4
26 files changed
+621 -389

No files matched your search

@@ -10,8 +10,6 @@
//===----------------------------------------------------------------------===//
extension BitSet: Sequence {
public typealias Element = Int
@inlinable
@inline(__always)
public var underestimatedCount: Int {
@@ -27,11 +25,11 @@ extension BitSet: Sequence {
_ element: Element
) -> Bool? {
guard let element = UInt(exactly: element) else { return false }
return _read { $0.contains(element) }
return _core._read { $0.contains(element) }
}
public struct Iterator: IteratorProtocol {
internal typealias _UnsafeHandle = BitSet._UnsafeHandle
internal typealias _UnsafeHandle = _BitSet.UnsafeHandle
internal let bitset: BitSet
internal var index: Int
@@ -41,25 +39,25 @@ extension BitSet: Sequence {
internal init(_ bitset: BitSet) {
self.bitset = bitset
self.index = 0
self.word = bitset._read { handle in
self.word = bitset._core._read { handle in
guard handle.wordCount > 0 else { return .empty }
return handle._words[0]
}
}
@_effects(releasenone)
public mutating func next() -> Int? {
public mutating func next() -> Element? {
if let bit = word.next() {
let i = _UnsafeHandle.Index(word: index, bit: bit)
return Int(truncatingIfNeeded: i.value)
return Element(truncatingIfNeeded: i.value)
}
return bitset._read { handle in
return bitset._core._read { handle in
while (index + 1) < handle.wordCount {
index += 1
word = handle._words[index]
if let bit = word.next() {
let i = _UnsafeHandle.Index(word: index, bit: bit)
return Int(truncatingIfNeeded: i.value)
return Element(truncatingIfNeeded: i.value)
}
}
return nil
@@ -69,28 +67,28 @@ extension BitSet: Sequence {
}
extension BitSet: Collection, BidirectionalCollection {
public var isEmpty: Bool { _count == 0 }
public var count: Int { _count }
public var isEmpty: Bool { _core._count == 0 }
public var count: Int { _core._count }
public var startIndex: Index {
// Note: This is O(n)
Index(_position: _read { $0.startIndex })
Index(_position: _core._read { $0.startIndex })
}
public var endIndex: Index {
Index(_position: .init(word: _storage.count, bit: 0))
Index(_position: .init(word: _core._storage.count, bit: 0))
}
public subscript(position: Index) -> Int {
Int(position._position.value)
public subscript(position: Index) -> Element {
Element(position._position.value)
}
public func index(after index: Index) -> Index {
Index(_position: _read { $0.index(after: index._position) })
Index(_position: _core._read { $0.index(after: index._position) })
}
public func index(before index: Index) -> Index {
Index(_position: _read { $0.index(before: index._position) })
Index(_position: _core._read { $0.index(before: index._position) })
}
#if false // TODO: Specialize these