-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlfu.cpp
More file actions
54 lines (49 loc) · 1.59 KB
/
lfu.cpp
File metadata and controls
54 lines (49 loc) · 1.59 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
#include "lfu.h"
namespace coen383
{
//CONSTRUCTOR
lfu::lfu()
{
pageRequest = 0;
numElements = 0;
pageNumber = 0;
numCount = 0;
//*used for debugging*//
// myfile.open ("output");
}
void lfu::pageReplaceAlgo()
{
pageItr = page.find(pageRequest);
if(pageItr == page.end())
{
//page does not already exist in the cache
std::cout<<"Page number "<<pageRequest<<" caused a page fault."<<std::endl;
myfile<<"Page number "<<pageRequest<<" caused a page fault."<<std::endl;
if(page.size() >= tableSize)
{
occItr = count.lower_bound(0);
counter = occItr->second;
count.erase(occItr);
page.erase(counter);
}
page.insert(std::pair <int, std::multimap<int, int> :: iterator> (pageRequest,count.insert(std::pair <int, int> (1, pageRequest))));
numCount++;
}
else
{
//page already exists in the cache
std::cerr<<"Page number "<<pageRequest<<" caused a page hit."<<std::endl;
//get occurence of the page
counter = pageItr->second->first;
//remove page from occurences
count.erase(pageItr->second);
//insert iterator for the page(key) as the page(value), which points to occurence of that page in occurence
pageItr->second = count.insert(std::make_pair (++counter, pageRequest));
}
//*used for debugging*//
// std::cerr<<"count: ";
// for (std::multimap <int, int> ::const_iterator it = count.begin(); it != count.end(); ++it)
// std::cerr<<"< "<<it->first<<","<<it->second<<"> ";
// std::cerr<<std::endl;
}
}