Last active
August 28, 2018 02:24
-
-
Save ntnmrndn/8ea4d51312f68b283978e3f3af7d1c81 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
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