TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
lru_cache.cpp
Go to the documentation of this file.
1
47
48
#include <cassert>
49
#include <cstdint>
50
#include <iostream>
51
#include <list>
52
#include <unordered_map>
53
58
namespace
others
{
64
namespace
lru_cache
{
68
class
LRUCache
{
69
uint64_t
pageFrame
;
70
std::list<uint64_t>
cache
;
71
std::unordered_map<uint64_t, std::list<uint64_t>::iterator>
72
pageMap
;
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
}
127
131
uint64_t
getHits
()
const
{
return
hits
; }
136
uint64_t
getPageFault
()
const
{
return
pageFault
; }
137
};
138
139
}
// namespace lru_cache
140
}
// namespace others
141
142
namespace
lru_tests {
148
template
<
typename
T>
149
void
log
(T msg) {
150
// It's just to avoid writing cout and endl
151
std::cout <<
"[TESTS] : ---> "
<< msg << std::endl;
152
}
153
160
static
void
test_1
() {
161
uint64_t expected_hits = 2;
162
uint64_t expected_pageFault = 4;
163
164
log
(
"Running Test-1..."
);
165
166
others::lru_cache::LRUCache
cache(4);
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
187
static
void
test_2
() {
188
uint64_t expected_hits = 4;
189
uint64_t expected_pageFault = 2;
190
191
log
(
"Running Test-2..."
);
192
193
others::lru_cache::LRUCache
cache(4);
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
214
static
void
test_3
() {
215
uint64_t expected_hits = 1;
216
uint64_t expected_pageFault = 5;
217
218
log
(
"Running Test-3..."
);
219
220
others::lru_cache::LRUCache
cache(4);
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
239
static
void
run_tests
() {
240
test_1
();
241
test_2
();
242
test_3
();
243
log
(
""
);
244
log
(
"TESTS COMPLETED!"
);
245
}
246
}
// namespace lru_tests
247
252
int
main
() {
253
lru_tests::run_tests
();
254
255
// Usage
256
others::lru_cache::LRUCache
cache(4);
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
}
others::lru_cache::LRUCache
LRU cache class.
Definition
lru_cache.cpp:68
others::lru_cache::LRUCache::pageFault
uint64_t pageFault
Definition
lru_cache.cpp:77
others::lru_cache::LRUCache::pageFrame
uint64_t pageFrame
Page frame, or total size of the cache.
Definition
lru_cache.cpp:69
others::lru_cache::LRUCache::cache
std::list< uint64_t > cache
Cache linked list (using the STL).
Definition
lru_cache.cpp:70
others::lru_cache::LRUCache::LRUCache
LRUCache(uint64_t pf)
Constructor, Initialize thee LRU class with page frame.
Definition
lru_cache.cpp:85
others::lru_cache::LRUCache::hits
uint64_t hits
was found in cache.
Definition
lru_cache.cpp:74
others::lru_cache::LRUCache::getPageFault
uint64_t getPageFault() const
A function to get page fault.
Definition
lru_cache.cpp:136
others::lru_cache::LRUCache::refer
void refer(uint64_t page)
Refer to a page, or request a page from memory.
Definition
lru_cache.cpp:92
others::lru_cache::LRUCache::getHits
uint64_t getHits() const
A function to get page hits.
Definition
lru_cache.cpp:131
others::lru_cache::LRUCache::pageMap
std::unordered_map< uint64_t, std::list< uint64_t >::iterator > pageMap
Hash map containing pages and their addresses.
Definition
lru_cache.cpp:72
others::lru_cache::LRUCache::display
void display()
A function to display the current cache.
Definition
lru_cache.cpp:121
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
lru_tests::test_3
static void test_3()
A simple test case The assert statement will check expected hist and miss to resultant hits and miss.
Definition
lru_cache.cpp:214
lru_tests::log
void log(T msg)
A function to print given message on console.
Definition
lru_cache.cpp:149
lru_tests::test_2
static void test_2()
A test case contains hits more than cache size The assert statement will check expected hist and miss...
Definition
lru_cache.cpp:187
lru_tests::test_1
static void test_1()
A simple test case The assert statement will check expected hist and miss to resultant hits and miss.
Definition
lru_cache.cpp:160
lru_tests::run_tests
static void run_tests()
A function to invoke all test cases.
Definition
lru_cache.cpp:239
lru_cache
Implementation of the LRU caching algorithm.
others
for vector
others
lru_cache.cpp
Generated by
1.18.0