public void Insert(CacheEntry head) { Prev = head; Next = head.Next; Next.Prev = this; Prev.Next = this; }
public new void Add(object key, object value) { CacheEntry entry; if (_map.TryGetValue(key, out entry)) { entry.Remove(); entry.Insert(_head); entry.Value = value; } else { entry = new CacheEntry(key, value); _map.Add(key, entry); entry.Insert(_head); if (_map.Count > _size) { CacheEntry randomEntry = FindRandomEntry(); _map.Remove(randomEntry.Key); randomEntry.Remove(); } } }
public void Add(object key, object value) { CacheEntry entry; if (_map.TryGetValue(key, out entry)) { entry.Remove(); entry.Insert(_head); entry.Value = value; } else { entry = new CacheEntry(key, value); _map.Add(key, entry); entry.Insert(_head); if (_map.Count > _size) { _map.Remove(_tail.Prev.Key); _tail.Prev.Remove(); } } }
public LRUCache(int size) { _size = size; _map = new Dictionary<object, CacheEntry>(size); _head = new CacheEntry(null, null); _tail = new CacheEntry(null, null); _head.Next = _tail; _tail.Prev = _head; }