由LeetCode的LRU Cache谈到操作系统中LRU算法

 1 class LRUCache{
 2 public:
 3     LRUCache(int capacity) {
 4         size = capacity;
 5     }
 6     int get(int key) {
 7         if(cacheMap.find(key)==cacheMap.end())
 8             return -1;
 9         cacheList.splice(cacheList.begin(),cacheList,cacheMap[key]);
10         cacheMap[key] = cacheList.begin();
11         return cacheMap[key]->second;
12     }
13     void set(int key, int value) {
14         if(cacheMap.find(key)!=cacheMap.end()){
15             cacheList.splice(cacheList.begin(),cacheList,cacheMap[key]);
16             cacheMap[key] = cacheList.begin();
17             cacheMap[key]->second = value;
18         }
19         else{
20             if(cacheList.size()==size){
21                 cacheMap.erase(cacheList.back().first);
22                 cacheList.pop_back();
23             }
24             cacheList.insert(cacheList.begin(),make_pair(key,value));
25             cacheMap[key] = cacheList.begin();
26         }
27     }
28 private:
29     int size;
30     list<pair<int,int>> cacheList;
31     map<int,list<pair<int,int>>::iterator> cacheMap;
32 };
举例
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
    1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
  0 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0
7 7 7 0 1 2 2 3 0 4 2 2 0 3 3 1 2 0 1 7

举例说明:第一行从左往右是要访问的页号,size值为3,每一列表示访问当前页后,cacheList中存在的页号以及各自的优先级别(从上往下默认为list从头至尾,按照LRU规则,头是最近访问的,尾是优先级别最低的,也就是最有可能会被置换),背景黄色说明产生了缺页中断。每一次访问当前页,size满时,当前页面若存在,虽然不产生页面置换,但是页面的优先级也会发生改变;若不存在,尾巴处的页面被置换,当前页面被插入list头。

ps:以前学LRU算法时,只要搞懂手算就可以(如上述举例图表),要想真正理解LRU算法,最好会写代码,考虑到用哪些数据结构来保存页号,使得增加,删除,替换改变页号的时间效率最好。下面是LeetCode中 LRU Cache的链接

https://oj.leetcode.com/problems/lru-cache/

时间: 2024-10-13 11:51:13

由LeetCode的LRU Cache谈到操作系统中LRU算法的相关文章

[leetcode]LRU Cache @ Python

原题地址:http://oj.leetcode.com/problems/lru-cache/ 题意:设计LRU Cache 参考文献:http://blog.csdn.net/hexinuaa/article/details/6630384 这篇博文总结的很到位.   https://github.com/Linzertorte/LeetCode-in-Python/blob/master/LRUCache.py 代码参考的github人写的,思路非常清晰,写的也很好. Cache简介: Ca

[LeetCode]LRU Cache, 解题报告

题目 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.

LeetCode:LRU cache

题目:LRU cache 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 r

【LeetCode 题解】 LRU Cache

设计并实现最近最久未使用(Least Recently Used)缓存. 链接:https://oj.leetcode.com/problems/lru-cache/ 题目描述: 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 (wi

[leetcode]LRU Cache (python)

LRU:最近最久未使用,为了得到这个最新最久的信息,需要一种策略来进行记录,如果加入类似时间戳式的字段,那么每次删除的时候,就必须通过遍历才能得到时间信息,或者对时间戳进行排序,但是无论哪种,都是需要额外的维护,维护成本都比较高. 广泛使用的策略是底层用双端队列来进行维护,双端使得在插入删除时操作更简单.而单单使用双端队列似乎还是不够,比如在get 时,还是需要顺序查找给定的key参数的,所以为了能在O(1) 时间获得key 需要类hash的结构,在python里,就用字典. 接下来的事情是,我

Java for LeetCode 146 LRU Cache 【HARD】

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

LeetCode 之 LRU Cache Java实现

LeetCode刷了41道题了,流程是按照戴兄的小书,很多不会的是参考Kim姐的代码,自己用Java抠腚的. 前几天做到了LRU Cache: C++的实现方法大同小异,大都用的是一个list加一个hash,hash中存储list节点地址,每次get从hash中寻key,有则将list相应节点放到链表头,没有则返回-1:每次set先判断hash中是否存在,存在则将相应节点移到表头,重置value值,如果不存在,判定长度是否达到预设的capacity,如果达到,删除表尾节点,新节点插入表头. 但是

LeetCode: LRU Cache [146]

[题目] 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

LeetCode解题报告:LRU Cache

LRU Cache 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 retu