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);
        }
    }
  • 相关阅读:
    Rust 包管理器 Cargo 入门
    如何设置对企业批量的图文档加密?2021企业首选公司文档加密方案,宁波风奥金甲数据防泄漏
    跟坚哥学QUIC系列:加密和传输握手
    如何在SQLServer中处理每天四亿三千万记录的
    高德最佳实践:Serverless 规模化落地有哪些价值?
    浅谈 Pull Request 与 Change Request 研发协作模式
    Dbeaver连接国产数据库人大金仓
    我对云原生软件架构的观察与思考
    Java Web整合开发(20) -- Hibernate入门
    2 修改与恢复
  • 原文地址:https://www.cnblogs.com/23lalala/p/3506828.html
Copyright © 2011-2022 走看看