This is not the best way to implement LRU Cache class. I just implement it for hackerrank problem. Concept of LRU Cache is not difficult but implement doubly linked list was a little bit hard without compiler :^O
------
// inheritance from the Cache class
class LRUCache : public Cache
{
public:
LRUCache(int _capacity)
: capacity(_capacity)
, count(0)
{
head = nullptr;
tail = nullptr;
}
void set(int key, int value) override
{
auto iter = mp.find(key);
bool keyExist = false;
if (iter != mp.end())
{
keyExist = true;
}
// if key is exist then update LRU
if (keyExist)
{
// for instance there are 3 2 1 5 6
// and then found 1
// data will be 1 3 2 5 6
// doesn't change any data size, just update head, tail pointer and map.
if (head == iter->second)
{
// update value.
iter->second->value = value;
}
else if (tail == iter->second)
{
Node* beforeTail = tail->prev;
Node* afterHead = head->next;
Node* backupHead = head;
head = tail;
tail = backupHead;
beforeTail->next = tail;
tail->prev = beforeTail;
tail->next = nullptr;
afterHead->prev = head;
head->next = afterHead;
head->prev = nullptr;
// update map pointer and value.
iter->second = head;
iter->second->value = value;
}
else
{
// value is in between head and tail.
Node* backupPrev = iter->second->prev;
Node* backupNext = iter->second->next;
backupPrev->next = backupNext;
backupNext->prev = backupPrev;
head->prev = iter->second;
iter->second->next = head;
head = iter->second;
iter->second = head;
iter->second->value = value;
}
}
else
{
// if key is not in the map then insert it and based on the capacity
// remove oldest one.
if (count >= capacity)
{
// insert new one into head
Node* node = new Node(key, value);
node->next = head;
head->prev = node;
head = node;
mp.insert(make_pair(key, head));
// remove old one
// first remove old one in the map and then update tail pointer.
Node* tailPrev = tail->prev;
tail->prev->next = nullptr;
mp.erase(tail->key);
delete tail;
tail = tailPrev;
}
else
{
// add new one and update count
if (head == nullptr)
{
head = new Node(key, value);
tail = head;
mp.insert(make_pair(key, head));
}
else
{
// update head
Node* node = new Node(key, value);
node->next = head;
head->prev = node;
head = node;
mp.insert(make_pair(key, head));
}
count++;
}
}
}
int get(int key) override
{
// if the key is in the cache then print the value
// otherwise print -1 (if the key is not in the cache)
auto iter = mp.find(key);
if (iter != mp.end())
{
return iter->second->value;
}
return -1;
}
private:
int capacity;
int count;
};
Subscribe to:
Post Comments (Atom)
Task in UnrealEngine
https://www.youtube.com/watch?v=1lBadANnJaw
-
Unity released very good FPS example for people and I decided to analysis how they make this. Personally I wanted to show you how I analys...
-
When we use DrawDebugSphere function for debugging, it is working well but when you are trying to use it in anim node's function it will...
-
If you press a key 'L' in the jupyter notebook then you can see the line number in the editor.
No comments:
Post a Comment