Skip to content

Instantly share code, notes, and snippets.

@MosheBerman
Last active February 2, 2016 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MosheBerman/4144545ca891e239d4d7 to your computer and use it in GitHub Desktop.
Save MosheBerman/4144545ca891e239d4d7 to your computer and use it in GitHub Desktop.
A queue for object recycling, similar to UITableView/UICollectionView recycling.
class ObjectQueue<T : NSObject> {
private var used : [T] = []
private var free : [T] = []
/** Recycle or create object. */
func dequeueObject () -> T {
var object : T
if let first = self.free.first {
object = first
free.removeAtIndex(0)
}
else
{
object = T()
}
used.append(object)
return object
}
/** Add an object to the queue for later. */
func enqueueObject(object : T) {
self.free.append(object)
if let index = self.used.indexOf(object) {
self.used.removeAtIndex(index)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment