-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSynchronizedDictionaryComposed.swift
More file actions
92 lines (78 loc) · 2.27 KB
/
SynchronizedDictionaryComposed.swift
File metadata and controls
92 lines (78 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// SynchronizedDictionaryComposed.swift
// Split
//
// Created by Javier on 3-Mar-2022.
// Copyright © 2022 Split. All rights reserved.
//
import Foundation
public class SynchronizedDictionaryComposed<K: Hashable, IK: Hashable>: @unchecked Sendable {
private var queue: DispatchQueue = DispatchQueue(label: "split-synchronized-dictionary-composed",
target: .global())
private var items = [K: [IK: Any]]()
public init() {}
public func count(forKey key: K) -> Int {
var count: Int?
queue.sync {
count = items[key]?.count
}
return count ?? 0
}
public func values(forKey key: K) -> [IK: Any]? {
var value: [IK: Any]?
queue.sync {
value = items[key]
}
return value
}
public func value(_ innerKey: IK, forKey key: K) -> Any? {
var value: Any?
queue.sync {
value = items[key]?[innerKey]
}
return value
}
public func contains(innerKey: IK, forKey key: K) -> Bool {
var hasValue: Bool?
queue.sync {
hasValue = items[key]?.keys.contains(innerKey)
}
return hasValue ?? false
}
public func set(_ values: [IK: Any], forKey key: K) {
queue.sync {
self.items[key] = values
}
}
public func set(_ value: Any, forInnerKey innerKey: IK, forKey key: K) {
queue.sync {
var values = self.items[key] ?? [:]
values[innerKey] = value
self.items[key] = values
}
}
public func putValues(_ values: [IK: Any], forKey key: K) {
queue.sync {
var newValues = self.items[key] ?? [:]
for (innerKey, value) in values {
newValues[innerKey] = value
}
self.items[key] = newValues
}
}
public func removeValue(_ innerKey: IK, forKey key: K) {
queue.sync {
_ = self.items[key]?.removeValue(forKey: innerKey)
}
}
public func removeValues(forKey key: K) {
queue.sync {
_ = self.items.removeValue(forKey: key)
}
}
public func removeAll() {
queue.sync {
self.items.removeAll()
}
}
}