Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions Sprint-2/implement_lru_cache/lru_cache.py
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):
Copy link

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 data as (key, value).

"""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
Copy link

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:

    # Just need to add the data; does not need to concern node creation logic
    new_node = self.list.add_to_head((key, value)); # Add key+value as a tuple.

Comment on lines +69 to +90
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems some of the properties and methods accessed here belong to LinkedList (not to LruCache).

self.map[key] = new_node