Advertisement
yarin0600

Untitled

Nov 17th, 2023
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. struct Node
  2. {
  3.     int key;
  4.     int val;
  5. };
  6.  
  7. class LRUCache
  8. {
  9. public:
  10.     LRUCache(int capacity) : m_Capacity(capacity)
  11.     {
  12.         m_Size = 0;
  13.     }
  14.  
  15.     int get(int key)
  16.     {
  17.         // empty list/hashtable / not found key -> then return -1
  18.         if (lst.size() == 0 || hashTable.find(key) == hashTable.end())
  19.             return -1;
  20.         // now we need to retrieve the value to return and place this node at first spot on the list
  21.         // using 'splice'
  22.         std::list<Node>::iterator iteratorToKeyNode = hashTable[key];
  23.         // now we need to move the current Node to the beginning of the lst.
  24.         // we will do it by using splice
  25.         lst.splice(lst.begin(), lst, iteratorToKeyNode); // using splice to move the current node to the beginning of the lst.
  26.         return (*iteratorToKeyNode).val;
  27.     }
  28.  
  29.     void put(int key, int value)
  30.     {
  31.         Node newNode = { key, value };
  32.         // two cases:
  33.         // A1: Key already exists
  34.         if (hashTable.find(key) != hashTable.end())
  35.         {
  36.            std::list<Node>::iterator iteratorToExistedNode = hashTable[key];
  37.            (*iteratorToExistedNode).val = value;
  38.            lst.splice(lst.begin(), lst, iteratorToExistedNode); // using splice to move the current node to the beginning of the lst.
  39.         }
  40.         // B1: Key does not exist
  41.         else
  42.         {
  43.             // two cases:
  44.             // A2: there is room left in the cache:
  45.             if (m_Size < m_Capacity)
  46.             {
  47.                 m_Size++; // incrementing size
  48.                 // hashTable.emplace(std::make_pair(key, newNode)); // updating the hashTable
  49.                 // lst.push_front(newNode);                         // updating lst
  50.             }
  51.             // B2: there is no room left in the cache
  52.             else
  53.             {
  54.                 // we need to remove the least recently used node, which is the tail of the list
  55.                 Node nodeToBeRemoved = lst.back();
  56.                 lst.pop_back();
  57.                 hashTable.erase(nodeToBeRemoved.key);
  58.             }
  59.             // adding the new element
  60.             //hashTable.emplace(std::make_pair(key, newNode)); // updating the hashTable
  61.             lst.push_front(newNode);
  62.             hashTable.emplace(std::make_pair(key, lst.begin()));
  63.         }
  64.  
  65.     }
  66.  
  67. private:
  68.     int m_Size;
  69.     int m_Capacity;
  70.     std::list<Node> lst;
  71.     std::unordered_map<int, std::list<Node>::iterator> hashTable;
  72. };
  73.  
  74. /**
  75.  * Your LRUCache object will be instantiated and called as such:
  76.  * LRUCache* obj = new LRUCache(capacity);
  77.  * int param_1 = obj->get(key);
  78.  * obj->put(key,value);
  79.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement