首页 > 代码库 > c++ LRUCache
c++ LRUCache
#include <iostream>#include <string>#include <vector>#include <unordered_map>using namespace std;template<typename K, typename T>struct LRUCacheEntry{ K key; T data; LRUCacheEntry* prev; LRUCacheEntry* next;};template<typename K, typename T>class LRUCache{ private: unordered_map<K, LRUCacheEntry<K, T>*> _mapping; vector< LRUCacheEntry<K, T>* > _freeEntries; LRUCacheEntry<K, T> * head; LRUCacheEntry<K, T> * tail; LRUCacheEntry<K, T> * entries; public: LRUCache(size_t size) { entries = new LRUCacheEntry<K, T>[size]; for(int i = 0; i < size; i++ ) _freeEntries.push_back(entries + i ); head = new LRUCacheEntry<K, T>; tail = new LRUCacheEntry<K, T>; head->prev = NULL; head->next = tail; tail->next = NULL; tail->prev = head; } ~LRUCache() { delete head; delete tail; delete [] entries; } void put(K key, T data) { LRUCacheEntry<K, T>* node = _mapping[key]; if(node) { detach(node); node->data = http://www.mamicode.com/data;""; } private: void detach(LRUCacheEntry<K, T>* node) { node->prev->next = node->next; node->next->prev = node->prev; } void attach(LRUCacheEntry<K, T>* node) { node->next = head->next; node->prev = head; head->next = node; node->next->prev = node; }};int main(int argc, const char *argv[]){ unordered_map<int, int> map_; map_[9] = 999; cout << map_[9] << endl; cout << map_[10] << endl; LRUCache<int ,string> lru_cache(100); lru_cache.put(1, "one"); cout << lru_cache.get(1) << endl; if(lru_cache.get(2) == "") lru_cache.put(2, "two"); cout << lru_cache.get(2) << endl; return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。