예제 #1
0
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;
}
예제 #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()};
 }
예제 #3
0
파일: lru.cpp 프로젝트: kamelzcs/study
 void update(HPIL::iterator it){
     int key = it->first;
     lru.erase(it->second.second);
     lru.push_front(key);
     it->second.second = lru.begin();
 }
예제 #4
0
 void update(HIPII::iterator it) {
     int key = it->first;
     used.erase(it->second.second);
     used.push_front(key);
     it->second.second = used.begin();
 }
예제 #5
0
 void push_front(int val){dt.push_front(val);}