TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
lru_cache.cpp
Go to the documentation of this file.
1
48#include <cassert>
49#include <cstdint>
50#include <iostream>
51#include <list>
52#include <unordered_map>
53
58namespace others {
64namespace lru_cache {
68class LRUCache {
69 uint64_t pageFrame;
70 std::list<uint64_t> cache;
71 std::unordered_map<uint64_t, std::list<uint64_t>::iterator>
73
74 uint64_t hits =
75 0;
77 uint64_t pageFault = 0;
79
80 public:
85 explicit LRUCache(uint64_t pf) { pageFrame = pf; }
86
92 void refer(uint64_t page) {
93 // If the page requested not in cache.
94 if (pageMap.find(page) == pageMap.end()) {
95 pageFault++;
96
97 // Check if the cache is full
98 if (cache.size() == pageFrame) {
99 // delete the last page from cache
100 uint64_t lastPage = cache.back();
101 cache.pop_back();
102 pageMap.erase(lastPage);
103 }
104 }
105 // The requested page is in the cache
106 else {
107 hits++;
108 // present in cache, erase from current position to bring in front
109 cache.erase(pageMap[page]);
110 }
111 // Push it in the front of the cache and update the page reference in
112 // page map.
113 cache.push_front(page);
114 pageMap[page] = cache.begin();
115 }
116
121 void display() {
122 for (uint64_t &it : cache) {
123 std::cout << it << " ";
124 }
125 std::cout << std::endl;
126 }
131 uint64_t getHits() const { return hits; }
136 uint64_t getPageFault() const { return pageFault; }
137};
138
139} // namespace lru_cache
140} // namespace others
141
142namespace lru_tests {
148template <typename T>
149void log(T msg) {
150 // It's just to avoid writing cout and endl
151 std::cout << "[TESTS] : ---> " << msg << std::endl;
152}
153
160static void test_1() {
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}
180
187static void test_2() {
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}
207
214static void test_3() {
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}
234
239static void run_tests() {
240 test_1();
241 test_2();
242 test_3();
243 log("");
244 log("TESTS COMPLETED!");
245}
246} // namespace lru_tests
247
252int main() {
253 lru_tests::run_tests();
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}
uint64_t pageFrame
Page frame, or total size of the cache.
Definition lru_cache.cpp:69
std::list< uint64_t > cache
Cache linked list (using the STL)
Definition lru_cache.cpp:70
LRUCache(uint64_t pf)
Constructor, Initialize thee LRU class with page frame.
Definition lru_cache.cpp:85
uint64_t hits
was found in cache.
Definition lru_cache.cpp:74
uint64_t getPageFault() const
A function to get page fault.
void refer(uint64_t page)
Refer to a page, or request a page from memory.
Definition lru_cache.cpp:92
uint64_t getHits() const
A function to get page hits.
std::unordered_map< uint64_t, std::list< uint64_t >::iterator > pageMap
Hash map containing pages and their addresses.
Definition lru_cache.cpp:72
void display()
A function to display the current cache.
static void test_3()
A simple test case The assert statement will check expected hist and miss to resultant hits and miss.
void log(T msg)
A function to print given message on console.
static void test_2()
A test case contains hits more than cache size The assert statement will check expected hist and miss...
static void test_1()
A simple test case The assert statement will check expected hist and miss to resultant hits and miss.
static void run_tests()
A function to invoke all test cases.
int main()
Main function.
Implementation of the LRU caching algorithm
for vector