Exemple #1
0
 private void AddKey(int key, LRUNode value)
 {
     if (!InCache(key))
     {
         _map.Add(key, value);
     }
 }
Exemple #2
0
        public void Put(int key, int value)
        {
            if (InCache(key))
            {
                var node = GetNode(key);
                node.Value = value;
                RemoveNode(node);
                AppendNode(node);
            }
            else
            {
                var node = new LRUNode(key, value);
                if (IsFull())
                {
                    RemoveKey(_end.Key);
                    RemoveNode(_end);
                    AppendNode(node);
                }
                else
                {
                    AppendNode(node);
                }

                AddKey(key, node);
            }
        }
Exemple #3
0
        private void AppendNode(LRUNode node)
        {
            node.Next     = _start;
            node.Previous = null;
            if (_start != null)
            {
                _start.Previous = node;
            }

            _start = node;

            if (_end == null)
            {
                _end = _start;
            }
        }
Exemple #4
0
        private void RemoveNode(LRUNode node)
        {
            if (node.Previous == null)
            {
                _start = node.Next;
            }
            else
            {
                node.Previous.Next = node.Next;
            }

            if (node.Next == null)
            {
                _end = node.Previous;
            }
            else
            {
                node.Next.Previous = node.Previous;
            }
        }