Design and implement a data structure for Least recently Used (LRU) cache. It should support the following operations: get
and set
.
get(key)
-Get The value ('ll always be positive) of the key if the key exists in the cache, otherwise return-1.
set(key, value)
-Set or insert the value if the key is not already present. When the cache is reached its capacity, it should invalidate the least recently used item before inserting a new item.
Hide TagsData Structure
Analysis
To make it more efficient to find, insert, and delete, we use a doubly linked list (std::list) and a hash table
(Std::unordered_map), because:
• Hash table saves the address of each node, and can basically guarantee to find the node within O (1) Time
• High efficiency in the insertion and deletion of doubly linked lists, and finding the node's precursor node when inserting and deleting one-way lists
Specific implementation Details:
• The closer to the list header indicates that the node's last access distance is now shortest, and the tail node represents the least recently accessed
• When accessing a node, if the node exists, swap the node to the head of the list and update the address of that node in the hash table
• When inserting a node, if the cache size reaches the upper limit of capacity, delete the tail node, and in the hash table
The new node is inserted into the list header, except for the corresponding item;
See http://www.cnblogs.com/diegodu/p/4569048.html http://www.cnblogs.com/dolphin0520/p/3741519.html for more analysis
structcachenode{intkey; intVal; Cachenode (intKintv) {key=K; Val=v; }};classlrucache{Private: intm_capacity; List<cacheNode> m_list;//double link of Cachenodemap<int, List<cachenode>::iterator > M_map;//map of key and List::iterator Public: LRUCache (intcapacity) {m_capacity=capacity; } int Get(intkey) { if(M_map.find (key) = =M_map.end ()) { return-1; } Else { //move the node to head of double listList<cachenode>::iterator it =M_map[key]; M_list.splice (M_list.begin (), m_list, it); returnM_list.begin ()Val; } } void Set(intKeyintvalue) { if(M_map.find (key) = =M_map.end ()) { //Delete the back one if reach capacity if(M_capacity = =m_list.size ()) {Cachenode tmp=M_list.back (); M_list.pop_back (); M_map.erase (Tmp.key); } //Insert new one into the headcachenode node (key, value); M_list.push_front (node); M_map[key]=M_list.begin (); } Else { //move the node to head of double listList<cachenode>::iterator it =M_map[key]; M_list.splice (M_list.begin (), m_list, it); //Update ValueM_list.begin ()->val =value; } } voidPrintcache () { for(List<cachenode>::iterator it = M_list.begin (); It! = M_list.end (); it++) {cout<<"key:\t"<< It->key <<"\tvalue\t"<< It->val <<Endl; } cout<<Endl; }};intMain () {LRUCache cache (5); Cache.Set(1,1); Cache.Set(2,2); Cache.Set(3,4); Cache.Set(4,4); Cache.Set(5,5); Cache.printcache (); Cache.Set(6,6); Cache.printcache (); Cache.Set(2,9); Cache.printcache (); Cache.Set(3,3); Cache.printcache (); Cache.Get(5); Cache.printcache (); return 0;}
[Leetcode] LRU Cache