-
Notifications
You must be signed in to change notification settings - Fork 2
refactor(datastructures): lfu cache #187
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5181b3b
refactor(datastructures, lfu-cache): v2 of cache
BrianLusina 0271ed9
updating DIRECTORY.md
d2cc78c
Update datastructures/lfucache/README.md
BrianLusina dbf5fa6
Update datastructures/lfucache/lfu_cache.py
BrianLusina ea89805
Update datastructures/lfucache/lfu_cache_v2.py
BrianLusina 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
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
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 |
|---|---|---|
| @@ -1,127 +1,5 @@ | ||
| from collections import defaultdict | ||
| from typing import Any, Union, Dict | ||
| from datastructures.lfucache.lfu_cache_node import LfuCacheNode | ||
| from datastructures.lfucache.lfu_cache import LFUCache | ||
| from datastructures.lfucache.lfu_cache_v2 import LFUCacheV2 | ||
|
|
||
| from datastructures.linked_lists.doubly_linked_list import DoublyLinkedList | ||
| from datastructures.linked_lists.doubly_linked_list.node import DoubleNode | ||
|
|
||
|
|
||
| class LfuCacheNode(DoubleNode): | ||
| def __init__(self, data): | ||
| super().__init__(data) | ||
| self.frequency = 1 | ||
|
|
||
|
|
||
| class LFUCache: | ||
| def __init__(self, capacity: int): | ||
| """ | ||
| Initializes an instance of a LFUCache | ||
| @param capacity: Capacity of the cache | ||
| @type capacity int | ||
|
|
||
| 1. Dict named node self._lookup for retrieval of all nodes given a key. O(1) time to retrieve a node given a key | ||
| 2. Each frequency has a DoublyLinkedList stored in self._frequency where key is the frequency and value is an | ||
| object of DoublyLinkedList | ||
| 3. minimum frequency through all nodes, this can be maintained in O(1) time, taking advantage of the fact that | ||
| the frequency can only increment by 1. use the following 2 rules: | ||
| i. Whenever we see the size of the DoublyLinkedList of current min frequency is 0, increment min_frequency | ||
| by 1 | ||
| ii. Whenever we put in a new (key, value), the min frequency must be 1 (the new node) | ||
| """ | ||
| self.capacity = capacity | ||
| self._current_size = 0 | ||
| self._lookup = dict() | ||
| self._frequency: Dict[int, DoublyLinkedList] = defaultdict(DoublyLinkedList) | ||
| self._minimum_frequency = 0 | ||
|
|
||
| def __update(self, node: LfuCacheNode): | ||
| """ | ||
| Helper function used in 2 cases: | ||
| 1. When get(key) is called | ||
| 2. When put(key, value) is called and key exists | ||
|
|
||
| Common point of the 2 cases: | ||
| 1. no new node comes in | ||
| 2. node is visited one more time -> node.frequency changed -> thus the place of this node will change | ||
|
|
||
| Logic: | ||
| 1. Pop node from 'old' DoublyLinkedList with frequency | ||
| 2. Append node to 'new' DoublyLinkedList with frequency + 1 | ||
| 3. If 'old' DoublyLinkedList has size 0 & self.minimum_frequency is frequency, update self.minimum_frequency | ||
| to frequency + 1 | ||
|
|
||
| Complexity Analysis: | ||
| Time Complexity: O(1) time | ||
|
|
||
| @param node: Node to update in the Cache | ||
| @type node LfuCacheNode | ||
| """ | ||
| frequency = node.frequency | ||
|
|
||
| # pop the node from the 'old' DoublyLinkedList | ||
| self._frequency[frequency].delete_node(node) | ||
|
|
||
| if self._minimum_frequency == frequency and not self._frequency[frequency]: | ||
| self._minimum_frequency += 1 | ||
|
|
||
| node.frequency += 1 | ||
| frequency = node.frequency | ||
|
|
||
| # add to 'new' DoublyLinkedList with new frequency | ||
| self._frequency[frequency].prepend(node) | ||
|
|
||
| def get(self, key: int) -> Union[Any, None]: | ||
| """ | ||
| Gets an item from the Cache given the key | ||
| @param key: Key to use to fetch data from Cache | ||
| @return: Data mapped to the key | ||
| """ | ||
| if key not in self._lookup: | ||
| return None | ||
|
|
||
| node = self._lookup[key] | ||
| data = node.data | ||
| self.__update(node) | ||
| return data | ||
|
|
||
| def put(self, key: int, value: Any) -> None: | ||
| """ | ||
| If key is already present in the self._lookup, we perform same operations as get, except updating the node data | ||
| to new value | ||
|
|
||
| Otherwise, below operations are performed: | ||
| 1. If cache reaches capacity, pop least frequently used item. | ||
| 2 Facts: | ||
| a. we maintain self._minimum_frequency, minimum possible frequency in cache | ||
| b. All cache with the same frequency are stored as a DoublyLinkedList, with recently used order (Always | ||
| append to head). | ||
|
|
||
| Consequence is that the tail of the DoublyLinkedList with self._minimum_frequency is the least recently used | ||
| one, pop it. | ||
|
|
||
| 2. Add new node to self._lookup | ||
| 3. add new node to DoublyLinkedList with frequency of 1 | ||
| 4. reset minimum_frequency to 1 | ||
|
|
||
| @param key: Key to use for lookup | ||
| @param value: Value to store in the cache | ||
| @return: None | ||
| """ | ||
|
|
||
| if self.capacity == 0: | ||
| return None | ||
|
|
||
| if key in self._lookup: | ||
| node = self._lookup[key] | ||
| self.__update(node) | ||
| node.data = value | ||
| else: | ||
| if self._current_size == self.capacity: | ||
| node = self._frequency[self._minimum_frequency].pop() | ||
| self._lookup.pop(node.key) | ||
| self._current_size -= 1 | ||
|
|
||
| node = DoubleNode(data=value, key=key) | ||
| self._lookup[key] = node | ||
| self._frequency[1].append(node) | ||
| self._minimum_frequency = 1 | ||
| self._current_size += 1 | ||
| __all__ = ["LFUCache", "LFUCacheV2", "LfuCacheNode"] |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,123 @@ | ||
| from collections import defaultdict | ||
| from typing import Any, Union, Dict | ||
|
|
||
| from datastructures.linked_lists.doubly_linked_list import DoublyLinkedList | ||
| from datastructures.lfucache.lfu_cache_node import LfuCacheNode | ||
|
|
||
|
|
||
| class LFUCache: | ||
| def __init__(self, capacity: int): | ||
| """ | ||
| Initializes an instance of a LFUCache | ||
| @param capacity: Capacity of the cache | ||
| @type capacity int | ||
|
|
||
| 1. Dict named node self._lookup for retrieval of all nodes given a key. O(1) time to retrieve a node given a key | ||
| 2. Each frequency has a DoublyLinkedList stored in self._frequency where key is the frequency and value is an | ||
| object of DoublyLinkedList | ||
| 3. minimum frequency through all nodes, this can be maintained in O(1) time, taking advantage of the fact that | ||
| the frequency can only increment by 1. use the following 2 rules: | ||
| i. Whenever we see the size of the DoublyLinkedList of current min frequency is 0, increment min_frequency | ||
| by 1 | ||
| ii. Whenever we put in a new (key, value), the min frequency must be 1 (the new node) | ||
| """ | ||
| self.capacity = capacity | ||
| self._current_size = 0 | ||
| self._lookup = dict() | ||
| self._frequency: Dict[int, DoublyLinkedList] = defaultdict(DoublyLinkedList) | ||
| self._minimum_frequency = 0 | ||
|
|
||
| def __update(self, node: LfuCacheNode): | ||
| """ | ||
| Helper function used in 2 cases: | ||
| 1. When get(key) is called | ||
| 2. When put(key, value) is called and key exists | ||
|
|
||
| Common point of the 2 cases: | ||
| 1. no new node comes in | ||
| 2. node is visited one more time -> node.frequency changed -> thus the place of this node will change | ||
|
|
||
| Logic: | ||
| 1. Pop node from 'old' DoublyLinkedList with frequency | ||
| 2. Append node to 'new' DoublyLinkedList with frequency + 1 | ||
| 3. If 'old' DoublyLinkedList has size 0 & self.minimum_frequency is frequency, update self.minimum_frequency | ||
| to frequency + 1 | ||
|
|
||
| Complexity Analysis: | ||
| Time Complexity: O(1) time | ||
|
|
||
| @param node: Node to update in the Cache | ||
| @type node LfuCacheNode | ||
| """ | ||
| frequency = node.frequency | ||
|
|
||
| # pop the node from the 'old' DoublyLinkedList | ||
| self._frequency[frequency].delete_node(node) | ||
|
|
||
| if self._minimum_frequency == frequency and not self._frequency[frequency]: | ||
| self._minimum_frequency += 1 | ||
|
|
||
| node.frequency += 1 | ||
| frequency = node.frequency | ||
|
|
||
| # add to 'new' DoublyLinkedList with new frequency | ||
| self._frequency[frequency].prepend(node) | ||
|
|
||
| def get(self, key: int) -> Union[Any, None]: | ||
| """ | ||
| Gets an item from the Cache given the key | ||
| @param key: Key to use to fetch data from Cache | ||
| @return: Data mapped to the key | ||
| """ | ||
| if key not in self._lookup: | ||
| return None | ||
|
|
||
| node = self._lookup[key] | ||
| data = node.data | ||
| self.__update(node) | ||
| return data | ||
|
|
||
| def put(self, key: int, value: Any) -> None: | ||
| """ | ||
| If key is already present in the self._lookup, we perform same operations as get, except updating the node data | ||
| to new value | ||
|
|
||
| Otherwise, below operations are performed: | ||
| 1. If cache reaches capacity, pop least frequently used item. | ||
| 2 Facts: | ||
| a. we maintain self._minimum_frequency, minimum possible frequency in cache | ||
| b. All cache with the same frequency are stored as a DoublyLinkedList, with recently used order (Always | ||
| append to head). | ||
|
|
||
| Consequence is that the tail of the DoublyLinkedList with self._minimum_frequency is the least recently used | ||
| one, pop it. | ||
|
|
||
| 2. Add new node to self._lookup | ||
| 3. add new node to DoublyLinkedList with frequency of 1 | ||
| 4. reset minimum_frequency to 1 | ||
|
|
||
| @param key: Key to use for lookup | ||
| @param value: Value to store in the cache | ||
| @return: None | ||
| """ | ||
|
|
||
| if self.capacity == 0: | ||
| return None | ||
|
|
||
| if key in self._lookup: | ||
| node = self._lookup[key] | ||
| self.__update(node) | ||
| node.data = value | ||
| return None | ||
| else: | ||
| if self._current_size == self.capacity: | ||
| node = self._frequency[self._minimum_frequency].pop() | ||
| self._lookup.pop(node.key) | ||
| self._current_size -= 1 | ||
|
|
||
| node = LfuCacheNode(data=value, key=key) | ||
| self._lookup[key] = node | ||
| self._frequency[1].prepend(node) | ||
| self._minimum_frequency = 1 | ||
| self._current_size += 1 | ||
| return None | ||
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,13 @@ | ||
| from datastructures.linked_lists.doubly_linked_list.node import DoubleNode | ||
|
|
||
|
|
||
| class LfuCacheNode(DoubleNode): | ||
| def __init__(self, data, key): | ||
| super().__init__(data, key=key) | ||
| self.frequency = 1 | ||
|
|
||
|
|
||
| class LfuCacheNodeV2(DoubleNode): | ||
| def __init__(self, data, key): | ||
| super().__init__(data, key=key) | ||
| self.frequency = 0 |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.