码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode LRU Cache

时间:2014-08-27 21:50:28      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   使用   io   for   ar   

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

 

public class LRUCache {
    
    private int cacheSize;
    private HashMap<Object, Entry> nodes;//缓存容器
    private int currentSize;
    private Entry first;//链表头
    private Entry last;//链表尾
    class Entry {
        Entry prev;//前一节点
        Entry next;//后一节点
        int value;//
        int key;//
    }
    public LRUCache(int capacity) {
        currentSize = 0;
        cacheSize = capacity;
        nodes = new HashMap<Object, Entry>(capacity);//缓存容器
    }
    
    /**
     * 获取缓存中对象,并把它放在最前面
     */
    public int get(int key) {
        Entry node = nodes.get(key);
        if (node != null) {
            moveToHead(node);
            return node.value;
        } else {
            return -1;
        }
    }
    
    /**
     * 添加 entry到hashtable, 并把entry 
     */
    public void set(int key, int value) {
        //先查看hashtable是否存在该entry, 如果存在,则只更新其value
        Entry node = nodes.get(key);
        
        if (node == null) {
            //缓存容器是否已经超过大小.
            if (currentSize >= cacheSize) {
                nodes.remove(last.key);
                removeLast();
    
            } else {
                currentSize++;
            }            
            node = new Entry();
        }
        node.value = value;
        node.key=key;
        //将最新使用的节点放到链表头,表示最新使用的.
        moveToHead(node);
        nodes.put(key, node);
    }

    /**
     * 将entry删除, 注意:删除操作只有在cache满了才会被执行
     */
    public void remove(Object key) {
        Entry node = nodes.get(key);
        //在链表中删除
        if (node != null) {
            if (node.prev != null) {
                node.prev.next = node.next;
            }
            if (node.next != null) {
                node.next.prev = node.prev;
            }
            if (last == node)
                last = node.prev;
            if (first == node)
                first = node.next;
        }
        //在hashtable中删除
        nodes.remove(key);
    }

    /**
     * 删除链表尾部节点,即使用最后 使用的entry
     */
    private void removeLast() {
        //链表尾不为空,则将链表尾指向null. 删除连表尾(删除最少使用的缓存对象)
        if (last != null) {
            if (last.prev != null)
                last.prev.next = null;
            else
                first = null;
            last = last.prev;
        }
    }
    
    /**
     * 移动到链表头,表示这个节点是最新使用过的
     */
    private void moveToHead(Entry node) {
        if (node == first)
            return;
        if (node.prev != null)
            node.prev.next = node.next;
        if (node.next != null)
            node.next.prev = node.prev;
        if (last == node)
            last = node.prev;
        if (first != null) {
            node.next = first;
            first.prev = node;
        }
        first = node;
        node.prev = null;
        if (last == null)
            last = first;
    }
    /*
     * 清空缓存
     */
    public void clear() {
        first = null;
        last = null;
        currentSize = 0;
    }

}

 

LeetCode LRU Cache

标签:des   style   blog   color   os   使用   io   for   ar   

原文地址:http://www.cnblogs.com/birdhack/p/3940340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!