Covalent Bond  0.0.1-alpha
'covalent bond' is a data middle office. As a 2022-2023 Fall SWE final project.
cbLRUCache.hpp
Go to the documentation of this file.
1 
11 #include <map>
12 #include <list>
13 #include "../../pch.hpp"
14 
19 #define find_list for (auto i = m_cacheList.begin(); i != m_cacheList.end(); i++)
20 
21 namespace cb {
26 struct listNode {
27  std::string key;
28 };
29 
30 typedef listNode node;
31 
38 template<typename V>
39 class cbLRUCache {
40  public:
46  cbLRUCache(int32_t max_size) : m_maxSize(max_size), m_sizeCounter(0), m_bufNode(nullptr) {}
47 
52  ~cbLRUCache() { delete m_bufNode; }
53 
61  void push(std::string key, V value) {
62  find_list {
63  if (i->key == key) {
64  m_cacheMap[key] = value;
65  this->update(key);
66  return;
67  }
68  }
69 
70  m_sizeCounter++;
71  if (m_sizeCounter > m_maxSize) {
73  this->eraseTail();
74  }
75  m_cacheMap.insert({key, value});
76  this->insertTop(key);
77  }
78 
86  V* get(const std::string& key) {
87  if (m_cacheMap.count(key)) {
88  this->update(key);
89  V* exit = nullptr;
90  exit = &m_cacheMap[key];
91  // this->__loop_map();
92  return exit;
93  }
94  V* not_exit = nullptr;
95  return not_exit;
96  }
97 
98  private:
103  void eraseTail() {
104  m_bufNode = &m_cacheList.back();
106  auto buf_ptr = m_cacheMap.find(m_bufKey);
107  m_cacheMap.erase(buf_ptr);
108  m_cacheList.pop_back();
109  }
110 
117  void update(const std::string& key) {
118  find_list {
119  if (i->key == key) {
120  m_cacheList.erase(i);
121  break;
122  }
123  }
124  this->insertTop(key);
125  }
126 
132  void insertTop(const std::string& key) {
133  m_bufNode = new (node);
134  m_bufNode->key = key;
135  m_cacheList.push_front(*m_bufNode);
136  delete (m_bufNode);
137  m_bufNode = nullptr;
138  }
139 
144  [[deprecated("For debug only")]] void __loop_map() {
145  std::string bs;
146  int32_t n = 0;
147  for (auto i = m_cacheList.begin(); i != m_cacheList.end(); i++) {
148  bs = i->key;
149  std::cout << "The" << n << "th element" << bs << " : " << m_cacheMap[bs] << std::endl;
150  n++;
151  }
152  }
153 
154  private:
155  int32_t m_maxSize;
156  int32_t m_sizeCounter;
158  std::string m_bufKey;
159  std::list<node> m_cacheList;
160  std::map<std::string, V> m_cacheMap;
161 };
162 } // namespace cb
cb::cbLRUCache::cbLRUCache
cbLRUCache(int32_t max_size)
Construct a new cb Least Recently Use Cache object.
Definition: cbLRUCache.hpp:46
cb::cbLRUCache::__loop_map
void __loop_map()
loop in cachae list and map
Definition: cbLRUCache.hpp:144
cb::listNode
Elements of list.
Definition: cbLRUCache.hpp:26
cb::cbLRUCache::insertTop
void insertTop(const std::string &key)
transit the node choosen to the top of list
Definition: cbLRUCache.hpp:132
cb::listNode::key
std::string key
Definition: cbLRUCache.hpp:27
cb
_WIN32
Definition: api.cpp:4
cb::cbLRUCache::get
V * get(const std::string &key)
Get the value of corresponding key of input, if key doesn't exist, it will return null pointer.
Definition: cbLRUCache.hpp:86
find_list
#define find_list
Macro define of loop in cache list.
Definition: cbLRUCache.hpp:19
cb::cbLRUCache::m_bufNode
node * m_bufNode
Definition: cbLRUCache.hpp:157
cb::cbLRUCache::m_maxSize
int32_t m_maxSize
Definition: cbLRUCache.hpp:155
cb::cbLRUCache::m_cacheList
std::list< node > m_cacheList
Definition: cbLRUCache.hpp:159
cb::cbLRUCache::push
void push(std::string key, V value)
Push key and value into cache. If key has been existed, the corresponding value will be update.
Definition: cbLRUCache.hpp:61
cb::cbLRUCache::update
void update(const std::string &key)
move the node has been used recently to the top of cache list
Definition: cbLRUCache.hpp:117
cb::cbLRUCache::eraseTail
void eraseTail()
delete the last element of list
Definition: cbLRUCache.hpp:103
cb::cbLRUCache
We used LRU alogrithm to implement our cache.
Definition: cbLRUCache.hpp:39
cb::cbLRUCache::m_sizeCounter
int32_t m_sizeCounter
Definition: cbLRUCache.hpp:156
cb::cbLRUCache::~cbLRUCache
~cbLRUCache()
Destroy the cb Least Recently Use Cache object.
Definition: cbLRUCache.hpp:52
cb::cbLRUCache::m_cacheMap
std::map< std::string, V > m_cacheMap
Definition: cbLRUCache.hpp:160
cb::cbLRUCache::m_bufKey
std::string m_bufKey
Definition: cbLRUCache.hpp:158
cb::node
listNode node
Definition: cbLRUCache.hpp:30