-
-
Notifications
You must be signed in to change notification settings - Fork 27
London|25-SDC-November|Donara Blanc |Sprint 2| Lru-cache #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| class _Node: | ||
| def __init__(self, key, value): | ||
| self.key = key | ||
| self.value = value | ||
| self.next = None | ||
| self.previous = None | ||
|
|
||
|
|
||
| class DoublyLinkedList: | ||
| def __init__(self): | ||
| self.head = None | ||
| self.tail = None | ||
|
|
||
| def _add_to_head(self, node): | ||
| """Put node at the front (most recently used).""" | ||
| node.previous = None | ||
| node.next = self.head | ||
|
|
||
| if self.head is not None: | ||
| self.head.previous = node | ||
| self.head = node | ||
|
|
||
| if self.tail is None: | ||
| # First node in the list | ||
| self.tail = node | ||
|
|
||
| def _remove_node(self, node): | ||
| """Detach node from the linked list.""" | ||
| prev = node.previous | ||
| nxt = node.next | ||
|
|
||
| if prev is not None: | ||
| prev.next = nxt | ||
| else: | ||
| # node was head | ||
| self.head = nxt | ||
|
|
||
| if nxt is not None: | ||
| nxt.previous = prev | ||
| else: | ||
| # node was tail | ||
| self.tail = prev | ||
|
|
||
| node.next = None | ||
| node.previous = None | ||
|
|
||
| def _move_to_head(self, node): | ||
| """Mark node as most recently used.""" | ||
| if node is self.head: | ||
| return | ||
| self._remove_node(node) | ||
| self._add_to_head(node) | ||
|
|
||
| # ---- public API ---- | ||
| class LruCache: | ||
| def __init__(self, limit): | ||
| if limit <= 0: | ||
| raise ValueError("limit must be positive") | ||
| self.limit = limit | ||
| self.map = {} | ||
| self.list = DoublyLinkedList() | ||
|
|
||
| def get(self, key): | ||
| node = self.map.get(key) | ||
| if node is None: | ||
| return None | ||
|
|
||
| # mark as recently used | ||
| self._move_to_head(node) | ||
| return node.value | ||
|
|
||
| def set(self, key, value): | ||
| node = self.map.get(key) | ||
|
|
||
| if node is not None: | ||
| # update existing | ||
| node.value = value | ||
| self._move_to_head(node) | ||
| return | ||
|
|
||
| # new key | ||
| if len(self.map) >= self.limit: | ||
| # evict least recently used (tail) | ||
| lru = self.tail | ||
| if lru is not None: | ||
| self._remove_node(lru) | ||
| del self.map[lru.key] | ||
|
|
||
| new_node = _Node(key, value) | ||
| self._add_to_head(new_node) | ||
|
Comment on lines
+89
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would make the LinkedList easier to use if
Comment on lines
+69
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems some of the properties and methods accessed here belong to |
||
| self.map[key] = new_node | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's your reason to to make the methods "private" (by naming convention)?
This could be just a normal linked list.
You could even import the LinkedList you implemented in the other exercise and use it here, and represent the value of
dataas(key, value).