|
| 1 | +// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project |
| 2 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <deque> |
| 7 | +#include <type_traits> |
| 8 | + |
| 9 | +#include "common/types.h" |
| 10 | + |
| 11 | +namespace Common { |
| 12 | + |
| 13 | +template <typename ObjectType, typename TickType> |
| 14 | +class LeastRecentlyUsedCache { |
| 15 | + struct Item { |
| 16 | + ObjectType obj; |
| 17 | + TickType tick; |
| 18 | + Item* next{}; |
| 19 | + Item* prev{}; |
| 20 | + }; |
| 21 | + |
| 22 | +public: |
| 23 | + LeastRecentlyUsedCache() : first_item{}, last_item{} {} |
| 24 | + ~LeastRecentlyUsedCache() = default; |
| 25 | + |
| 26 | + size_t Insert(ObjectType obj, TickType tick) { |
| 27 | + const auto new_id = Build(); |
| 28 | + auto& item = item_pool[new_id]; |
| 29 | + item.obj = obj; |
| 30 | + item.tick = tick; |
| 31 | + Attach(item); |
| 32 | + return new_id; |
| 33 | + } |
| 34 | + |
| 35 | + void Touch(size_t id, TickType tick) { |
| 36 | + auto& item = item_pool[id]; |
| 37 | + if (item.tick >= tick) { |
| 38 | + return; |
| 39 | + } |
| 40 | + item.tick = tick; |
| 41 | + if (&item == last_item) { |
| 42 | + return; |
| 43 | + } |
| 44 | + Detach(item); |
| 45 | + Attach(item); |
| 46 | + } |
| 47 | + |
| 48 | + void Free(size_t id) { |
| 49 | + auto& item = item_pool[id]; |
| 50 | + Detach(item); |
| 51 | + item.prev = nullptr; |
| 52 | + item.next = nullptr; |
| 53 | + free_items.push_back(id); |
| 54 | + } |
| 55 | + |
| 56 | + template <typename Func> |
| 57 | + void ForEachItemBelow(TickType tick, Func&& func) { |
| 58 | + static constexpr bool RETURNS_BOOL = |
| 59 | + std::is_same_v<std::invoke_result<Func, ObjectType>, bool>; |
| 60 | + Item* iterator = first_item; |
| 61 | + while (iterator) { |
| 62 | + if (static_cast<s64>(tick) - static_cast<s64>(iterator->tick) < 0) { |
| 63 | + return; |
| 64 | + } |
| 65 | + Item* next = iterator->next; |
| 66 | + if constexpr (RETURNS_BOOL) { |
| 67 | + if (func(iterator->obj)) { |
| 68 | + return; |
| 69 | + } |
| 70 | + } else { |
| 71 | + func(iterator->obj); |
| 72 | + } |
| 73 | + iterator = next; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | +private: |
| 78 | + size_t Build() { |
| 79 | + if (free_items.empty()) { |
| 80 | + const size_t item_id = item_pool.size(); |
| 81 | + auto& item = item_pool.emplace_back(); |
| 82 | + item.next = nullptr; |
| 83 | + item.prev = nullptr; |
| 84 | + return item_id; |
| 85 | + } |
| 86 | + const size_t item_id = free_items.front(); |
| 87 | + free_items.pop_front(); |
| 88 | + auto& item = item_pool[item_id]; |
| 89 | + item.next = nullptr; |
| 90 | + item.prev = nullptr; |
| 91 | + return item_id; |
| 92 | + } |
| 93 | + |
| 94 | + void Attach(Item& item) { |
| 95 | + if (!first_item) { |
| 96 | + first_item = &item; |
| 97 | + } |
| 98 | + if (!last_item) { |
| 99 | + last_item = &item; |
| 100 | + } else { |
| 101 | + item.prev = last_item; |
| 102 | + last_item->next = &item; |
| 103 | + item.next = nullptr; |
| 104 | + last_item = &item; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + void Detach(Item& item) { |
| 109 | + if (item.prev) { |
| 110 | + item.prev->next = item.next; |
| 111 | + } |
| 112 | + if (item.next) { |
| 113 | + item.next->prev = item.prev; |
| 114 | + } |
| 115 | + if (&item == first_item) { |
| 116 | + first_item = item.next; |
| 117 | + if (first_item) { |
| 118 | + first_item->prev = nullptr; |
| 119 | + } |
| 120 | + } |
| 121 | + if (&item == last_item) { |
| 122 | + last_item = item.prev; |
| 123 | + if (last_item) { |
| 124 | + last_item->next = nullptr; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + std::deque<Item> item_pool; |
| 130 | + std::deque<size_t> free_items; |
| 131 | + Item* first_item{}; |
| 132 | + Item* last_item{}; |
| 133 | +}; |
| 134 | + |
| 135 | +} // namespace Common |
0 commit comments