-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathLRUCache.cs
87 lines (75 loc) · 2.24 KB
/
LRUCache.cs
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
using System;
using System.Collections.Generic;
namespace LRUCacheNamespace
{
public class LRUCache<K, V>
{
private readonly int _capacity;
private readonly Dictionary<K, Node<K, V>> _cache;
private readonly Node<K, V> _head;
private readonly Node<K, V> _tail;
public LRUCache(int capacity)
{
_capacity = capacity;
_cache = new Dictionary<K, Node<K, V>>(capacity);
// Initialize head and tail dummy nodes
_head = new Node<K, V>(default, default);
_tail = new Node<K, V>(default, default);
_head.Next = _tail;
_tail.Prev = _head;
}
public V Get(K key)
{
if (!_cache.TryGetValue(key, out var node))
{
return default; // Key doesn't exist, return null
}
MoveToHead(node); // Move the accessed node to the head
return node.Value;
}
public void Put(K key, V value)
{
if (_cache.TryGetValue(key, out var node))
{
node.Value = value; // Update the value
MoveToHead(node);
}
else
{
var newNode = new Node<K, V>(key, value);
_cache[key] = newNode;
AddToHead(newNode);
if (_cache.Count > _capacity)
{
var removedNode = RemoveTail();
_cache.Remove(removedNode.Key);
}
}
}
private void AddToHead(Node<K, V> node)
{
node.Prev = _head;
node.Next = _head.Next;
_head.Next.Prev = node;
_head.Next = node;
}
private void RemoveNode(Node<K, V> node)
{
var prevNode = node.Prev;
var nextNode = node.Next;
prevNode.Next = nextNode;
nextNode.Prev = prevNode;
}
private void MoveToHead(Node<K, V> node)
{
RemoveNode(node);
AddToHead(node);
}
private Node<K, V> RemoveTail()
{
var node = _tail.Prev;
RemoveNode(node);
return node;
}
}
}