Refactoring

This commit is contained in:
Justin Mazzocchi 2020-09-26 15:36:22 -07:00
parent 083e960166
commit 3f0f19a75b
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -11,19 +11,19 @@ struct BitArray {
} }
extension BitArray { extension BitArray {
var bitCount: Int { bytes.count * Self.bitsInByte } var bitCount: Int { bytes.count * UInt8.bitWidth }
var data: Data { Data(bytes) } var data: Data { Data(bytes) }
subscript(index: Int) -> Bool { subscript(index: Int) -> Bool {
get { get {
let (byteIndex, bitIndex) = index.quotientAndRemainder(dividingBy: Self.bitsInByte) let (byteIndex, bitIndex) = index.quotientAndRemainder(dividingBy: UInt8.bitWidth)
return bytes[byteIndex] & Self.mask(bitIndex: bitIndex) > 0 return bytes[byteIndex] & Self.mask(bitIndex: bitIndex) > 0
} }
set { set {
let (byteIndex, bitIndex) = index.quotientAndRemainder(dividingBy: Self.bitsInByte) let (byteIndex, bitIndex) = index.quotientAndRemainder(dividingBy: UInt8.bitWidth)
if newValue { if newValue {
bytes[byteIndex] |= Self.mask(bitIndex: bitIndex) bytes[byteIndex] |= Self.mask(bitIndex: bitIndex)
@ -47,8 +47,6 @@ extension BitArray: Codable {
} }
private extension BitArray { private extension BitArray {
static let bitsInByte = 8
static func mask(bitIndex: Int) -> UInt8 { static func mask(bitIndex: Int) -> UInt8 {
UInt8(2 << (bitIndex - 1)) UInt8(2 << (bitIndex - 1))
} }