forked from fuzziqersoftware/phosg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRUSet.hh
53 lines (37 loc) · 942 Bytes
/
LRUSet.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include <stdint.h>
#include <unordered_map>
template <typename K>
class LRUSet {
protected:
struct Item {
Item* prev;
Item* next;
const K* key;
size_t size;
Item(size_t size);
};
Item* head;
Item* tail;
std::unordered_map<K, Item> items;
size_t total_size;
bool after_emplace(
const std::pair<typename std::unordered_map<K, Item>::iterator, bool>& emplace_ret, size_t size);
void unlink_item(Item* i);
void link_item(Item* i);
public:
LRUSet();
virtual ~LRUSet();
bool insert(const K& k, size_t size = 0);
bool emplace(K&& k, size_t size = 0);
bool erase(const K& k);
void clear();
bool change_size(const K& k, size_t new_size);
bool touch(const K& k, ssize_t new_size = -1);
size_t size() const;
size_t count() const;
std::pair<K, size_t> evict_object();
std::pair<K, size_t> peek();
void swap(LRUSet<K>& other);
};
#include "LRUSet-inl.hh"