LRU演算法之hash+list實現

來源:互聯網
上載者:User

標籤:

LRU主要的耗時操作就是尋找,因此,我們可以使用hash_map來代替map,因此時間複雜度可以從O(logN)降低到O(1)

#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 ;}

原文連結:http://blog.csdn.net/doufei_ccst/article/details/23094987 

LRU演算法之hash+list實現(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.