欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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ù)結構。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) 如果關鍵字 key 存在于緩存中,則返回關鍵字的值,否則返回 -1 。
  3. void put(int key, int value) 如果關鍵字已經存在,則變更其數(shù)據(jù)值;如果關鍵字不存在,則插入該組「關鍵字-值」。當緩存容量達到上限時,它應該在寫入新數(shù)據(jù)之前刪除最久未使用的數(shù)據(jù)值,從而為新的數(shù)據(jù)值留出空間。
  4. O(1) 時間復雜度內完成這兩種操作

所用數(shù)據(jù)結構:

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

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

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

所用數(shù)據(jù)結構:

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

  1. 使用哈希表 (STL unordered_map ) 儲存 “key” 到 “value frequency” 的關系映射 (使用 STL pair {value, frequency} 表示)
  2. 使用哈希表 (STL unordered_map ) 儲存 “frequency” 到 “對應所有的 key” 的關系映射 (key 使用雙向鏈表,即 STL list 存儲)
  3. 使用哈希表 (STL unordered_map ) 儲存 “key” 到 “2 中存儲 key 所用 list 中對應 iterator ” 的關系映射
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)

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

相關文章

  • 詳解C語言中結構體的使用

    詳解C語言中結構體的使用

    結構體是一些值的集合,這些值稱為成員變量,結構體的每個成員可以是不同類型的變量。本文將通過示例為大家詳細講講C語言中結構體的使用,需要的可以參考一下
    2022-07-07
  • 詳解如何利用C++實現(xiàn)一個反射類

    詳解如何利用C++實現(xiàn)一個反射類

    這篇文章主要為大家詳細介紹了如何利用C++實現(xiàn)一個反射類,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-03-03
  • fatal error LNK1104: 無法打開文件“l(fā)ibc.lib”的解決方法

    fatal error LNK1104: 無法打開文件“l(fā)ibc.lib”的解決方法

    本篇文章是對fatal error LNK1104: 無法打開文件“l(fā)ibc.lib”的解決方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言數(shù)據(jù)結構之順序表和單鏈表

    C語言數(shù)據(jù)結構之順序表和單鏈表

    在數(shù)據(jù)結構中,線性表是入門級數(shù)據(jù)結構,線性表又分為順序表和鏈表,這篇文章主要給大家介紹了關于C語言數(shù)據(jù)結構之順序表和單鏈表的相關資料,需要的朋友可以參考下
    2021-06-06
  • C語言實現(xiàn)加密解密功能

    C語言實現(xiàn)加密解密功能

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)加密解密功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C++學校運動會管理系統(tǒng)的實現(xiàn)

    C++學校運動會管理系統(tǒng)的實現(xiàn)

    這篇文章主要為大家詳細介紹了C++如何實現(xiàn)學校運動會管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • C語言結構體定義的方法匯總

    C語言結構體定義的方法匯總

    結構體是一種工具,用這個工具可以定義自己的數(shù)據(jù)類型。下面通過本文給大家分享了C語言結構體定義的方法匯總,需要的朋友參考下吧
    2017-12-12
  • c++中inline的用法分析

    c++中inline的用法分析

    本篇文章是對c++中inline的用法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C/C++實現(xiàn)7bit與8bit編碼互相轉換

    C/C++實現(xiàn)7bit與8bit編碼互相轉換

    這篇文章主要為大家詳細介紹了如何使用C/C++實現(xiàn)7bit與8bit編碼互相轉換功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-10-10
  • C語言文件讀寫操作介紹與簡單示例

    C語言文件讀寫操作介紹與簡單示例

    這篇文章主要給大家介紹了關于C語言文件讀寫操作的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01

最新評論