MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / put

Method put

other/lfu_cache.py:251–278  ·  view source on GitHub ↗

Sets the value for the input key and updates the Double Linked List

(self, key: T, value: U)

Source from the content-addressed store, hash-verified

249 return None
250
251 def put(self, key: T, value: U) -> None:
252 """
253 Sets the value for the input key and updates the Double Linked List
254 """
255
256 if key not in self.cache:
257 if self.num_keys >= self.capacity:
258 # delete first node when over capacity
259 first_node = self.list.head.next
260
261 # guaranteed to have a non-None first node when num_keys > 0
262 # explain to type checker via assertions
263 assert first_node is not None
264 assert first_node.key is not None
265 assert self.list.remove(first_node) is not None
266 # first_node guaranteed to be in list
267
268 del self.cache[first_node.key]
269 self.num_keys -= 1
270 self.cache[key] = DoubleLinkedListNode(key, value)
271 self.list.add(self.cache[key])
272 self.num_keys += 1
273
274 else:
275 node = self.list.remove(self.cache[key])
276 assert node is not None # node guaranteed to be in list
277 node.val = value
278 self.list.add(node)
279
280 @classmethod
281 def decorator(

Callers 1

Calls 3

removeMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected