-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRUCache.h
37 lines (33 loc) · 897 Bytes
/
LRUCache.h
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
#include <unordered_map>
#ifndef P11_LRU_CACHE_LRUCACHE_H
#define P11_LRU_CACHE_LRUCACHE_H
class LRUCache {
public:
LRUCache(int capacity);
bool read(int address);
void printCache();
~LRUCache();
private:
// Definition of a cacheBlock as a doubly linked list
struct cacheBlock {
int tag;
cacheBlock* next;
cacheBlock* prev;
cacheBlock(int x): tag(x), next(nullptr), prev(nullptr){};
};
// Attributes
// cacheSet uses an unordered_map to look up the tag
std::unordered_map<int, cacheBlock*> cacheSet;
cacheBlock* head; //head is always the beginning of the cacheBlock linked list
int setCapacity;
int blockCount;
double readCount;
double hitCount;
double missCount;
int LRU; // LRU is always the last block's tag in the cacheBlock linked list
// Methods
void blockToMRU(int tag, cacheBlock* block, bool present);
int getTag(int address);
void evict();
};
#endif