2021-11-16 14:01:17 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-10-28 18:04:26 +00:00
|
|
|
import sys
|
|
|
|
from collections import deque
|
2021-11-16 14:01:17 +00:00
|
|
|
from typing import Generic, TypeVar
|
|
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
|
|
|
|
|
|
class LRUCache(Generic[T]):
|
|
|
|
"""
|
|
|
|
Page Replacement Algorithm, Least Recently Used (LRU) Caching.
|
|
|
|
|
|
|
|
>>> lru_cache: LRUCache[str | int] = LRUCache(4)
|
|
|
|
>>> lru_cache.refer("A")
|
|
|
|
>>> lru_cache.refer(2)
|
|
|
|
>>> lru_cache.refer(3)
|
|
|
|
|
|
|
|
>>> lru_cache
|
|
|
|
LRUCache(4) => [3, 2, 'A']
|
2019-10-28 18:04:26 +00:00
|
|
|
|
2021-11-16 14:01:17 +00:00
|
|
|
>>> lru_cache.refer("A")
|
|
|
|
>>> lru_cache
|
|
|
|
LRUCache(4) => ['A', 3, 2]
|
2019-11-14 18:59:43 +00:00
|
|
|
|
2021-11-16 14:01:17 +00:00
|
|
|
>>> lru_cache.refer(4)
|
|
|
|
>>> lru_cache.refer(5)
|
|
|
|
>>> lru_cache
|
|
|
|
LRUCache(4) => [5, 4, 'A', 3]
|
2019-10-28 18:04:26 +00:00
|
|
|
|
2021-11-16 14:01:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
dq_store: deque[T] # Cache store of keys
|
|
|
|
key_reference: set[T] # References of the keys in cache
|
2019-11-14 18:59:43 +00:00
|
|
|
_MAX_CAPACITY: int = 10 # Maximum capacity of cache
|
2019-10-28 18:04:26 +00:00
|
|
|
|
2021-11-16 14:01:17 +00:00
|
|
|
def __init__(self, n: int) -> None:
|
2020-09-10 08:31:26 +00:00
|
|
|
"""Creates an empty store and map for the keys.
|
2019-10-28 18:04:26 +00:00
|
|
|
The LRUCache is set the size n.
|
|
|
|
"""
|
|
|
|
self.dq_store = deque()
|
2021-11-16 14:01:17 +00:00
|
|
|
self.key_reference = set()
|
2019-10-28 18:04:26 +00:00
|
|
|
if not n:
|
|
|
|
LRUCache._MAX_CAPACITY = sys.maxsize
|
|
|
|
elif n < 0:
|
2019-11-14 18:59:43 +00:00
|
|
|
raise ValueError("n should be an integer greater than 0.")
|
2019-10-28 18:04:26 +00:00
|
|
|
else:
|
|
|
|
LRUCache._MAX_CAPACITY = n
|
|
|
|
|
2021-11-16 14:01:17 +00:00
|
|
|
def refer(self, x: T) -> None:
|
2019-10-28 18:04:26 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
Looks for a page in the cache store and adds reference to the set.
|
|
|
|
Remove the least recently used key if the store is full.
|
|
|
|
Update store to reflect recent access.
|
2019-10-28 18:04:26 +00:00
|
|
|
"""
|
2021-11-16 14:01:17 +00:00
|
|
|
if x not in self.key_reference:
|
2019-10-28 18:04:26 +00:00
|
|
|
if len(self.dq_store) == LRUCache._MAX_CAPACITY:
|
|
|
|
last_element = self.dq_store.pop()
|
2021-11-16 14:01:17 +00:00
|
|
|
self.key_reference.remove(last_element)
|
2019-10-28 18:04:26 +00:00
|
|
|
else:
|
2021-11-16 14:01:17 +00:00
|
|
|
self.dq_store.remove(x)
|
2019-10-28 18:04:26 +00:00
|
|
|
|
|
|
|
self.dq_store.appendleft(x)
|
2021-11-16 14:01:17 +00:00
|
|
|
self.key_reference.add(x)
|
2019-10-28 18:04:26 +00:00
|
|
|
|
2021-11-16 14:01:17 +00:00
|
|
|
def display(self) -> None:
|
2019-10-28 18:04:26 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
Prints all the elements in the store.
|
2019-10-28 18:04:26 +00:00
|
|
|
"""
|
|
|
|
for k in self.dq_store:
|
|
|
|
print(k)
|
|
|
|
|
2021-11-16 14:01:17 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"LRUCache({self._MAX_CAPACITY}) => {list(self.dq_store)}"
|
|
|
|
|
2019-11-14 18:59:43 +00:00
|
|
|
|
2019-10-28 18:04:26 +00:00
|
|
|
if __name__ == "__main__":
|
2021-11-16 14:01:17 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|
|
|
|
|
|
|
|
lru_cache: LRUCache[str | int] = LRUCache(4)
|
|
|
|
lru_cache.refer("A")
|
2019-10-28 18:04:26 +00:00
|
|
|
lru_cache.refer(2)
|
|
|
|
lru_cache.refer(3)
|
2021-11-16 14:01:17 +00:00
|
|
|
lru_cache.refer("A")
|
2019-10-28 18:04:26 +00:00
|
|
|
lru_cache.refer(4)
|
|
|
|
lru_cache.refer(5)
|
|
|
|
lru_cache.display()
|
2021-11-16 14:01:17 +00:00
|
|
|
|
|
|
|
print(lru_cache)
|
|
|
|
assert str(lru_cache) == "LRUCache(4) => [5, 4, 'A', 3]"
|