// 最近最少缓存算法(key,value)
class LRUCache
{
private:
// 1、list双向链表
std::list<std::pair< int, int> > _list;
// 2、使用unordered_map
// 由于需要快速定位链表的结点,故在map中使用value字段来存储链表的结点,这里是使用了迭代器。
std::unordered_map< int, std::list<std::pair<int, int>>::iterator > _map;
unsigned int _capacity;
public:
LRUCache(unsigned int capacity):_capacity(capacity)
{
}
// 获取
int get(int key);
// 设置
void set(int key, int value);
};
// 设置
void LRUCache::set(int key, int value)
{
// 1、查询是否在缓存中
auto iteramap = _map.find(key);
if(iteramap != _map.end()){
// 2、在缓存中,需要在链表中擦除。
_list.erase(iteramap->second);
// 3、把数据放到链表头
_list.push_front(std::pair<int, int>(key, value));
_map[key] = _list.begin();
}else{
if(_map.size() >= _capacity){
// 4、缓存已经满了
// 4.1 hash处要删除
_map.erase(_list.back().first);
// 4.2 链表也要删除尾巴部分
_list.pop_back();
}
// 5、双向链表首结点插入
_list.push_front(std::pair<int, int>(key, value));
// 6、在hash中增加
_map[key] = _list.begin();
}
}
// 获取:根据key,获取缓存的value
int LRUCache::get(int key)
{
// 1、先从hash中查找
auto iteramap = _map.find(key);
if(iteramap == _map.end()){
// 没找到,TODO
return -1;
}
// 2、如果在缓存中,需要把数据放到链表头部。
_list.push_front(std::pair<int, int>(key, iteramap->second->second));
_list.erase(iteramap->second);
// 3、hash原来存储的失效,需要重新设置
_map[key] = _list.begin();
// 4、返回value值
return iteramap->second->second;
}
参考:LRU
本文详细介绍了LRU(Least Recently Used)缓存淘汰算法的实现,使用C++编程语言,结合双向链表和unordered_map数据结构。当缓存满时,最近最少使用的数据会被淘汰。文章通过set和get操作展示了LRU缓存的工作流程,包括查找、更新和淘汰过程。

3460

被折叠的 条评论
为什么被折叠?



