yuzu-fork/src/common/lru_cache.h

140 lines
3.4 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2021-08-15 14:35:53 +01:00
#pragma once
#include <deque>
#include <memory>
#include <type_traits>
#include "common/common_types.h"
namespace Common {
template <class Traits>
class LeastRecentlyUsedCache {
using ObjectType = typename Traits::ObjectType;
using TickType = typename Traits::TickType;
struct Item {
ObjectType obj;
TickType tick;
Item* next{};
Item* prev{};
};
public:
LeastRecentlyUsedCache() : first_item{}, last_item{} {}
~LeastRecentlyUsedCache() = default;
size_t Insert(ObjectType obj, TickType tick) {
2021-08-29 17:19:53 +01:00
const auto new_id = Build();
2021-08-15 14:35:53 +01:00
auto& item = item_pool[new_id];
item.obj = obj;
item.tick = tick;
2021-08-29 17:19:53 +01:00
Attach(item);
2021-08-15 14:35:53 +01:00
return new_id;
}
void Touch(size_t id, TickType tick) {
auto& item = item_pool[id];
if (item.tick >= tick) {
return;
}
item.tick = tick;
if (&item == last_item) {
return;
}
2021-08-29 17:19:53 +01:00
Detach(item);
Attach(item);
2021-08-15 14:35:53 +01:00
}
void Free(size_t id) {
auto& item = item_pool[id];
2021-08-29 17:19:53 +01:00
Detach(item);
2021-08-15 14:35:53 +01:00
item.prev = nullptr;
item.next = nullptr;
free_items.push_back(id);
}
template <typename Func>
void ForEachItemBelow(TickType tick, Func&& func) {
static constexpr bool RETURNS_BOOL =
std::is_same_v<std::invoke_result<Func, ObjectType>, bool>;
Item* iterator = first_item;
while (iterator) {
if (static_cast<s64>(tick) - static_cast<s64>(iterator->tick) < 0) {
return;
}
Item* next = iterator->next;
if constexpr (RETURNS_BOOL) {
if (func(iterator->obj)) {
return;
}
} else {
func(iterator->obj);
}
iterator = next;
}
}
private:
2021-08-29 17:19:53 +01:00
size_t Build() {
2021-08-15 14:35:53 +01:00
if (free_items.empty()) {
const size_t item_id = item_pool.size();
2021-08-29 17:19:53 +01:00
auto& item = item_pool.emplace_back();
2021-08-15 14:35:53 +01:00
item.next = nullptr;
item.prev = nullptr;
return item_id;
}
const size_t item_id = free_items.front();
free_items.pop_front();
auto& item = item_pool[item_id];
item.next = nullptr;
item.prev = nullptr;
return item_id;
}
2021-08-29 17:19:53 +01:00
void Attach(Item& item) {
2021-08-15 14:35:53 +01:00
if (!first_item) {
first_item = &item;
}
if (!last_item) {
last_item = &item;
} else {
item.prev = last_item;
last_item->next = &item;
item.next = nullptr;
last_item = &item;
}
}
2021-08-29 17:19:53 +01:00
void Detach(Item& item) {
2021-08-15 14:35:53 +01:00
if (item.prev) {
item.prev->next = item.next;
}
if (item.next) {
item.next->prev = item.prev;
}
if (&item == first_item) {
first_item = item.next;
if (first_item) {
first_item->prev = nullptr;
}
}
if (&item == last_item) {
last_item = item.prev;
if (last_item) {
last_item->next = nullptr;
}
}
}
std::deque<Item> item_pool;
std::deque<size_t> free_items;
2021-08-29 17:19:53 +01:00
Item* first_item{};
Item* last_item{};
2021-08-15 14:35:53 +01:00
};
} // namespace Common