Format code with prettier (#3375)

This commit is contained in:
acbin
2022-10-03 17:23:00 +08:00
committed by GitHub
parent 32b9b11ed5
commit e96f567bfc
464 changed files with 11483 additions and 6189 deletions

View File

@ -7,141 +7,136 @@ 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> {
public class LFUCache<K, V> {
private class Node {
private K key;
private V value;
private int frequency;
private Node previous;
private Node next;
private class Node {
public Node(K key, V value, int frequency) {
this.key = key;
this.value = value;
this.frequency = frequency;
}
}
private K key;
private V value;
private int frequency;
private Node previous;
private Node next;
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 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<>();
}
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
* @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;
}
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);
Node node = map.get(key);
removeNode(node);
node.frequency += 1;
addNodeWithUpdatedFrequency(node);
return node.value;
}
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
* @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);
}
}
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
* @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;
node.previous = temp.previous;
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;
}
}
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;
node.previous = temp.previous;
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
* 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;
}
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;
}
}
if (node.next != null) {
node.next.previous = node.previous;
} else {
this.tail = node.previous;
}
}
}

View File

@ -126,10 +126,14 @@ public class LRUCache<K, V> {
private I key;
private J value;
public Entry() {
}
public Entry() {}
public Entry(Entry<I, J> preEntry, Entry<I, J> nextEntry, I key, J value) {
public Entry(
Entry<I, J> preEntry,
Entry<I, J> nextEntry,
I key,
J value
) {
this.preEntry = preEntry;
this.nextEntry = nextEntry;
this.key = key;

View File

@ -124,10 +124,14 @@ public class MRUCache<K, V> {
private I key;
private J value;
public Entry() {
}
public Entry() {}
public Entry(Entry<I, J> preEntry, Entry<I, J> nextEntry, I key, J value) {
public Entry(
Entry<I, J> preEntry,
Entry<I, J> nextEntry,
I key,
J value
) {
this.preEntry = preEntry;
this.nextEntry = nextEntry;
this.key = key;