[BitSet] Revert to non-generic BitSet

Also, remove members that were generic over FixedWidthInteger.
This commit is contained in:
Karoy Lorentey committed 2022-08-12 19:22:24 -07:00
1 parent d8bace07c8
commit cbf933665b
27 files changed
+356 -711

No files matched your search

@@ -10,6 +10,8 @@
//===----------------------------------------------------------------------===//
extension BitSet: Sequence {
public typealias Element = Int
@inlinable
@inline(__always)
public var underestimatedCount: Int {
@@ -22,14 +24,14 @@ extension BitSet: Sequence {
}
public func _customContainsEquatableElement(
_ element: Element
_ element: Int
) -> Bool? {
guard let element = UInt(exactly: element) else { return false }
return _core._read { $0.contains(element) }
return _contains(element)
}
public struct Iterator: IteratorProtocol {
internal typealias _UnsafeHandle = _BitSet.UnsafeHandle
internal typealias _UnsafeHandle = BitSet._UnsafeHandle
internal let bitset: BitSet
internal var index: Int
@@ -39,25 +41,25 @@ extension BitSet: Sequence {
internal init(_ bitset: BitSet) {
self.bitset = bitset
self.index = 0
self.word = bitset._core._read { handle in
self.word = bitset._read { handle in
guard handle.wordCount > 0 else { return .empty }
return handle._words[0]
}
}
@_effects(releasenone)
public mutating func next() -> Element? {
public mutating func next() -> Int? {
if let bit = word.next() {
let i = _UnsafeHandle.Index(word: index, bit: bit)
return Element(truncatingIfNeeded: i.value)
return Int(truncatingIfNeeded: i.value)
}
return bitset._core._read { handle in
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 Element(truncatingIfNeeded: i.value)
return Int(truncatingIfNeeded: i.value)
}
}
return nil
@@ -67,28 +69,28 @@ extension BitSet: Sequence {
}
extension BitSet: Collection, BidirectionalCollection {
public var isEmpty: Bool { _core._count == 0 }
public var count: Int { _core._count }
public var isEmpty: Bool { _count == 0 }
public var count: Int { _count }
public var startIndex: Index {
// Note: This is O(n)
Index(_position: _core._read { $0.startIndex })
Index(_position: _read { $0.startIndex })
}
public var endIndex: Index {
Index(_position: .init(word: _core._storage.count, bit: 0))
Index(_position: .init(word: _storage.count, bit: 0))
}
public subscript(position: Index) -> Element {
Element(position._position.value)
public subscript(position: Index) -> Int {
Int(bitPattern: position._position.value)
}
public func index(after index: Index) -> Index {
Index(_position: _core._read { $0.index(after: index._position) })
Index(_position: _read { $0.index(after: index._position) })
}
public func index(before index: Index) -> Index {
Index(_position: _core._read { $0.index(before: index._position) })
Index(_position: _read { $0.index(before: index._position) })
}
#if false // TODO: Specialize these
@@ -107,14 +109,12 @@ extension BitSet: Collection, BidirectionalCollection {
}
#endif
public func _customIndexOfEquatableElement(_ element: Element) -> Index?? {
public func _customIndexOfEquatableElement(_ element: Int) -> Index?? {
guard contains(element) else { return .some(nil) }
return Index(_value: UInt(element))
return Index(_value: UInt(bitPattern: element))
}
public func _customLastIndexOfEquatableElement(
_ element: Element
) -> Index?? {
public func _customLastIndexOfEquatableElement(_ element: Int) -> Index?? {
_customIndexOfEquatableElement(element)
}
}
@@ -11,6 +11,6 @@
extension BitSet: Equatable {
public static func ==(left: Self, right: Self) -> Bool {
left._core._storage == right._core._storage
left._storage == right._storage
}
}
@@ -10,7 +10,8 @@
//===----------------------------------------------------------------------===//
extension BitSet: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: Element...) {
@inlinable
public init(arrayLiteral elements: Int...) {
self.init(elements)
}
}
@@ -11,6 +11,6 @@
extension BitSet: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(_core._storage)
hasher.combine(_storage)
}
}
@@ -17,7 +17,7 @@ extension BitSet {
@inlinable
public init<S: Sequence>(
_ elements: __owned S
) where S.Element == Element {
) where S.Element == Int {
if S.self == BitSet.self {
self = (elements as! BitSet)
return
@@ -31,45 +31,38 @@ extension BitSet {
@inlinable
internal init<S: Sequence>(
_validMembersOf elements: __owned S
) where S.Element == Element {
) where S.Element == Int {
if S.self == BitSet.self {
self = (elements as! BitSet)
return
}
if S.self == Range<Element>.self {
let r = (elements as! Range<Element>)
self.init(_core: _BitSet(r._clampedToUInt()))
if S.self == Range<Int>.self {
let r = (elements as! Range<Int>)
self.init(_range: r._clampedToUInt())
return
}
self.init()
for value in elements {
guard let value = UInt(exactly: value) else { continue }
_ = self._core.insert(value)
self._insert(value)
}
}
}
extension BitSet {
@inlinable
public init(_ range: Range<Element>) {
guard
let lower = UInt(exactly: range.lowerBound),
let upper = UInt(exactly: range.upperBound)
else {
public init(_ range: Range<Int>) {
guard let range = range._toUInt() else {
preconditionFailure("BitSet can only hold nonnegative integers")
}
let range = Range(uncheckedBounds: (lower, upper))
self.init(_core: _BitSet(range))
self.init(_range: range)
}
}
extension _BitSet {
@usableFromInline
internal init(_ range: Range<UInt>) {
internal init(_range range: Range<UInt>) {
_count = range.count
_storage = []
let lower = UnsafeHandle.Index(range.lowerBound)
let upper = UnsafeHandle.Index(range.upperBound)
let lower = _UnsafeHandle.Index(range.lowerBound)
let upper = _UnsafeHandle.Index(range.upperBound)
if lower.word > 0 {
_storage.append(contentsOf: repeatElement(.empty, count: lower.word))
}
@@ -98,9 +91,9 @@ extension BitSet {
}
}
extension _BitSet {
extension BitSet {
internal init(
_combining handles: (UnsafeHandle, UnsafeHandle),
_combining handles: (_UnsafeHandle, _UnsafeHandle),
includingTail: Bool,
using function: (_Word, _Word) -> _Word
) {
@@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
extension BitSet {
#if COLLECTIONS_INTERNAL_CHECKS
@inline(never)
@_effects(releasenone)
@@ -9,65 +9,55 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
extension BitSet {
@usableFromInline
internal func contains(_ member: UInt) -> Bool {
internal func _contains(_ member: UInt) -> Bool {
_read { $0.contains(member) }
}
}
extension BitSet {
@inlinable
public func contains(_ member: Element) -> Bool {
guard let value = UInt(exactly: member) else { return false }
return _core.contains(value)
}
}
extension _BitSet {
@usableFromInline
internal mutating func insert(
_ newMember: UInt
) -> Bool {
_ensureCapacity(forValue: newMember)
return _update { $0.insert(newMember) }
public func contains(_ member: Int) -> Bool {
guard let member = UInt(exactly: member) else { return false }
return _contains(member)
}
}
extension BitSet {
@inlinable
@discardableResult
public mutating func _insert(_ i: UInt) -> Bool {
_ensureCapacity(forValue: i)
let inserted = _update { $0.insert(i) }
return inserted
}
@discardableResult
public mutating func insert(
_ newMember: Element
) -> (inserted: Bool, memberAfterInsert: Element) {
_ newMember: Int
) -> (inserted: Bool, memberAfterInsert: Int) {
guard let i = UInt(exactly: newMember) else {
preconditionFailure("Value out of range")
}
return (_core.insert(i), newMember)
return (_insert(i), newMember)
}
@inlinable
@discardableResult
public mutating func update(with newMember: Element) -> Element? {
public mutating func update(with newMember: Int) -> Int? {
insert(newMember).inserted ? newMember : nil
}
}
extension _BitSet {
extension BitSet {
@discardableResult
@usableFromInline
internal mutating func remove(_ member: UInt) -> Bool {
internal mutating func _remove(_ member: UInt) -> Bool {
_updateThenShrink { handle, shrink in
shrink = handle.remove(member)
return shrink
}
}
}
extension BitSet {
@inlinable
@discardableResult
public mutating func remove(_ member: Element) -> Element? {
public mutating func remove(_ member: Int) -> Int? {
guard let m = UInt(exactly: member) else { return nil }
return _core.remove(m) ? member : nil
return _remove(m) ? member : nil
}
}
@@ -9,9 +9,8 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal mutating func formIntersection(_ other: Self) {
extension BitSet {
public mutating func formIntersection(_ other: Self) {
other._read { source in
if source.wordCount < _storage.count {
self._storage.removeLast(_storage.count - source.wordCount)
@@ -23,13 +22,24 @@ extension _BitSet {
}
}
@usableFromInline
internal mutating func formIntersection(_ other: Range<UInt>) {
guard let last = other.last else {
self = _BitSet()
@inlinable
public mutating func formIntersection<S: Sequence>(
_ other: __owned S
) where S.Element == Int {
if S.self == Range<Int>.self {
formIntersection(other as! Range<Int>)
return
}
let lastWord = UnsafeHandle.Index(last).word
formIntersection(BitSet(_validMembersOf: other))
}
public mutating func formIntersection(_ other: Range<Int>) {
let other = other._clampedToUInt()
guard let last = other.last else {
self = BitSet()
return
}
let lastWord = _UnsafeHandle.Index(last).word
if _storage.count - lastWord - 1 > 0 {
_storage.removeLast(_storage.count - lastWord - 1)
}
@@ -38,33 +48,3 @@ extension _BitSet {
}
}
}
extension BitSet {
@inlinable
public mutating func formIntersection(_ other: Self) {
_core.formIntersection(other._core)
}
@inlinable
public mutating func formIntersection<I: FixedWidthInteger>(
_ other: BitSet<I>
) {
_core.formIntersection(other._core)
}
@inlinable
public mutating func formIntersection<S: Sequence>(
_ other: __owned S
) where S.Element == Element {
if S.self == Range<S.Element>.self {
formIntersection(other as! Range<S.Element>)
return
}
formIntersection(BitSet(_validMembersOf: other))
}
@inlinable
public mutating func formIntersection(_ other: Range<Element>) {
_core.formIntersection(other._clampedToUInt())
}
}
@@ -9,9 +9,8 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal mutating func formSymmetricDifference(_ other: __owned Self) {
extension BitSet {
public mutating func formSymmetricDifference(_ other: __owned Self) {
_ensureCapacity(limit: other._capacity)
_updateThenShrink { target, shrink in
other._read { source in
@@ -21,8 +20,23 @@ extension _BitSet {
}
}
@usableFromInline
internal mutating func formSymmetricDifference(_ other: Range<UInt>) {
@inlinable
public mutating func formSymmetricDifference<S: Sequence>(
_ other: __owned S
) where S.Element == Int {
if S.self == Range<Int>.self {
formSymmetricDifference(other as! Range<Int>)
return
}
formSymmetricDifference(BitSet(other))
}
}
extension BitSet {
public mutating func formSymmetricDifference(_ other: Range<Int>) {
guard let other = other._toUInt() else {
preconditionFailure("Invalid range")
}
guard !other.isEmpty else { return }
_ensureCapacity(limit: other.upperBound)
_updateThenShrink { handle, shrink in
@@ -30,33 +44,3 @@ extension _BitSet {
}
}
}
extension BitSet {
@inlinable
public mutating func formSymmetricDifference(_ other: __owned Self) {
_core.formSymmetricDifference(other._core)
}
@inlinable
public mutating func formSymmetricDifference<S: Sequence>(
_ other: __owned S
) where S.Element == Element {
if S.self == Range<S.Element>.self {
formSymmetricDifference(other as! Range<S.Element>)
return
}
_core.formSymmetricDifference(BitSet(other)._core)
}
}
extension BitSet {
@inlinable
public mutating func formSymmetricDifference(
_ other: Range<Element>
) {
guard let other = other._toUInt() else {
preconditionFailure("Invalid range")
}
_core.formSymmetricDifference(other)
}
}
@@ -9,9 +9,14 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
extension BitSet {
@usableFromInline
internal mutating func formUnion(_ other: __owned Self) {
internal mutating func formUnion(_ other: Range<UInt>) {
}
}
extension BitSet {
public mutating func formUnion(_ other: __owned Self) {
_ensureCapacity(limit: other._capacity)
_update { target in
other._read { source in
@@ -20,8 +25,27 @@ extension _BitSet {
}
}
@usableFromInline
internal mutating func formUnion(_ other: Range<UInt>) {
@inlinable
public mutating func formUnion<S: Sequence>(
_ other: __owned S
) where S.Element == Int {
if S.self == BitSet.self {
formUnion(other as! BitSet)
return
}
if S.self == Range<Int>.self {
formUnion(other as! Range<Int>)
return
}
for value in other {
self.insert(value)
}
}
public mutating func formUnion(_ other: Range<Int>) {
guard let other = other._toUInt() else {
preconditionFailure("Invalid range")
}
guard !other.isEmpty else { return }
_ensureCapacity(limit: other.upperBound)
_update { handle in
@@ -30,35 +54,3 @@ extension _BitSet {
}
}
extension BitSet {
@inlinable
public mutating func formUnion(_ other: __owned Self) {
_core.formUnion(other._core)
}
@inlinable
public mutating func formUnion<S: Sequence>(
_ other: __owned S
) where S.Element == Element {
if S.self == BitSet.self {
formUnion(other as! BitSet)
return
}
if S.self == Range<S.Element>.self {
formUnion(other as! Range<S.Element>)
return
}
for value in other {
self.insert(value)
}
}
@inlinable
public mutating func formUnion(_ other: Range<Element>) {
guard let other = other._toUInt() else {
preconditionFailure("Invalid range")
}
_core.formUnion(other)
}
}
@@ -9,9 +9,8 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal __consuming func intersection(_ other: Self) -> Self {
extension BitSet {
public __consuming func intersection(_ other: Self) -> Self {
self._read { first in
other._read { second in
Self(
@@ -21,30 +20,23 @@ extension _BitSet {
}
}
}
}
extension BitSet {
public __consuming func intersection(_ other: Self) -> Self {
Self(_core: _core.intersection(other._core))
}
@inlinable
public __consuming func intersection<S: Sequence>(
_ other: __owned S
) -> Self
where S.Element == Element
where S.Element == Int
{
if S.self == Self.self {
return intersection(other as! BitSet)
}
if S.self == Range<Element>.self {
return intersection(other as! Range<Element>)
if S.self == Range<Int>.self {
return intersection(other as! Range<Int>)
}
return intersection(BitSet(_validMembersOf: other))
}
@inlinable
public __consuming func intersection(_ other: Range<Element>) -> Self {
public __consuming func intersection(_ other: Range<Int>) -> Self {
var result = self
result.formIntersection(other)
return result
@@ -9,9 +9,8 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal func isDisjoint(with other: Self) -> Bool {
extension BitSet {
public func isDisjoint(with other: Self) -> Bool {
self._read { first in
other._read { second in
let w1 = first._words
@@ -24,34 +23,15 @@ extension _BitSet {
}
}
@usableFromInline
internal func isDisjoint(with other: Range<UInt>) -> Bool {
_read { $0.isDisjoint(with: other) }
}
}
extension BitSet {
@inlinable
public func isDisjoint(with other: Self) -> Bool {
_core.isDisjoint(with: other._core)
}
@inlinable
public func isDisjoint<I: FixedWidthInteger>(
with other: BitSet<I>
) -> Bool {
_core.isDisjoint(with: other._core)
}
@inlinable
public func isDisjoint<S: Sequence>(with other: S) -> Bool
where S.Element == Element
where S.Element == Int
{
if S.self == BitSet.self {
return self.isDisjoint(with: other as! BitSet)
}
if S.self == Range<Element>.self {
return self.isDisjoint(with: other as! Range<Element>)
if S.self == Range<Int>.self {
return self.isDisjoint(with: other as! Range<Int>)
}
for value in other {
guard !contains(value) else { return false }
@@ -61,15 +41,7 @@ extension BitSet {
}
extension BitSet {
@inlinable
public func isDisjoint(with other: Range<Element>) -> Bool {
_core.isDisjoint(with: other._clampedToUInt())
}
@inlinable
public func isDisjoint<I: FixedWidthInteger>(
with other: Range<I>
) -> Bool {
_core.isDisjoint(with: other._clampedToUInt())
public func isDisjoint(with other: Range<Int>) -> Bool {
_read { $0.isDisjoint(with: other._clampedToUInt()) }
}
}
@@ -9,9 +9,8 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal func isStrictSubset(of other: Self) -> Bool {
extension BitSet {
public func isStrictSubset(of other: Self) -> Bool {
self._read { first in
other._read { second in
let w1 = first._words
@@ -30,30 +29,16 @@ extension _BitSet {
}
}
}
}
extension BitSet {
@inlinable
public func isStrictSubset(of other: Self) -> Bool {
_core.isStrictSubset(of: other._core)
}
@inlinable
public func isStrictSubset<I: FixedWidthInteger>(
of other: BitSet<I>
) -> Bool {
_core.isStrictSubset(of: other._core)
}
@inlinable
public func isStrictSubset<S: Sequence>(of other: S) -> Bool
where S.Element == Element
where S.Element == Int
{
if S.self == BitSet.self {
return isStrictSubset(of: other as! BitSet)
}
if S.self == Range<Element>.self {
return isStrictSubset(of: other as! Range<Element>)
if S.self == Range<Int>.self {
return isStrictSubset(of: other as! Range<Int>)
}
if isEmpty {
@@ -62,7 +47,7 @@ extension BitSet {
}
return _UnsafeHandle.withTemporaryBitset(
wordCount: _core._storage.count
wordCount: _storage.count
) { seen in
var strict = false
var it = other.makeIterator()
@@ -83,15 +68,7 @@ extension BitSet {
}
}
@inlinable
public func isStrictSubset(of other: Range<Element>) -> Bool {
isSubset(of: other) && !isSuperset(of: other)
}
@inlinable
public func isStrictSubset<I: FixedWidthInteger>(
of other: Range<I>
) -> Bool {
public func isStrictSubset(of other: Range<Int>) -> Bool {
isSubset(of: other) && !isSuperset(of: other)
}
}
@@ -9,64 +9,37 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal func isStrictSuperset(of other: Range<UInt>) -> Bool {
_read { $0.isSuperset(of: other) && !$0.isSubset(of: other) }
}
}
extension BitSet {
@inlinable
public func isStrictSuperset(of other: Self) -> Bool {
other.isStrictSubset(of: self)
}
@inlinable
public func isStrictSuperset<I: FixedWidthInteger>(
of other: BitSet<I>
) -> Bool {
other.isStrictSubset(of: self)
}
@inlinable
public func isStrictSuperset<S: Sequence>(of other: S) -> Bool
where S.Element == Element
where S.Element == Int
{
guard !isEmpty else { return false }
if S.self == BitSet.self {
return isStrictSuperset(of: other as! BitSet)
}
if S.self == Range<Element>.self {
return isStrictSuperset(of: other as! Range<Element>)
if S.self == Range<Int>.self {
return isStrictSuperset(of: other as! Range<Int>)
}
return _UnsafeHandle.withTemporaryBitset(
wordCount: _core._storage.count
wordCount: _storage.count
) { seen in
for i in other {
guard let i = UInt(exactly: i) else { return false }
if !_core.contains(i) { return false }
seen.insert(i)
guard contains(i) else { return false }
seen.insert(UInt(i))
}
return seen._count < self.count
}
}
@inlinable
public func isStrictSuperset(of other: Range<Element>) -> Bool {
public func isStrictSuperset(of other: Range<Int>) -> Bool {
if other.isEmpty { return !isEmpty }
if isEmpty { return false }
guard let r = other._toUInt() else { return false }
return _core.isStrictSuperset(of: r)
}
@inlinable
public func isStrictSuperset<I: FixedWidthInteger>(
of other: Range<I>
) -> Bool {
if other.isEmpty { return !isEmpty }
if isEmpty { return false }
guard let r = other._toUInt() else { return false }
return _core.isStrictSuperset(of: r)
return _read { $0.isSuperset(of: r) && !$0.isSubset(of: r) }
}
}
@@ -9,9 +9,8 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal func isSubset(of other: Self) -> Bool {
extension BitSet {
public func isSubset(of other: Self) -> Bool {
self._read { first in
other._read { second in
let w1 = first._words
@@ -29,56 +28,28 @@ extension _BitSet {
}
}
@usableFromInline
internal func isSubset(of other: Range<UInt>) -> Bool {
_read { $0.isSubset(of: other) }
}
}
extension BitSet {
@inlinable
public func isSubset(of other: Self) -> Bool {
_core.isSubset(of: other._core)
}
@inlinable
public func isSubset<I: FixedWidthInteger>(
of other: BitSet<I>
) -> Bool {
_core.isSubset(of: other._core)
}
@inlinable
public func isSubset<S: Sequence>(of other: S) -> Bool
where S.Element == Element
where S.Element == Int
{
if S.self == BitSet.self {
return self.isSubset(of: other as! BitSet)
}
if S.self == Range<Element>.self {
return self.isSubset(of: other as! Range<Element>)
if S.self == Range<Int>.self {
return self.isSubset(of: other as! Range<Int>)
}
guard !isEmpty else { return true }
var t = self
for i in other {
guard let i = UInt(exactly: i) else { continue }
if t._core.remove(i), t.isEmpty { return true }
if t._remove(i), t.isEmpty { return true }
}
assert(!t.isEmpty)
return false
}
@inlinable
public func isSubset(of other: Range<Element>) -> Bool {
public func isSubset(of other: Range<Int>) -> Bool {
guard !isEmpty else { return true }
return _core.isSubset(of: other._clampedToUInt())
}
@inlinable
public func isSubset<I: FixedWidthInteger>(
of other: Range<I>
) -> Bool {
guard !isEmpty else { return true }
return _core.isSubset(of: other._clampedToUInt())
return _read { $0.isSubset(of: other._clampedToUInt()) }
}
}
@@ -9,58 +9,32 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal func isSuperset(of other: Range<UInt>) -> Bool {
_read { $0.isSuperset(of: other) }
}
}
extension BitSet {
@inlinable
public func isSuperset(of other: Self) -> Bool {
other.isSubset(of: self)
}
@inlinable
public func isSuperset<I: FixedWidthInteger>(
of other: BitSet<I>
) -> Bool {
other.isSubset(of: self)
}
@inlinable
public func isSuperset<S: Sequence>(of other: S) -> Bool
where S.Element == Element
where S.Element == Int
{
if S.self == BitSet.self {
return self.isSuperset(of: other as! BitSet)
}
if S.self == Range<Element>.self {
return self.isSuperset(of: other as! Range<Element>)
if S.self == Range<Int>.self {
return self.isSuperset(of: other as! Range<Int>)
}
for i in other {
guard let i = UInt(exactly: i) else { return false }
if !_core.contains(i) { return false }
if !_contains(i) { return false }
}
return true
}
@inlinable
public func isSuperset(of other: Range<Element>) -> Bool {
public func isSuperset(of other: Range<Int>) -> Bool {
if other.isEmpty { return true }
if isEmpty { return false }
guard let r = other._toUInt() else { return false }
return _core.isSuperset(of: r)
}
@inlinable
public func isSuperset<I: FixedWidthInteger>(
of other: Range<I>
) -> Bool {
if other.isEmpty { return true }
if isEmpty { return false }
guard let r = other._toUInt() else { return false }
return _core.isSuperset(of: r)
return _read { $0.isSuperset(of: r) }
}
}
@@ -9,9 +9,8 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal mutating func subtract(_ other: Self) {
extension BitSet {
public mutating func subtract(_ other: Self) {
_updateThenShrink { target, shrink in
other._read { source in
target.combineSharedPrefix(
@@ -23,7 +22,7 @@ extension _BitSet {
}
@usableFromInline
internal mutating func subtract(_ next: () -> UInt?) {
internal mutating func _subtract(_ next: () -> UInt?) {
_updateThenShrink { handle, shrink in
while let value = next() {
handle.remove(value)
@@ -31,42 +30,20 @@ extension _BitSet {
}
}
@usableFromInline
internal mutating func subtract(_ other: Range<UInt>) {
guard !other.isEmpty else { return }
_updateThenShrink { handle, shrink in
handle.subtract(other)
}
}
}
extension BitSet {
@inlinable
public mutating func subtract(_ other: Self) {
_core.subtract(other._core)
}
@inlinable
public mutating func subtract<I: FixedWidthInteger>(
_ other: BitSet<I>
) {
_core.subtract(other._core)
}
@inlinable
public mutating func subtract<S: Sequence>(
_ other: __owned S
) where S.Element: FixedWidthInteger {
) where S.Element == Int {
if S.self == BitSet.self {
self.subtract(other as! BitSet)
return
}
if S.self == Range<Element>.self {
self.subtract(other as! Range<Element>)
if S.self == Range<Int>.self {
self.subtract(other as! Range<Int>)
return
}
var it = other.makeIterator()
_core.subtract {
_subtract {
while let value = it.next() {
if let value = UInt(exactly: value) {
return value
@@ -76,15 +53,15 @@ extension BitSet {
}
}
@inlinable
public mutating func subtract(_ other: Range<Element>) {
_core.subtract(other._clampedToUInt())
@usableFromInline
internal mutating func _subtract(_ other: Range<UInt>) {
guard !other.isEmpty else { return }
_updateThenShrink { handle, shrink in
handle.subtract(other)
}
}
@inlinable
public mutating func subtract<I: FixedWidthInteger>(
_ other: Range<I>
) {
_core.subtract(other._clampedToUInt())
public mutating func subtract(_ other: Range<Int>) {
_subtract(other._clampedToUInt())
}
}
@@ -9,9 +9,8 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal __consuming func subtracting(_ other: Self) -> Self {
extension BitSet {
public __consuming func subtracting(_ other: Self) -> Self {
self._read { first in
other._read { second in
Self(
@@ -21,43 +20,19 @@ extension _BitSet {
}
}
}
}
extension BitSet {
@inlinable
public __consuming func subtracting(_ other: Self) -> Self {
BitSet(_core: _core.subtracting(other._core))
}
@inlinable
public __consuming func subtracting<I: FixedWidthInteger>(
_ other: BitSet<I>
) -> Self {
BitSet(_core: _core.subtracting(other._core))
}
@inlinable
public __consuming func subtracting<S: Sequence>(
_ other: __owned S
) -> Self
where S.Element: FixedWidthInteger
where S.Element == Int
{
var result = self
result.subtract(other)
return result
}
@inlinable
public __consuming func subtracting(_ other: Range<Element>) -> Self {
var result = self
result.subtract(other)
return result
}
@inlinable
public __consuming func subtracting<I: FixedWidthInteger>(
_ other: Range<I>
) -> Self {
public __consuming func subtracting(_ other: Range<Int>) -> Self {
var result = self
result.subtract(other)
return result
@@ -9,9 +9,8 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal __consuming func symmetricDifference(_ other: __owned Self) -> Self {
extension BitSet {
public __consuming func symmetricDifference(_ other: __owned Self) -> Self {
self._read { first in
other._read { second in
Self(
@@ -21,27 +20,20 @@ extension _BitSet {
}
}
}
}
extension BitSet {
public __consuming func symmetricDifference(_ other: __owned Self) -> Self {
BitSet(_core: _core.symmetricDifference(other._core))
}
@inlinable
public __consuming func symmetricDifference<S: Sequence>(
_ other: __owned S
) -> Self
where S.Element == Element
where S.Element == Int
{
if S.self == Range<Element>.self {
return symmetricDifference(other as! Range<Element>)
if S.self == Range<Int>.self {
return symmetricDifference(other as! Range<Int>)
}
return symmetricDifference(BitSet(other))
}
@inlinable
public __consuming func symmetricDifference(_ other: Range<Element>) -> Self {
public __consuming func symmetricDifference(_ other: Range<Int>) -> Self {
var result = self
result.formSymmetricDifference(other)
return result
@@ -9,9 +9,8 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
@usableFromInline
internal __consuming func union(_ other: __owned Self) -> Self {
extension BitSet {
public __consuming func union(_ other: __owned Self) -> Self {
self._read { first in
other._read { second in
Self(
@@ -21,19 +20,12 @@ extension _BitSet {
}
}
}
}
extension BitSet {
@inlinable
public __consuming func union(_ other: __owned Self) -> Self {
BitSet(_core: _core.union(other._core))
}
@inlinable
public __consuming func union<S: Sequence>(
_ other: __owned S
) -> Self
where S.Element == Element
where S.Element == Int
{
if S.self == BitSet.self {
return union(other as! BitSet)
@@ -43,8 +35,7 @@ extension BitSet {
return result
}
@inlinable
public __consuming func union(_ other: Range<Element>) -> Self {
public __consuming func union(_ other: Range<Int>) -> Self {
var result = self
result.formUnion(other)
return result
@@ -9,12 +9,12 @@
//
//===----------------------------------------------------------------------===//
extension _BitSet {
extension BitSet {
/// An unsafe-unowned bitset view over `UInt` storage, providing bit set
/// primitives.
@usableFromInline
@frozen
internal struct UnsafeHandle {
internal struct _UnsafeHandle {
/// An unsafe-unowned storage view.
@usableFromInline
internal let _words: UnsafeBufferPointer<_Word>
@@ -98,7 +98,7 @@ extension _BitSet {
}
}
extension _BitSet.UnsafeHandle {
extension BitSet._UnsafeHandle {
@inlinable
@inline(__always)
internal var wordCount: Int {
@@ -112,7 +112,7 @@ extension _BitSet.UnsafeHandle {
}
}
extension _BitSet.UnsafeHandle {
extension BitSet._UnsafeHandle {
@inlinable
@inline(__always)
static func withTemporaryBitset<R>(
@@ -170,7 +170,7 @@ extension _BitSet.UnsafeHandle {
}
}
extension _BitSet.UnsafeHandle {
extension BitSet._UnsafeHandle {
@usableFromInline
@_effects(readnone)
@inline(__always)
@@ -275,7 +275,7 @@ extension _BitSet.UnsafeHandle {
}
}
extension _BitSet.UnsafeHandle: Sequence {
extension BitSet._UnsafeHandle: Sequence {
@usableFromInline
internal typealias Element = UInt
@@ -295,10 +295,10 @@ extension _BitSet.UnsafeHandle: Sequence {
@frozen
internal struct Iterator: IteratorProtocol {
@usableFromInline
internal typealias _UnsafeHandle = _BitSet.UnsafeHandle
internal typealias _UnsafeHandle = BitSet._UnsafeHandle
@usableFromInline
internal let bitset: _BitSet.UnsafeHandle
internal let bitset: _UnsafeHandle
@usableFromInline
internal var index: Int
@@ -331,7 +331,7 @@ extension _BitSet.UnsafeHandle: Sequence {
}
}
extension _BitSet.UnsafeHandle: BidirectionalCollection {
extension BitSet._UnsafeHandle: BidirectionalCollection {
@usableFromInline
internal typealias Index = _BitPosition
@@ -400,7 +400,7 @@ extension _BitSet.UnsafeHandle: BidirectionalCollection {
}
}
extension _BitSet.UnsafeHandle {
extension BitSet._UnsafeHandle {
internal mutating func combineSharedPrefix(
with other: Self,
using function: (inout _Word, _Word) -> Void
@@ -414,7 +414,7 @@ extension _BitSet.UnsafeHandle {
}
}
extension _BitSet.UnsafeHandle {
extension BitSet._UnsafeHandle {
internal func count(in range: Range<UInt>) -> Int {
let l = Index(Swift.min(range.lowerBound, capacity))
let u = Index(Swift.min(range.upperBound, capacity))
+79 -9
View File
@@ -14,20 +14,90 @@
///
/// Bit sets implement `SetAlgebra` and provide efficient implementations
/// for set operations based on standard binary logic operations.
public struct BitSet<Element: FixedWidthInteger> {
public struct BitSet {
@usableFromInline
internal typealias _UnsafeHandle = _BitSet.UnsafeHandle
internal var _storage: [_Word]
@usableFromInline
internal var _core: _BitSet
@usableFromInline
init(_core: _BitSet) {
self._core = _core
}
internal var _count: Int
@usableFromInline
init(_storage: [_Word], count: Int) {
self.init(_core: _BitSet(storage: _storage, count: count))
self._storage = _storage
self._count = count
}
}
extension BitSet {
@inline(__always)
internal func _read<R>(
_ body: (_UnsafeHandle) throws -> R
) rethrows -> R {
try _storage.withUnsafeBufferPointer { words in
let handle = _UnsafeHandle(words: words, count: _count, mutable: false)
return try body(handle)
}
}
@usableFromInline
internal var _capacity: UInt {
UInt(_storage.count) &* UInt(_Word.capacity)
}
internal mutating func _ensureCapacity(limit capacity: UInt) {
let desired = _UnsafeHandle.wordCount(forCapacity: capacity)
guard _storage.count < desired else { return }
_storage.append(
contentsOf: repeatElement(.empty, count: desired - _storage.count))
}
internal mutating func _ensureCapacity(forValue value: UInt) {
let desiredWord = _UnsafeHandle.Index(value).word
guard desiredWord >= _storage.count else { return }
_storage.append(
contentsOf:
repeatElement(.empty, count: desiredWord - _storage.count + 1))
}
internal mutating func _shrink() {
let suffix = _read { $0.emptySuffix() }
if suffix > 0 { _storage.removeLast(suffix) }
}
@inline(__always)
internal mutating func _update<R>(
_ body: (inout _UnsafeHandle) throws -> R
) rethrows -> R {
var c = _count
defer {
_count = c
_checkInvariants()
}
return try _storage.withUnsafeMutableBufferPointer { words in
var handle = _UnsafeHandle(words: words, count: c, mutable: true)
defer {
assert(c <= words.count * _Word.capacity)
c = handle.count
}
return try body(&handle)
}
}
@inline(__always)
internal mutating func _updateThenShrink<R>(
_ body: (_ handle: inout _UnsafeHandle, _ shrink: inout Bool) throws -> R
) rethrows -> R {
var shrink = true
defer {
if shrink { _shrink() }
_checkInvariants()
}
return try _storage.withUnsafeMutableBufferPointer { words in
var handle = _UnsafeHandle(words: words, count: _count, mutable: true)
defer {
_count = handle.count
}
return try body(&handle, &shrink)
}
}
}
-105
View File
@@ -1,105 +0,0 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
public struct _BitSet {
@usableFromInline
internal var _storage: [_Word]
@usableFromInline
internal var _count: Int
@usableFromInline
init() {
_storage = []
_count = 0
}
@usableFromInline
init(storage: [_Word], count: Int) {
self._storage = storage
self._count = count
}
}
extension _BitSet {
@inline(__always)
internal func _read<R>(
_ body: (UnsafeHandle) throws -> R
) rethrows -> R {
try _storage.withUnsafeBufferPointer { words in
let handle = UnsafeHandle(words: words, count: _count, mutable: false)
return try body(handle)
}
}
@usableFromInline
internal var _capacity: UInt {
UInt(_storage.count) &* UInt(_Word.capacity)
}
internal mutating func _ensureCapacity(limit capacity: UInt) {
let desired = UnsafeHandle.wordCount(forCapacity: capacity)
guard _storage.count < desired else { return }
_storage.append(
contentsOf: repeatElement(.empty, count: desired - _storage.count))
}
internal mutating func _ensureCapacity(forValue value: UInt) {
let desiredWord = UnsafeHandle.Index(value).word
guard desiredWord >= _storage.count else { return }
_storage.append(
contentsOf:
repeatElement(.empty, count: desiredWord - _storage.count + 1))
}
internal mutating func _shrink() {
let suffix = _read { $0.emptySuffix() }
if suffix > 0 { _storage.removeLast(suffix) }
}
@inline(__always)
internal mutating func _update<R>(
_ body: (inout UnsafeHandle) throws -> R
) rethrows -> R {
var c = _count
defer {
_count = c
_checkInvariants()
}
return try _storage.withUnsafeMutableBufferPointer { words in
var handle = UnsafeHandle(words: words, count: c, mutable: true)
defer {
assert(c <= words.count * _Word.capacity)
c = handle.count
}
return try body(&handle)
}
}
@inline(__always)
internal mutating func _updateThenShrink<R>(
_ body: (_ handle: inout UnsafeHandle, _ shrink: inout Bool) throws -> R
) rethrows -> R {
var shrink = true
defer {
if shrink { _shrink() }
_checkInvariants()
}
return try _storage.withUnsafeMutableBufferPointer { words in
var handle = UnsafeHandle(
words: words, count: _count, mutable: true)
defer {
_count = handle.count
}
return try body(&handle, &shrink)
}
}
}