3 #include <unordered_map>
12 template <
typename K,
typename V>
19 LRUCache(
size_t c) : objs(), cap(c), head(nullptr), tail(nullptr) {}
21 bool exists(
const K& k)
const {
return objs.find(k) != objs.end(); }
23 size_t size()
const {
return objs.size(); }
30 auto itr = objs.find(k);
31 if (itr == objs.end()) {
35 auto& node = itr->second;
41 void put(
const K& k,
const V& v) {
42 if (objs.size() >= cap) remove_tail();
43 auto itr = objs.find(k);
44 if (itr == objs.end()) {
47 n.prev = n.next =
nullptr;
51 auto& node = itr->second;
64 std::unordered_map<K, Node> objs;
69 void make_recent(Node* val) {
70 if (head == val)
return;
75 if (val->prev !=
nullptr) val->prev->next = val->next;
76 if (val->next !=
nullptr) val->next->prev = val->prev;
80 if (head !=
nullptr) head->prev = val;
82 if (tail ==
nullptr) tail = head;
86 if (tail ==
nullptr)
return;
87 const K& key = tail->key;