Files
swift-collections/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift
T
Károly Lőrentey 8f2e0c79c3 Review & update BitSet
This is a work in progress; in particular, it adds a cached `count` to BitSet, which may turn out to be a bad idea. (It generates extra work for bulk operations even if the count is never needed.) I think replacing this with a cached `startIndex` might be more useful in practice.

The `_UnsafeBitset` type from the OrderedCollections module is brought over as `BitSet._UnsafeHandle`. This implements an (internal-only) fixed-size bitset.

`BitSet` now conforms to `SetAlgebra`. We decided against this in the initial implementation, but on reflection, it seems like the right move. (The somewhat nonsensical `insert` and `update` methods are unfortunate, but consistency is valuable.)
2022-01-10 23:35:33 -08:00

123 lines
3.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021-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: Sequence {
public typealias Element = Int
@inlinable
@inline(__always)
public var underestimatedCount: Int {
return count
}
@inlinable
public func makeIterator() -> Iterator {
return Iterator(self)
}
public func _customContainsEquatableElement(
_ element: Element
) -> Bool? {
guard let element = UInt(exactly: element) else { return false }
return _read { $0.contains(element) }
}
public struct Iterator: IteratorProtocol {
internal typealias _UnsafeHandle = BitSet._UnsafeHandle
internal let bitset: BitSet
internal var index: Int
internal var word: _Word
@usableFromInline
internal init(_ bitset: BitSet) {
self.bitset = bitset
self.index = 0
self.word = bitset._read { handle in
guard handle.wordCount > 0 else { return .empty }
return handle._words[0]
}
}
@_effects(releasenone)
public mutating func next() -> Int? {
if let bit = word.next() {
let i = _UnsafeHandle.Index(word: index, bit: bit)
return Int(truncatingIfNeeded: i.value)
}
return bitset._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 nil
}
}
}
}
extension BitSet: Collection, BidirectionalCollection {
public var isEmpty: Bool { _count == 0 }
public var count: Int { _count }
public var startIndex: Index {
// Note: This is O(n)
Index(_position: _read { $0.startIndex })
}
public var endIndex: Index {
Index(_position: .init(word: _storage.count, bit: 0))
}
public subscript(position: Index) -> Int {
Int(position._position.value)
}
public func index(after index: Index) -> Index {
Index(_position: _read { $0.index(after: index._position) })
}
public func index(before index: Index) -> Index {
Index(_position: _read { $0.index(before: index._position) })
}
#if false // TODO: Specialize these
public func distance(from start: Index, to end: Index) -> Int {
fatalError("Unimplemented")
}
public func index(_ index: Index, offsetBy distance: Int) -> Index {
fatalError("Unimplemented")
}
public func index(
_ i: Index, offsetBy distance: Int, limitedBy limit: Index
) -> Index? {
fatalError("Unimplemented")
}
#endif
public func _customIndexOfEquatableElement(_ element: Element) -> Index?? {
guard contains(element) else { return .some(nil) }
return Index(_value: UInt(element))
}
public func _customLastIndexOfEquatableElement(
_ element: Element
) -> Index?? {
_customIndexOfEquatableElement(element)
}
}