Skip to content

Instantly share code, notes, and snippets.

@jtbandes
Created December 24, 2014 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jtbandes/42464f0ed4d24cbb5fdc to your computer and use it in GitHub Desktop.
Save jtbandes/42464f0ed4d24cbb5fdc to your computer and use it in GitHub Desktop.
// 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