-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRU_cache.hpp
More file actions
106 lines (103 loc) · 4.09 KB
/
LRU_cache.hpp
File metadata and controls
106 lines (103 loc) · 4.09 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
#include <unordered_map>
#include <utility>
#include <tuple>
#include <list>
#include <functional>
namespace gtd {
template <typename T>
inline void hash_combine(size_t& hash, const T& obj) {
hash ^= std::hash<T>{}(obj) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
}
}
namespace std {
template <typename... Ts>
class hash<std::tuple<Ts...>> {
public:
std::size_t operator()(const std::tuple<Ts...>& _tuple) const {
std::size_t hash = 00;
std::apply([&hash](const Ts&... args) {
(gtd::hash_combine(hash,args),...);
}, _tuple);
return hash;
}
};
}
namespace gtd {
template <typename rT, typename... argTs>
class LRU_cache {
public:
/// @param _ptr the function pointer.
explicit LRU_cache(rT(*_ptr)(argTs...)) : __func_ptr{_ptr} {}
/// @param _ptr the function pointer.
/// @param _size the maximum size of the cache.
explicit LRU_cache(rT(*_ptr)(argTs...), std::size_t _size) : __func_ptr{_ptr}, __cache_size{_size} {}
rT operator()(argTs... args) {
std::tuple<argTs...> tuple_args = std::make_tuple(args...);
if(__cache_map.count(tuple_args)) {
__cache.splice(__cache.begin(),__cache,__cache_map[tuple_args]);
return __cache_map[tuple_args]->second;
} else {
if(__cache_size != 0ull) { if(__cache_map.size() == __cache_size) {
__cache_map.erase(__cache.back().first);
__cache.pop_back();
} }
rT _return = __func_ptr(args...);
__cache.push_front({tuple_args,_return});
__cache_map[tuple_args] = __cache.begin();
return _return;
}
}
/// @brief Returns the size of the cache.
/// @return Cache size as std::size_t.
std::size_t cache_size() const {
return __cache_map.size();
}
/// @brief Reduces the size of the cache to the specified size.
/// @param new_size the new size of the cache as std::size_t.
void reduce_cache_size(std::size_t new_size) {
while(cache_size() > new_size) {
__cache_map.erase(__cache.back().first);
__cache.pop_back();
}
}
/// @brief Returns the maximum size of the cache.
/// @return Maximum cache size as std::size_t (0 - if an infinity size).
std::size_t cache_capacity() const {
return __cache_size;
}
/// @brief Changes the maximum size of the cache to the specified size.
/// @param new_size the new maximum size of the cache as std::size_t (0 - to an infinity size).
void change_cache_capacity(std::size_t new_size) {
if((__cache_size > new_size || __cache_size == 0) && new_size != 0) {
while(cache_size() > new_size) {
__cache_map.erase(__cache.back().first);
__cache.pop_back();
}
}
__cache_size = new_size;
}
/// @brief Clear the all cache.
void clear_cache() {
__cache_map.clear();
__cache.clear();
}
/// @brief Clear the cache's entry with the specified arguments.
/// @param args... the set of the function's arguments.
void clear_cache(argTs... args) {
clear_cache(std::make_tuple(args...));
}
/// @brief Clear the cache's entry with the specified arguments.
/// @param args the set of the function's arguments as std::tuple.
void clear_cache(std::tuple<argTs...> args) {
if(__cache_map.count(args) == 0) return;
__cache.erase(__cache_map[args]);
__cache_map.erase(args);
}
private:
rT(*__func_ptr)(argTs...){};
std::size_t __cache_size{};
std::list<std::pair<std::tuple<argTs...>,rT>> __cache{};
std::unordered_map<std::tuple<argTs...>, typename std::list<std::pair<std::tuple<argTs...>,rT>>::iterator> __cache_map{};
};
}