Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
other solutions
  • Loading branch information
amoito committed Mar 25, 2020
commit 20e428ae2371510a338862de789458fe3f1e6288
141 changes: 117 additions & 24 deletions lru_cache/lru_cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
sys.path.append('./doubly_linked_list')
# import sys
# sys.path.append('./doubly_linked_list')
from doubly_linked_list import DoublyLinkedList
from doubly_linked_list import ListNode
# from doubly_linked_list import ListNode

class LRUCache:
"""
Expand All @@ -11,11 +11,25 @@ class LRUCache:
order, as well as a storage dict that provides fast access
to every node stored in the cache.
"""
# def __init__(self, limit=10):
# self.hash_map = {}
# self.orderList = DoublyLinkedList()
# self.limit = limit
# self.size = 0

# # 2nd version
# def __init__(self, limit=10):
# self.size = 0
# self.storage = {}
# self.dll = DoublyLinkedList()
# self.limit = limit

# instructors
def __init__(self, limit=10):
self.hash_map = {}
self.orderList = DoublyLinkedList()
self.limit = limit
self.size = 0
self.storage = {}
self.order = DoublyLinkedList()

"""
Retrieves the value associated with the given key. Also
Expand All @@ -24,14 +38,48 @@ def __init__(self, limit=10):
Returns the value associated with the key or None if the
key-value pair doesn't exist in the cache.
"""
# def get(self, key):
# if key not in self.hash_map.keys():
# return None
# else:
# self.orderList.move_to_front(self.hash_map[key][1])
# self.hash_map[key][1] = self.orderList.head
# return self.hash_map[key][0]

# 2nd version
# def get(self, key):
# if not key in self.storage:
# return None
# else:
# node = self.storage[key]
# self.dll.move_to_end(node)
# return node.value[1]

# instructor
def get(self, key):
if key not in self.hash_map.keys():
return None
# if key is in storage
if key in self.storage:
# move it to the end
node = self.storage[key]
self.order.move_to_end(node)
# return the value
return node.value[1]
# if not
else:
self.orderList.move_to_front(self.hash_map[key][1])
self.hash_map[key][1] = self.orderList.head
return self.hash_map[key][0]

# return none
return None



# test
# def get(self, key):
# if key in self.storage:
# node = self.storage[key]
# self.order.move_to_end(node)
# return node.value[1]
# else:
# return None

"""
Adds the given key-value pair to the cache. The newly-
added pair should be considered the most-recently used
Expand All @@ -42,18 +90,63 @@ def get(self, key):
want to overwrite the old value associated with the key with
the newly-specified value.
"""
# def set(self, key, value):
# if key in self.hash_map.keys():
# self.orderList.move_to_front(self.hash_map[key][1])

# else:
# if self.size == self.limit:
# del self.hash_map[self.orderList.tail.value]
# self.orderList.remove_from_tail()
# else:
# self.size += 1
# self.orderList.add_to_head(key)
# self.hash_map[key] = [None, None]
# self.hash_map[key][0] = value
# self.hash_map[key][1] = self.orderList.head

# 2nd version
# def set(self, key, value):

# if key in self.storage:
# node = self.storage[key]
# node.value = (key,value)
# self.dll.move_to_end(node)
# return
# if self.size == self.limit:
# self.storage.pop(self.dll.head.value[0])
# self.dll.remove_from_head()
# self.size -=1
# self.dll.add_to_tail((key,value))
# self.storage[key] = self.dll.tail
# self.size += 1

# Instructors
def set(self, key, value):
if key in self.hash_map.keys():
self.orderList.move_to_front(self.hash_map[key][1])
# check and see if the key is in the dict
if key in self.storage:
# if it is
node = self.storage[key]
# overwrite the value
node.value = (key, value)
# move it to the end
self.order.move_to_end(node)
# do nothing exit the function
return

# check and see if the cache is full
if self.size == self.limit:
# remove oldest entry from the dictionary
# del self.storage[self.order.head.value[0]]
del self.storage[self.order.head.value[0]]
# and LinkedList
self.order.remove_from_head()
# reduce the size
self.size -= 1

else:
if self.size == self.limit:
del self.hash_map[self.orderList.tail.value]
self.orderList.remove_from_tail()
else:
self.size += 1
self.orderList.add_to_head(key)
self.hash_map[key] = [None, None]
self.hash_map[key][0] = value
self.hash_map[key][1] = self.orderList.head

# add to the linked list(key and the value)
self.order.add_to_tail((key, value))
# add the key and the value to the dictionary
self.storage[key] = self.order.tail
# increment size
self.size += 1