Skip to content

Commit 351bc70

Browse files
committed
splitting cache header and driver
1 parent 0712fb1 commit 351bc70

File tree

2 files changed

+55
-35
lines changed

2 files changed

+55
-35
lines changed

01-basics/cache.cc

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,14 @@
66
//
77
//-----------------------------------------------------------------------------
88
//
9-
// Example for LRU cache in C++
9+
// Example for LRU cache in C++: simple driver program
1010
//
1111
//----------------------------------------------------------------------------
1212

1313
#include <cassert>
1414
#include <iostream>
15-
#include <iterator>
16-
#include <list>
17-
#include <unordered_map>
18-
#include <vector>
1915

20-
template <typename T, typename KeyT = int> struct cache_t {
21-
size_t sz_;
22-
std::list<T> cache_;
23-
24-
using ListIt = typename std::list<T>::iterator;
25-
std::unordered_map<KeyT, ListIt> hash_;
26-
27-
cache_t(size_t sz) : sz_(sz) {}
28-
29-
bool full() const { return (cache_.size() == sz_); }
30-
31-
template <typename F> bool lookup_update(KeyT key, F slow_get_page) {
32-
auto hit = hash_.find(key);
33-
if (hit == hash_.end()) {
34-
if (full()) {
35-
hash_.erase(cache_.back());
36-
cache_.pop_back();
37-
}
38-
cache_.push_front(slow_get_page(key));
39-
hash_[key] = cache_.begin();
40-
return false;
41-
}
42-
43-
auto eltit = hit->second;
44-
if (eltit != cache_.begin())
45-
cache_.splice(cache_.begin(), cache_, eltit, std::next(eltit));
46-
return true;
47-
}
48-
};
16+
#include "cache.hpp"
4917

5018
// slow get page imitation
5119
int slow_get_page_int(int key) { return key; }
@@ -57,7 +25,7 @@ int main() {
5725

5826
std::cin >> m >> n;
5927
assert(std::cin.good());
60-
cache_t<int> c{m};
28+
caches::cache_t<int> c{m};
6129

6230
for (int i = 0; i < n; ++i) {
6331
int q;

01-basics/cache.hpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//-----------------------------------------------------------------------------
2+
//
3+
// Source code for MIPT ILab
4+
// Slides: https://sourceforge.net/projects/cpp-lects-rus/files/cpp-graduate/
5+
// Licensed after GNU GPL v3
6+
//
7+
//-----------------------------------------------------------------------------
8+
//
9+
// Example for LRU cache in C++
10+
//
11+
//----------------------------------------------------------------------------
12+
13+
#pragma once
14+
15+
#include <iterator>
16+
#include <list>
17+
#include <unordered_map>
18+
#include <vector>
19+
20+
namespace caches {
21+
22+
template <typename T, typename KeyT = int> struct cache_t {
23+
size_t sz_;
24+
std::list<T> cache_;
25+
26+
using ListIt = typename std::list<T>::iterator;
27+
std::unordered_map<KeyT, ListIt> hash_;
28+
29+
cache_t(size_t sz) : sz_(sz) {}
30+
31+
bool full() const { return (cache_.size() == sz_); }
32+
33+
template <typename F> bool lookup_update(KeyT key, F slow_get_page) {
34+
auto hit = hash_.find(key);
35+
if (hit == hash_.end()) {
36+
if (full()) {
37+
hash_.erase(cache_.back());
38+
cache_.pop_back();
39+
}
40+
cache_.push_front(slow_get_page(key));
41+
hash_[key] = cache_.begin();
42+
return false;
43+
}
44+
45+
auto eltit = hit->second;
46+
if (eltit != cache_.begin())
47+
cache_.splice(cache_.begin(), cache_, eltit, std::next(eltit));
48+
return true;
49+
}
50+
};
51+
52+
} // namespace caches

0 commit comments

Comments
 (0)