Example #1
0
        private void PlaceTail(CustomLRUNode node)
        {
            node.Prev = _cacheTail.Prev;
            node.Next = _cacheTail;

            node.Prev.Next = node;
            node.Next.Prev = node;
        }
Example #2
0
        public CustomLRUCache(int capacity)
        {
            _cache = new Dictionary <int, CustomLRUNode>();

            _cacheHead      = new CustomLRUNode(-1, -1);
            _cacheTail      = new CustomLRUNode(-1, -1);
            _cacheHead.Next = _cacheTail;
            _cacheTail.Prev = _cacheHead;

            _capacity = capacity;
        }
Example #3
0
        public void Put(int key, int value)
        {
            if (_cache.ContainsKey(key))
            {
                MoveToTail(key);
                _cache[key].Data = value;
            }
            else
            {
                var newNode = new CustomLRUNode(key, value);
                PlaceTail(newNode);
                _cache[key] = newNode;
            }

            if (_cache.Count > _capacity)
            {
                _cache.Remove(_cacheHead.Next.Key);
                Remove(_cacheHead.Next);
            }
        }
Example #4
0
 private void Remove(CustomLRUNode node)
 {
     node.Prev.Next = node.Next;
     node.Next.Prev = node.Prev;
 }