LRU(Least Recently Used)最近最少使用算法
#include <unordered_map>
#include <deque>
#include <queue>
using namespace std;
struct Node
{
int key, val;
Node* prve, *next;
Node() : key(0), val(0), prve(nullptr), next(nullptr) {};
Node(int _key, int _val) : key(_key), val(_val), prve(nullptr), next(nullptr) {};
};
class LRUCache
{
public:
LRUCache(int _capacity) : capacity_(_capacity), current_size_(0)
{
head_ = new Node();
tail_ = new Node();
head_->next = tail_;
tail_->prve = head_;
}
~LRUCache();
void RemoveNode(Node* node)
{
node->prve->next = node->next;
node->next->prve = node->prve;
//delete node;
}
void AddNode(Node* node)
{
node->next = head_->next;
head_->next->prve = node;
node->prve = head_;
head_->next = node;
}
int GetVal(int key)
{
if (!date_.count(key)) return -1;
Node* node = date_[key];
RemoveNode(node);
AddNode(node);
return node->val;
}
void PutNode(int key, int val)
{
if (date_.count(key))
{
Node* node = date_[key];
node->val = val;
RemoveNode(node);
AddNode(node);
}
else
{
if (current_size_ == capacity_)
{
Node* remove_node = tail_->prve;
RemoveNode(remove_node);
date_.erase(remove_node->key);
current_size_--;
}
Node* node = new Node(key, val);
AddNode(node);
date_[key] = node;
current_size_++;
}
}
private:
Node* head_, * tail_;
unordered_map<int, Node*> date_;
int capacity_, current_size_;
};
#include <list>
#include <unordered_map>
#include <iostream>
using namespace std;
class LRUCache{
private:
int capacity;
std::list<std::pair<int, int>> cache_; // 双向链表,存储key值和value值
// 哈希表,存储key和链表的迭代器
std::unordered_map<int, std::list<std::pair<int, int>>::iterator> unmap_;
public:
LRUCache(int capacity) : capacity(capacity) {}
int Get(int key) {
if (unmap_.find(key) == unmap_.end()) {
return -1;
}
auto it = unmap_[key];
// 将链表中的值移动道量表的头部
cache_.splice(cache_.begin(), cache_, it);
// 更新哈希表中的value值
unmap_[key] = cache_.begin();
return it->second;
}
void Put(int key, int value) {
auto it = unmap_.find(key);
if (it != unmap_.end()) {
// 删除链表中的值,并插入新的值
cache_.erase(unmap_[key]);
cache_.emplace_front(std::make_pair(key, value));
unmap_[key] = cache_.begin();
} else {
if (cache_.size() == capacity) {
auto it = cache_.back();
cache_.pop_back();
unmap_.erase(it.first);
}
cache_.emplace_front(std::make_pair(key, value));
unmap_[key] = cache_.begin();
}
}
int GetFirst() {
if (cache_.empty()) {
return -1;
}
return cache_.front().second;
}
~LRUCache() {}
};
int main() {
LRUCache cache(2);
cache.Put(1, 1);
cout << "cache.get(1):" << cache.Get(1) << endl;
cout << cache.GetFirst() << endl;
cout << "---------------------------" << endl;
cache.Put(2, 2);
cout << "cache.get(2):" << cache.Get(2) << endl;
cout << cache.GetFirst() << endl;
cout << "---------------------------" << endl;
cache.Put(3, 3);
cout << "cache.get(3):" << cache.Get(1) << endl;
cout << cache.GetFirst() << endl;
cout << "---------------------------" << endl;
cache.Put(4, 4);
cout << "cache.get(2):" << cache.Get(2) << endl;
cout << "cache.get(3):" << cache.Get(3) << endl;
cout << "cache.get(4):" << cache.Get(4) << endl;
cout << cache.GetFirst() << endl;
return 0;
}