auto commit

This commit is contained in:
CyC2018
2018-09-03 22:34:58 +08:00
parent 64b26639c8
commit 137710c645
3 changed files with 13 additions and 22 deletions

View File

@ -58,6 +58,7 @@ public class LRU<K, V> implements Iterable<K> {
}
}
public LRU(int maxSize) {
this.maxSize = maxSize;
@ -70,6 +71,7 @@ public class LRU<K, V> implements Iterable<K> {
tail.pre = head;
}
public V get(K key) {
if (!map.containsKey(key)) {
@ -83,6 +85,7 @@ public class LRU<K, V> implements Iterable<K> {
return node.v;
}
public void put(K key, V value) {
if (map.containsKey(key)) {
@ -100,30 +103,34 @@ public class LRU<K, V> implements Iterable<K> {
}
}
private void unlink(Node node) {
Node pre = node.pre;
node.pre = node.next;
node.next = pre;
Node next = node.next;
pre.next = next;
next.pre = pre;
}
private void appendHead(Node node) {
node.next = head.next;
node.pre = head;
head.next = node;
}
private Node removeTail() {
Node node = tail.pre;
node.pre = tail;
tail.pre = node.pre;
return node;
}
@Override
public Iterator<K> iterator() {
return new Iterator<K>() {
private Node cur = head.next;
@Override
public boolean hasNext() {
return cur != tail;