|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cassert> |
| 4 | +#include <unordered_map> |
| 5 | +#include <cstddef> |
| 6 | + |
| 7 | +#include "./../ManapiErrors.hpp" |
| 8 | +#include "./../ManapiUtils.hpp" |
| 9 | +#include "./../std/ManapiChain.hpp" |
| 10 | + |
| 11 | +namespace manapi { |
| 12 | + template<typename K, typename V> |
| 13 | + class lru_cache { |
| 14 | + typedef typename std::pair<K, V> key_value_pair_t; |
| 15 | + |
| 16 | + typedef typename manapi::chain<key_value_pair_t>::iterator list_iterator_t; |
| 17 | + |
| 18 | + struct item_t { |
| 19 | + list_iterator_t item; |
| 20 | + std::size_t weight; |
| 21 | + }; |
| 22 | + public: |
| 23 | + lru_cache () : lru_cache(0) {} |
| 24 | + |
| 25 | + lru_cache (std::size_t max_size) { |
| 26 | + this->m_max = max_size; |
| 27 | + this->m_used = 0; |
| 28 | + } |
| 29 | + |
| 30 | + MANAPIHTTP_NODISCARD std::size_t size () const MANAPIHTTP_NOEXCEPT { |
| 31 | + return this->m_items.size(); |
| 32 | + } |
| 33 | + |
| 34 | + MANAPIHTTP_NODISCARD bool contains (const K &key) const MANAPIHTTP_NOEXCEPT { |
| 35 | + return this->m_items.contains(key); |
| 36 | + } |
| 37 | + |
| 38 | + void put (const K &key, const V &value, std::size_t weight) { |
| 39 | + auto it = this->m_items.find(key); |
| 40 | + if (it == this->m_items.end()) { |
| 41 | + this->m_list.push_front(key_value_pair_t (key, value)); |
| 42 | + try { |
| 43 | + item_t item (this->m_list.begin(), weight); |
| 44 | + this->m_items.insert({key, std::move(item)}); |
| 45 | + } |
| 46 | + catch (...) { |
| 47 | + this->m_list.pop_front(); |
| 48 | + std::rethrow_exception(std::current_exception()); |
| 49 | + } |
| 50 | + } |
| 51 | + else { |
| 52 | + it->second.item->second = value; // malloc can be only here |
| 53 | + |
| 54 | + std::unique_ptr<chain_item<key_value_pair_t>> un; |
| 55 | + this->m_used -= it->second.weight; |
| 56 | + this->m_list.erase(it->second.item, un); |
| 57 | + assert(!!un); |
| 58 | + this->m_list.push_front_chain(std::move(un)); |
| 59 | + it->second.item = this->m_list.begin(); |
| 60 | + it->second.weight = weight; |
| 61 | + } |
| 62 | + |
| 63 | + this->m_used += weight; |
| 64 | + |
| 65 | + this->cleanup(); |
| 66 | + } |
| 67 | + |
| 68 | + MANAPIHTTP_NODISCARD manapi::status_or<V*> get (const K &key) MANAPIHTTP_NOEXCEPT { |
| 69 | + auto it = this->m_items.find(key); |
| 70 | + if (it == this->m_items.end()) { |
| 71 | + return manapi::status_not_found(); |
| 72 | + } |
| 73 | + |
| 74 | + std::unique_ptr<chain_item<key_value_pair_t>> un; |
| 75 | + this->m_list.erase(it->second.item, un); |
| 76 | + assert(!!un); |
| 77 | + this->m_list.push_front_chain(std::move(un)); |
| 78 | + it->second.item = this->m_list.begin(); |
| 79 | + |
| 80 | + return &it->second.item->second; |
| 81 | + } |
| 82 | + |
| 83 | + void remove (const K &key) MANAPIHTTP_NOEXCEPT { |
| 84 | + auto it = this->m_items.find(key); |
| 85 | + if (it != this->m_items.end()) { |
| 86 | + this->m_list.erase(it->second.item); |
| 87 | + this->m_used -= it->second.weight; |
| 88 | + this->m_items.erase(it); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + void clear () MANAPIHTTP_NOEXCEPT { |
| 93 | + this->m_used = 0; |
| 94 | + this->m_items.clear(); |
| 95 | + this->m_list.clear(); |
| 96 | + } |
| 97 | + |
| 98 | + MANAPIHTTP_NODISCARD std::size_t used () const MANAPIHTTP_NOEXCEPT { |
| 99 | + return this->m_used; |
| 100 | + } |
| 101 | + |
| 102 | + void max (std::size_t m) { |
| 103 | + this->m_max = m; |
| 104 | + this->cleanup(); |
| 105 | + } |
| 106 | + |
| 107 | + MANAPIHTTP_NODISCARD std::size_t max () MANAPIHTTP_NOEXCEPT { |
| 108 | + return this->m_max; |
| 109 | + } |
| 110 | + |
| 111 | + void cleanup () MANAPIHTTP_NOEXCEPT { |
| 112 | + while (this->m_used > this->m_max) { |
| 113 | + auto it = this->m_list.rbegin(); |
| 114 | + auto data = this->m_items.extract(it->first); |
| 115 | + assert(!data.empty()); |
| 116 | + this->m_used -= data.mapped().weight; |
| 117 | + this->m_list.erase(it); |
| 118 | + } |
| 119 | + } |
| 120 | + private: |
| 121 | + std::size_t m_max; |
| 122 | + |
| 123 | + std::size_t m_used; |
| 124 | + |
| 125 | + manapi::chain<key_value_pair_t> m_list; |
| 126 | + |
| 127 | + std::unordered_map<K, item_t> m_items; |
| 128 | + }; |
| 129 | +} |
0 commit comments