internal LruLinkedList() { _head = new LruNode <TValue>(); _tail = new LruNode <TValue>(); _head.Next = _tail; _tail.Previous = _head; }
public void AddToTop(LruNode <TValue> node) { node.Next = _head.Next; _head.Next.Previous = node; node.Previous = _head; _head.Next = node; }
/// <summary> /// 向当前节点之前添加新节点。 /// </summary> /// <param name="node">要添加的新节点。</param> public void AddBefore(LruNode <TKey, TValue> node) { node.Next = this; node.Prev = this.Prev; this.Prev.Next = node; this.Prev = node; }
public void Delete(int key) { if (map.ContainsKey(key)) { LruNode <T> node = map[key]; doubleLinkedList.RemoveNode(node); } }
public void MoveAfter(LruNode node) { if (node.Next != this) { this.Next.Prev = this.Prev; this.Prev.Next = this.Next; InsertAfter(node); } }
private void Touch(LruNode node) { if (node != this.oldestNode) { node.MoveAfter(this.oldestNode); } else { this.oldestNode = node.Prev; } }
public T Get(int key) { if (!map.ContainsKey(key)) { return(null); } LruNode <T> node = map[key]; doubleLinkedList.RemoveNode(node); doubleLinkedList.AddToTop(node); return(node.Value); }
public void Clear() { lock (lockObj) { this.cacheMap.Clear(); var node = this.oldestNode; this.oldestNode = null; while (node != null) { var temp = node; node = node.Prev; temp.Clear(); } } }
public void Add(K key, V value) { lock (lockObj) { Entry entry; if (!this.cacheMap.TryGetValue(key, out entry)) { LruNode node; if (this.cacheMap.Count >= capacity) { node = this.oldestNode; this.cacheMap.Remove(node.Key); node.Key = key; this.oldestNode = node.Prev; } else { node = new LruNode(key); if (this.oldestNode != null) { node.InsertAfter(this.oldestNode); } else { this.oldestNode = node; } } entry.node = node; entry.value = value; this.cacheMap.Add(key, entry); } else { entry.value = value; this.cacheMap[key] = entry; Touch(entry.node); } } }
public void Add(int key, T value) { if (map.ContainsKey(key)) { LruNode <T> node = map[key]; doubleLinkedList.RemoveNode(node); node.Value = value; doubleLinkedList.AddToTop(node); } else { if (count == capacity) { LruNode <T> lru = doubleLinkedList.RemoveLRUNode(); map.Remove(lru.Key); count--; } LruNode <T> node = new LruNode <T>(key, value); doubleLinkedList.AddToTop(node); map[key] = node; count++; } }
public void Clear() { Key = default(K); Prev = Next = null; }
public void InsertAfter(LruNode node) { this.Prev = node; this.Next = node.Next; node.Next = this.Next.Prev = this; }
public LruNode(K key) { Key = key; Prev = Next = this; }
public Entry(LruNode node, V value) { this.node = node; this.value = value; }