Created
December 24, 2014 20:52
-
-
Save jtbandes/42464f0ed4d24cbb5fdc 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
// Before realizing that NSFastGenerator exists... | |
func enumerate(c: NSFastEnumeration) -> SequenceOf<AnyObject> { | |
return SequenceOf<AnyObject> { () -> GeneratorOf<AnyObject> in | |
let bufSize = 16 | |
var objects = UnsafeMutablePointer<AnyObject?>.alloc(bufSize) | |
var state = NSFastEnumerationState() | |
var currentCount = 0 | |
var currentIndex = 0 | |
return GeneratorOf<AnyObject> { | |
if currentIndex >= currentCount { | |
// Enumerate the next chunk of items | |
currentIndex = 0 | |
currentCount = c.countByEnumeratingWithState(&state, objects: AutoreleasingUnsafeMutablePointer(objects), count: bufSize) | |
if currentCount == 0 { | |
// Iteration is finished. | |
objects.dealloc(bufSize) | |
objects = nil | |
return nil | |
} | |
} | |
return state.itemsPtr[currentIndex++] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment