forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLFUCache.java
143 lines (128 loc) · 3.97 KB
/
LFUCache.java
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
133
134
135
136
137
138
139
140
141
142
143
package com.thealgorithms.datastructures.caches;
import java.util.HashMap;
import java.util.Map;
/**
* Java program for LFU Cache (https://en.wikipedia.org/wiki/Least_frequently_used)
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
*/
public class LFUCache<K, V> {
private class Node {
private K key;
private V value;
private int frequency;
private Node previous;
private Node next;
Node(K key, V value, int frequency) {
this.key = key;
this.value = value;
this.frequency = frequency;
}
}
private Node head;
private Node tail;
private Map<K, Node> map = null;
private Integer capacity;
private static final int DEFAULT_CAPACITY = 100;
public LFUCache() {
this.capacity = DEFAULT_CAPACITY;
}
public LFUCache(Integer capacity) {
this.capacity = capacity;
this.map = new HashMap<>();
}
/**
* This method returns value present in the cache corresponding to the key passed as parameter
*
* @param <K> key for which value is to be retrieved
* @returns <V> object corresponding to the key passed as parameter, returns null if <K> key is
* not present in the cache
*/
public V get(K key) {
if (this.map.get(key) == null) {
return null;
}
Node node = map.get(key);
removeNode(node);
node.frequency += 1;
addNodeWithUpdatedFrequency(node);
return node.value;
}
/**
* This method stores <K> key and <V> value in the cache
*
* @param <K> key which is to be stored in the cache
* @param <V> value which is to be stored in the cache
*/
public void put(K key, V value) {
if (map.containsKey(key)) {
Node node = map.get(key);
node.value = value;
node.frequency += 1;
removeNode(node);
addNodeWithUpdatedFrequency(node);
} else {
if (map.size() >= capacity) {
map.remove(this.head.key);
removeNode(head);
}
Node node = new Node(key, value, 1);
addNodeWithUpdatedFrequency(node);
map.put(key, node);
}
}
/**
* This method stores the node in the cache with updated frequency
*
* @param Node node which is to be updated in the cache
*/
private void addNodeWithUpdatedFrequency(Node node) {
if (tail != null && head != null) {
Node temp = this.head;
while (temp != null) {
if (temp.frequency > node.frequency) {
if (temp == head) {
node.next = temp;
temp.previous = node;
this.head = node;
break;
} else {
node.next = temp;
node.previous = temp.previous;
temp.previous.next = node;
temp.previous = node;
break;
}
} else {
temp = temp.next;
if (temp == null) {
tail.next = node;
node.previous = tail;
node.next = null;
tail = node;
break;
}
}
}
} else {
tail = node;
head = tail;
}
}
/**
* This method removes node from the cache
*
* @param Node node which is to be removed in the cache
*/
private void removeNode(Node node) {
if (node.previous != null) {
node.previous.next = node.next;
} else {
this.head = node.next;
}
if (node.next != null) {
node.next.previous = node.previous;
} else {
this.tail = node.previous;
}
}
}