Returns the value for the input key and updates the Double Linked List. Returns None if key is not present in cache
(self, key: T)
| 244 | return key in self.cache |
| 245 | |
| 246 | def get(self, key: T) -> U | None: |
| 247 | """ |
| 248 | Returns the value for the input key and updates the Double Linked List. |
| 249 | Returns None if key is not present in cache |
| 250 | """ |
| 251 | # Note: pythonic interface would throw KeyError rather than return None |
| 252 | |
| 253 | if key in self.cache: |
| 254 | self.hits += 1 |
| 255 | value_node: DoubleLinkedListNode[T, U] = self.cache[key] |
| 256 | node = self.list.remove(self.cache[key]) |
| 257 | assert node == value_node |
| 258 | |
| 259 | # node is guaranteed not None because it is in self.cache |
| 260 | assert node is not None |
| 261 | self.list.add(node) |
| 262 | return node.val |
| 263 | self.miss += 1 |
| 264 | return None |
| 265 | |
| 266 | def put(self, key: T, value: U) -> None: |
| 267 | """ |