Last active
September 3, 2020 15:57
-
-
Save SergLam/88c3e46ee5388302d6630823eabfb81d to your computer and use it in GitHub Desktop.
Weak set of object - inspired by article - https://nshipster.com/nshashtable-and-nsmaptable/
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
import Foundation | |
/// The WeakSet is weak: References to objects in the collection are held weakly. | |
/// If there is no other reference to an object stored in the WeakSet, they can be garbage collected. | |
public class WeakSet<T>: Sequence, ExpressibleByArrayLiteral, CustomStringConvertible, CustomDebugStringConvertible { | |
private var objects = NSHashTable<AnyObject>.weakObjects() | |
public init(_ objects: [T]) { | |
for object in objects { | |
insert(object) | |
} | |
} | |
public required convenience init(arrayLiteral elements: T...) { | |
self.init(elements) | |
} | |
public var allObjects: [T] { | |
return objects.allObjects as? [T] ?? [] | |
} | |
public var count: Int { | |
return objects.count | |
} | |
public func contains(_ object: T) -> Bool { | |
return objects.contains(object as AnyObject) | |
} | |
public func insert(_ object: T) { | |
objects.add(object as AnyObject) | |
} | |
public func remove(_ object: T) { | |
objects.remove(object as AnyObject) | |
} | |
public func removeAll() { | |
objects.removeAllObjects() | |
} | |
public func makeIterator() -> AnyIterator<T> { | |
let iterator = objects.objectEnumerator() | |
return AnyIterator { | |
return iterator.nextObject() as? T | |
} | |
} | |
public var description: String { | |
return objects.description | |
} | |
public var debugDescription: String { | |
return objects.debugDescription | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment