# Algorithms_in_C++: others::lru_cache::LRUCache Class Reference

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. More...
void | refer (uint64_t page)
| Refer to a page, or request a page from memory. More...
void | display ()
| A function to display the current cache. More...
uint64_t | getHits () const
| A function to get page hits. More...
uint64_t | getPageFault () const
| A function to get page fault. More...

## 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. More...
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

  pf | Page frame or total size of cache.

{ pageFrame = pf; }
others::lru_cache::LRUCache::pageFrame
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

{
for (uint64_t &it : cache) {
std::cout << it << " ";
}
std::cout << std::endl;
}
std::cout
others::lru_cache::LRUCache::cache
std::list< uint64_t > cache
Cache linked list (using the STL)
Definition: lru_cache.cpp:69
std::endl
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

{ return hits; }
others::lru_cache::LRUCache::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

{ return pageFault; }
others::lru_cache::LRUCache::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

  page | The page that you are referring to.

Returns
  void

< Increase the page fault by one.

{
// If the page requested not in cache.
if (pageMap.find(page) == pageMap.end()) {
pageFault++; ///< Increase the page fault by one.
// Check if the cache is full
if (cache.size() == pageFrame) {
// delete the last page from cache
uint64_t lastPage = cache.back();
cache.pop_back();
pageMap.erase(lastPage);
}
}
// The requested page is in the cache
else {
hits++;
// present in cache, erase from current position to bring in front
cache.erase(pageMap[page]);
}
// Push it in the front of the cache and update the page reference in
// page map.
cache.push_front(page);
pageMap[page] = cache.begin();
}
std::list::back
T back(T... args)
std::list::begin
T begin(T... args)
others::lru_cache::LRUCache::pageMap
std::unordered_map< uint64_t, std::list< uint64_t >::iterator > pageMap
Hash map containing pages and their addresses.
Definition: lru_cache.cpp:71
std::unordered_map::end
T end(T... args)
std::unordered_map::erase
T erase(T... args)
std::unordered_map::find
T find(T... args)
std::list::pop_back
T pop_back(T... args)
std::list::push_front
T push_front(T... args)
std::list::size
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:

- others/lru_cache.cpp