mirror of
https://github.com/jkl1337/SwiftPFFFT.git
synced 2026-01-03 12:14:30 -06:00
initial commit
This commit is contained in:
46
Sources/PFFFT/SetupCache.swift
Normal file
46
Sources/PFFFT/SetupCache.swift
Normal file
@@ -0,0 +1,46 @@
|
||||
import Foundation
|
||||
|
||||
public class SetupCache: @unchecked Sendable {
|
||||
struct CacheKey: Hashable {
|
||||
let n: Int
|
||||
let type: ObjectIdentifier
|
||||
|
||||
init(n: Int, type: any FFTElement.Type) {
|
||||
self.n = n
|
||||
self.type = ObjectIdentifier(type)
|
||||
}
|
||||
}
|
||||
|
||||
var cache: [CacheKey: Setup?] = [:]
|
||||
let queue = DispatchQueue(label: String(describing: SetupCache.self), attributes: .concurrent)
|
||||
|
||||
public init() {}
|
||||
|
||||
public func get<T: FFTElement>(n: Int, type: T.Type) throws -> Setup {
|
||||
var setup: Setup??
|
||||
queue.sync {
|
||||
setup = cache[CacheKey(n: n, type: type)]
|
||||
}
|
||||
if setup == nil {
|
||||
queue.sync(flags: .barrier) {
|
||||
let key = CacheKey(n: n, type: type)
|
||||
setup = cache[key]
|
||||
if setup == nil {
|
||||
let entry = try? Setup(n: n, type: type)
|
||||
cache[key] = entry
|
||||
setup = entry
|
||||
}
|
||||
}
|
||||
}
|
||||
guard let s = setup! else { throw FFTError.invalidSize }
|
||||
return s
|
||||
}
|
||||
|
||||
public func clear() {
|
||||
queue.sync(flags: .barrier) {
|
||||
cache.removeAll()
|
||||
}
|
||||
}
|
||||
|
||||
public static let shared = SetupCache()
|
||||
}
|
||||
Reference in New Issue
Block a user