Advertisement
Guest User

Custom cache

a guest
Apr 4th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <string>
  4.  
  5. template<typename T, typename U>
  6. struct custom_cache{
  7.     struct Node {
  8.         Node() = default;
  9.         Node(T key, U value) : key(key), value(value) {}
  10.         T key;
  11.         U value;
  12.         Node* next = nullptr;
  13.         Node* prev = nullptr;
  14.     };
  15.  
  16.     unsigned int N = 0;
  17.     unsigned int amount = 0;
  18.     custom_cache(unsigned int N) : N(N) {}
  19.  
  20.     std::unordered_map<T, Node*> addresses;
  21.     Node* begin = nullptr;
  22.     Node* end = nullptr;
  23.  
  24.     U get(T key) {
  25.         if (!begin || !end || addresses.count(key) == 0) {
  26.             return NULL;
  27.         }
  28.         if (addresses[key]->next) {
  29.             addresses[key]->next->prev = addresses[key]->prev;
  30.         }
  31.         if (addresses[key]->prev) {
  32.             addresses[key]->prev->next = addresses[key]->next;
  33.         }
  34.         begin->next = addresses[key];
  35.         addresses[key]->next = nullptr;
  36.         addresses[key]->prev = begin;
  37.         begin = addresses[key];
  38.         return begin->value;
  39.     }
  40.  
  41.     void put(T key, U value) {
  42.         if (addresses.count(key) != 0) {
  43.             return;
  44.         }
  45.         amount++;
  46.         auto node = new Node(key, value);
  47.         addresses[key] = node;
  48.         if (!begin || !end) {
  49.             begin = end = node;
  50.         } else {
  51.             node->prev = begin;
  52.             begin->next = node;
  53.             begin = node;
  54.             if (amount > N) {
  55.                 amount = N;
  56.                 addresses[end->key] = end->next->prev = nullptr;
  57.                 delete end->next->prev;
  58.                 end = end->next;
  59.             }
  60.         }
  61.     }
  62.  
  63.     void printList() {
  64.         auto node = begin;
  65.         while (node) {
  66.             std::cout << node->key << ' ' << node->value << '\n';
  67.             node = node->prev;
  68.         }
  69.     }
  70. };
  71.  
  72. void getAll(custom_cache<int, std::string>* cache) {
  73.     std::cout << cache->get(3) << ' ';
  74.     std::cout << cache->get(4) << ' ';
  75.     std::cout << cache->get(5) << '\n';
  76. }
  77.  
  78. void test1() {
  79.     int a = 3, b = 4, c = 5;
  80.     std::string s1 = "a", s2 = "b", s3 = "c";
  81.     auto cache = custom_cache<int, std::string>(3);
  82.     cache.put(a, s1);
  83.     cache.put(b, s2);
  84.     cache.put(c, s3);
  85.     getAll(&cache);
  86.     cache.printList();
  87. }
  88.  
  89. void test2() {
  90.     int a = 3, b = 4, c = 5;
  91.     std::string s1 = "a", s2 = "b", s3 = "c";
  92.     auto cache = custom_cache<int, std::string>(2);
  93.     cache.put(a, s1);
  94.     cache.put(b, s2);
  95.     cache.put(c, s3);
  96.     cache.printList();
  97. }
  98.  
  99. void test3() {
  100.     int a = 3, b = 4, c = 5;
  101.     std::string s1 = "a", s2 = "b", s3 = "c";
  102.     auto cache = custom_cache<int, std::string>(1);
  103.     cache.put(a, s1);
  104.     cache.put(b, s2);
  105.     cache.put(c, s3);
  106.     cache.printList();
  107. }
  108.  
  109. int main() {
  110.     test1();
  111.     std::cout << "\n\n";
  112.     test2();
  113.     std::cout << "\n\n";
  114.     test3();
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement