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
5119int 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;
0 commit comments