mirror of
https://git.eden-emu.dev/eden-emu/eden.git
synced 2025-10-14 20:27:51 +00:00
WIP: DO-NOT-MERGE: NCE experiments: Initial LRU
This commit is contained in:
parent
27ceda2c6c
commit
3d43fecece
2 changed files with 71 additions and 1 deletions
57
src/core/arm/nce/lru_cache.h
Normal file
57
src/core/arm/nce/lru_cache.h
Normal file
|
@ -0,0 +1,57 @@
|
|||
#pragma once
|
||||
|
||||
#include <list>
|
||||
#include <unordered_map>
|
||||
|
||||
template<typename KeyType, typename ValueType>
|
||||
class LRUCache {
|
||||
public:
|
||||
explicit LRUCache(size_t capacity) : capacity(capacity) {}
|
||||
|
||||
ValueType* get(const KeyType& key) {
|
||||
auto it = cache_map.find(key);
|
||||
if (it == cache_map.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Move the accessed item to the front of the list (most recently used)
|
||||
cache_list.splice(cache_list.begin(), cache_list, it->second.first);
|
||||
return &(it->second.second);
|
||||
}
|
||||
|
||||
void put(const KeyType& key, const ValueType& value) {
|
||||
auto it = cache_map.find(key);
|
||||
|
||||
if (it != cache_map.end()) {
|
||||
// Key exists, update value and move to front
|
||||
it->second.second = value;
|
||||
cache_list.splice(cache_list.begin(), cache_list, it->second.first);
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove the least recently used item if cache is full
|
||||
if (cache_map.size() >= capacity) {
|
||||
auto last = cache_list.back();
|
||||
cache_map.erase(last);
|
||||
cache_list.pop_back();
|
||||
}
|
||||
|
||||
// Insert new item at the front
|
||||
cache_list.push_front(key);
|
||||
cache_map[key] = {cache_list.begin(), value};
|
||||
}
|
||||
|
||||
void clear() {
|
||||
cache_map.clear();
|
||||
cache_list.clear();
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return cache_map.size();
|
||||
}
|
||||
|
||||
private:
|
||||
size_t capacity;
|
||||
std::list<KeyType> cache_list;
|
||||
std::unordered_map<KeyType, std::pair<typename std::list<KeyType>::iterator, ValueType>> cache_map;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue