int LRUCacheMiss(vector<int> input, int maxSize) { typedef list<int> LI; typedef list<int>::iterator LII; // key iterator typedef unordered_map<int, LII> MLII; // key, (key iterator) LI cache; MLII check; int count = 0; for (int i = 0; i < input.size(); i++) { if (check.count(input[i]) > 0) { // found in the LRU cache. cache.erase(check[input[i]]); cache.push_front(input[i]); check[input[i]] = cache.begin(); } else { // not found, miss happens. count++; cache.push_front(input[i]); check[input[i]] = cache.begin(); if (cache.size() > maxSize) { int key = cache.back(); cache.pop_back(); check.erase(key); } } } return count; }
void set(int key, int value) { auto it = cache.find(key); if(it!=cache.end()) { update(it); } else { if(used.size()==cap) { cache.erase(used.back()); used.pop_back(); } used.push_front(key); } cache[key] = {value, used.begin()}; }
void update(HPIL::iterator it){ int key = it->first; lru.erase(it->second.second); lru.push_front(key); it->second.second = lru.begin(); }
void update(HIPII::iterator it) { int key = it->first; used.erase(it->second.second); used.push_front(key); it->second.second = used.begin(); }
void push_front(int val){dt.push_front(val);}