-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLFUCache460.cpp
More file actions
196 lines (165 loc) · 4.97 KB
/
LFUCache460.cpp
File metadata and controls
196 lines (165 loc) · 4.97 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
class LFUCache {
public:
LFUCache(int capacity) {
limit = capacity;
th = 0;
}
int get(int key) {
if (cache.find(key) == cache.end()) {
return -1;
} else {
cache[key][1]++;
cache[key][2] = th;
th++;
return cache[key][0];
}
}
void put(int key, int value) {
if (limit == 0) return;
if (cache.size() < limit) {
if (cache.find(key) == cache.end()) {
vector<int>temp;
temp.emplace_back(value);
temp.emplace_back(1);
temp.emplace_back(th);
th++;
cache[key] = temp;
} else {
cache[key][0] = value;
cache[key][1]++;
cache[key][2] = th;
th++;
}
} else {
if (cache.find(key) != cache.end()) {
cache[key][0] = value;
cache[key][1]++;
cache[key][2] = th;
th++;
return;
}
int least = INT_MAX, Th = INT_MAX, targetKey;
//find the least frequently used
for (auto it:cache) {
if (it.second[1] < least) {
least = it.second[1];
Th = it.second[2];
targetKey = it.first;
} else if (it.second[1] == least && Th > it.second[2]) {
Th = it.second[2];
targetKey = it.first;
}
}
//cout << "go erase " << targetKey << endl;
cache.erase(targetKey);
vector<int>temp;
temp.emplace_back(value);
temp.emplace_back(1);
temp.emplace_back(th);
th++;
cache[key] = temp;
}
}
private:
int limit, th;
//0 : value 1 : count 2: th
map<int,vector<int>>cache;
};
/**
* Your LFUCache object will be instantiated and called as such:
* LFUCache* obj = new LFUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/
//the fatest solution
static const auto ______ = [](){
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
return nullptr;
}();
class LFUCache {
public:
LFUCache(int capacity) : capacity_(capacity) {}
int get(int key) {
auto it = lookupMap.find(key);
if (it == lookupMap.end()) {
return -1;
}
touch(it);
return it->second.value;
}
void put(int key, int value) {
if (capacity_ == 0) {
return;
}
auto it = lookupMap.find(key);
if (it == lookupMap.end()) {
if (lookupMap.size() == capacity_) {
auto leastSlot = freqList.front().slots.begin();
lookupMap.erase(leastSlot->key);
removeSlot(leastSlot);
}
FreqRecord record;
record.freq = 0;
auto freqRecordIt = freqList.insert(freqList.begin(), record);
Slot slot;
slot.key = key;
auto slotIt = record.slots.insert(freqRecordIt->slots.end(), slot);
slotIt->freqRecord = freqRecordIt;
Record r;
r.value = value;
r.slot = slotIt;
touch(lookupMap.insert(std::make_pair(key, r)).first);
} else {
it->second.value = value;
touch(it);
}
}
private:
int capacity_;
struct FreqRecord;
struct Slot {
int key;
std::list<FreqRecord>::iterator freqRecord;
};
using SlotList = std::list<Slot>;
struct FreqRecord {
int freq;
SlotList slots;
};
using FreqList = std::list<FreqRecord>;
FreqList freqList;
struct Record {
int value;
SlotList::iterator slot;
};
// key -> record
using Map = std::unordered_map<int, Record>;
Map lookupMap;
void touch(Map::iterator it) {
Record& record = it->second;
FreqList::iterator freqRecordIt = record.slot->freqRecord;
int freq = freqRecordIt->freq + 1;
FreqList::iterator nextFreqRecordIt = std::next(freqRecordIt);
// Move slot
if (nextFreqRecordIt == freqList.end() || nextFreqRecordIt->freq != freq) {
FreqRecord record;
record.freq = freq;
freqList.insert(nextFreqRecordIt, record);
nextFreqRecordIt = std::prev(nextFreqRecordIt);
}
Slot slot = *record.slot;
slot.freqRecord = nextFreqRecordIt;
SlotList& slotList = nextFreqRecordIt->slots;
auto slotIt = slotList.insert(slotList.end(), slot);
removeSlot(record.slot);
record.slot = slotIt;
}
void removeSlot(SlotList::iterator slotIt) {
auto freqRecordIt = slotIt->freqRecord;
freqRecordIt->slots.erase(slotIt);
if (freqRecordIt->slots.empty()) {
freqList.erase(freqRecordIt);
}
}
};