auto commit

This commit is contained in:
CyC2018 2018-10-16 22:02:49 +08:00
parent 29c318b846
commit 4435076f73

View File

@ -88,14 +88,12 @@ public class LRU<K, V> implements Iterable<K> {
public void put(K key, V value) { public void put(K key, V value) {
Node node;
if (map.containsKey(key)) { if (map.containsKey(key)) {
node = map.get(key); Node node = map.get(key);
unlink(node); unlink(node);
} }
if (node == null) {
node = new Node(key, value); Node node = new Node(key, value);
}
map.put(key, node); map.put(key, node);
appendHead(node); appendHead(node);
@ -107,28 +105,38 @@ public class LRU<K, V> implements Iterable<K> {
private void unlink(Node node) { private void unlink(Node node) {
Node pre = node.pre; Node pre = node.pre;
Node next = node.next; Node next = node.next;
pre.next = next; pre.next = next;
next.pre = pre; next.pre = pre;
node.pre = null; node.pre = null;
node.next = null; node.next = null;
} }
private void appendHead(Node node) { private void appendHead(Node node) {
node.next = head.next; Node next = head.next;
node.next.pre = node; node.next = next;
next.pre = node;
node.pre = head; node.pre = head;
head.next = node; head.next = node;
} }
private Node removeTail() { private Node removeTail() {
Node node = tail.pre; Node node = tail.pre;
tail.pre = node.pre;
node.pre.next = tail; Node pre = node.pre;
tail.pre = pre;
pre.next = tail;
node.pre = null;
node.next = null;
return node; return node;
} }
@ -138,6 +146,7 @@ public class LRU<K, V> implements Iterable<K> {
return new Iterator<K>() { return new Iterator<K>() {
private Node cur = head.next; private Node cur = head.next;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return cur != tail; return cur != tail;