Advertisement
yarin0600

Untitled

Nov 17th, 2023
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. class LRUCache
  2. {
  3. public:
  4.    LRUCache(int capacity) : _capacity(capacity)
  5.    {
  6.       _dummy_head = new Node(-1, -1);
  7.       _dummy_tail = new Node(-1, -1);
  8.       _dummy_head->_next = _dummy_tail;
  9.       _dummy_tail->_prev = _dummy_head;
  10.    }
  11.  
  12.    ~LRUCache()
  13.    {
  14.       delete _dummy_head;
  15.       delete _dummy_tail;
  16.    }
  17.  
  18.    int get(int key)
  19.    {
  20.       int result = -1;
  21.       // two options:
  22.       // case A: key exists
  23.       // if (this->_map.contains(key))
  24.       if (this->_map.find(key) != this->_map.end())
  25.       {
  26.          Node *resNode = this->_map[key];
  27.  
  28.          // first remove it
  29.          this->removeNode(resNode);
  30.          this->addNode(resNode);
  31.          this->_map[key] = this->_dummy_head->_next;
  32.  
  33.          result = this->_map[key]->_value;
  34.       }
  35.       // case B: key is missing
  36.       return result;
  37.    }
  38.  
  39.    void put(int key, int value)
  40.    {
  41.       // Case A:
  42.       // key is already here
  43.       if (this->_map.find(key) != this->_map.end())
  44.       {
  45.          Node *currentNode = this->_map[key];
  46.          removeNode(currentNode);
  47.       }
  48.       // Case B:
  49.       // map is full and we need to delete the last node [remember, head and tail are dummy head / tail...]
  50.       else if (this->_map.size() == _capacity)
  51.       {
  52.          this->_map.erase(this->_dummy_tail->_prev->_key);
  53.          removeNode(this->_dummy_tail->_prev);
  54.       }
  55.       this->addNode(new Node(key, value));
  56.       this->_map[key] = this->_dummy_head->_next;
  57.    }
  58.  
  59. private:
  60.    struct Node
  61.    {
  62.       Node *_prev;
  63.       Node *_next;
  64.       int _key;
  65.       int _value;
  66.       Node(int key, int value) : _key(key), _value(value) {}
  67.    };
  68.  
  69.    void addNode(Node *newNode)
  70.    {
  71.       Node *tmp = this->_dummy_head->_next;
  72.       this->_dummy_head->_next = newNode;
  73.       newNode->_next = tmp;
  74.       tmp->_prev = newNode;
  75.       newNode->_prev = this->_dummy_head;
  76.    }
  77.  
  78.    void removeNode(Node *delNode)
  79.    {
  80.       Node *prevNode = delNode->_prev;
  81.       Node *nextNode = delNode->_next;
  82.  
  83.       prevNode->_next = nextNode;
  84.       nextNode->_prev = prevNode;
  85.    }
  86.  
  87.    std::unordered_map<int, Node *> _map;
  88.    int _capacity;
  89.    Node *_dummy_head;
  90.    Node *_dummy_tail;
  91. };
  92. /**
  93.  * Your LRUCache object will be instantiated and called as such:
  94.  * LRUCache* obj = new LRUCache(capacity);
  95.  * int param_1 = obj->get(key);
  96.  * obj->put(key,value);
  97.  */
  98. Console
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement