Skip to content

Instantly share code, notes, and snippets.

@codelynx
Created June 2, 2016 17:48
Show Gist options
  • Select an option

  • Save codelynx/4e49e84b066d90437b5eaa843c4b6a36 to your computer and use it in GitHub Desktop.

Select an option

Save codelynx/4e49e84b066d90437b5eaa843c4b6a36 to your computer and use it in GitHub Desktop.
CFBitVector Swift wrapper
//
// ZBitVector.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
class ZBitVector {
let bitVector: CFMutableBitVector
init(capacity: Int) {
self.bitVector = CFBitVectorCreateMutable(nil, CFIndex(capacity))
}
var count: Int {
return CFBitVectorGetCount(self.bitVector)
}
func append(bits: [Bool]) {
let count = CFBitVectorGetCount(self.bitVector)
CFBitVectorSetCount(self.bitVector, count + bits.count)
for index in 0 ..< bits.count {
CFBitVectorSetBitAtIndex(self.bitVector, count + index, CFBit(bits[index]))
}
}
subscript(index: Int) -> Bool {
get {
assert(index >= 0 && index < CFBitVectorGetCount(self.bitVector))
let bitValue = CFBitVectorGetBitAtIndex(bitVector, index)
return !(bitValue == 0)
}
set {
if index > CFBitVectorGetCount(self.bitVector) {
CFBitVectorSetCount(self.bitVector, index + 1)
}
CFBitVectorSetBitAtIndex(bitVector, index, CFBit(newValue))
}
}
subscript(range: NSRange) -> [Bool] {
get {
var bitValues = [Bool]()
for index in range.location ..< NSMaxRange(range) {
let bit = CFBitVectorGetBitAtIndex(bitVector, CFIndex(index))
bitValues.append(bit != 0)
}
return bitValues
}
}
func setBitsAtIndex(index: Int, bits: [Bool]) {
let count = CFBitVectorGetCount(self.bitVector)
if index + bits.count > count {
CFBitVectorSetCount(self.bitVector, index + bits.count)
}
for offset in 0 ..< bits.count {
CFBitVectorSetBitAtIndex(bitVector, index + offset, CFBit(bits[offset]))
}
}
func data() -> NSData {
let counts = Int(CFBitVectorGetCount(self.bitVector))
assert(CFBitVectorGetCount(self.bitVector) > 0)
let bytes = Int(ceil((Double(counts) / 8.0)))
let data = NSMutableData()
for index in 0 ..< bytes {
let range = CFRange(location: CFIndex(index * 8), length: 8)
var bits: UInt8 = 0
CFBitVectorGetBits(self.bitVector, range, &bits)
data.appendBytes(&bits, length: 1)
}
return data
}
func containsBit(range: NSRange, value: Bool) -> Bool {
return CFBitVectorContainsBit(self.bitVector, CFRange(range), CFBit(value))
}
func flipBitAtIndex(index: Int) {
CFBitVectorFlipBitAtIndex(self.bitVector, index)
}
func setAllBits(value: Bool) {
CFBitVectorSetAllBits(self.bitVector, CFBit(value))
}
}
extension CFRange {
init(_ range: NSRange) {
self = CFRangeMake(range.location, range.length)
}
}
extension CFBit {
init(_ bool: Bool) {
self = CFBit(bool ? 1 : 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment