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;
}
Beispiel #2
0
 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()};
 }