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

Implementation for [LFU Cache] (https://en.wikipedia.org/wiki/Least_frequently_used) More...

#include <cassert>
#include <iostream>
#include <unordered_map>
Include dependency graph for lfu_cache.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::LFUCache< K, V >
 LFUCache. More...
 

Namespaces

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

Typedefs

template<typename K , typename V >
using others::Cache::CacheNode = D_Node<std::pair<K, V>>
 

Functions

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

Detailed Description

Implementation for [LFU Cache] (https://en.wikipedia.org/wiki/Least_frequently_used)

LFU discards the least frequently used value. if there are multiple items with the same minimum frequency then, the least recently used among them is discarded. Data structures used - doubly linked list and unordered_map(hash map).

Hashmap maps the key to the address of the node of the linked list and its current usage frequency. If the element is accessed the element is removed from the linked list of the current frequency and added to the linked list of incremented frequency.

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

Author
Karan Sharma

Definition in file lfu_cache.cpp.

Function Documentation

◆ main()

int main ( void )

main function

Returns
0 on exit

Definition at line 301 of file lfu_cache.cpp.

301 {
302 test(); // run the self test implementation
303 return 0;
304}
static void test()
self test implementation

◆ test()

static void test ( )
static

self test implementation

Returns
void

Definition at line 250 of file lfu_cache.cpp.

250 {
252
253 // test the initial state of the cache
254 assert(cache.size() == 0);
255 assert(cache.capacity() == 5);
256 assert(cache.empty());
257
258 // test insertion in the cache
259 cache.put(1, 10);
260 cache.put(-2, 20);
261
262 // test the state of cache after inserting some items
263 assert(cache.size() == 2);
264 assert(cache.capacity() == 5);
265 assert(!cache.empty());
266
267 // test getting items from the cache
268 assert(cache.get(1) == 10);
269 assert(cache.get(-2) == 20);
270
271 cache.put(-3, -30);
272 cache.put(4, 40);
273 cache.put(5, -50);
274 cache.put(6, 60);
275
276 // test the state after inserting more items than the capacity
277 assert(cache.size() == 5);
278 assert(cache.capacity() == 5);
279 assert(!cache.empty());
280
281 // test retrieval of all items in the cache
282 assert(cache.get(1) == 10);
283 assert(cache.get(-2) == 20);
284
285 // fetching -3 throws runtime_error
286 // as -3 was evicted being the least frequently used
287 // when 6 was added
288 // assert(cache.get(-3) == -30);
289
290 assert(cache.get(4) == 40);
291 assert(cache.get(5) == -50);
292 assert(cache.get(6) == 60);
293
294 std::cout << "test - passed\n";
295}