-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLRUCache.swift
182 lines (137 loc) · 3.71 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// LRUCache.swift
//
// Created by Hovik Melikyan on 31/10/2019.
//
import Foundation
struct LRUCache<K: Hashable, E>: Sequence {
private(set) var capacity: Int
var count: Int { list.count }
var isEmpty: Bool { list.isEmpty }
private var list = BubbleList()
private var dict = Dictionary<K, BubbleList.Node>()
init(capacity: Int) {
precondition(capacity > 0)
self.capacity = capacity
}
mutating func set(_ element: E, forKey key: K) {
if let node = dict[key] {
node.value = element
list.moveToTop(node: node)
}
else {
if list.count == capacity {
let key = list.removeBottom().key
dict.removeValue(forKey: key)
}
dict[key] = list.add(key: key, value: element)
}
}
mutating func touch(key: K) -> E? {
if let node = dict[key] {
list.moveToTop(node: node)
return node.value
}
return nil
}
@discardableResult
mutating func remove(key: K) -> E? {
if let node = dict[key] {
list.remove(node: node)
dict.removeValue(forKey: key)
return node.value
}
return nil
}
func has(key: K) -> Bool {
dict[key] != nil
}
mutating func removeAll() {
list.removeAll()
dict.removeAll()
}
// MARK: - iterator/sequence
struct Iterator: IteratorProtocol {
typealias Element = E
private var currentNode: BubbleList.Node?
init(iteree: LRUCache) {
currentNode = iteree.list.top
}
mutating func next() -> E? {
defer { currentNode = currentNode?.down }
return currentNode?.value
}
}
__consuming func makeIterator() -> Iterator {
return Iterator(iteree: self)
}
// MARK: - linked list, internal
private struct BubbleList {
final class Node {
let key: K
var value: E
var down: Node?
weak var up: Node?
init(key: K, value: E) {
self.key = key
self.value = value
}
}
private(set) var top: Node?
private(set) var bottom: Node?
private(set) var count: Int = 0
var isEmpty: Bool { count == 0 }
@discardableResult
mutating func add(key: K, value: E) -> Node {
return add(node: Node(key: key, value: value))
}
mutating func moveToTop(node: Node) {
if top !== node {
remove(node: node)
add(node: node)
}
}
@discardableResult
mutating func removeBottom() -> Node {
remove(node: bottom!)
}
mutating func removeAll() {
top = nil
bottom = nil
count = 0
}
@discardableResult
private mutating func add(node: Node) -> Node {
if let top {
node.down = top
top.up = node
}
else {
bottom = node
}
top = node
count += 1
return node
}
@discardableResult
mutating func remove(node: Node) -> Node {
let up = node.up
let down = node.down
if let up {
up.down = down
} else {
top = down
}
if let down {
down.up = up
}
else {
bottom = up
}
node.up = nil
node.down = nil
count -= 1
return node
}
}
}