generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Open
donarbl
wants to merge
3
commits into
CodeYourFuture:main
Choose a base branch
from
donarbl:sprint-2-lru-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| class _Node: | ||
| def __init__(self, value): | ||
| self.value = value # stores as (key, value) tuple | ||
| self.next = None | ||
| self.previous = None | ||
|
|
||
|
|
||
| class DoublyLinkedList: | ||
| """responsible only for managing a doubly linked list. | ||
| stores values as (key, value) tuples but knows nothing about cachin | ||
| """ | ||
| def __init__(self): | ||
| self.head = None # most recently used end | ||
| self.tail = None # least recently used end | ||
|
|
||
| def add_to_head(self, value): | ||
| """sreates a new node with value, add to head, returns the node.""" | ||
| node = _Node(value) | ||
| node.next = self.head | ||
| node.previous = None | ||
| if self.head is not None: | ||
| self.head.previous = node | ||
| self.head = node | ||
| if self.tail is None: | ||
| self.tail = node | ||
| return node | ||
|
|
||
| def remove_node(self, node): | ||
| """detaches any node from the list.""" | ||
| prev = node.previous | ||
| nxt = node.next | ||
| if prev is not None: | ||
| prev.next = nxt | ||
| else: | ||
| self.head = nxt | ||
| if nxt is not None: | ||
| nxt.previous = prev | ||
| else: | ||
| self.tail = prev | ||
| node.next = None | ||
| node.previous = None | ||
|
|
||
| def remove_tail(self): | ||
| """removes and returns the tail node """ | ||
| lru = self.tail | ||
| if lru is not None: | ||
| self.remove_node(lru) | ||
| return lru | ||
|
|
||
|
|
||
| class LruCache: | ||
| """responsible only for cache logic (get, set, eviction). | ||
| gives all all ordering to DoublyLinkedList. | ||
| """ | ||
| 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 | ||
| # re-inserts at head to mark as most recently used | ||
| self.list.remove_node(node) | ||
| new_node = self.list.add_to_head(node.value) | ||
| self.map[key] = new_node # updates map to point to new node | ||
| return new_node.value[1] | ||
|
|
||
| def set(self, key, value): | ||
| node = self.map.get(key) | ||
| if node is not None: | ||
| # key exists then remove old, adds updated to head | ||
| self.list.remove_node(node) | ||
| new_node = self.list.add_to_head((key, value)) | ||
| self.map[key] = new_node # update map to new node | ||
| return | ||
| # new key gets rid of LRU | ||
| if len(self.map) >= self.limit: | ||
| lru = self.list.remove_tail() | ||
| if lru is not None: | ||
| del self.map[lru.value[0]] | ||
| new_node = self.list.add_to_head((key, value)) | ||
| self.map[key] = new_node | ||
| new_node = _Node(key, value) | ||
| self._add_to_head(new_node) | ||
| self.map[key] = new_node | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It would make the LinkedList easier to use if
add_to_head()is designed in such a way that the caller can push a new node to the front as: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.
I have separate the DoublyLinkedList and LruCache
They don't overlap. LruCache never touches node pointers directly,it always goes through self.list.