From 092e69ffe368279844ad26e75b59fdd298a724d4 Mon Sep 17 00:00:00 2001 From: Adarsh Pandey <165834547+devxadarsh@users.noreply.github.com> Date: Tue, 23 Apr 2024 10:20:28 +0530 Subject: [PATCH 01/14] Create ARCCache.java --- .../datastructures/caches/ARCCache.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java diff --git a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java new file mode 100644 index 000000000000..e0afac4dfea9 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java @@ -0,0 +1,105 @@ +package com.thealgorithms.datastructures.caches; + +import java.util.LinkedHashMap; +import java.util.Map; +/** + * Adaptive Replacement Cache (ARC) + *

+ * dynamically adjusts cache size based on recent access patterns. + * It aims to provide better performance compared to traditional caching algorithms + * like LRU (Least Recently Used) and LFU (Least Frequently Used). + * It combines elements of LRU (Least Recently Used) and LFU (Least Frequently Used) algorithms + * to efficiently manage frequently accessed and recently used items, + * optimizing cache performance in changing workload scenarios. + *

+ * ... + * @author Adarsh Pandey (...) + * + * @param key type + * @param value type + */ + +public class ARCCache { + private final int capacity; + private final Map cache; + private final LinkedHashMap usageCounts; + private final int p; + private int totalCount; + + public ARCCache(int capacity) { + this.capacity = capacity; + this.cache = new LinkedHashMap<>(); + this.usageCounts = new LinkedHashMap<>(); + this.p = capacity / 2; + this.totalCount = 0; + } + + // Function to get data from cache corresponding to the key passed as parameter + public V get(K key) { + if (cache.containsKey(key)) { + usageCounts.put(key, usageCounts.getOrDefault(key, 0) + 1); + return cache.get(key); + } + return null; + } + + // Function to put the data in the cache corresponding to the key passed as parameter + public void put(K key, V value) { + if (cache.size() >= capacity) { + evict(); + } + cache.put(key, value); + usageCounts.put(key, 1); + totalCount++; + } + + // Function to implement the logic of ARCCache + private void evict() { + if (!cache.isEmpty()) { + K keyToRemove = null; + int minUsageCount = Integer.MAX_VALUE; + for (Map.Entry entry : usageCounts.entrySet()) { + if (entry.getValue() < minUsageCount) { + keyToRemove = entry.getKey(); + minUsageCount = entry.getValue(); + } + } + cache.remove(keyToRemove); + usageCounts.remove(keyToRemove); + totalCount--; + } + } + + + public static void main(String[] args) { + + // Defining capacity of cache Using Map + ARCCache cache = new ARCCache<>(5); + cache.put("A", 1); + cache.put("B", 2); + cache.put("C", 3); + cache.put("D", 4); + cache.put("E", 5); + + // Printing Cache contents using enhanced for loop + System.out.println("Cache contents:"); + for (Map.Entry entry : cache.cache.entrySet()) { + System.out.println(entry.getKey() + ": " + entry.getValue()); + } + + // Accessing an existing key + System.out.println("Value of key 'B': " + cache.get("B")); + + // Adding a new key + cache.put("F", 6); + + // Printing Cache contents after adding Key using enhanced for loop + System.out.println("Cache contents after adding key 'F':"); + for (Map.Entry entry : cache.cache.entrySet()) { + System.out.println(entry.getKey() + ": " + entry.getValue()); + } + + // Accessing a non existing key + System.out.println("Value of key 'A': " + cache.get("A")); + } +} From 45fb360c77ff5e3a6a83f290a4d98a1f334e3c21 Mon Sep 17 00:00:00 2001 From: Adarsh Pandey <165834547+devxadarsh@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:05:53 +0530 Subject: [PATCH 02/14] Delete src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java --- .../datastructures/caches/ARCCache.java | 105 ------------------ 1 file changed, 105 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java diff --git a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java deleted file mode 100644 index e0afac4dfea9..000000000000 --- a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.thealgorithms.datastructures.caches; - -import java.util.LinkedHashMap; -import java.util.Map; -/** - * Adaptive Replacement Cache (ARC) - *

- * dynamically adjusts cache size based on recent access patterns. - * It aims to provide better performance compared to traditional caching algorithms - * like LRU (Least Recently Used) and LFU (Least Frequently Used). - * It combines elements of LRU (Least Recently Used) and LFU (Least Frequently Used) algorithms - * to efficiently manage frequently accessed and recently used items, - * optimizing cache performance in changing workload scenarios. - *

- * ... - * @author Adarsh Pandey (...) - * - * @param key type - * @param value type - */ - -public class ARCCache { - private final int capacity; - private final Map cache; - private final LinkedHashMap usageCounts; - private final int p; - private int totalCount; - - public ARCCache(int capacity) { - this.capacity = capacity; - this.cache = new LinkedHashMap<>(); - this.usageCounts = new LinkedHashMap<>(); - this.p = capacity / 2; - this.totalCount = 0; - } - - // Function to get data from cache corresponding to the key passed as parameter - public V get(K key) { - if (cache.containsKey(key)) { - usageCounts.put(key, usageCounts.getOrDefault(key, 0) + 1); - return cache.get(key); - } - return null; - } - - // Function to put the data in the cache corresponding to the key passed as parameter - public void put(K key, V value) { - if (cache.size() >= capacity) { - evict(); - } - cache.put(key, value); - usageCounts.put(key, 1); - totalCount++; - } - - // Function to implement the logic of ARCCache - private void evict() { - if (!cache.isEmpty()) { - K keyToRemove = null; - int minUsageCount = Integer.MAX_VALUE; - for (Map.Entry entry : usageCounts.entrySet()) { - if (entry.getValue() < minUsageCount) { - keyToRemove = entry.getKey(); - minUsageCount = entry.getValue(); - } - } - cache.remove(keyToRemove); - usageCounts.remove(keyToRemove); - totalCount--; - } - } - - - public static void main(String[] args) { - - // Defining capacity of cache Using Map - ARCCache cache = new ARCCache<>(5); - cache.put("A", 1); - cache.put("B", 2); - cache.put("C", 3); - cache.put("D", 4); - cache.put("E", 5); - - // Printing Cache contents using enhanced for loop - System.out.println("Cache contents:"); - for (Map.Entry entry : cache.cache.entrySet()) { - System.out.println(entry.getKey() + ": " + entry.getValue()); - } - - // Accessing an existing key - System.out.println("Value of key 'B': " + cache.get("B")); - - // Adding a new key - cache.put("F", 6); - - // Printing Cache contents after adding Key using enhanced for loop - System.out.println("Cache contents after adding key 'F':"); - for (Map.Entry entry : cache.cache.entrySet()) { - System.out.println(entry.getKey() + ": " + entry.getValue()); - } - - // Accessing a non existing key - System.out.println("Value of key 'A': " + cache.get("A")); - } -} From bfb26f694fef4240d3ef686ffd45d2d3969e9d91 Mon Sep 17 00:00:00 2001 From: Adarsh Pandey <165834547+devxadarsh@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:10:04 +0530 Subject: [PATCH 03/14] Adaptive Replacement Cache (ARC) It aims to provide better performance compared to traditional caching algorithms like LRU (Least Recently Used) and LFU (Least Frequently Used). --- .../datastructures/caches/ARCCache.java | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java diff --git a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java new file mode 100644 index 000000000000..8d1525756cf8 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java @@ -0,0 +1,103 @@ +package com.thealgorithms.datastructures.caches; + +import java.util.LinkedHashMap; +import java.util.Map; +/** + * Adaptive Replacement Cache (ARC) + *

+ * dynamically adjusts cache size based on recent access patterns. + * It aims to provide better performance compared to traditional caching algorithms + * like LRU (Least Recently Used) and LFU (Least Frequently Used). + * It combines elements of LRU (Least Recently Used) and LFU (Least Frequently Used) algorithms + * to efficiently manage frequently accessed and recently used items, + * optimizing cache performance in changing workload scenarios. + * ... + * @author Adarsh Pandey (...) + * + * @param key type + * @param value type + */ + +public class ARCCache { + private final int capacity; + private final Map cache; + private final LinkedHashMap usageCounts; + private final int p; + private int totalCount; + + public ARCCache(int capacity) { + this.capacity = capacity; + this.cache = new LinkedHashMap<>(); + this.usageCounts = new LinkedHashMap<>(); + this.p = capacity / 2; + this.totalCount = 0; + } + + // Function to get data from cache corresponding to the key passed as parameter + public V get(K key) { + if (cache.containsKey(key)) { + usageCounts.put(key, usageCounts.getOrDefault(key, 0) + 1); + return cache.get(key); + } + return null; + } + + // Function to put the data in the cache corresponding to the key passed as parameter + public void put(K key, V value) { + if (cache.size() >= capacity) { + evict(); + } + cache.put(key, value); + usageCounts.put(key, 1); + totalCount++; + } + + // Function to implement the logic of ARCCache + private void evict() { + if (!cache.isEmpty()) { + K keyToRemove = null; + int minUsageCount = Integer.MAX_VALUE; + for (Map.Entry entry : usageCounts.entrySet()) { + if (entry.getValue() < minUsageCount) { + keyToRemove = entry.getKey(); + minUsageCount = entry.getValue(); + } + } + cache.remove(keyToRemove); + usageCounts.remove(keyToRemove); + totalCount--; + } + } + + public static void main(String[] args) { + + // Defining capacity of cache Using Map + ARCCache cache = new ARCCache<>(5); + cache.put("A", 1); + cache.put("B", 2); + cache.put("C", 3); + cache.put("D", 4); + cache.put("E", 5); + + // Printing Cache contents using enhanced for loop + System.out.println("Cache contents:"); + for (Map.Entry entry : cache.cache.entrySet()) { + System.out.println(entry.getKey() + ": " + entry.getValue()); + } + + // Accessing an existing key + System.out.println("Value of key 'B': " + cache.get("B")); + + // Adding a new key + cache.put("F", 6); + + // Printing Cache contents after adding Key using enhanced for loop + System.out.println("Cache contents after adding key 'F':"); + for (Map.Entry entry : cache.cache.entrySet()) { + System.out.println(entry.getKey() + ": " + entry.getValue()); + } + + // Accessing a non existing key + System.out.println("Value of key 'A': " + cache.get("A")); + } +} From 4d5a3963049e67980b4a1c7e2b36037b6118c5e4 Mon Sep 17 00:00:00 2001 From: Adarsh Pandey <165834547+devxadarsh@users.noreply.github.com> Date: Wed, 24 Apr 2024 00:28:50 +0530 Subject: [PATCH 04/14] Update ARCCache.java Main Method Removed for ARCCache.java --- .../datastructures/caches/ARCCache.java | 32 ------------------- 1 file changed, 32 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java index 8d1525756cf8..6de139a1681b 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java @@ -68,36 +68,4 @@ private void evict() { totalCount--; } } - - public static void main(String[] args) { - - // Defining capacity of cache Using Map - ARCCache cache = new ARCCache<>(5); - cache.put("A", 1); - cache.put("B", 2); - cache.put("C", 3); - cache.put("D", 4); - cache.put("E", 5); - - // Printing Cache contents using enhanced for loop - System.out.println("Cache contents:"); - for (Map.Entry entry : cache.cache.entrySet()) { - System.out.println(entry.getKey() + ": " + entry.getValue()); - } - - // Accessing an existing key - System.out.println("Value of key 'B': " + cache.get("B")); - - // Adding a new key - cache.put("F", 6); - - // Printing Cache contents after adding Key using enhanced for loop - System.out.println("Cache contents after adding key 'F':"); - for (Map.Entry entry : cache.cache.entrySet()) { - System.out.println(entry.getKey() + ": " + entry.getValue()); - } - - // Accessing a non existing key - System.out.println("Value of key 'A': " + cache.get("A")); - } } From 5249c26c8942473a847dfc5a5cd78d4f77403cda Mon Sep 17 00:00:00 2001 From: Adarsh Pandey <165834547+devxadarsh@users.noreply.github.com> Date: Wed, 24 Apr 2024 00:32:44 +0530 Subject: [PATCH 05/14] Added ARCCacheTest.java Test cases added for the ARC --- .../datastructures/caches/ARCCacheTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java diff --git a/src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java b/src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java new file mode 100644 index 000000000000..e97b78dc1fb9 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java @@ -0,0 +1,69 @@ +package com.thealgorithms.datastructures.caches; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ARCCacheTest { + private ARCCache cache; + + @BeforeEach + public void setUp() { + cache = new ARCCache<>(3); // Set capacity to 3 for testing purposes + } + + @Test + public void testPutAndGet() { + cache.put(1, "Value1"); + cache.put(2, "Value2"); + cache.put(3, "Value3"); + + assertEquals("Value1", cache.get(1)); + assertEquals("Value2", cache.get(2)); + assertEquals("Value3", cache.get(3)); + } + + @Test + public void testEviction() { + cache.put(1, "Value1"); + cache.put(2, "Value2"); + cache.put(3, "Value3"); + + cache.put(4, "Value4"); // This should evict key 1 + + assertNull(cache.get(1)); // Key 1 should have been evicted + assertEquals("Value2", cache.get(2)); // Other keys should still be present + assertEquals("Value3", cache.get(3)); + assertEquals("Value4", cache.get(4)); + } + + @Test + public void nullKeysAndValues() { + cache.put(null, "Value1"); + cache.put(2, null); + + assertEquals("Value1", cache.get(null)); + assertNull(cache.get(2)); + assertNull(cache.get(6)); + } + + @Test + public void testRepeatedGet() { + cache.put(1, "Value1"); + + // Repeated get calls should not affect eviction + cache.get(1); + cache.get(1); + cache.get(1); + + // Adding new elements should still evict old ones + cache.put(2, "Value2"); + cache.put(3, "Value3"); + cache.put(4, "Value4"); + + assertNull(cache.get(2)); // Key 2 should have been evicted + assertEquals("Value1", cache.get(1)); // Other keys should still be present + assertEquals("Value3", cache.get(3)); // Other keys should still be present + } +} From afc2e191208447d4530404ca5eadc761dfda22c5 Mon Sep 17 00:00:00 2001 From: Adarsh Pandey <165834547+devxadarsh@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:34:32 +0530 Subject: [PATCH 06/14] Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java Co-authored-by: SOZEL <80200848+TruongNhanNguyen@users.noreply.github.com> --- .../com/thealgorithms/datastructures/caches/ARCCache.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java index 6de139a1681b..338e4c2c12eb 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java @@ -24,7 +24,13 @@ public class ARCCache { private final LinkedHashMap usageCounts; private final int p; private int totalCount; - +/** + * Retrieves the value associated with the given key from the cache. + * If the key is present in the cache, its usage count is incremented. + * + * @param key the key whose associated value is to be retrieved + * @return the value associated with the key, or null if the key is not present in the cache + */ public ARCCache(int capacity) { this.capacity = capacity; this.cache = new LinkedHashMap<>(); From 67e2053a3e7a4af44b0eb047dad2769e84a2b311 Mon Sep 17 00:00:00 2001 From: Adarsh Pandey <165834547+devxadarsh@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:34:55 +0530 Subject: [PATCH 07/14] Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java Co-authored-by: SOZEL <80200848+TruongNhanNguyen@users.noreply.github.com> --- .../com/thealgorithms/datastructures/caches/ARCCache.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java index 338e4c2c12eb..ee73a4d617c1 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java @@ -39,7 +39,13 @@ public ARCCache(int capacity) { this.totalCount = 0; } - // Function to get data from cache corresponding to the key passed as parameter + /** + * Retrieves the value associated with the given key from the cache. + * If the key is present in the cache, its usage count is incremented. + * + * @param key the key whose associated value is to be retrieved + * @return the value associated with the key, or null if the key is not present in the cache + */ public V get(K key) { if (cache.containsKey(key)) { usageCounts.put(key, usageCounts.getOrDefault(key, 0) + 1); From e27f0b8a73c1454f92adb19cb839c78211fa538f Mon Sep 17 00:00:00 2001 From: Adarsh Pandey <165834547+devxadarsh@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:35:17 +0530 Subject: [PATCH 08/14] Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java Co-authored-by: SOZEL <80200848+TruongNhanNguyen@users.noreply.github.com> --- .../thealgorithms/datastructures/caches/ARCCache.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java index ee73a4d617c1..50f97731c90d 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java @@ -54,7 +54,14 @@ public V get(K key) { return null; } - // Function to put the data in the cache corresponding to the key passed as parameter + /** + * Adds the specified key-value pair to the cache. + * If the cache exceeds its capacity after adding the new entry, eviction is performed. + * Updates the usage count for the added key. + * + * @param key the key with which the specified value is to be associated + * @param value the value to be associated with the specified key + */ public void put(K key, V value) { if (cache.size() >= capacity) { evict(); From 158970136587ad34014edaf81952db0b7e9523d7 Mon Sep 17 00:00:00 2001 From: Adarsh Pandey <165834547+devxadarsh@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:35:52 +0530 Subject: [PATCH 09/14] Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java Co-authored-by: SOZEL <80200848+TruongNhanNguyen@users.noreply.github.com> --- .../com/thealgorithms/datastructures/caches/ARCCache.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java index 50f97731c90d..e7891d4ac970 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java @@ -71,7 +71,11 @@ public void put(K key, V value) { totalCount++; } - // Function to implement the logic of ARCCache + /** + * Evicts an item from the cache when it exceeds its capacity. + * Implements the Adaptive Replacement Cache (ARC) algorithm logic for eviction. + * Removes the least recently used item based on its usage count. + */ private void evict() { if (!cache.isEmpty()) { K keyToRemove = null; From 600554c2fdb526bd003b7e1550431b54c49dfa70 Mon Sep 17 00:00:00 2001 From: Adarsh Pandey <165834547+devxadarsh@users.noreply.github.com> Date: Wed, 24 Apr 2024 12:04:14 +0530 Subject: [PATCH 10/14] Update ARCCache.java Replaced private final p(Pivot) using private final int t1Capacity; private final int b1Capacity; adjustCacheSize(); --- .../datastructures/caches/ARCCache.java | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java index e7891d4ac970..93192ff9acb5 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java @@ -22,7 +22,8 @@ public class ARCCache { private final int capacity; private final Map cache; private final LinkedHashMap usageCounts; - private final int p; + private final int t1Capacity; // Capacity for the t1 cache + private final int b1Capacity; // Capacity for the b1 cache private int totalCount; /** * Retrieves the value associated with the given key from the cache. @@ -35,7 +36,8 @@ public ARCCache(int capacity) { this.capacity = capacity; this.cache = new LinkedHashMap<>(); this.usageCounts = new LinkedHashMap<>(); - this.p = capacity / 2; + this.t1Capacity = capacity / 2; // Capacity for the t1 cache + this.b1Capacity = capacity - t1Capacity; // Capacity for the b1 cache this.totalCount = 0; } @@ -89,6 +91,33 @@ private void evict() { cache.remove(keyToRemove); usageCounts.remove(keyToRemove); totalCount--; + adjustCacheSize(); + } + } + + /** + * Adjust the cache sizes based on t1capacity and b1capacity after eviction from cache + */ + private void adjustCacheSize() { + if (cache.size() > capacity) { + int excess = cache.size() - capacity; + int t1Size = cache.size() - b1Capacity; + while (excess > 0 && !cache.isEmpty()) { + K keyToRemove = usageCounts.keySet().iterator().next(); + if (t1Size > t1Capacity || (t1Size > 0 && usageCounts.get(keyToRemove) > 1)) { + cache.remove(keyToRemove); + usageCounts.remove(keyToRemove); + totalCount--; + if (t1Size > 0) { + t1Size--; + } + } else { + cache.remove(keyToRemove); + usageCounts.remove(keyToRemove); + totalCount--; + } + excess--; + } } } } From fa53c3c5b4e89aab3d2b59adbdb8961891b4b503 Mon Sep 17 00:00:00 2001 From: Adarsh Pandey <165834547+devxadarsh@users.noreply.github.com> Date: Wed, 24 Apr 2024 12:07:47 +0530 Subject: [PATCH 11/14] Update ARCCacheTest.java updated test case for t1capacity and b1 capacity --- .../thealgorithms/datastructures/caches/ARCCacheTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java b/src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java index e97b78dc1fb9..dbca7c24c037 100644 --- a/src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java +++ b/src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java @@ -10,7 +10,10 @@ public class ARCCacheTest { @BeforeEach public void setUp() { - cache = new ARCCache<>(3); // Set capacity to 3 for testing purposes + int t1Capacity = 2; + int b1Capacity = 1; + int totalCapacity = t1Capacity + b1Capacity; + cache = new ARCCache<>(totalCapacity); // Set capacity to 3 for testing purposes } @Test From 75b9f7eb7401a94eb824d95e83c0eb18f4e048a4 Mon Sep 17 00:00:00 2001 From: Adarshdm <39560104+Adarshdm@users.noreply.github.com> Date: Wed, 8 May 2024 11:34:21 +0530 Subject: [PATCH 12/14] Updated capacity method --- .../datastructures/caches/ARCCache.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java index 93192ff9acb5..d7d38335ff80 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java @@ -19,21 +19,16 @@ */ public class ARCCache { - private final int capacity; private final Map cache; private final LinkedHashMap usageCounts; private final int t1Capacity; // Capacity for the t1 cache private final int b1Capacity; // Capacity for the b1 cache private int totalCount; -/** - * Retrieves the value associated with the given key from the cache. - * If the key is present in the cache, its usage count is incremented. - * - * @param key the key whose associated value is to be retrieved - * @return the value associated with the key, or null if the key is not present in the cache - */ + + /** + * This constructor initializes an ARCCache object with the given capacity and initializes other necessary fields + */ public ARCCache(int capacity) { - this.capacity = capacity; this.cache = new LinkedHashMap<>(); this.usageCounts = new LinkedHashMap<>(); this.t1Capacity = capacity / 2; // Capacity for the t1 cache @@ -41,6 +36,15 @@ public ARCCache(int capacity) { this.totalCount = 0; } + /** + * Returns the total capacity of the cache + * + * @return the total capacity of the cache + */ + private int capacity() { + return t1Capacity + b1Capacity; + } + /** * Retrieves the value associated with the given key from the cache. * If the key is present in the cache, its usage count is incremented. @@ -65,7 +69,7 @@ public V get(K key) { * @param value the value to be associated with the specified key */ public void put(K key, V value) { - if (cache.size() >= capacity) { + if (cache.size() >= capacity()) { evict(); } cache.put(key, value); @@ -99,8 +103,8 @@ private void evict() { * Adjust the cache sizes based on t1capacity and b1capacity after eviction from cache */ private void adjustCacheSize() { - if (cache.size() > capacity) { - int excess = cache.size() - capacity; + if (cache.size() > capacity()) { + int excess = cache.size() - capacity(); int t1Size = cache.size() - b1Capacity; while (excess > 0 && !cache.isEmpty()) { K keyToRemove = usageCounts.keySet().iterator().next(); From 3007f2dc826839e081642b26515428ade18ce3f1 Mon Sep 17 00:00:00 2001 From: Adarshdm <39560104+Adarshdm@users.noreply.github.com> Date: Wed, 8 May 2024 12:08:39 +0530 Subject: [PATCH 13/14] Updated capacity method --- .../com/thealgorithms/datastructures/caches/ARCCache.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java index d7d38335ff80..5185f8583a57 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java @@ -27,8 +27,13 @@ public class ARCCache { /** * This constructor initializes an ARCCache object with the given capacity and initializes other necessary fields + * @param capacity the initial capacity of the cache + * @throws IllegalArgumentException if the capacity is negative */ public ARCCache(int capacity) { + if (capacity < 0) { + throw new IllegalArgumentException("Capacity cannot be negative"); + } this.cache = new LinkedHashMap<>(); this.usageCounts = new LinkedHashMap<>(); this.t1Capacity = capacity / 2; // Capacity for the t1 cache From 04be24359d971a97136fbf1966a51ce33fca900f Mon Sep 17 00:00:00 2001 From: devxadarsh Date: Wed, 8 May 2024 13:24:50 +0530 Subject: [PATCH 14/14] Updated Imports --- .../com/thealgorithms/datastructures/caches/ARCCacheTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java b/src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java index dbca7c24c037..79787125dbf7 100644 --- a/src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java +++ b/src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.datastructures.caches; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test;