www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當前位置:首頁 > 芯聞號 > 充電吧
[導讀]原理LeetCode上有著樣一道題目:Design and implement a data structure for Least Recently Used (LRU) cache. It sho

原理

LeetCode上有著樣一道題目:
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
首先我覺得這個題目很有意思,能夠自己實現(xiàn)一個操作系統(tǒng)中的算法本身就是比較有意思的事情,同時又能夠復習一下操作系統(tǒng)的知識,何樂而不為呢。
LRU(Least recently used,最近最少使用)算法根據(jù)數(shù)據(jù)的歷史訪問記錄來進行淘汰數(shù)據(jù),其核心思想是“如果數(shù)據(jù)最近被訪問過,那么將來被訪問的幾率也更高”。
1.LRU Cache簡單版

最常見的實現(xiàn)是使用一個鏈表保存緩存數(shù)據(jù),如下圖所示:


1)新數(shù)據(jù)插入到鏈表頭部;
2)每當緩存命中(即緩存數(shù)據(jù)被訪問),則將數(shù)據(jù)移到鏈表頭部;
3)當鏈表滿的時候,將鏈表尾部的數(shù)據(jù)丟棄。
【命中率】
當存在熱點數(shù)據(jù)時,LRU的效率很好,但偶發(fā)性的、周期性的批量操作會導致LRU命中率急劇下降,緩存污染情況比較嚴重。
【復雜度】
實現(xiàn)簡單。
【代價】
命中時需要遍歷鏈表,找到命中的數(shù)據(jù)塊索引,然后需要將數(shù)據(jù)移到頭部。

2.LRU Cache復雜版—LRU-K

LRU-K中的K代表最近使用的次數(shù),因此LRU可以認為是LRU-1。LRU-K的主要目的是為了解決LRU算法“緩存污染”的問題,其核心思想是將“最近使用過1次”的判斷標準擴展為“最近使用過K次”。
相比LRU,LRU-K需要多維護一個隊列,用于記錄所有緩存數(shù)據(jù)被訪問的歷史。只有當數(shù)據(jù)的訪問次數(shù)達到K次的時候,才將數(shù)據(jù)放入緩存。當需要淘汰數(shù)據(jù)時,LRU-K會淘汰第K次訪問時間距當前時間最大的數(shù)據(jù)。如下圖所示:


1)數(shù)據(jù)第一次被訪問,加入到訪問歷史列表;
2)如果數(shù)據(jù)在訪問歷史列表里后沒有達到K次訪問,則按照一定規(guī)則(FIFO,LRU)淘汰;
3)當訪問歷史隊列中的數(shù)據(jù)訪問次數(shù)達到K次后,將數(shù)據(jù)索引從歷史隊列刪除,將數(shù)據(jù)移到緩存隊列中,并緩存此數(shù)據(jù),緩存隊列重新按照時間排序;
4)緩存數(shù)據(jù)隊列中被再次訪問后,重新排序;
5)需要淘汰數(shù)據(jù)時,淘汰緩存隊列中排在末尾的數(shù)據(jù),即:淘汰“倒數(shù)第K次訪問離現(xiàn)在最久”的數(shù)據(jù)。
實現(xiàn)

OK,針對本題目,我們給出一段簡單的實現(xiàn)代碼,在下面的代碼中我們使用map+list的方式來實現(xiàn),其復雜度是O(logN)。其實,也就是使用紅黑樹+雙向鏈表的方式來實現(xiàn),map相當于一個紅黑樹,用來負責查找一塊Cashe是否已經(jīng)在內(nèi)存中,而list相當于一個雙向鏈表,能夠方便地插入和刪除某個元素。我們可以發(fā)現(xiàn),map的查找效率是O(logN),list插入刪除的效率是O(1),因此總體的復雜度是O(logN)。好了下面上代碼:

#include#include#include#includeusing?namespace?std;??
??
class?LRUCache{??
public:??
????LRUCache(int?capacity)?{??
????????m_capacity?=?capacity?;??
????}??
??
????int?get(int?key)?{??
????????int?retValue?=?-1?;??
????????map<int,?list<pair>?::?iterator>?::iterator?it?=?cachesMap.find(key)?;??
??
?????????//如果在Cashe中,將記錄移動到鏈表的最前端??
????????if?(it?!=?cachesMap.end())??
????????{??
????????????retValue?=?it?->second->second?;??
????????????//移動到最前端??
????????????list<pair>?::?iterator?ptrPair?=?it?->?second?;??
????????????pairtmpPair?=?*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>?::?iterator>?::iterator?it?=?cachesMap.find(key)?;??
??
????????if?(it?!=?cachesMap.end())?//已經(jīng)存在其中??
????????{??
?????????????list<pair>?::?iterator?ptrPait?=?it?->second?;??
????????????ptrPait->second?=?value?;??
????????????//移動到最前面??
????????????pairtmpPair?=?*ptrPait?;??
????????????caches.erase(ptrPait)?;??
????????????caches.push_front(tmpPair)?;??
??
????????????//更新map??
????????????cachesMap[key]?=?caches.begin()?;??
????????}??
????????else?//不存在其中??
????????{??
????????????pairtmpPair?=?make_pair(key,?value)?;??
??
????????????if?(m_capacity?==?caches.size())?//已經(jīng)滿??
????????????{??
????????????????int?delKey?=?caches.back().first?;??
????????????????caches.pop_back()?;?//刪除最后一個??
??
????????????????//刪除在map中的相應項??
????????????????map<int,?list<pair>?::?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>?caches?;??????????????????????????????????????????//用一個雙鏈表存儲cashe的內(nèi)容??
????map<?int,?list<pair>?::?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?;??
}

其實,我們可以發(fā)現(xiàn),主要的耗時操作就是查找,因此,我們可以使用hash_map來代替map,因此時間復雜度可以降低到O(1)。



#include#include#include#includeusing?namespace?std;??
using?namespace?stdext;??
??
class?LRUCache{??
public:??
????LRUCache(int?capacity)?{??
????????m_capacity?=?capacity?;??
????}??
??
??
????int?get(int?key)?{??
????????int?retValue?=?-1?;??
????????hash_map<int,?list<pair>?::?iterator>?::iterator?it?=?cachesMap.find(key)?;??
??
??
????????//如果在Cashe中,將記錄移動到鏈表的最前端??
????????if?(it?!=?cachesMap.end())??
????????{??
????????????retValue?=?it?->second->second?;??
????????????//移動到最前端??
????????????list<pair>?::?iterator?ptrPair?=?it?->?second?;??
????????????pairtmpPair?=?*ptrPair?;??
????????????caches.erase(ptrPair)?;??
????????????caches.push_front(tmpPair)?;??
??
??
????????????//修改map中的值??
????????????cachesMap[key]?=?caches.begin()?;??
????????}??
????????return?retValue?;??
????}??
??
??
????void?set(int?key,?int?value)?{??
??
??
????????hash_map<int,?list<pair>?::?iterator>?::iterator?it?=?cachesMap.find(key)?;??
??
??
????????if?(it?!=?cachesMap.end())?//已經(jīng)存在其中??
????????{??
????????????list<pair>?::?iterator?ptrPait?=?it?->second?;??
????????????ptrPait->second?=?value?;??
????????????//移動到最前面??
????????????pairtmpPair?=?*ptrPait?;??
????????????caches.erase(ptrPait)?;??
????????????caches.push_front(tmpPair)?;??
??
??
????????????//更新map??
????????????cachesMap[key]?=?caches.begin()?;??
????????}??
????????else?//不存在其中??
????????{??
????????????pairtmpPair?=?make_pair(key,?value)?;??
??
??
????????????if?(m_capacity?==?caches.size())?//已經(jīng)滿??
????????????{??
????????????????int?delKey?=?caches.back().first?;??
????????????????caches.pop_back()?;?//刪除最后一個??
??
??
????????????????//刪除在map中的相應項??
????????????????hash_map<int,?list<pair>?::?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>?caches?;??????????????????????????????????????????//用一個雙鏈表存儲cashe的內(nèi)容??
????hash_map<?int,?list<pair>?::?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?;??
}


本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫毥谦F公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務中斷的風險,如企業(yè)系統(tǒng)復雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務引領增長 以科技創(chuàng)新為引領,提升企業(yè)核心競爭力 堅持高質(zhì)量發(fā)展策略,塑強核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉