-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_cache.hpp
108 lines (91 loc) · 2.04 KB
/
lru_cache.hpp
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
107
108
#include <iostream>
#include<string>
#include<unordered_map>
#include<list>
#include "fnv-hash/fnv.h"
#include "fnv-hash/hash_32.c"
#define MAX_CACHE_SIZE 8
using namespace std;
class lruCache
{
// store keys of cache
list<pair<string, string>> dq;
// store references of key in cache
unordered_map<string, list<pair<string, string>>::iterator> ma;
public:
void refer(string, string);
void display();
string getlist(string key);
void dellist(string key);
void putInSet(string key, string value);
string getValue(string key);
void deleteKey(string key);
};
/* Refers key x with in the LRU cache */
void lruCache::refer(string key, string value = "none")
{
auto it = ma.find(key);
if (it == ma.end())
{
// cache is full
if (dq.size() == MAX_CACHE_SIZE)
{
//delete least recently used element
auto last = dq.back();
dq.pop_back();
ma.erase(last.first);
}
}
// present in cache
else
{
auto del = ma[key];
dq.erase(del);
}
// update reference
dq.push_front(make_pair(key, value));
ma[key] = dq.begin();
}
// display contents of cache
void lruCache::display()
{
for (auto it = dq.begin(); it != dq.end();
it++)
cout << "key: " << (it->first) << " value: " << (it->second) << " ";
cout << endl;
}
string lruCache::getlist(string key)
{
string val;
auto it = ma.find(key);
if (it != ma.end())
{
auto valptr = ma[key];
val = valptr->second;
}
else
val = "none";
return val;
}
void lruCache::dellist(string key)
{
if (ma.find(key) != ma.end())
{
auto del = ma[key];
ma.erase(del->first);
dq.erase(del);
}
}
void lruCache::putInSet(string key, string value)
{
//Fnv32_t index = fnv_32_str(key, FNV1_32_INIT);
this->refer(key, value);
}
string lruCache::getValue(string key)
{
return this->getlist(key);
}
void lruCache::deleteKey(string key)
{
this->dellist(key);
}