Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
others::lru_cache::LRUCache Class Reference

LRU cache class. More...

Collaboration diagram for others::lru_cache::LRUCache:
[legend]

Public Member Functions

 LRUCache (uint64_t pf)
 Constructor, Initialize thee LRU class with page frame.
 
void refer (uint64_t page)
 Refer to a page, or request a page from memory.
 
void display ()
 A function to display the current cache.
 
uint64_t getHits () const
 A function to get page hits.
 
uint64_t getPageFault () const
 A function to get page fault.
 

Private Attributes

uint64_t pageFrame
 Page frame, or total size of the cache.
 
std::list< uint64_t > cache
 Cache linked list (using the STL)
 
std::unordered_map< uint64_t, std::list< uint64_t >::iterator > pageMap
 Hash map containing pages and their addresses.
 
uint64_t hits
 was found in cache.
 
uint64_t pageFault = 0
 

Detailed Description

LRU cache class.

Constructor & Destructor Documentation

◆ LRUCache()

others::lru_cache::LRUCache::LRUCache ( uint64_t pf)
inlineexplicit

Constructor, Initialize thee LRU class with page frame.

Parameters
pfPage frame or total size of cache.
84{ pageFrame = pf; }
uint64_t pageFrame
Page frame, or total size of the cache.
Definition lru_cache.cpp:68

Member Function Documentation

◆ display()

void others::lru_cache::LRUCache::display ( )
inline

A function to display the current cache.

Returns
Void
120 {
121 for (uint64_t &it : cache) {
122 std::cout << it << " ";
123 }
125 }
std::list< uint64_t > cache
Cache linked list (using the STL)
Definition lru_cache.cpp:69
T endl(T... args)
Here is the call graph for this function:

◆ getHits()

uint64_t others::lru_cache::LRUCache::getHits ( ) const
inline

A function to get page hits.

Returns
int
130{ return hits; }
uint64_t hits
was found in cache.
Definition lru_cache.cpp:73

◆ getPageFault()

uint64_t others::lru_cache::LRUCache::getPageFault ( ) const
inline

A function to get page fault.

Returns
int
135{ return pageFault; }
uint64_t pageFault
Definition lru_cache.cpp:76

◆ refer()

void others::lru_cache::LRUCache::refer ( uint64_t page)
inline

Refer to a page, or request a page from memory.

Parameters
pageThe page that you are referring to.
Returns
void

< Increase the page fault by one.

91 {
92 // If the page requested not in cache.
93 if (pageMap.find(page) == pageMap.end()) {
94 pageFault++; ///< Increase the page fault by one.
95
96 // Check if the cache is full
97 if (cache.size() == pageFrame) {
98 // delete the last page from cache
99 uint64_t lastPage = cache.back();
100 cache.pop_back();
101 pageMap.erase(lastPage);
102 }
103 }
104 // The requested page is in the cache
105 else {
106 hits++;
107 // present in cache, erase from current position to bring in front
108 cache.erase(pageMap[page]);
109 }
110 // Push it in the front of the cache and update the page reference in
111 // page map.
112 cache.push_front(page);
113 pageMap[page] = cache.begin();
114 }
T back(T... args)
T begin(T... args)
std::unordered_map< uint64_t, std::list< uint64_t >::iterator > pageMap
Hash map containing pages and their addresses.
Definition lru_cache.cpp:71
T end(T... args)
T erase(T... args)
T find(T... args)
T pop_back(T... args)
T push_front(T... args)
T size(T... args)
Here is the call graph for this function:

Member Data Documentation

◆ hits

uint64_t others::lru_cache::LRUCache::hits
private
Initial value:
=
0

was found in cache.

Total number of hits, or total number of times a page

◆ pageFault

uint64_t others::lru_cache::LRUCache::pageFault = 0
private

Total number of miss/page fault, or total number of times a page was not found in cache


The documentation for this class was generated from the following file: