TheAlgorithms/C++ 1.0.0
All the 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.

Definition at line 68 of file lru_cache.cpp.

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.

Definition at line 85 of file lru_cache.cpp.

85{ pageFrame = pf; }
uint64_t pageFrame
Page frame, or total size of the cache.
Definition lru_cache.cpp:69

Member Function Documentation

◆ display()

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

A function to display the current cache.

Returns
Void

Definition at line 121 of file lru_cache.cpp.

121 {
122 for (uint64_t &it : cache) {
123 std::cout << it << " ";
124 }
125 std::cout << std::endl;
126 }
std::list< uint64_t > cache
Cache linked list (using the STL)
Definition lru_cache.cpp:70

◆ getHits()

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

A function to get page hits.

Returns
int

Definition at line 131 of file lru_cache.cpp.

131{ return hits; }
uint64_t hits
was found in cache.
Definition lru_cache.cpp:74

◆ getPageFault()

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

A function to get page fault.

Returns
int

Definition at line 136 of file lru_cache.cpp.

136{ return pageFault; }

◆ 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.

Definition at line 92 of file lru_cache.cpp.

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

Member Data Documentation

◆ cache

std::list<uint64_t> others::lru_cache::LRUCache::cache
private

Cache linked list (using the STL)

Definition at line 70 of file lru_cache.cpp.

◆ 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

Definition at line 74 of file lru_cache.cpp.

◆ 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

Definition at line 77 of file lru_cache.cpp.

◆ pageFrame

uint64_t others::lru_cache::LRUCache::pageFrame
private

Page frame, or total size of the cache.

Definition at line 69 of file lru_cache.cpp.

◆ pageMap

std::unordered_map<uint64_t, std::list<uint64_t>::iterator> others::lru_cache::LRUCache::pageMap
private

Hash map containing pages and their addresses.

Definition at line 72 of file lru_cache.cpp.


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