-
-
Save natecook1000/552dc3d23d2fc4a54d2e9fcd309e59e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct BitArray: RandomAccessCollection, MutableCollection { | |
typealias Data = (UInt, UInt) | |
var data: Data = (0, 0) | |
var startIndex: Int { | |
return 0 | |
} | |
var endIndex: Int { | |
return MemoryLayout<Data>.size * 8 | |
} | |
subscript(_ i: Int) -> Bool { | |
get { | |
return withUnsafeBytes(of: data) { buffer in | |
return (buffer[i / 8] >> (i % 8)) & 1 == 1 | |
} | |
} | |
set { | |
withUnsafeMutableBytes(of: &data) { buffer in | |
let v = (1 as UInt8) << (i % 8) | |
if newValue { | |
buffer[i / 8] |= v | |
} else { | |
buffer[i / 8] &= ~v | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment