Last active
February 2, 2016 17:28
-
-
Save MosheBerman/4144545ca891e239d4d7 to your computer and use it in GitHub Desktop.
A queue for object recycling, similar to UITableView/UICollectionView recycling.
This file contains hidden or 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
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