LRUCache可以用于在内存中保持当前的热点数据,下面实现一个有大小限制的lru cache,相关如下:
1. 模板化;
2. 利用std::unordered_map实现o(1)查找,利用std::list实现o(1)删除 (双链表+hash表);
3. 用map保持key和结点在链表中的位置(iterator)
4. 需要同时考虑如下情况:
put操作:
(1) 如果当前key存在,则将对于的结点剪切到链表的头部,同时更新哈希表中value的值;
(2) 如果当前key不存在于hash表中,且元素个数已经达到最大值,则删除链表的最后一个结点,同时把新结点插入到链表的头部,同时更新hash表(增加新节点和删除旧结点表项);
get操作:
(1)检查当前hash表中是否有该key,如果存在,则将该key对应的结点move到list的头部,并同步更新map的value;
(2)如果hash表中不存在改key,则返回-1;
对应的代码如下:
#include <iostream>
#include <unordered_map>
#include <list>
#include <string>
#include <utility>
template<class KeyType, class ValueType>
class LRUCache {
public:
LRUCache(int capacity) : m_capacity(capacity) {}
int get(KeyType key, ValueType& value);//0-找到, -1-没找到
int put(KeyType key, ValueType value);// 0-插入成功, -1 -插入失败
void show() const; //展示当前LRUcache中的内容
private:
typedef std::pair<KeyType, ValueType> Node;
typedef typename std::list<Node>::iterator Iter;
std::list<Node> m_list;//双向链表
std::unordered_map<KeyType, Iter> m_map;//哈希表
int m_capacity;//cache的最大元素个数
};
template<class KeyType, class ValueType>
int LRUCache<KeyType, ValueType>::get(KeyType key, ValueType& value) {
if (m_map.count(key) <= 0) {
return -1;
}
//获取值
Iter iter = m_map[key];
//将值移到head
m_list.splice(m_list.begin(), m_list, iter);
return 0;
}
template<class KeyType, class ValueType>
int LRUCache<KeyType, ValueType>::put(KeyType key, ValueType value) {
//检查当前map中是否有该key,如果存在,则将该key对应的结点move到list的头部,并同步更新map的value
if (m_map.count(key) > 0) {
Iter iter = m_map[key];
iter->second = value; //充值map中key对应的value
m_list.splice(m_list.begin(), m_list, iter); //将对应的结点move到header
} else {
//检查当前元素是否达到容器的最大值,如果达到了阈值,则先将最后一个元素删除,将新元素插入到首部,同时更新map
Node node(key, value);
if (m_list.size() == m_capacity) {
m_map.erase(m_list.back().first);//将元素从map中删除
m_list.pop_back();//移除最后一个元素
}
//将新元素插入到map和list中
m_list.push_front(node);
m_map[key] = m_list.begin();
}
return 0;
}
template<class KeyType, class ValueType>
void LRUCache<KeyType, ValueType>::show() const {
auto iter = m_list.begin();
auto iter_end = m_list.end();
while (iter != iter_end) {
std::cout << "(" << iter->first << "," << iter->second << ") ";
iter++;
}
std::cout << std::endl;
}
int main(int argc, char* argv[]) {
LRUCache<std::string, int> cache(5);
//先插入4个元素
std::cout << "insert (aaa,111) (bbb,222) (ccc,333) (ddd,444) (eee,555)" << std::endl;
cache.put("aaa", 111);
cache.put("bbb", 222);
cache.put("ccc", 333);
cache.put("ddd", 444);
cache.put("eee", 555);
cache.show();
//访问元素ccc
int result = 0;
cache.get("ccc", result);
std::cout << "after visit ccc" << std::endl;
cache.show();
//再插入一个元素("fff",666)
cache.put("fff", 666);
std::cout << "insert (fff,666)" << std::endl;
cache.show();
return 0;
}
输出结果如下:
insert (aaa,111) (bbb,222) (ccc,333) (ddd,444) (eee,555)
(eee,555) (ddd,444) (ccc,333) (bbb,222) (aaa,111)
after visit ccc
(ccc,333) (eee,555) (ddd,444) (bbb,222) (aaa,111)
insert (fff,666)
(fff,666) (ccc,333) (eee,555) (ddd,444) (bbb,222)