zoukankan      html  css  js  c++  java
  • LRU Cache

            hash+双向链表

    #include <iostream>
    #include <map>
    #include <list>
    #include <utility>
    using namespace std;
    
    class LRUCache{
    public:
        LRUCache(int capacity) {
            m_capacity = capacity ;
        }
    
        int get(int key) {
            int retValue = -1 ;
            map<int, list<pair<int, int> > :: iterator> ::iterator it = cachesMap.find(key) ;
    
             //如果在Cashe中,将记录移动到链表的最前端
            if (it != cachesMap.end())
            {
                retValue = it ->second->second ;
                //移动到最前端
                list<pair<int, int> > :: iterator ptrPair = it -> second ;
                pair<int, int> tmpPair = *ptrPair ;
                caches.erase(ptrPair) ;
                caches.push_front(tmpPair) ;
    
                //修改map中的值
                cachesMap[key] = caches.begin() ;
            }
            return retValue ;
    
            
        }
    
        void set(int key, int value) {
    
            map<int, list<pair<int, int> > :: iterator> ::iterator it = cachesMap.find(key) ;
    
            if (it != cachesMap.end()) //已经存在其中
            {
                 list<pair<int, int> > :: iterator ptrPait = it ->second ;
                ptrPait->second = value ;
                //移动到最前面
                pair<int , int > tmpPair = *ptrPait ;
                caches.erase(ptrPait) ;
                caches.push_front(tmpPair) ;
    
                //更新map
                cachesMap[key] = caches.begin() ;
            }
            else //不存在其中
            {
                pair<int , int > tmpPair = make_pair(key, value) ;
    
                if (m_capacity == caches.size()) //已经满
                {
                    int delKey = caches.back().first ;
                    caches.pop_back() ; //删除最后一个
    
                    //删除在map中的相应项
                    map<int, list<pair<int, int> > :: iterator> ::iterator delIt = cachesMap.find(delKey) ;
                    cachesMap.erase(delIt) ;
                }
    
                caches.push_front(tmpPair) ;
                cachesMap[key] = caches.begin() ; //更新map
            }
        }
    
    private:
        int m_capacity ;                                                                        //cashe的大小
        list<pair<int, int> > caches ;                                                    //用一个双链表存储cashe的内容
        map< int, list<pair<int, int> > :: iterator> cachesMap ;            //使用map加快查找的速度
    };
    
    int main(int argc, char **argv)
    {
        LRUCache s(2) ;
        s.set(2, 1) ;
        s.set(1, 1) ;
        cout << s.get(2) << endl;
        s.set(4, 1) ;
        cout << s.get(1) << endl;
        cout << s.get(2) << endl;
        return 0 ;
    }
  • 相关阅读:
    【简●解】[AHOI2009]中国象棋
    【讲●解】KMP算法
    【简●解】POJ 1185,LG P2704【炮兵阵地】
    学习网站整理
    【讲●解】火车进出栈类问题 & 卡特兰数应用
    洛谷4556 [Vani有约会]雨天的尾巴
    BZOJ2212或洛谷3521 [POI2011]ROT-Tree Rotations
    洛谷1119 灾后重建
    洛谷1462(重题1951) 通往奥格瑞玛的道路(收费站_NOI导刊2009提高(2))
    BZOJ2721或洛谷1445 [Violet]樱花
  • 原文地址:https://www.cnblogs.com/theCambrian/p/3950965.html
Copyright © 2011-2022 走看看