Beispiel #1
0
 public void RemoveNode(LRUNode <T> node)
 {
     node.Previous.Next = node.Next;
     node.Next.Previous = node.Previous;
     node.Next          = null;
     node.Previous      = null;
 }
Beispiel #2
0
 public void AddToTop(LRUNode <T> node)
 {
     node.Next          = Head.Next;
     Head.Next.Previous = node;
     node.Previous      = Head;
     Head.Next          = node;
 }
Beispiel #3
0
        public void Add(string key, T value)
        {
            if (DataSource.ContainsKey(key))
            {
                LRUNode <T> node = DataSource[key];

                DoubleLinkedList.RemoveNode(node);
                node.Value = value;
                DoubleLinkedList.AddToTop(node);
            }
            else
            {
                if (Count == Capacity)
                {
                    LRUNode <T> lru = DoubleLinkedList.RemoveLRUNode();
                    DataSource.TryRemove(lru.Key, out _);
                    Count--;
                }

                LRUNode <T> node = new LRUNode <T>(key, value);

                DoubleLinkedList.AddToTop(node);
                DataSource.TryAdd(key, node);
                Count++;
            }
        }
Beispiel #4
0
        public LRUNode <T> RemoveLRUNode()
        {
            LRUNode <T> target = Tail.Previous;

            RemoveNode(target);

            return(target);
        }
Beispiel #5
0
        public LRUDoubleLinkedList()
        {
            Head = new LRUNode <T>();
            Tail = new LRUNode <T>();

            Head.Next     = Tail;
            Tail.Previous = Head;
        }
Beispiel #6
0
        public T Get(string key)
        {
            if (!DataSource.ContainsKey(key))
            {
                return(null);
            }

            LRUNode <T> node = DataSource[key];

            DoubleLinkedList.RemoveNode(node);
            DoubleLinkedList.AddToTop(node);

            return(node.Value);
        }