Skip to content

Commit

Permalink
auto commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CyC2018 committed Oct 16, 2018
1 parent 29c318b commit 4435076
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions notes/缓存.md
Expand Up @@ -88,14 +88,12 @@ public class LRU<K, V> implements Iterable<K> {

public void put(K key, V value) {

Node node;
if (map.containsKey(key)) {
node = map.get(key);
Node node = map.get(key);
unlink(node);
}
if (node == null) {
node = new Node(key, value);
}

Node node = new Node(key, value);
map.put(key, node);
appendHead(node);

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


private void unlink(Node node) {

Node pre = node.pre;
Node next = node.next;

pre.next = next;
next.pre = pre;

node.pre = null;
node.next = null;
}


private void appendHead(Node node) {
node.next = head.next;
node.next.pre = node;
Node next = head.next;
node.next = next;
next.pre = node;
node.pre = head;
head.next = node;
}


private Node removeTail() {

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;
}

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

return new Iterator<K>() {
private Node cur = head.next;

@Override
public boolean hasNext() {
return cur != tail;
Expand Down

0 comments on commit 4435076

Please sign in to comment.