-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ThreadSafeMap and ThreadSafeList
- Loading branch information
Showing
5 changed files
with
204 additions
and
19 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import Foundation | ||
|
||
public class ThreadSafeList<V> { | ||
|
||
private var items = [V]() | ||
private var itemQueue: DispatchQueue | ||
public init(identifier: String = UUID().uuidString) { | ||
itemQueue = DispatchQueue( | ||
label: "ThreadSafeList.\(String(describing: V.self)).queue.\(identifier)", | ||
qos: .userInitiated, | ||
attributes: .concurrent, | ||
target: DispatchQueue.global(qos: .userInitiated) | ||
) | ||
} | ||
|
||
public func get(_ index: Int) -> V? { | ||
var value: V? | ||
itemQueue.sync { // safely read | ||
if self.items.indices.contains(index) { | ||
value = self.items[index] | ||
} else { | ||
value = nil | ||
} | ||
} | ||
return value | ||
} | ||
|
||
public func insert(_ value: V, at index: Int) { | ||
itemQueue.async(flags: .barrier) { // safely write | ||
self.items.insert(value, at: index) | ||
} | ||
} | ||
|
||
@discardableResult public func remove(at index: Int) -> V? { | ||
guard let value = get(index) else { | ||
// make sure the index exists | ||
return nil | ||
} | ||
itemQueue.async(flags: .barrier) { // safely write | ||
self.items.remove(at: index) | ||
} | ||
return value | ||
} | ||
|
||
public func append(_ value: V) { | ||
itemQueue.async(flags: .barrier) { // safely write | ||
self.items.append(value) | ||
} | ||
} | ||
|
||
public func append(contentsOf values: [V]) { | ||
itemQueue.async(flags: .barrier) { // safely write | ||
self.items.append(contentsOf: values) | ||
} | ||
} | ||
|
||
public func removeAll() { | ||
itemQueue.async(flags: .barrier) { // safely write | ||
self.items.removeAll() | ||
} | ||
} | ||
|
||
public func getAll() -> [V] { | ||
var allItems = [V]() | ||
itemQueue.sync { // safely read | ||
allItems = self.items | ||
} | ||
return allItems | ||
} | ||
|
||
public func count() -> Int { | ||
var count = 0 | ||
itemQueue.sync { // safely read | ||
count = self.items.count | ||
} | ||
return count | ||
} | ||
|
||
public subscript(index: Int) -> V? { | ||
get { | ||
return self.get(index) | ||
} | ||
set(newValue) { | ||
if let value = newValue { | ||
self.insert(value, at: index) | ||
} else { | ||
self.remove(at: index) | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Foundation | ||
|
||
public class ThreadSafeMap<K: Hashable, V> { | ||
|
||
private var items = [K: V]() | ||
private var itemQueue: DispatchQueue | ||
|
||
public init(identifier: String = UUID().uuidString) { | ||
itemQueue = DispatchQueue( | ||
label: "ThreadSafeMap.\(String(describing: K.self)).\(String(describing: V.self)).queue.\(identifier)", | ||
qos: .userInitiated, | ||
attributes: .concurrent, | ||
target: DispatchQueue.global(qos: .userInitiated) | ||
) | ||
} | ||
|
||
public func get(_ key: K) -> V? { | ||
var value: V? | ||
itemQueue.sync { // safely read | ||
value = self.items[key] | ||
} | ||
return value | ||
} | ||
|
||
public func put(_ key: K, _ value: V?) { | ||
itemQueue.async(flags: .barrier) { // safely write | ||
self.items[key] = value | ||
} | ||
} | ||
|
||
public func removeAll() { | ||
itemQueue.async(flags: .barrier) { // safely write | ||
self.items.removeAll() | ||
} | ||
} | ||
|
||
public func removeValue(forKey key: K) { | ||
itemQueue.async(flags: .barrier) { // safely write | ||
self.items.removeValue(forKey: key) | ||
} | ||
} | ||
|
||
public func getAll() -> [K: V] { | ||
var allItems = [K: V]() | ||
itemQueue.sync { // safely read | ||
allItems = self.items | ||
} | ||
return allItems | ||
} | ||
|
||
public func putAll(_ allItems: [K: V]) { | ||
itemQueue.async(flags: .barrier) { // safely write | ||
self.items = allItems | ||
} | ||
} | ||
|
||
public subscript(key: K) -> V? { | ||
get { | ||
return self.get(key) | ||
} | ||
set(newValue) { | ||
self.put(key, newValue) | ||
} | ||
} | ||
|
||
public func count() -> Int { | ||
var count = 0 | ||
itemQueue.sync { // safely read | ||
count = self.items.count | ||
} | ||
return count | ||
} | ||
} |
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