zoukankan      html  css  js  c++  java
  • LRU Cache

    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.

    import java.util.HashMap;
    
    public class LRUCache {
    
        private class CacheObject {
            public CacheObject pre;
            public CacheObject next;
            public int key;
            public int value;
            public CacheObject(int key,int value) {
                this.key = key;
                this.value = value;
                this.next = null;
                this.pre = null;
            }
        }
    
        public HashMap table = new HashMap ();
    
        public CacheObject head;
    
        public CacheObject tail;
    
        public int capacity;
    
        private boolean isTail(CacheObject co){
            if (tail==null) {
                return false;
            }
            return tail==co;
        }
    
        private void fixTail(CacheObject co) {
            if (tail==head){
                return;
            }
            if (isTail(co)){
                tail = co.pre;
                tail.next = null;
            }
        }
    
        private void move2head(CacheObject co){
            if (head==co){
                return;
            }
            if (tail==head){
                CacheObject oldHead = head;
                head = co;
                head.next = oldHead;
                head.pre = null;
                oldHead.pre = co;
                oldHead.next = null;
                return;
            }
            if (head != null){
                CacheObject pre = co.pre;
                CacheObject next = co.next;
                if (pre!=null && next !=null){
                    pre.next = next;
                    next.pre = pre;
                }
                head.pre = co;
                co.next = head;
                co.pre = null;
            }
            head = co;
        }
    
        public LRUCache(int capacity) {
            this.capacity = capacity;
        }
    
        public int get(int key) {
            if (table.containsKey(key)) {
                CacheObject added = (CacheObject)table.get(key);
                if (head!=added){
                    fixTail(added);
                    move2head(added);
                }
                return added.value;
            }
            return -1;
        }
    
    
        public void set(int key, int value) {
            if (table.containsKey(key)){
                CacheObject tmp = (CacheObject) table.get(key);
                fixTail(tmp);
                move2head(tmp);
                tmp.value = value;
                return;
            }
            CacheObject added = new CacheObject(key, value);
            if (this.capacity == table.size()) {
                table.remove(tail.key);
                fixTail(tail);
            }
            if (table.size()==0) {
                tail = added;
                head = added;
            } else {
                move2head(added);
            }
            table.put(key,added);
        }
    }
  • 相关阅读:
    详解单例模式
    Spring整合Mybatis案例,献给初学的朋友
    Java反射学习总结
    IDEA JSP项目构建及学习心得
    SQL Server数据库表锁定原理以及如何解除表的锁定
    MySQL锁定机制简介
    Cassandra简介
    Linux机器上实现程序自动部署以及更新
    服务器时间同步平台化
    内存查看平台化
  • 原文地址:https://www.cnblogs.com/23lalala/p/3506828.html
Copyright © 2011-2022 走看看