Skip to content

Instantly share code, notes, and snippets.

@ntnmrndn
Last active August 28, 2018 02:24
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 ntnmrndn/8ea4d51312f68b283978e3f3af7d1c81 to your computer and use it in GitHub Desktop.
Save ntnmrndn/8ea4d51312f68b283978e3f3af7d1c81 to your computer and use it in GitHub Desktop.
private var metaPool = [String:[Any]]()
protocol Poolable: Any {
init()
}
class Pool<T: Poolable> {
private static var typeKey: String {
return "\(T.self)"
}
static func get() -> T {
if metaPool[typeKey] == nil {
metaPool[typeKey] = [T]()
}
if metaPool[typeKey]!.isEmpty {
return
}
return metaPool[typeKey]!.popLast() as? T ?? T()
}
static func release(_ element: T) {
metaPool[typeKey]!.append(element)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment