27 lines
566 B
Swift
27 lines
566 B
Swift
|
import Foundation
|
||
|
|
||
|
class ThreadSafeSet<T: Hashable> {
|
||
|
private var set: Set<T> = []
|
||
|
private let accessQueue = DispatchQueue(label: "com.example.ThreadSafeSet")
|
||
|
|
||
|
func insert(_ newElement: T) {
|
||
|
_ = accessQueue.sync {
|
||
|
set.insert(newElement)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func remove(_ element: T) {
|
||
|
_ = accessQueue.sync {
|
||
|
set.remove(element)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var elements: Set<T> {
|
||
|
accessQueue.sync { set }
|
||
|
}
|
||
|
|
||
|
func contains(_ element: T) -> Bool {
|
||
|
accessQueue.sync { set.contains(element) }
|
||
|
}
|
||
|
}
|