@@ -7,6 +7,14 @@
8import Foundation
8import Foundation
10+func getRandomNum(_ min: Int, _ max: Int) -> Int {
12+ return Int(random() % max) + min
14+ return Int(arc4random_uniform(UInt32(max)) + UInt32(min))
10struct SafeDict<Key: Hashable, Value> {
18struct SafeDict<Key: Hashable, Value> {
11 private var queue = DispatchQueue(label: "soy.iko.safeDict.queue", attributes: .concurrent)
19 private var queue = DispatchQueue(label: "soy.iko.safeDict.queue", attributes: .concurrent)
@@ -33,9 +41,9 @@ struct SafeDict<Key: Hashable, Value> {
33 mutating func removeRandom() -> Key? {
41 mutating func removeRandom() -> Key? {
34 var key: Key?
42 var key: Key?
35 queue.sync(flags: .barrier) {
43 queue.sync(flags: .barrier) {
36− key = dict.keys.randomElement()
44+ if dict.count > 0 {
37− if let key = key {
45+ key = Array(dict.keys)[getRandomNum(0, dict.count)]
38− dict[key] = nil
46+ dict[key!] = nil
41 return key
49 return key