65 lines
1.1 KiB
Swift
65 lines
1.1 KiB
Swift
//
|
|
// Copyright Amazon.com Inc. or its affiliates.
|
|
// All Rights Reserved.
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
import Foundation
|
|
|
|
final class AtomicDictionary<Key: Hashable, Value> {
|
|
let lock = NSLock()
|
|
|
|
private var value: [Key: Value]
|
|
|
|
init(initialValue: [Key: Value] = [Key: Value]()) {
|
|
self.value = initialValue
|
|
}
|
|
|
|
var count: Int {
|
|
lock.execute {
|
|
value.count
|
|
}
|
|
}
|
|
|
|
var keys: [Key] {
|
|
lock.execute {
|
|
Array(value.keys)
|
|
}
|
|
}
|
|
|
|
var values: [Value] {
|
|
lock.execute {
|
|
Array(value.values)
|
|
}
|
|
}
|
|
|
|
// MARK: - Functions
|
|
|
|
func getValue(forKey key: Key) -> Value? {
|
|
lock.execute {
|
|
value[key]
|
|
}
|
|
}
|
|
|
|
func removeAll() {
|
|
lock.execute {
|
|
value = [:]
|
|
}
|
|
}
|
|
|
|
@discardableResult
|
|
func removeValue(forKey key: Key) -> Value? {
|
|
lock.execute {
|
|
value.removeValue(forKey: key)
|
|
}
|
|
}
|
|
|
|
func set(value: Value, forKey key: Key) {
|
|
lock.execute {
|
|
self.value[key] = value
|
|
}
|
|
}
|
|
|
|
}
|