TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
lru_cache2.cpp File Reference

Implementation for [LRU Cache] (https://en.wikipedia.org/wiki/Cache_replacement_policies#:~:text=Least%20Recently%20Used%20(LRU)) More...

#include <cassert>
#include <cstdint>
#include <iostream>
#include <unordered_map>
Include dependency graph for lru_cache2.cpp:

Go to the source code of this file.

Classes

class  others::Cache::D_Node< T >
 Node for a doubly linked list with data, prev and next pointers. More...
 
class  others::Cache::LRUCache< K, V >
 LRUCache. More...
 

Namespaces

namespace  others
 for vector
 
namespace  others::Cache
 Cache algorithm.
 

Functions

static void test ()
 self test implementations
 
int main ()
 main function
 

Detailed Description

Implementation for [LRU Cache] (https://en.wikipedia.org/wiki/Cache_replacement_policies#:~:text=Least%20Recently%20Used%20(LRU))

LRU discards the least recently used value. Data structures used - doubly linked list and unordered_map

unordered_map maps the key to the address of the node of the linked list. If the element is accessed, the element is moved to the beginning of the linked list.

When the cache is full, the last element in the linked list is popped.

Author
Karan Sharma

Definition in file lru_cache2.cpp.

Function Documentation

◆ main()

int main ( void )

main function

Returns
0 on exit

Definition at line 274 of file lru_cache2.cpp.

274 {
275 test(); // run the self test implementation
276 return 0;
277}
static void test()
self test implementations

◆ test()

static void test ( )
static

self test implementations

Returns
void

Definition at line 220 of file lru_cache2.cpp.

220 {
222
223 // test the initial state of the cache
224 assert(cache.size() == 0);
225 assert(cache.capacity() == 5);
226 assert(cache.empty());
227
228 // test insertion in the cache
229 cache.put(1, 10);
230 cache.put(-2, 20);
231
232 // test the state of cache after inserting some items
233 assert(cache.size() == 2);
234 assert(cache.capacity() == 5);
235 assert(!cache.empty());
236
237 // test getting items from the cache
238 assert(cache.get(1) == 10);
239 assert(cache.get(-2) == 20);
240
241 cache.put(-3, -30);
242 cache.put(4, 40);
243 cache.put(5, -50);
244 cache.put(6, 60);
245
246 // test the state after inserting more items than the capacity
247 assert(cache.size() == 5);
248 assert(cache.capacity() == 5);
249 assert(!cache.empty());
250
251 // fetching 1 throws runtime_error
252 // as 1 was evicted being the least recently used
253 // when 6 was added
254 try {
255 cache.get(1);
256 } catch (const std::runtime_error &e) {
257 assert(std::string(e.what()) == "key is not present in the cache");
258 }
259
260 // test retrieval of all items in the cache
261 assert(cache.get(-2) == 20);
262 assert(cache.get(-3) == -30);
263 assert(cache.get(4) == 40);
264 assert(cache.get(5) == -50);
265 assert(cache.get(6) == 60);
266
267 std::cout << "test - passed\n";
268}