zoukankan      html  css  js  c++  java
  • LRU缓存机制

    运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。

    获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
    写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。

    思路,用了哈希表存储,以及双向链表结构。

    class LRUCache {
    public:
        unordered_map<int, list<pair<int,int> >::iterator > map;
        list<pair<int, int> > cache; //双向链表
        int cap;
        
    public:
        
        LRUCache(int capacity) {
            this->cap = capacity;
        }
        
        int get(int key) {
            unordered_map<int, list<pair<int,int> >::iterator>::iterator find;
            find = map.find(key);
            if(find == map.end()){
                return -1;
            } 
            pair<int,int> tmp = *map[key];
            cache.erase(map[key]);
            cache.push_front(tmp);
            map[key] = cache.begin();
            
            //cache.erase(find->second);
            //cache.push_front(make_pair(key, find->second->second));
    //        pair<int, int> tmp(key, find->second->second);
            //map.insert(make_pair(key,cache.begin()));
            return map[key]->second;        
        }
        
        void put(int key, int value) {
            unordered_map<int, list<pair<int,int> >::iterator>::iterator find;
            find = map.find(key);
            //先判断是否有键,如果有,则直接改变对应的值,并换到表头
            if(find != map.end()){
                //map[]
                find->second->second = value;                        
                pair<int,int> tmp = *map[key];
                cache.erase(map[key]);
                cache.push_front(tmp);
                map[key] = cache.begin();
            } else {
            //如果没有键值,则判断是否满,满则在链表删除最后一个值,删除对应哈希表的键。
                if(cache.size() == cap){
                    pair<int, int> tmp = cache.back();                
                    find = map.find(tmp.first);
                   // pair<int, int> tmp = *map[tmp.first];
                    cache.pop_back();                
                    map.erase(tmp.first);
                    //map.insert(make_pair(key, cache.begin()));
                } 
                //直接添加键值。
                cache.push_front(make_pair(key, value));
                //pair<int, int> tmp(key, value);
                map.insert(make_pair(key, cache.begin()));
                                       
            }        
        }
    };
    
    /**
     * Your LRUCache object will be instantiated and called as such:
     * LRUCache* obj = new LRUCache(capacity);
     * int param_1 = obj->get(key);
     * obj->put(key,value);
     */
    The Safest Way to Get what you Want is to Try and Deserve What you Want.
  • 相关阅读:
    Java中一对多映射关系
    Java中多对多映射关系
    java中的匿名内部类总结
    (自己转)比较ArrayList、LinkedList、Vector
    Statement 接口的应用(存在sql语句的注入风险)
    创建Jutil (单元测试)
    自己(转)抽象类和接口联系与区别
    SQL查询语句
    数据库基础查询语句中的几个细节
    数据库查询语句
  • 原文地址:https://www.cnblogs.com/Shinered/p/11430998.html
Copyright © 2011-2022 走看看