-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLruCache.java
More file actions
132 lines (119 loc) · 3.63 KB
/
Copy pathLruCache.java
File metadata and controls
132 lines (119 loc) · 3.63 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
package algorithms.study;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
/**
* https://java2blog.com/lru-cache-implementation-java/
* https://dzone.com/articles/java-based-simple-cache-lru-eviction
* https://algs4.cs.princeton.edu/13stacks/DoublyLinkedList.java.html
*
* created by Cenk Canarslan on 2021-05-02
*/
public class LruCache {
private int capacity;
private Map<Integer, Node> nodeMap;
private Node head;
private Node tail;
public LruCache(int capacity) {
this.capacity = capacity;
nodeMap = new HashMap<>(capacity);
}
public void deleteNode(Node node) {
if (node == null) {
return ;
}
if (node.prev != null) {
// node could be somewhere in between head and tail or just the tail (it's next is null)
node.prev.next = node.next;
} else {
head = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
} else {
tail = node.prev;
}
}
private void setHead(Node node) {
if (node == null) {
return;
}
node.next = head;
node.prev = null;
if(head!=null)
head.prev = node;
head = node;
if(tail ==null)
tail = head;
}
public int get(int key) {
if (nodeMap.containsKey(key)) {
Node node = nodeMap.get(key);
// cache accessed so we need to move it to head position.
// in order to do that, first we need to remove it
deleteNode(node);
setHead(node);
return node.value;
}
return -1;
}
public void put(int key, int value) {
if (nodeMap.containsKey(key)) {
// var olan node u sil (remove)
// value güncelle
// en başa koy (setHead)
Node oldNode = nodeMap.get(key);
deleteNode(oldNode);
oldNode.value = value;
setHead(oldNode);
} else {
// kapasite dolduysa en sondakini sil
// en başa koy
Node newNode = new Node(key, value);
if (nodeMap.size() >= capacity) {
// kapasite dolduysa en son elemani (tail) ucur
// tail node'u sil
// yeni node u en basa koy
nodeMap.remove(tail.key);
deleteNode(tail);
setHead(newNode);
} else {
// kapasite dolmadi daha yer var
// yeni node u en basa koy
setHead(newNode);
}
// yeni node u map e ekle
nodeMap.put(key, newNode);
}
}
public static void main(String[] args) {
LruCache lrucache = new LruCache(4);
lrucache.put(1, 100);
lrucache.put(10, 99);
lrucache.put(15, 98);
lrucache.put(10, 97);
lrucache.put(12, 96);
lrucache.put(18, 95);
lrucache.put(1, 94);
System.out.println(lrucache.get(1));
System.out.println(lrucache.get(10));
System.out.println(lrucache.get(15));
}
private class Node {
private int key;
private int value;
private Node prev;
private Node next;
public Node(int key, int value) {
this.key = key;
this.value = value;
}
@Override
public String toString() {
return new StringJoiner(", ", Node.class.getSimpleName() + "[", "]")
.add("key=" + key)
.add("value=" + value)
.toString();
}
}
}