Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4435076

Browse filesBrowse files
committed
auto commit
1 parent 29c318b commit 4435076
Copy full SHA for 4435076

File tree

Expand file treeCollapse file tree

1 file changed

+19
-10
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+19
-10
lines changed

‎notes/缓存.md

Copy file name to clipboardExpand all lines: notes/缓存.md
+19-10Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,12 @@ public class LRU<K, V> implements Iterable<K> {
8888

8989
public void put(K key, V value) {
9090

91-
Node node;
9291
if (map.containsKey(key)) {
93-
node = map.get(key);
92+
Node node = map.get(key);
9493
unlink(node);
9594
}
96-
if (node == null) {
97-
node = new Node(key, value);
98-
}
95+
96+
Node node = new Node(key, value);
9997
map.put(key, node);
10098
appendHead(node);
10199

@@ -107,28 +105,38 @@ public class LRU<K, V> implements Iterable<K> {
107105

108106

109107
private void unlink(Node node) {
108+
110109
Node pre = node.pre;
111110
Node next = node.next;
111+
112112
pre.next = next;
113113
next.pre = pre;
114-
114+
115115
node.pre = null;
116116
node.next = null;
117117
}
118118

119119

120120
private void appendHead(Node node) {
121-
node.next = head.next;
122-
node.next.pre = node;
121+
Node next = head.next;
122+
node.next = next;
123+
next.pre = node;
123124
node.pre = head;
124125
head.next = node;
125126
}
126127

127128

128129
private Node removeTail() {
130+
129131
Node node = tail.pre;
130-
tail.pre = node.pre;
131-
node.pre.next = tail;
132+
133+
Node pre = node.pre;
134+
tail.pre = pre;
135+
pre.next = tail;
136+
137+
node.pre = null;
138+
node.next = null;
139+
132140
return node;
133141
}
134142

@@ -138,6 +146,7 @@ public class LRU<K, V> implements Iterable<K> {
138146

139147
return new Iterator<K>() {
140148
private Node cur = head.next;
149+
141150
@Override
142151
public boolean hasNext() {
143152
return cur != tail;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.