19
19
* @author [Karan Sharma](https://github.com/deDSeC00720)
20
20
*/
21
21
22
- #include < iostream>
23
- #include < unordered_map>
22
+ #include < cassert> // for assert
23
+ #include < iostream> // for IO operations
24
+ #include < unordered_map> // for hash map
24
25
26
+ /* *
27
+ * @namespace
28
+ * @brief Other algorithms
29
+ */
30
+ namespace others {
25
31
namespace Cache {
26
32
27
33
template <typename T>
@@ -31,7 +37,7 @@ class D_Node {
31
37
D_Node<T> *prev;
32
38
D_Node<T> *next;
33
39
34
- D_Node (T data) : data(data), prev(nullptr ), next(nullptr ) {}
40
+ explicit D_Node (T data) : data(data), prev(nullptr ), next(nullptr ) {}
35
41
};
36
42
37
43
template <typename K, typename V>
@@ -65,8 +71,9 @@ class LFUCache {
65
71
void push (int freq, CacheNode<K, V> *node) {
66
72
// if freq is not present, then make a new list with node as the head as
67
73
// well as tail.
68
- if (!freq_map.count (key )) {
74
+ if (!freq_map.count (freq )) {
69
75
freq_map[freq] = {node, node};
76
+ return ;
70
77
}
71
78
72
79
std::pair<CacheNode<K, V> *, CacheNode<K, V> *> &p = freq_map[freq];
@@ -116,7 +123,7 @@ class LFUCache {
116
123
}
117
124
}
118
125
push (freq + 1 , node);
119
- p_node.second ++ ;
126
+ ++ p_node.second ;
120
127
}
121
128
122
129
/* *
@@ -177,7 +184,7 @@ class LFUCache {
177
184
*/
178
185
V get (K key) {
179
186
if (!node_map.count (key)) {
180
- throw std::exception (" key is not present in the cache" );
187
+ throw std::runtime_error (" key is not present in the cache" );
181
188
}
182
189
183
190
// increase the frequency and return the value
@@ -190,19 +197,19 @@ class LFUCache {
190
197
* @brief Returns the number of items present in the cache.
191
198
* @return number of items in the cache
192
199
*/
193
- int size () { return node_map.size (); }
200
+ int size () const { return node_map.size (); }
194
201
195
202
/* *
196
203
* @brief Returns the total capacity of the cache
197
204
* @return Total capacity of the cache
198
205
*/
199
- int capacity () { return _capacity; }
206
+ int capacity () const { return _capacity; }
200
207
201
208
/* *
202
209
* @brief returns true if the cache is empty, false otherwise.
203
210
* @return true if the cache is empty, false otherwise.
204
211
*/
205
- bool empty () { return node_map.size () == 0 ; }
212
+ bool empty () const { return node_map.empty () ; }
206
213
207
214
/* *
208
215
* @brief destructs the cache, iterates on the map and deletes every node
@@ -212,8 +219,80 @@ class LFUCache {
212
219
auto it = node_map.begin ();
213
220
while (it != node_map.end ()) {
214
221
delete it->second .first ;
215
- it++ ;
222
+ ++it ;
216
223
}
217
224
}
218
225
};
219
226
} // namespace Cache
227
+ } // namespace others
228
+
229
+ /* *
230
+ * @brief A simple test case
231
+ * @return void
232
+ */
233
+ void test1 () {
234
+ others::Cache::LFUCache<int , int > cache (5 );
235
+ assert (cache.size () == 0 );
236
+ assert (cache.capacity () == 5 );
237
+ assert (cache.empty ());
238
+ cache.put (1 , 10 );
239
+ cache.put (2 , 20 );
240
+
241
+ assert (cache.size () == 2 );
242
+ assert (cache.capacity () == 5 );
243
+ assert (!cache.empty ());
244
+
245
+ cache.put (3 , 30 );
246
+ cache.put (4 , 40 );
247
+ cache.put (5 , 50 );
248
+ cache.put (6 , 60 );
249
+
250
+ assert (cache.size () == 5 );
251
+ assert (cache.capacity () == 5 );
252
+ assert (!cache.empty ());
253
+
254
+ std::cout << " test1 - passed\n " ;
255
+ }
256
+
257
+ /* *
258
+ * @brief A simple test case
259
+ * @return void
260
+ */
261
+ void test2 () {
262
+ others::Cache::LFUCache<int , int > cache (5 );
263
+ cache.put (1 , 10 );
264
+ cache.put (2 , 20 );
265
+ cache.put (3 , 30 );
266
+
267
+ assert (cache.get (1 ) == 10 );
268
+ cache.get (2 );
269
+ cache.get (3 );
270
+
271
+ cache.put (4 , 40 );
272
+ cache.put (5 , 50 );
273
+ cache.put (6 , 60 );
274
+
275
+ assert (cache.get (5 ) == 50 );
276
+
277
+ std::cout << " test2 - passed\n " ;
278
+ }
279
+
280
+ /* *
281
+ * @brief test method with 2 tests
282
+ * @return void
283
+ */
284
+
285
+ void test () {
286
+ test1 ();
287
+ test2 ();
288
+ }
289
+
290
+ /* *
291
+ * @brief main function
292
+ * @return 0 on exit
293
+ */
294
+
295
+ int main () {
296
+ test ();
297
+ return 0 ;
298
+ }
0 commit comments