-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryCacheLink.swift
More file actions
218 lines (181 loc) · 5.18 KB
/
Copy pathMemoryCacheLink.swift
File metadata and controls
218 lines (181 loc) · 5.18 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// MemoryCacheLink.swift
// PracticeCache
//
// Created by Vassily on 2018/10/22.
//
import Foundation
public class AnyLinkNode<K: Hashable, V> {
public typealias Key = K
public typealias Value = V
public private(set) var key: Key
public private(set) var value: Value
init(key: Key, value: Value) {
self.key = key
self.value = value
}
}
final class LinkNode<K: Hashable, V>: AnyLinkNode<K, V>, NodeStandard {
public fileprivate(set) weak var pre: LinkNode?
public fileprivate(set) weak var next: LinkNode?
public init(key: Key, value: Value, pre: LinkNode? = nil, next: LinkNode? = nil) {
self.pre = pre
self.next = next
super.init(key: key, value: value)
}
}
public struct LinkedNodeListIndex<K: Hashable, V>: LinkedNodeListIndexStandard {
var node: LinkNode<K, V>?
init(node: LinkNode<K, V>?) {
self.node = node
}
}
public struct LinkedList<K: Hashable, V>: LinkedNodeListStandard, CustomStringConvertible {
public typealias Key = K
public typealias Value = V
public typealias Node = AnyLinkNode<K, V>
public typealias Index = LinkedNodeListIndex<K, V>
private typealias RealNode = LinkNode<K, V>
private var head: RealNode?
private var trail: RealNode?
private var dictContainer = Dictionary<K, RealNode>()
public init() {}
public subscript(key: K) -> V? {
mutating get {
return value(for: key)
}
set {
if let value = newValue {
push(value, for: key)
} else {
remove(for: key)
}
}
}
public func contains(where predicate: (Key) throws -> Bool) rethrows -> Bool {
do {
for key in dictContainer.keys where try predicate(key) {
return true
}
return false
} catch {
return false
}
}
}
extension LinkedList: BidirectionalCollection {
public var startIndex: Index {
return Index(node: head)
}
public var endIndex: Index {
return Index(node: trail?.next)
}
public func index(before i: Index) -> Index {
if i == endIndex { return Index(node: trail) }
return Index(node: i.node?.pre)
}
public func index(after i: Index) -> Index {
return Index(node: i.node?.next)
}
public subscript(position: Index) -> Value {
return position.node!.value
}
}
// MARK: - add
extension LinkedList {
// head-first insertion
public mutating func push(_ value: Value, for key: Key) {
if let node = dictContainer[key] {
bringNodeToHead(node)
} else {
let node = RealNode(key: key, value: value, next: head)
dictContainer[key] = node
head?.pre = node
head = node
if trail == nil {
trail = head
}
}
}
private mutating func push(_ node: Node) {
push(node.value, for: node.key)
}
}
// MARK: - remove
extension LinkedList {
@discardableResult
public mutating func remove(for key: Key) -> Node? {
if let node = dictContainer.removeValue(forKey: key) {
if node == head {
node.next?.pre = nil
head = node.next
node.next = nil
} else if node == trail {
node.pre?.next = nil
trail = node.pre
node.pre = nil
} else {
node.pre?.next = node.next
node.next?.pre = node.pre
}
return node
}
return nil
}
@discardableResult
private mutating func remove(_ node: Node) -> Node? {
return remove(for: node.key)
}
public mutating func removeAll() {
head = nil
trail = nil
dictContainer.removeAll()
}
@discardableResult
public mutating func removeTrail() -> Node? {
let removedNode = trail
if trail == head {
trail = nil
head = nil
} else {
trail = trail?.pre
trail?.next?.pre = nil
trail?.next = nil
}
if let removedNode = removedNode {
return dictContainer.removeValue(forKey: removedNode.key)
}
return nil
}
}
// MARK: - query
extension LinkedList {
public mutating func value(for key: Key) -> V? {
if let node = dictContainer[key] {
bringNodeToHead(node)
return node.value
}
return nil
}
}
// MARK: - update
extension LinkedList {
private mutating func bringNodeToHead(_ node: RealNode) {
if node == head { return }
if node == trail {
trail = trail?.pre
trail?.next = nil
} else {
node.next?.pre = node.pre
node.pre?.next = node.next
}
node.next = head
head?.pre = node
head = node
}
}
extension LinkedList {
public var description: String {
return head?.description ?? "is empty"
}
}