mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
BitArrayModule ⟹ BitCollections
This commit is contained in:
1 parent
49a82fe6e1
commit
537382f2a6
14 files changed
+58
-10
No files matched your search
@@ -0,0 +1,76 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 BitArray: Collection, RandomAccessCollection {
|
||||
|
||||
|
||||
public func index(after i: Int) -> Int {
|
||||
return i + 1
|
||||
}
|
||||
|
||||
public var count: Int {
|
||||
let remaining: Int = (excess == 0) ? WORD.bitWidth : Int(excess)
|
||||
return (storage.count)*WORD.bitWidth - (WORD.bitWidth - remaining)
|
||||
}
|
||||
|
||||
public var startIndex: Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
public var endIndex: Int {
|
||||
return count
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
extension BitArray: BidirectionalCollection {
|
||||
|
||||
public func index(before i: Int) -> Int {
|
||||
return i - 1
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension BitArray: MutableCollection {
|
||||
|
||||
public subscript(position: Int) -> Bool {
|
||||
|
||||
get {
|
||||
precondition(position < endIndex && position >= startIndex, "Index out of bounds")
|
||||
|
||||
let (index, mask) = _split(_position: position)
|
||||
return (storage[index] & mask != 0)
|
||||
}
|
||||
set {
|
||||
let (index, mask) = _split(_position: position)
|
||||
if (newValue) { storage[index] |= mask } else { storage[index] &= ~mask }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private func _split(_position: Int) -> (Int, WORD) {
|
||||
|
||||
let index: Int = _position/WORD.bitWidth
|
||||
let subPosition: Int = _position - index*WORD.bitWidth
|
||||
let mask: WORD = 1 << subPosition
|
||||
|
||||
return (index, mask)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension BitArray: Equatable {
|
||||
public static func == (lhs: BitArray, rhs: BitArray) -> Bool {
|
||||
return ((lhs.excess == rhs.excess) && (lhs.storage == rhs.storage))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user