TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
others::Cache::LRUCache< K, V > Class Template Reference

LRUCache. More...

Collaboration diagram for others::Cache::LRUCache< K, V >:
[legend]

Public Member Functions

 LRUCache (int _capacity)
 Constructor, Initialize the head and tail pointers to nullptr and initialize the _capacity of the cache.
 
void put (K key, V value)
 upsert a key-value pair
 
get (K key)
 get the value of the key-value pair if exists
 
int size () const
 Returns the number of items present in the cache.
 
int capacity () const
 Returns the total capacity of the cache.
 
bool empty () const
 returns whether the cache is empty or not
 
 ~LRUCache ()
 destructs the cache, iterates on the map and deletes every node present in the cache.
 

Private Member Functions

void push_front (CacheNode< K, V > *node_ptr)
 push the node to the front of the linked list.
 
void make_recent (CacheNode< K, V > *node_ptr)
 move the existing node in the list to the beginning of the list.
 
void pop_back ()
 pop the last node in the linked list.
 

Private Attributes

CacheNode< K, V > * head
 head of the doubly linked list
 
CacheNode< K, V > * tail
 tail of the doubly linked list
 
std::uint32_t _capacity
 maximum capacity of the cache
 
std::unordered_map< K, CacheNode< K, V > * > node_map
 maps the key to the node address
 

Detailed Description

template<typename K, typename V>
class others::Cache::LRUCache< K, V >

LRUCache.

Template Parameters
Ktype of key in the LRU
Vtype of value in the LRU

Definition at line 61 of file lru_cache2.cpp.

Constructor & Destructor Documentation

◆ LRUCache()

template<typename K , typename V >
others::Cache::LRUCache< K, V >::LRUCache ( int _capacity)
inlineexplicit

Constructor, Initialize the head and tail pointers to nullptr and initialize the _capacity of the cache.

Parameters
_capacityTotal capacity of the cache

Definition at line 75 of file lru_cache2.cpp.

76 : head(nullptr), tail(nullptr), _capacity(_capacity) {}
CacheNode< K, V > * head
head of the doubly linked list
CacheNode< K, V > * tail
tail of the doubly linked list
std::uint32_t _capacity
maximum capacity of the cache

◆ ~LRUCache()

template<typename K , typename V >
others::Cache::LRUCache< K, V >::~LRUCache ( )
inline

destructs the cache, iterates on the map and deletes every node present in the cache.

Definition at line 205 of file lru_cache2.cpp.

205 {
206 auto it = node_map.begin();
207 while (it != node_map.end()) {
208 delete it->second;
209 ++it;
210 }
211 }
std::unordered_map< K, CacheNode< K, V > * > node_map
maps the key to the node address

Member Function Documentation

◆ capacity()

template<typename K , typename V >
int others::Cache::LRUCache< K, V >::capacity ( ) const
inline

Returns the total capacity of the cache.

Returns
Total capacity of the cache

Definition at line 193 of file lru_cache2.cpp.

193{ return _capacity; }

◆ empty()

template<typename K , typename V >
bool others::Cache::LRUCache< K, V >::empty ( ) const
inline

returns whether the cache is empty or not

Returns
true if the cache is empty, false otherwise.

Definition at line 199 of file lru_cache2.cpp.

199{ return node_map.empty(); }

◆ get()

template<typename K , typename V >
V others::Cache::LRUCache< K, V >::get ( K key)
inline

get the value of the key-value pair if exists

Parameters
keykey of the key-value pair
Returns
the value mapped to the given key
Exceptions
exceptionis thrown if the key is not present in the cache

Definition at line 172 of file lru_cache2.cpp.

172 {
173 if (!node_map.count(key)) {
174 throw std::runtime_error("key is not present in the cache");
175 }
176
177 // move node to the beginning of the list
178 V value = node_map[key]->data.second;
179 make_recent(node_map[key]);
180 return value;
181 }
void make_recent(CacheNode< K, V > *node_ptr)
move the existing node in the list to the beginning of the list.

◆ make_recent()

template<typename K , typename V >
void others::Cache::LRUCache< K, V >::make_recent ( CacheNode< K, V > * node_ptr)
inlineprivate

move the existing node in the list to the beginning of the list.

Parameters
node_ptrnode to be moved to the beginning.

Definition at line 99 of file lru_cache2.cpp.

99 {
100 if (head == node_ptr) {
101 return;
102 }
103
104 CacheNode<K, V> *prev = node_ptr->prev;
105 CacheNode<K, V> *next = node_ptr->next;
106
107 prev->next = next;
108 if (next) {
109 next->prev = prev;
110 } else {
111 tail = prev;
112 }
113
114 node_ptr->prev = nullptr;
115 node_ptr->next = nullptr;
116 push_front(node_ptr);
117 }
void push_front(CacheNode< K, V > *node_ptr)
push the node to the front of the linked list.

◆ pop_back()

template<typename K , typename V >
void others::Cache::LRUCache< K, V >::pop_back ( )
inlineprivate

pop the last node in the linked list.

Definition at line 122 of file lru_cache2.cpp.

122 {
123 if (!head) {
124 return;
125 }
126 if (head == tail) {
127 delete head;
128 head = nullptr;
129 tail = nullptr;
130 return;
131 }
132
133 CacheNode<K, V> *temp = tail;
134 tail = tail->prev;
135 tail->next = nullptr;
136 delete temp;
137 }

◆ push_front()

template<typename K , typename V >
void others::Cache::LRUCache< K, V >::push_front ( CacheNode< K, V > * node_ptr)
inlineprivate

push the node to the front of the linked list.

Parameters
node_ptrthe node to be pushed

Definition at line 83 of file lru_cache2.cpp.

83 {
84 if (!head) {
85 head = node_ptr;
86 tail = node_ptr;
87 return;
88 }
89
90 node_ptr->next = head;
91 head->prev = node_ptr;
92 head = node_ptr;
93 }

◆ put()

template<typename K , typename V >
void others::Cache::LRUCache< K, V >::put ( K key,
V value )
inline

upsert a key-value pair

Parameters
keykey of the key-value pair
valuevalue of the key-value pair

Definition at line 145 of file lru_cache2.cpp.

145 {
146 // update the value if key already exists
147 if (node_map.count(key)) {
148 node_map[key]->data.second = value;
149 make_recent(node_map[key]);
150 return;
151 }
152
153 // if the cache is full
154 // remove the least recently used item
155 if (node_map.size() == _capacity) {
156 node_map.erase(tail->data.first);
157 pop_back();
158 }
159
160 CacheNode<K, V> *newNode = new CacheNode<K, V>({key, value});
161
162 node_map[key] = newNode;
163 push_front(newNode);
164 }
void pop_back()
pop the last node in the linked list.

◆ size()

template<typename K , typename V >
int others::Cache::LRUCache< K, V >::size ( ) const
inline

Returns the number of items present in the cache.

Returns
number of items in the cache

Definition at line 187 of file lru_cache2.cpp.

187{ return node_map.size(); }

Member Data Documentation

◆ _capacity

template<typename K , typename V >
std::uint32_t others::Cache::LRUCache< K, V >::_capacity
private

maximum capacity of the cache

Definition at line 64 of file lru_cache2.cpp.

◆ head

template<typename K , typename V >
CacheNode<K, V>* others::Cache::LRUCache< K, V >::head
private

head of the doubly linked list

Definition at line 62 of file lru_cache2.cpp.

◆ node_map

template<typename K , typename V >
std::unordered_map<K, CacheNode<K, V> *> others::Cache::LRUCache< K, V >::node_map
private

maps the key to the node address

Definition at line 67 of file lru_cache2.cpp.

◆ tail

template<typename K , typename V >
CacheNode<K, V>* others::Cache::LRUCache< K, V >::tail
private

tail of the doubly linked list

Definition at line 63 of file lru_cache2.cpp.


The documentation for this class was generated from the following file: