Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
lru_cache.cpp File Reference

An implementation of LRU Cache. Lru is a part of cache algorithms (also frequently called cache replacement algorithms or cache replacement policies). More...

#include <cassert>
#include <iostream>
#include <list>
#include <unordered_map>
Include dependency graph for lru_cache.cpp:

Classes

class  others::lru_cache::LRUCache
 LRU cache class. More...
 

Namespaces

namespace  others
 for vector
 
namespace  lru_cache
 Implementation of the LRU caching algorithm
 

Functions

template<typename T >
void lru_tests::log (T msg)
 A function to print given message on console.
 
static void lru_tests::test_1 ()
 A simple test case The assert statement will check expected hist and miss to resultant hits and miss.
 
static void lru_tests::test_2 ()
 A test case contains hits more than cache size The assert statement will check expected hist and miss to resultant hits and miss.
 
static void lru_tests::test_3 ()
 A simple test case The assert statement will check expected hist and miss to resultant hits and miss.
 
static void lru_tests::run_tests ()
 A function to invoke all test cases.
 
int main ()
 Main function.
 

Detailed Description

An implementation of LRU Cache. Lru is a part of cache algorithms (also frequently called cache replacement algorithms or cache replacement policies).

Logic

  • Discards the least recently used items first.
  • This algorithm requires keeping track of what was used when, which is expensive if one wants to make sure the algorithm always discards the least recently used item.
  • General implementations of this technique require keeping "age bits" for cache-lines and track the "Least Recently Used" cache-line based on age-bits.
  • In such an implementation, every time a cache-line is used, the age of all other cache-lines changes

Algorithm explanation

For a cache of page frame x:

  • Check if the page is present in cache.
  • If not present, then check is the cache is full or not:
  • If the cache is full, REMOVE the last element from the cache.
  • If the element is present in cache, then shift that element to first position in cache from its original position.
    • This way you can keep the least recently used elements in the last and most recently used in front of the cache.

Every time a requested page is not found in cache, that is a miss or page fault, and if the page is present in cache, then its a hit.

Data Structure used

  • In the algorithm below we used two different data structure, one is linked list and other one is a hash map
  • The linked list is used to contain the pages and the hash map contains the pages and their address.
  • Every time a new page is requested, we first check in the hash map if the page is present or not.
  • If not present, and the cache is full, we simply delete the last entry in the cache.
  • If present, we shift that page from its current location to beginning of the cache and update the address in hash map for that page.
Author
Nitin Sharma

Function Documentation

◆ log()

template<typename T >
void lru_tests::log ( T msg)

A function to print given message on console.

Template Parameters
TType of the given message.
Returns
void
148 {
149 // It's just to avoid writing cout and endl
150 std::cout << "[TESTS] : ---> " << msg << std::endl;
151}
T endl(T... args)
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
251 {
253
254 // Usage
256 cache.refer(1);
257 cache.refer(2);
258 cache.refer(3);
259 cache.refer(4);
260 cache.refer(5);
261 cache.refer(5);
262
263 cache.display();
264
265 std::cout << "Hits: " << cache.getHits()
266 << " Miss: " << cache.getPageFault() << std::endl;
267 return 0;
268}
LRU cache class.
Definition lru_cache.cpp:67
static void run_tests()
A function to invoke all test cases.
Definition lru_cache.cpp:238
Here is the call graph for this function:

◆ run_tests()

static void lru_tests::run_tests ( )
static

A function to invoke all test cases.

Returns
void
238 {
239 test_1();
240 test_2();
241 test_3();
242 log("");
243 log("TESTS COMPLETED!");
244}
static void test_1()
Definition heavy_light_decomposition.cpp:505
static void test_2()
Definition heavy_light_decomposition.cpp:549
static void test_3()
Definition heavy_light_decomposition.cpp:592
T log(T... args)
Here is the call graph for this function:

◆ test_1()

static void lru_tests::test_1 ( )
static

A simple test case The assert statement will check expected hist and miss to resultant hits and miss.

Returns
void
159 {
160 uint64_t expected_hits = 2;
161 uint64_t expected_pageFault = 4;
162
163 log("Running Test-1...");
164
166 cache.refer(1);
167 cache.refer(2);
168 cache.refer(5);
169 cache.refer(1);
170 cache.refer(4);
171 cache.refer(5);
172
173 log("Checking assert statement...");
174 assert(cache.getHits() == expected_hits &&
175 cache.getPageFault() == expected_pageFault);
176 log("Assert successful!");
177 log("Test-1 complete!");
178}
Here is the call graph for this function:

◆ test_2()

static void lru_tests::test_2 ( )
static

A test case contains hits more than cache size The assert statement will check expected hist and miss to resultant hits and miss.

Returns
void
186 {
187 uint64_t expected_hits = 4;
188 uint64_t expected_pageFault = 2;
189
190 log("Running Test-2...");
191
193 cache.refer(1);
194 cache.refer(1);
195 cache.refer(1);
196 cache.refer(1);
197 cache.refer(1);
198 cache.refer(5);
199
200 log("Checking assert statement...");
201 assert(cache.getHits() == expected_hits &&
202 cache.getPageFault() == expected_pageFault);
203 log("Assert successful!");
204 log("Test-2 complete!");
205}
Here is the call graph for this function:

◆ test_3()

static void lru_tests::test_3 ( )
static

A simple test case The assert statement will check expected hist and miss to resultant hits and miss.

Returns
void
213 {
214 uint64_t expected_hits = 1;
215 uint64_t expected_pageFault = 5;
216
217 log("Running Test-3...");
218
220 cache.refer(1);
221 cache.refer(2);
222 cache.refer(3);
223 cache.refer(4);
224 cache.refer(5);
225 cache.refer(5);
226
227 log("Checking assert statement...");
228 assert(cache.getHits() == expected_hits &&
229 cache.getPageFault() == expected_pageFault);
230 log("Assert successful!");
231 log("Test-3 complete!");
232}
Here is the call graph for this function: