TheAlgorithms/C++ 1.0.0
All the 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 <cstdint>
#include <iostream>
#include <list>
#include <unordered_map>
Include dependency graph for lru_cache.cpp:

Go to the source code of this file.

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

Definition in file lru_cache.cpp.

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

Definition at line 149 of file lru_cache.cpp.

149 {
150 // It's just to avoid writing cout and endl
151 std::cout << "[TESTS] : ---> " << msg << std::endl;
152}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 252 of file lru_cache.cpp.

252 {
254
255 // Usage
257 cache.refer(1);
258 cache.refer(2);
259 cache.refer(3);
260 cache.refer(4);
261 cache.refer(5);
262 cache.refer(5);
263
264 cache.display();
265
266 std::cout << "Hits: " << cache.getHits()
267 << " Miss: " << cache.getPageFault() << std::endl;
268 return 0;
269}
static void run_tests()
A function to invoke all test cases.

◆ run_tests()

static void lru_tests::run_tests ( )
static

A function to invoke all test cases.

Returns
void

Definition at line 239 of file lru_cache.cpp.

239 {
240 test_1();
241 test_2();
242 test_3();
243 log("");
244 log("TESTS COMPLETED!");
245}
static void test_1()
static void test_2()
static void test_3()
void log(T msg)
A function to print given message on console.

◆ 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

Definition at line 160 of file lru_cache.cpp.

160 {
161 uint64_t expected_hits = 2;
162 uint64_t expected_pageFault = 4;
163
164 log("Running Test-1...");
165
167 cache.refer(1);
168 cache.refer(2);
169 cache.refer(5);
170 cache.refer(1);
171 cache.refer(4);
172 cache.refer(5);
173
174 log("Checking assert statement...");
175 assert(cache.getHits() == expected_hits &&
176 cache.getPageFault() == expected_pageFault);
177 log("Assert successful!");
178 log("Test-1 complete!");
179}

◆ 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

Definition at line 187 of file lru_cache.cpp.

187 {
188 uint64_t expected_hits = 4;
189 uint64_t expected_pageFault = 2;
190
191 log("Running Test-2...");
192
194 cache.refer(1);
195 cache.refer(1);
196 cache.refer(1);
197 cache.refer(1);
198 cache.refer(1);
199 cache.refer(5);
200
201 log("Checking assert statement...");
202 assert(cache.getHits() == expected_hits &&
203 cache.getPageFault() == expected_pageFault);
204 log("Assert successful!");
205 log("Test-2 complete!");
206}

◆ 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

Definition at line 214 of file lru_cache.cpp.

214 {
215 uint64_t expected_hits = 1;
216 uint64_t expected_pageFault = 5;
217
218 log("Running Test-3...");
219
221 cache.refer(1);
222 cache.refer(2);
223 cache.refer(3);
224 cache.refer(4);
225 cache.refer(5);
226 cache.refer(5);
227
228 log("Checking assert statement...");
229 assert(cache.getHits() == expected_hits &&
230 cache.getPageFault() == expected_pageFault);
231 log("Assert successful!");
232 log("Test-3 complete!");
233}