最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++ 實現(xiàn)LRU 與 LFU 的緩存算法

 更新時間:2021年09月13日 11:11:31   作者:Ito Schum  
設計和實現(xiàn)一個LRU 緩存機制。其支持獲取數(shù)據(jù) get 和 寫入數(shù)據(jù) put,設計并實現(xiàn)最少訪問頻率(LFU)緩存的數(shù)據(jù)結(jié)構(gòu)。LFU的每個數(shù)據(jù)塊都有一個引用計數(shù),所有數(shù)據(jù)塊按照引用計數(shù)排序,具有相同引用計數(shù)的數(shù)據(jù)塊則按照時間進行排序。其支持get 和 put,具體了解請看下文

一、LRU (Least Recently Used) 緩存

詳見 LeetCode Q146

https:// leetcode.com/problems/l ru-cache/

https:// leetcode-cn.com/problem s/lru-cache/

問題描述:

  1. LRUCache(int capacity) 以正整數(shù)作為容量 capacity 初始化 LRU 緩存
  2. int get(int key) 如果關(guān)鍵字 key 存在于緩存中,則返回關(guān)鍵字的值,否則返回 -1 。
  3. void put(int key, int value) 如果關(guān)鍵字已經(jīng)存在,則變更其數(shù)據(jù)值;如果關(guān)鍵字不存在,則插入該組「關(guān)鍵字-值」。當緩存容量達到上限時,它應該在寫入新數(shù)據(jù)之前刪除最久未使用的數(shù)據(jù)值,從而為新的數(shù)據(jù)值留出空間。
  4. O(1) 時間復雜度內(nèi)完成這兩種操作

所用數(shù)據(jù)結(jié)構(gòu):

為了使 get put 操作的平均時間復雜度為 O(1)

使用雙向鏈表 (STL list ) 儲存緩存內(nèi)容 (使用 STL pair {key, value} 表示),
使用哈希表 (STL unordered_map ) 儲存 “key” 到 “pair iterator ” 的關(guān)系映射

typedef std::unordered_map<int, std::list<std::pair<int, int> >::iterator > CacheMap;
typedef std::list<std::pair<int, int> > LRUList;

流程圖:

  • get function

  • put function

代碼實現(xiàn):

#include <iostream>
#include <list>
#include <unordered_map>

typedef std::unordered_map<int, std::list<std::pair<int, int> >::iterator > CacheMap;
typedef std::list<std::pair<int, int> > LRUList;

class LRUCache {
public:
    LRUCache(int capacity) {
        _capacity = capacity;
    }

    int get(int key) {
        CacheMap::iterator cache_itr = _cacheMap.find(key);
        if (cache_itr == _cacheMap.end() ) { 
            return -1; 
        }
        makeMostRecent(key, _cacheMap[key]->second);
        LRUList::iterator list_itr = _LRUList.end();
        --list_itr;
        return list_itr->second;
    }

    void put(int key, int value) {
        if (_cacheMap.find(key) != _cacheMap.end()) {
            makeMostRecent(key, value);
            return;
        }
        if (_LRUList.size() >= _capacity) {
            removeLeastRecentTask(key);
        }
        addMostRecentTask(key, value);
    }

private:
    void makeMostRecent(int key, int value) {
        _LRUList.erase(_cacheMap[key]);
        _LRUList.push_back(std::make_pair(key, value) );
        LRUList::iterator list_itr = _LRUList.end();
        _cacheMap[key] = --list_itr;
    }

    void removeLeastRecentTask(int key) {
        int keyToRemove = _LRUList.begin()->first;
        _LRUList.erase(_LRUList.begin());
        _cacheMap.erase(keyToRemove);
    }

    void addMostRecentTask(int key, int value) {
        _LRUList.push_back(std::make_pair(key, value) );
        LRUList::iterator list_itr = _LRUList.end();
        _cacheMap[key] = --list_itr;
    }

    int _capacity;
    LRUList _LRUList;
    CacheMap _cacheMap;
};

// n = item number of the LRU list, aka capacity
// Time: O(1)
// Space: O(n)

運行測試:

Accepted
22/22 cases passed (412 ms)
Your runtime beats 69.45 % of cpp submissions
Your memory usage beats 48.08 % of cpp submissions (174 MB)

二、LFU (Least Frequently Used) 緩存

詳見 LeetCode Q460

https:// leetcode.com/problems/l fu-cache/

https:// leetcode-cn.com/problem s/lru-cache/

問題描述:

  1. LFUCache(int capacity) - 用數(shù)據(jù)結(jié)構(gòu)的容量 capacity 初始化對象
  2. int get(int key) - 如果鍵存在于緩存中,則獲取鍵的值,否則返回 -1 。
  3. void put(int key, int value) - 如果鍵已存在,則變更其值;如果鍵不存在,請插入鍵值對。當緩存達到其容量時,則應該在插入新項之前,使最不經(jīng)常使用的項無效。在此問題中,當存在平局(即兩個或更多個鍵具有相同使用頻率)時,應該去除 最近最久未使用的鍵。
  4. 「項的使用次數(shù)」就是自插入該項以來對其調(diào)用 get 和 put 函數(shù)的次數(shù)之和。使用次數(shù)會在對應項被移除后置為 0 。
  5. 為了確定最不常使用的鍵,可以為緩存中的每個鍵維護一個 使用計數(shù)器 。使用計數(shù)最小的鍵是最久未使用的鍵。
  6. 當一個鍵首次插入到緩存中時,它的使用計數(shù)器被設置為 1 (由于 put 操作)。對緩存中的鍵執(zhí)行 get 或 put 操作,使用計數(shù)器的值將會遞增。

所用數(shù)據(jù)結(jié)構(gòu):

為了使 get put 操作的平均時間復雜度為 O(1) ,

  1. 使用哈希表 (STL unordered_map ) 儲存 “key” 到 “value frequency” 的關(guān)系映射 (使用 STL pair {value, frequency} 表示)
  2. 使用哈希表 (STL unordered_map ) 儲存 “frequency” 到 “對應所有的 key” 的關(guān)系映射 (key 使用雙向鏈表,即 STL list 存儲)
  3. 使用哈希表 (STL unordered_map ) 儲存 “key” 到 “2 中存儲 key 所用 list 中對應 iterator ” 的關(guān)系映射
std::unordered_map<int, std::pair<int, int> > _keyToValFreq;
std::unordered_map<int, std::list<int> > _freqToKeyList;
std::unordered_map<int, std::list<int>::iterator> _keyToKeyListItr;


流程圖:

  • get function

  • put function

代碼實現(xiàn):

#include <iostream>
#include <list>
#include <unordered_map>

class LFUCache {
public:
    LFUCache(int capacity) {
        _capacity = capacity;
    }

    int get(int key) {
        // If key doesn't exist
        if (_keyToValFreq.find(key) == _keyToValFreq.end() ) {
            return -1;
        }
        // if key exists, increse frequency and reorder
        increaseFreq(key);
        return _keyToValFreq[key].first;
    }

    void put(int key, int value) {
        if (_capacity <= 0) { return; }
        // if key exists
        if (_keyToValFreq.find(key) != _keyToValFreq.end() ) {
            _keyToValFreq[key].first = value;
            increaseFreq(key);
            return;
        }
        // if key doesn't exist
        // if reached hashmap's max capacity, remove the LFU (LRU if tie)
        if (_keyToValFreq.size() >= _capacity) {
            int keyToRmove = _freqToKeyList[_minFreq].back();
            _freqToKeyList[_minFreq].pop_back();
            _keyToKeyListItr.erase(keyToRmove);
            _keyToValFreq.erase(keyToRmove);
        }
        // Then add new item with frequency = 1
        addNewTask(key, value);
    }

    void increaseFreq(int key) {
        // Update the freq in the pair
        int oldFreq = _keyToValFreq[key].second++;

        // Detele the old freq by itr
        _freqToKeyList[oldFreq].erase(_keyToKeyListItr[key]);
        // Add the new freq and re-assign the itr
        _freqToKeyList[oldFreq + 1].emplace_front(key);
        _keyToKeyListItr[key] = _freqToKeyList[oldFreq + 1].begin();

        // Update minFreq
        if (_freqToKeyList[_minFreq].empty() ) {
            _minFreq = oldFreq + 1;
        }
    }

    void addNewTask(int key, int value) {
        // Add new key-value/freq to all hashmaps
        _minFreq = 1;
        _keyToValFreq[key] = std::make_pair(value, _minFreq);
        _freqToKeyList[_minFreq].emplace_front(key);
        _keyToKeyListItr[key] = _freqToKeyList[_minFreq].begin();
    }

private:
    int _capacity;
    int _minFreq;
    std::unordered_map<int, std::pair<int, int> > _keyToValFreq;
    std::unordered_map<int, std::list<int> > _freqToKeyList;
    std::unordered_map<int, std::list<int>::iterator> _keyToKeyListItr;
};

// n = item number of the LFU, aka capacity
// Time: O(1)
// Space: O(n)

運行測試:

Accepted
24/24 cases passed (464 ms)
Your runtime beats 72.37 % of cpp submissions
Your memory usage beats 45.99 % of cpp submissions (186.7 MB)

到此這篇關(guān)于C++ 實現(xiàn)LRU 與 LFU 的緩存算法的文章就介紹到這了,更多相關(guān)C++ 實現(xiàn)LRU 與 LFU 緩存算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C/C++?pthread線程庫使用示例詳解

    C/C++?pthread線程庫使用示例詳解

    這篇文章主要介紹了C/C++?pthread線程庫使用示例詳解,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • C語言實現(xiàn)井字棋(三子棋)

    C語言實現(xiàn)井字棋(三子棋)

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)井字棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • VisualStudio2022編寫C語言的實現(xiàn)步驟

    VisualStudio2022編寫C語言的實現(xiàn)步驟

    VisualStudio2022是一款強大的集成開發(fā)環(huán)境,可以用來編寫C語言程序,本文主要介紹了VisualStudio2022編寫C語言的實現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • C語言二叉樹的三種遍歷方式的實現(xiàn)及原理

    C語言二叉樹的三種遍歷方式的實現(xiàn)及原理

    這篇文章主要介紹了C語言二叉樹的三種遍歷方式的實現(xiàn)及原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • 如何通過指針突破C++類的訪問權(quán)限

    如何通過指針突破C++類的訪問權(quán)限

    這篇文章主要介紹了通過指針突破C++類的訪問權(quán)限,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • C++存儲持續(xù)性生命周期原理解析

    C++存儲持續(xù)性生命周期原理解析

    這篇文章主要為大家介紹了C++存儲持續(xù)性生命周期原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • 詳細解析命令行的getopt_long()函數(shù)

    詳細解析命令行的getopt_long()函數(shù)

    getopt_long支持長選項的命令行解析,函數(shù)中的參數(shù)argc和argv通常直接從main()的兩個參數(shù)傳遞而來
    2013-09-09
  • 在c和c++中實現(xiàn)函數(shù)回調(diào)

    在c和c++中實現(xiàn)函數(shù)回調(diào)

    如何在c和c++中實現(xiàn)函數(shù)回調(diào)呢?現(xiàn)在小編就和大家分享一下在c/c++中實現(xiàn)函數(shù)回調(diào)的示例代碼,需要的朋友可以參考下
    2013-07-07
  • C++實現(xiàn)日期類(Date類)的方法

    C++實現(xiàn)日期類(Date類)的方法

    下面小編就為大家?guī)硪黄狢++實現(xiàn)日期類(Date類)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • C++的靜態(tài)聯(lián)編和動態(tài)聯(lián)編詳解

    C++的靜態(tài)聯(lián)編和動態(tài)聯(lián)編詳解

    這篇文章主要介紹了C++的靜態(tài)聯(lián)編和動態(tài)聯(lián)編詳解,對于深入理解C++編譯運行原理有很大幫助,需要的朋友可以參考下
    2014-07-07

最新評論

临江市| 辽源市| 旅游| 安仁县| 哈尔滨市| 文成县| 航空| 崇州市| 临沂市| 阆中市| 应城市| 六枝特区| 达州市| 侯马市| 山东省| 石首市| 黄梅县| 宁远县| 望都县| 中宁县| 宣城市| 东至县| 江达县| 泌阳县| 泉州市| 库尔勒市| 始兴县| 沙湾县| 门头沟区| 成武县| 阳城县| 英德市| 邢台县| 张掖市| 达日县| 柘城县| 赫章县| 临高县| 灯塔市| 阿瓦提县| 武汉市|