private void PlaceTail(CustomLRUNode node) { node.Prev = _cacheTail.Prev; node.Next = _cacheTail; node.Prev.Next = node; node.Next.Prev = node; }
public CustomLRUCache(int capacity) { _cache = new Dictionary <int, CustomLRUNode>(); _cacheHead = new CustomLRUNode(-1, -1); _cacheTail = new CustomLRUNode(-1, -1); _cacheHead.Next = _cacheTail; _cacheTail.Prev = _cacheHead; _capacity = capacity; }
public void Put(int key, int value) { if (_cache.ContainsKey(key)) { MoveToTail(key); _cache[key].Data = value; } else { var newNode = new CustomLRUNode(key, value); PlaceTail(newNode); _cache[key] = newNode; } if (_cache.Count > _capacity) { _cache.Remove(_cacheHead.Next.Key); Remove(_cacheHead.Next); } }
private void Remove(CustomLRUNode node) { node.Prev.Next = node.Next; node.Next.Prev = node.Prev; }