LRU算法

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值