Skip to content

Commit d49174c

Browse files
authored
Update lru_cache.py
refactored by following the recommendation to separate DoublyLinkedList from LruCache
1 parent ab69cdb commit d49174c

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

Sprint-2/implement_lru_cache/lru_cache.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,10 @@ def __init__(self, key, value):
66
self.previous = None
77

88

9-
class LruCache:
10-
def __init__(self, limit):
11-
if limit <= 0:
12-
raise ValueError("limit must be positive")
13-
14-
self.limit = limit
15-
self.map = {} # key -> _Node
16-
self.head = None # most recently used
17-
self.tail = None # least recently used
18-
19-
# ---- internal helpers for the linked list ----
9+
class DoublyLinkedList:
10+
def __init__(self):
11+
self.head = None
12+
self.tail = None
2013

2114
def _add_to_head(self, node):
2215
"""Put node at the front (most recently used)."""
@@ -59,7 +52,14 @@ def _move_to_head(self, node):
5952
self._add_to_head(node)
6053

6154
# ---- public API ----
62-
55+
class LruCache:
56+
def __init__(self, limit):
57+
if limit <= 0:
58+
raise ValueError("limit must be positive")
59+
self.limit = limit
60+
self.map = {}
61+
self.list = DoublyLinkedList()
62+
6363
def get(self, key):
6464
node = self.map.get(key)
6565
if node is None:

0 commit comments

Comments
 (0)