mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-25 17:50:59 +00:00
Refactoring
This commit is contained in:
parent
3f0f19a75b
commit
718dfccf7d
1 changed files with 10 additions and 7 deletions
|
@ -17,18 +17,18 @@ extension BitArray {
|
||||||
|
|
||||||
subscript(index: Int) -> Bool {
|
subscript(index: Int) -> Bool {
|
||||||
get {
|
get {
|
||||||
let (byteIndex, bitIndex) = index.quotientAndRemainder(dividingBy: UInt8.bitWidth)
|
let (byteIndex, mask) = Self.byteIndexAndMask(index: index)
|
||||||
|
|
||||||
return bytes[byteIndex] & Self.mask(bitIndex: bitIndex) > 0
|
return bytes[byteIndex] & mask > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
set {
|
set {
|
||||||
let (byteIndex, bitIndex) = index.quotientAndRemainder(dividingBy: UInt8.bitWidth)
|
let (byteIndex, mask) = Self.byteIndexAndMask(index: index)
|
||||||
|
|
||||||
if newValue {
|
if newValue {
|
||||||
bytes[byteIndex] |= Self.mask(bitIndex: bitIndex)
|
bytes[byteIndex] |= mask
|
||||||
} else {
|
} else {
|
||||||
bytes[byteIndex] &= ~Self.mask(bitIndex: bitIndex)
|
bytes[byteIndex] &= ~mask
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,10 @@ extension BitArray: Codable {
|
||||||
}
|
}
|
||||||
|
|
||||||
private extension BitArray {
|
private extension BitArray {
|
||||||
static func mask(bitIndex: Int) -> UInt8 {
|
static func byteIndexAndMask(index: Int) -> (Int, UInt8) {
|
||||||
UInt8(2 << (bitIndex - 1))
|
let (byteIndex, bitIndex) = index.quotientAndRemainder(dividingBy: UInt8.bitWidth)
|
||||||
|
let mask = UInt8(2 << (bitIndex - 1))
|
||||||
|
|
||||||
|
return (byteIndex, mask)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue