-
Notifications
You must be signed in to change notification settings - Fork 5k
/
LRUCache.swift
59 lines (49 loc) · 1.24 KB
/
LRUCache.swift
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
//
// LRUCache.swift
//
//
// Created by Kai Chen on 16/07/2017.
//
//
import Foundation
public class LRUCache<KeyType: Hashable> {
private let maxSize: Int
private var cache: [KeyType: Any] = [:]
private var priority: LinkedList<KeyType> = LinkedList<KeyType>()
private var key2node: [KeyType: LinkedList<KeyType>.LinkedListNode<KeyType>] = [:]
public init(_ maxSize: Int) {
self.maxSize = maxSize
}
public func get(_ key: KeyType) -> Any? {
guard let val = cache[key] else {
return nil
}
remove(key)
insert(key, val: val)
return val
}
public func set(_ key: KeyType, val: Any) {
if cache[key] != nil {
remove(key)
} else if priority.count >= maxSize, let keyToRemove = priority.last?.value {
remove(keyToRemove)
}
insert(key, val: val)
}
private func remove(_ key: KeyType) {
cache.removeValue(forKey: key)
guard let node = key2node[key] else {
return
}
priority.remove(node: node)
key2node.removeValue(forKey: key)
}
private func insert(_ key: KeyType, val: Any) {
cache[key] = val
priority.insert(key, atIndex: 0)
guard let first = priority.first else {
return
}
key2node[key] = first
}
}