예제 #1
0
        // pop the current tail.
        private DLinkedListNode popTail()
        {
            DLinkedListNode res = tail.previous;

            this.removeNode(res);
            return(res);
        }
예제 #2
0
        public void put(int key, int value)
        {
            DLinkedListNode node = cache[key];

            if (node == null)
            {
                DLinkedListNode newNode = new DLinkedListNode();
                newNode.key   = key;
                newNode.value = value;

                this.cache.Add(key, newNode);
                this.addNode(newNode);

                ++count;

                if (count > capacity)
                {
                    // pop the tail
                    DLinkedListNode tail = this.popTail();
                    this.cache.Remove(tail.key);
                    --count;
                }
            }
            else
            {
                // update the value.
                node.value = value;
                this.moveToHead(node);
            }
        }
예제 #3
0
        /**
         * Always add the new node right after head;
         */
        private void addNode(DLinkedListNode node)
        {
            node.previous = head;
            node.next     = head.next;

            head.next.previous = node;
            head.next          = node;
        }
예제 #4
0
        /**
         * Remove an existing node from the linked list.
         */
        private void removeNode(DLinkedListNode node)
        {
            DLinkedListNode previous = node.previous;
            DLinkedListNode next     = node.next;

            previous.next = next;
            next.previous = previous;
        }
예제 #5
0
        public int get(int key)
        {
            DLinkedListNode node = cache[key];

            if (node == null)
            {
                return(-1); // should raise exception here.
            }

            // move the accessed node to the head;
            this.moveToHead(node);

            return(node.value);
        }
예제 #6
0
        public LRUCache(int capacity)
        {
            this.count    = 0;
            this.capacity = capacity;

            head          = new DLinkedListNode();
            head.previous = null;

            tail      = new DLinkedListNode();
            tail.next = null;

            head.next     = tail;
            tail.previous = head;
        }
예제 #7
0
 /**
  * Move certain node in between to the head.
  */
 private void moveToHead(DLinkedListNode node)
 {
     this.removeNode(node);
     this.addNode(node);
 }