forked from yangxu02/LFUCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLFUCache.h
55 lines (40 loc) · 1.07 KB
/
LFUCache.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef __LFU_CACHE_H__
#define __LFU_CACHE_H__
#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include "DoubleLinkedList.h"
#include "ItemNode.h"
#include "Node.h"
#include "FreqNode.h"
class LFUCache {
public:
LFUCache(int capacity):capacity(capacity), size(0) {}
~LFUCache() {
Node* cur = freqList.getHead();
Node* next;
while (NULL != cur) {
next = cur->next;
delete cur;
cur = next;
}
if (dataSet.empty()) return;
for (std::map<int, ItemNode*>::iterator it = dataSet.begin(); it != dataSet.end(); ++it) {
Node* node = it->second;
dataSet.erase(it);
delete node;
}
}
void set(int k, int v);
int get(int k);
std::string repr();
private:
void updateFreq(ItemNode* node);
private:
std::map<int, ItemNode*> dataSet;
DoubleLinkedList freqList;
int capacity;
int size;
};
#endif