Esempio n. 1
0
 internal LruLinkedList()
 {
     _head          = new LruNode <TValue>();
     _tail          = new LruNode <TValue>();
     _head.Next     = _tail;
     _tail.Previous = _head;
 }
Esempio n. 2
0
 public void AddToTop(LruNode <TValue> node)
 {
     node.Next           = _head.Next;
     _head.Next.Previous = node;
     node.Previous       = _head;
     _head.Next          = node;
 }
Esempio n. 3
0
 /// <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;
 }
Esempio n. 4
0
 public void Delete(int key)
 {
     if (map.ContainsKey(key))
     {
         LruNode <T> node = map[key];
         doubleLinkedList.RemoveNode(node);
     }
 }
Esempio n. 5
0
 public void MoveAfter(LruNode node)
 {
     if (node.Next != this)
     {
         this.Next.Prev = this.Prev;
         this.Prev.Next = this.Next;
         InsertAfter(node);
     }
 }
Esempio n. 6
0
 private void Touch(LruNode node)
 {
     if (node != this.oldestNode)
     {
         node.MoveAfter(this.oldestNode);
     }
     else
     {
         this.oldestNode = node.Prev;
     }
 }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
 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();
         }
     }
 }
Esempio n. 9
0
 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);
         }
     }
 }
Esempio n. 10
0
        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++;
            }
        }
Esempio n. 11
0
 public void Clear()
 {
     Key  = default(K);
     Prev = Next = null;
 }
Esempio n. 12
0
 public void InsertAfter(LruNode node)
 {
     this.Prev = node;
     this.Next = node.Next;
     node.Next = this.Next.Prev = this;
 }
Esempio n. 13
0
 public LruNode(K key)
 {
     Key  = key;
     Prev = Next = this;
 }
Esempio n. 14
0
 public Entry(LruNode node, V value)
 {
     this.node  = node;
     this.value = value;
 }