BitArrayModule ⟹ BitCollections

This commit is contained in:
Károly Lőrentey committed 2021-12-28 15:45:38 -08:00
1 parent 49a82fe6e1
commit 537382f2a6
14 files changed
+58 -10

No files matched your search

@@ -0,0 +1,66 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 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: ExpressibleByArrayLiteral, Codable {
var storage = BitArray()
public init() { }
public init<S>(_ sequence: __owned S) where S : Sequence, Int == S.Element {
for value in sequence {
while (value >= storage.count) {
storage.storage.append(0)
}
storage[value] = true
}
storage.excess = 0
}
public init(arrayLiteral elements: Int...) {
for value in elements {
while (value >= storage.count) {
storage.storage.append(0)
}
storage[value] = true
}
storage.excess = 0
}
public init(_ bitArray: BitArray) {
storage = bitArray
// clean up overly excess false values
while(storage.storage.count != 0 && storage.storage[storage.storage.endIndex-1] == 0) {
storage.storage.removeLast()
}
storage.excess = 0
}
public struct Index {
var bitArrayIndex: Int
internal init(bitArrayIndex: Int) {
self.bitArrayIndex = bitArrayIndex
}
}
}
extension BitSet.Index: Comparable, Hashable {
public static func < (lhs: BitSet.Index, rhs: BitSet.Index) -> Bool {
return (lhs.bitArrayIndex < rhs.bitArrayIndex)
}
}
@@ -0,0 +1,105 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 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: Collection, BidirectionalCollection {
public var isEmpty: Bool { return count == 0 }
public var startIndex: Index { return _getStartIndex() }
// leaving it like this might be correct if firstTrueIndex was fast enough -- BENCHMARKS
private func _getStartIndex() -> Index {
guard let index = storage.firstTrue() else {
return Index(bitArrayIndex: storage.endIndex)
}
return Index(bitArrayIndex: index)
}
public var endIndex: Index { return Index(bitArrayIndex: storage.endIndex) }
public var count: Int {
var count = 0
for byte in storage.storage {
count += byte.nonzeroBitCount
}
return count
}
public subscript(position: Index) -> Int {
get {
precondition((startIndex.bitArrayIndex <= position.bitArrayIndex) && (endIndex.bitArrayIndex > position.bitArrayIndex), "Given Index is out of range")
precondition((storage[position.bitArrayIndex]), "Index passed in is invalid: does not exist in the set")
return position.bitArrayIndex
}
}
public func index(after: Index) -> Index {
precondition((startIndex.bitArrayIndex <= after.bitArrayIndex) && (endIndex.bitArrayIndex > after.bitArrayIndex), "Given Index is out of range")
precondition((storage[after.bitArrayIndex]), "Index passed in is invalid: does not exist in the set")
precondition(after.bitArrayIndex != endIndex.bitArrayIndex, "Passed in Index is already the endIndex, and has no existing Indexes after it")
// Potentially optimizeable using leadingZeroCount/trailingZeroCount
for i in (after.bitArrayIndex+1)..<storage.count {
if (storage[i]) {
return Index(bitArrayIndex: i)
}
}
return endIndex
}
public func index(before: Index) -> Index {
precondition(before.bitArrayIndex != startIndex.bitArrayIndex, "Passed in Index is already the startIndex, and has no existing Indexes before it")
precondition((startIndex.bitArrayIndex < before.bitArrayIndex) && (endIndex.bitArrayIndex >= before.bitArrayIndex), "Given Index is out of range")
precondition( ((before == endIndex) || (storage[before.bitArrayIndex])), "Index passed in is invalid: does not exist in the set")
// Potentially optimizeable using leadingZeroCount/trailingZeroCount
for i in stride(from: (before.bitArrayIndex-1), through: 0, by: -1) {
if (storage[i]) {
return Index(bitArrayIndex: i)
}
}
fatalError("Before not found :(")
}
public func index(_ index: Index, offsetBy distance: Int) -> Index {
precondition((startIndex.bitArrayIndex <= index.bitArrayIndex) && (endIndex.bitArrayIndex >= index.bitArrayIndex), "Given Index is out of range")
precondition((index == endIndex) || (storage[index.bitArrayIndex]), "Index passed in is invalid: does not exist in the set")
precondition(self.count != 0, "Set is empty and has no indexes to iterate through")
// Potentially optimizeable using leadingZeroCount/trailingZeroCount
var counter = 0
if (distance == 0) {
return index
} else if (distance > 0) {
for i in (index.bitArrayIndex+1)..<storage.count {
if (storage[i]) {
counter += 1
}
if (counter == distance) {
return Index(bitArrayIndex: i)
}
}
fatalError("positive distance not found")
} else {
for i in (0..<index.bitArrayIndex).reversed() {
if (storage[i]) {
counter -= 1
}
if (counter == distance) {
return Index(bitArrayIndex: i)
}
}
fatalError("negative distance not found")
}
fatalError("Passed in distance points to an Index beyond the scope of the set")
}
}
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 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 {
/// Checks whether the set contains a value or not
///
/// Example:
/// let bitSet: BitSet = [0, 2, 5, 6, 7]
/// let doesContain: Bool = bitSet.contains(0)
/// print(doesContain) // prints "true"
/// doesContain = bitSet.contains(8)
/// print(doesContain) // prints "false"
///
/// - Parameter member: An element to look for in the set.
/// - Returns: `true` if `member` exists in the set; otherwise returns `false`.
public func contains(_ member: Int) -> Bool {
precondition(member >= 0, "Input must be a positive number")
if (member >= storage.count) {
return false
}
return storage[member]
}
}
@@ -0,0 +1,217 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 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: Equatable {
/// Inserts a new member into the set
/// The following examples insert a 2 into a set that already has a 2 and a set that doesn't, respectively
/// var bitSetWithTwo: BitSet = [0, 2, 4, 5]
/// var didInsert: Bool = bitSetWithTwo.insert(2)
/// print(didInsert)
/// // Prints "false"
/// print(Array(bitSetWithTwo))
/// // Prints "[0, 2, 4, 5]"
///
/// var bitSetWithoutTwo: BitSet = [0, 3, 4, 6]
/// didInsert = bitSetWithoutTwo.insert(2)
/// print(didInsert)
/// // Prints "true"
/// print(Array(bitSetWithoutTwo))
/// // Prints "[0, 2, 3, 4, 6]"
///
/// - Parameter newMember: An element to insert into the set
/// - Returns: `true` if `newMember` was not contained in the
/// set. If an element equal to `newMember` was already contained in the
/// set, the method returns `false`. Returned value is discarded without giving a warning if unused
@discardableResult
public mutating func insert(_ newMember: Int) -> Bool {
precondition(newMember >= 0, "Inserts can only be executed with a positive number. Bit sets do not hold negative values.")
if (newMember >= storage.count) {
// can also try ceil()
let padding: Double = (Double(newMember - storage.count + 1)/Double(BitArray.WORD.bitWidth)).rounded(.up)
storage.storage += Array(repeating: 0, count: Int(padding))
}
let returnVal = !storage[newMember]
storage[newMember] = true
return returnVal
}
internal mutating func _forceInsert(_ newMember: Int) {
precondition(newMember >= 0, "Inserts can only be executed with a positive number. Bit sets do not hold negative values.")
while (storage.count-1 < newMember) {
storage.storage.append(0)
}
storage[newMember] = true
}
/// Removes a member from the set
/// The following examples remove a 2 from a set that has a 2 and a set that doesn't, respectively
/// var bitSetWithTwo: BitSet = [0, 2, 4, 5]
/// var didRemove: Bool = bitSetWithTwo.remove(2)
/// print(didRemove)
/// // Prints "true"
/// print(Array(bitSetWithTwo))
/// // Prints "[0, 4, 5]"
///
/// var bitSetWithoutTwo: BitSet = [0, 3, 4, 6]
/// didRemove = bitSetWithoutTwo.remove(2)
/// print(didRemove)
/// // Prints "false"
/// print(Array(bitSetWithoutTwo))
/// // Prints "[0, 3, 4, 6]"
///
/// - Parameter member: An element to remove from the set.
/// - Returns: `true` if `member` was contained in the
/// set. If an element equal to `member` was not already contained in the
/// set, the method returns `false`. Returned value is discarded without giving a warning if unused
@discardableResult
public mutating func remove(_ member: Int) -> Bool {
precondition(member >= 0, "Removals can only be executed with a positive number. Bit sets do not hold negative values.")
if (member >= storage.endIndex) {
return false
}
let returnVal = storage[member]
storage[member] = false
return returnVal
}
/// creates a new BitSet that is the resulting union of itself and another set
/// The union is the set of values that exist in either or both sets
///
/// The following example creates a set that is the union of two sets
/// let firstBitSet: BitSet = [0, 2, 3, 5]
/// let secondBitSet: BitSet = [2, 4, 5, 6]
/// let resultSet = firstBitSet.union(secondBitSet)
/// print(Array(resultSet)) // prints "[0, 2, 3, 4, 5, 6]"
///
/// - Parameter other: Another BitSet
/// - Returns: A new BitSet with the elements that are in this set or `other` or both.
public func union(_ other: BitSet) -> BitSet {
var newBitSet = self
newBitSet.formUnion(other)
return newBitSet
}
/// creates a new BitSet that is the resulting intersection of itself and another set
/// The intersection is the set of values that exist both sets
///
/// The following example creates a set that is the union of two sets
/// let firstBitSet: BitSet = [0, 2, 3, 5]
/// let secondBitSet: BitSet = [2, 4, 5, 6]
/// let resultSet = firstBitSet.intersection(secondBitSet)
/// print(Array(resultSet)) // prints "[2, 5]"
///
/// - Parameter other: Another BitSet
/// - Returns: A new BitSet with the elements that are both in this set and `other`.
public func intersection(_ other: BitSet) -> BitSet {
var newBitSet = self
newBitSet.formIntersection(other)
return newBitSet
}
/// creates a new BitSet that is the resulting symmetric difference of itself and another set
/// The symmetric difference is the set of values that exist one set or the other, but not both
///
/// The following example creates a set that is the symmetric difference of two sets
/// let firstBitSet: BitSet = [0, 2, 3, 5]
/// let secondBitSet: BitSet = [2, 4, 5, 6]
/// let resultSet = firstBitSet.symmetricDifference(secondBitSet)
/// print(Array(resultSet)) // prints "[0, 3, 4, 6]"
///
/// - Parameter other: Another BitSet
/// - Returns: A new BitSet with the elements that are in either this set or the other set, but not both
public func symmetricDifference(_ other: BitSet) -> BitSet {
var newBitSet = self
newBitSet.formSymmetricDifference(other)
return newBitSet
}
/// mutates self to be the resulting union of itself and another set
/// The union is the set of values that exist in either or both sets
///
/// The following example takes a set and transforms it into the union of itself and another set
/// var firstBitSet: BitSet = [0, 2, 3, 5]
/// let secondBitSet: BitSet = [2, 4, 5, 6]
/// firstBitSet.formUnion(secondBitSet)
/// print(Array(firstBitSet)) // prints "[0, 2, 3, 4, 5, 6]"
///
/// - Parameter other: Another BitSet. `other` must be finite
public mutating func formUnion(_ other: BitSet) {
if (other.storage.count < self.storage.count) {
for i in 0..<other.storage.storage.count {
self.storage.storage[i] |= other.storage.storage[i]
}
} else if (self.storage.count < other.storage.count){
for j in 0..<self.storage.storage.count {
self.storage.storage[j] |= other.storage.storage[j]
}
for a in self.storage.count..<other.storage.count {
if(other.storage[a]) {
self._forceInsert(a)
}
}
} else {
for b in 0..<self.storage.storage.count {
self.storage.storage[b] |= other.storage.storage[b]
}
}
}
/// mutates self to be the resulting intersection of itself and another set
/// The intersection is the set of values that exist both sets
///
/// The following example takes a set and transforms it into the intersection of itself and another set
/// var firstBitSet: BitSet = [0, 2, 3, 5]
/// let secondBitSet: BitSet = [2, 4, 5, 6]
/// firstBitSet.formIntersection(secondBitSet)
/// print(Array(firstBitSet)) // prints "[2, 5]"
///
/// - Parameter other: Another BitSet
public mutating func formIntersection(_ other: BitSet) {
let size: Int = (self.storage.storage.count >= other.storage.storage.count) ? other.storage.storage.count : self.storage.storage.count // take the set with the smaller BitArray
for i in 0..<size {
self.storage.storage[i] &= other.storage.storage[i]
}
for i in size..<self.storage.storage.count {
self.storage.storage[i] = 0
}
}
/// mutates self to be the resulting symmetric difference of itself and another set
/// The symmetric difference is the set of values that exist one set or the other, but not both
///
/// The following example takes a set and mutates it into the symmetric difference between itself and another set
/// var firstBitSet: BitSet = [0, 2, 3, 5]
/// let secondBitSet: BitSet = [2, 4, 5, 6]
/// firstBitSet.formSymmetricDifference(secondBitSet)
/// print(Array(firstBitSet)) // prints "[0, 3, 4, 6]"
///
/// - Parameter other: Another BitSet
public mutating func formSymmetricDifference(_ other: BitSet) { // can be optimized later
if (other.storage.storage.count > self.storage.storage.count) {
for i in 0..<self.storage.storage.count {
self.storage.storage[i] ^= other.storage.storage[i]
}
for i in self.storage.storage.count..<other.storage.storage.count {
self.storage.storage.append(other.storage.storage[i])
}
} else {
for i in 0..<other.storage.storage.count {
self.storage.storage[i] ^= other.storage.storage[i]
}
}
}
public static func == (lhs: BitSet, rhs: BitSet) -> Bool {
return (lhs.storage == rhs.storage)
}
}