Skip to content

Commit cbe98e6

Browse files
authored
Merge branch 'master' into test/PalindromeTest
2 parents 32cf7f7 + a6fcbf5 commit cbe98e6

File tree

2 files changed

+97
-27
lines changed

2 files changed

+97
-27
lines changed

src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
public class LFUCache<K, V> {
1111

1212
private class Node {
13-
14-
private K key;
13+
private final K key;
1514
private V value;
1615
private int frequency;
1716
private Node previous;
@@ -26,67 +25,67 @@ private class Node {
2625

2726
private Node head;
2827
private Node tail;
29-
private Map<K, Node> map = null;
30-
private Integer capacity;
28+
private final Map<K, Node> cache;
29+
private final int capacity;
3130
private static final int DEFAULT_CAPACITY = 100;
3231

3332
public LFUCache() {
34-
this.capacity = DEFAULT_CAPACITY;
33+
this(DEFAULT_CAPACITY);
3534
}
3635

37-
public LFUCache(Integer capacity) {
36+
public LFUCache(int capacity) {
37+
if (capacity <= 0) {
38+
throw new IllegalArgumentException("Capacity must be greater than zero.");
39+
}
3840
this.capacity = capacity;
39-
this.map = new HashMap<>();
41+
this.cache = new HashMap<>();
4042
}
4143

4244
/**
43-
* This method returns value present in the cache corresponding to the key passed as parameter
45+
* Retrieves the value for the given key from the cache. Increases the frequency of the node.
4446
*
45-
* @param <K> key for which value is to be retrieved
46-
* @returns <V> object corresponding to the key passed as parameter, returns null if <K> key is
47-
* not present in the cache
47+
* @param key The key to look up.
48+
* @return The value associated with the key, or null if the key is not present.
4849
*/
4950
public V get(K key) {
50-
if (this.map.get(key) == null) {
51+
Node node = cache.get(key);
52+
if (node == null) {
5153
return null;
5254
}
53-
54-
Node node = map.get(key);
5555
removeNode(node);
5656
node.frequency += 1;
5757
addNodeWithUpdatedFrequency(node);
58-
5958
return node.value;
6059
}
6160

6261
/**
63-
* This method stores <K> key and <V> value in the cache
62+
* Adds or updates a key-value pair in the cache. If the cache is full, the least frequently used item is evicted.
6463
*
65-
* @param <K> key which is to be stored in the cache
66-
* @param <V> value which is to be stored in the cache
64+
* @param key The key to insert or update.
65+
* @param value The value to insert or update.
6766
*/
6867
public void put(K key, V value) {
69-
if (map.containsKey(key)) {
70-
Node node = map.get(key);
68+
if (cache.containsKey(key)) {
69+
Node node = cache.get(key);
7170
node.value = value;
7271
node.frequency += 1;
7372
removeNode(node);
7473
addNodeWithUpdatedFrequency(node);
7574
} else {
76-
if (map.size() >= capacity) {
77-
map.remove(this.head.key);
75+
if (cache.size() >= capacity) {
76+
cache.remove(this.head.key);
7877
removeNode(head);
7978
}
8079
Node node = new Node(key, value, 1);
8180
addNodeWithUpdatedFrequency(node);
82-
map.put(key, node);
81+
cache.put(key, node);
8382
}
8483
}
8584

8685
/**
87-
* This method stores the node in the cache with updated frequency
86+
* Adds a node to the linked list in the correct position based on its frequency.
8887
*
89-
* @param Node node which is to be updated in the cache
88+
* @param node The node to add.
9089
*/
9190
private void addNodeWithUpdatedFrequency(Node node) {
9291
if (tail != null && head != null) {
@@ -123,9 +122,9 @@ private void addNodeWithUpdatedFrequency(Node node) {
123122
}
124123

125124
/**
126-
* This method removes node from the cache
125+
* Removes a node from the linked list.
127126
*
128-
* @param Node node which is to be removed in the cache
127+
* @param node The node to remove.
129128
*/
130129
private void removeNode(Node node) {
131130
if (node.previous != null) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.thealgorithms.datastructures.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.NoSuchElementException;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class LinkedListStackTest {
12+
13+
private LinkedListStack stack;
14+
15+
@BeforeEach
16+
public void setUp() {
17+
stack = new LinkedListStack();
18+
}
19+
20+
@Test
21+
public void testPushAndPeek() {
22+
stack.push(1);
23+
stack.push(2);
24+
stack.push(3);
25+
26+
assertEquals(3, stack.peek());
27+
assertEquals(3, stack.getSize());
28+
}
29+
30+
@Test
31+
public void testPop() {
32+
stack.push(1);
33+
stack.push(2);
34+
stack.push(3);
35+
36+
assertEquals(3, stack.pop());
37+
assertEquals(2, stack.pop());
38+
assertEquals(1, stack.pop());
39+
assertTrue(stack.isEmpty());
40+
}
41+
42+
@Test
43+
public void testPopEmptyStack() {
44+
org.junit.jupiter.api.Assertions.assertThrows(NoSuchElementException.class, () -> stack.pop());
45+
}
46+
47+
@Test
48+
public void testPeekEmptyStack() {
49+
org.junit.jupiter.api.Assertions.assertThrows(NoSuchElementException.class, () -> stack.peek());
50+
}
51+
52+
@Test
53+
public void testIsEmpty() {
54+
assertTrue(stack.isEmpty());
55+
56+
stack.push(1);
57+
assertFalse(stack.isEmpty());
58+
59+
stack.pop();
60+
assertTrue(stack.isEmpty());
61+
}
62+
63+
@Test
64+
public void testToString() {
65+
stack.push(1);
66+
stack.push(2);
67+
stack.push(3);
68+
69+
assertEquals("3->2->1", stack.toString());
70+
}
71+
}

0 commit comments

Comments
 (0)