on master help sign in
Added linux random numbers
b113ab0ilyakooo0
Sources/App/Models/SafeDict.swift+11−3
@@ -7,6 +7,14 @@
7
7
8import Foundation
8import Foundation
9
9
10+func getRandomNum(_ min: Int, _ max: Int) -> Int {
11+ #if os(Linux)
12+ return Int(random() % max) + min
13+ #else
14+ return Int(arc4random_uniform(UInt32(max)) + UInt32(min))
15+ #endif
16+}
17+
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)
12
20
@@ -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
39 }
47 }
40 }
48 }
41 return key
49 return key
Sources/Run/main.swift+5
@@ -1,3 +1,8 @@
1import App
1import App
2
2
3+#if os(Linux)
4+srandom(UInt32(time(nil)))
5+#endif
6+
7+
3try app(.detect()).run()
8try app(.detect()).run()