|
27 | 27 | // lRUCache.get(3); // return 3
|
28 | 28 | // lRUCache.get(4); // return 4
|
29 | 29 |
|
| 30 | +//Using LinkList |
| 31 | +public class LRUCache |
| 32 | +{ |
| 33 | + // Define the capacity of the LRU Cache |
| 34 | + private readonly int capacity; |
| 35 | + |
| 36 | + // Dictionary to store the cache items with key as the integer key and value as the LinkedListNode containing a tuple (key, value) |
| 37 | + private readonly Dictionary<int, LinkedListNode<(int key, int value)>> cache; |
| 38 | + |
| 39 | + // LinkedList to maintain the order of usage, with the most recently used item at the front |
| 40 | + private readonly LinkedList<(int key, int value)> lruList; |
30 | 41 |
|
31 |
| -// |
32 |
| -public class LRUCache { |
33 |
| -private readonly int _capacity; |
34 |
| - private readonly Dictionary<int, LinkedListNode<KeyValuePair<int, int>>> _cache; |
35 |
| - private readonly LinkedList<KeyValuePair<int, int>> _lru; |
36 |
| - |
37 |
| - public LRUCache(int capacity) |
| 42 | + // Constructor to initialize the LRU Cache with a given capacity |
| 43 | + public LRUCache(int capacity) |
38 | 44 | {
|
39 |
| - _capacity = capacity; |
40 |
| - _cache = new(); |
41 |
| - _lru = new(); |
| 45 | + this.capacity = capacity; |
| 46 | + cache = new Dictionary<int, LinkedListNode<(int key, int value)>>(); |
| 47 | + lruList = new LinkedList<(int key, int value)>(); |
42 | 48 | }
|
43 |
| - |
44 |
| - public int Get(int key) |
| 49 | + |
| 50 | + // Method to get the value associated with a key |
| 51 | + public int Get(int key) |
45 | 52 | {
|
46 |
| - if (!_cache.TryGetValue(key, out var node)) |
| 53 | + // Check if the key exists in the cache |
| 54 | + if (cache.TryGetValue(key, out LinkedListNode<(int key, int value)> node)) |
47 | 55 | {
|
48 |
| - return -1; |
| 56 | + // If key exists, move the node to the front of the LinkedList (most recently used) |
| 57 | + lruList.Remove(node); |
| 58 | + lruList.AddFirst(node); |
| 59 | + // Return the value associated with the key |
| 60 | + return node.Value.value; |
49 | 61 | }
|
50 |
| - |
51 |
| - _lru.Remove(node); |
52 |
| - _cache[key] = _lru.AddFirst(node.Value); |
53 |
| - |
54 |
| - return node.Value.Value; |
| 62 | + // If key does not exist, return -1 |
| 63 | + return -1; // Key not found |
55 | 64 | }
|
56 |
| - |
57 |
| - public void Put(int key, int value) |
| 65 | + |
| 66 | + // Method to put a key-value pair into the cache |
| 67 | + public void Put(int key, int value) |
58 | 68 | {
|
59 |
| - if (_cache.TryGetValue(key, out var node)) |
| 69 | + // Check if the key already exists in the cache |
| 70 | + if (cache.TryGetValue(key, out LinkedListNode<(int key, int value)> node)) |
60 | 71 | {
|
61 |
| - _lru.Remove(node); |
62 |
| - _cache.Remove(key); |
| 72 | + // If key exists, remove the node from its current position |
| 73 | + lruList.Remove(node); |
| 74 | + // Update the value of the node |
| 75 | + node.Value = (key, value); |
| 76 | + // Add the node to the front of the LinkedList (most recently used) |
| 77 | + lruList.AddFirst(node); |
63 | 78 | }
|
64 |
| - |
65 |
| - node = _lru.AddFirst(new KeyValuePair<int, int>(key, value)); |
66 |
| - _cache[key] = node; |
67 |
| - |
68 |
| - if (_lru.Count > _capacity) |
| 79 | + else |
69 | 80 | {
|
70 |
| - node = _lru.Last; |
71 |
| - _lru.RemoveLast(); |
72 |
| - _cache.Remove(node.Value.Key); |
| 81 | + // If key does not exist and cache is at full capacity |
| 82 | + if (cache.Count >= capacity) |
| 83 | + { |
| 84 | + // Remove the least recently used item from the LinkedList and the cache |
| 85 | + var lruNode = lruList.Last; |
| 86 | + lruList.RemoveLast(); |
| 87 | + cache.Remove(lruNode.Value.key); |
| 88 | + } |
| 89 | + // Create a new node for the key-value pair |
| 90 | + var newNode = new LinkedListNode<(int key, int value)>((key, value)); |
| 91 | + // Add the new node to the front of the LinkedList (most recently used) |
| 92 | + lruList.AddFirst(newNode); |
| 93 | + // Add the new node to the cache |
| 94 | + cache[key] = newNode; |
73 | 95 | }
|
74 | 96 | }
|
75 | 97 | }
|
76 | 98 |
|
| 99 | +/** |
| 100 | + * Your LRUCache object will be instantiated and called as such: |
| 101 | + * LRUCache obj = new LRUCache(capacity); |
| 102 | + * int param_1 = obj.Get(key); |
| 103 | + * obj.Put(key,value); |
| 104 | + */ |
| 105 | + |
77 | 106 | //self defined DLL
|
78 | 107 | public class LRUCache {
|
79 | 108 | public class DoublyLinkList
|
|
0 commit comments