Files
swift-collections/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift
T
Karoy Lorentey cbf933665b [BitSet] Revert to non-generic BitSet
Also, remove members that were generic over FixedWidthInteger.
2022-08-12 19:22:24 -07:00

121 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: Int
) -> Bool? {
guard let element = UInt(exactly: element) else { return false }
return _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(bitPattern: 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: Int) -> Index?? {
guard contains(element) else { return .some(nil) }
return Index(_value: UInt(bitPattern: element))
}
public func _customLastIndexOfEquatableElement(_ element: Int) -> Index?? {
_customIndexOfEquatableElement(element)
}
}