mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
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.)
51 lines
1.2 KiB
Swift
51 lines
1.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 {
|
|
@frozen
|
|
public struct Index {
|
|
@usableFromInline
|
|
var _value: UInt
|
|
|
|
@inlinable
|
|
internal init(_value: UInt) {
|
|
self._value = _value
|
|
}
|
|
|
|
@inlinable
|
|
internal init(_position: _UnsafeHandle.Index) {
|
|
self._value = _position.value
|
|
}
|
|
|
|
@inline(__always)
|
|
internal var _position: _UnsafeHandle.Index {
|
|
_UnsafeHandle.Index(_value)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension BitSet.Index: Comparable, Hashable {
|
|
@inlinable
|
|
public static func ==(left: Self, right: Self) -> Bool {
|
|
left._value == right._value
|
|
}
|
|
|
|
@inlinable
|
|
public static func < (left: Self, right: Self) -> Bool {
|
|
left._value < right._value
|
|
}
|
|
|
|
@inlinable
|
|
public func hash(into hasher: inout Hasher) {
|
|
hasher.combine(_value)
|
|
}
|
|
}
|