Пример #1
0
 public void Insert(CacheEntry head)
 {
     Prev = head;
     Next = head.Next;
     Next.Prev = this;
     Prev.Next = this;
 }
Пример #2
0
 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();
         }
     }
 }
Пример #3
0
 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();
         }
     }
 }
Пример #4
0
 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;
 }