revert to origin

This commit is contained in:
Vu
2021-03-26 23:50:38 +07:00
parent c0531421c8
commit 9b92b8963b
45 changed files with 3384 additions and 3384 deletions

View File

@@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin) . Source and license info is on [GitHub](https://github.com/donnemartin/system-design-primer) ."
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/system-design-primer)."
]
},
{
@@ -52,67 +52,67 @@
],
"source": [
"%%writefile lru_cache.py\n",
"class Node(object) :\n",
"class Node(object):\n",
"\n",
" def __init__(self, results) :\n",
" def __init__(self, results):\n",
" self.results = results\n",
" self.prev = None\n",
" self.next = None\n",
"\n",
"\n",
"class LinkedList(object) :\n",
"class LinkedList(object):\n",
"\n",
" def __init__(self) :\n",
" def __init__(self):\n",
" self.head = None\n",
" self.tail = None\n",
"\n",
" def move_to_front(self, node) : # ...\n",
" def append_to_front(self, node) : # ...\n",
" def remove_from_tail(self) : # ...\n",
" def move_to_front(self, node): # ...\n",
" def append_to_front(self, node): # ...\n",
" def remove_from_tail(self): # ...\n",
"\n",
"\n",
"class Cache(object) :\n",
"class Cache(object):\n",
"\n",
" def __init__(self, MAX_SIZE) :\n",
" def __init__(self, MAX_SIZE):\n",
" self.MAX_SIZE = MAX_SIZE\n",
" self.size = 0\n",
" self.lookup = {} # key: query, value: node\n",
" self.linked_list = LinkedList() \n",
" self.linked_list = LinkedList()\n",
"\n",
" def get(self, query) \n",
" def get(self, query)\n",
" \"\"\"Get the stored query result from the cache.\n",
" \n",
" Accessing a node updates its position to the front of the LRU list.\n",
" \"\"\"\n",
" node = self.lookup.get(query) \n",
" node = self.lookup.get(query)\n",
" if node is None:\n",
" return None\n",
" self.linked_list.move_to_front(node) \n",
" self.linked_list.move_to_front(node)\n",
" return node.results\n",
"\n",
" def set(self, results, query) :\n",
" def set(self, results, query):\n",
" \"\"\"Set the result for the given query key in the cache.\n",
" \n",
" When updating an entry, updates its position to the front of the LRU list.\n",
" If the entry is new and the cache is at capacity, removes the oldest entry\n",
" before the new entry is added.\n",
" \"\"\"\n",
" node = self.lookup.get(query) \n",
" node = self.lookup.get(query)\n",
" if node is not None:\n",
" # Key exists in cache, update the value\n",
" node.results = results\n",
" self.linked_list.move_to_front(node) \n",
" self.linked_list.move_to_front(node)\n",
" else:\n",
" # Key does not exist in cache\n",
" if self.size == self.MAX_SIZE:\n",
" # Remove the oldest entry from the linked list and lookup\n",
" self.lookup.pop(self.linked_list.tail.query, None) \n",
" self.linked_list.remove_from_tail() \n",
" self.lookup.pop(self.linked_list.tail.query, None)\n",
" self.linked_list.remove_from_tail()\n",
" else:\n",
" self.size += 1\n",
" # Add the new key and value\n",
" new_node = Node(results) \n",
" self.linked_list.append_to_front(new_node) \n",
" new_node = Node(results)\n",
" self.linked_list.append_to_front(new_node)\n",
" self.lookup[query] = new_node"
]
}

View File

@@ -1,66 +1,66 @@
class Node(object) :
class Node(object):
def __init__(self, results) :
def __init__(self, results):
self.results = results
self.next = next
class LinkedList(object) :
class LinkedList(object):
def __init__(self) :
def __init__(self):
self.head = None
self.tail = None
def move_to_front(self, node) :
def move_to_front(self, node):
pass
def append_to_front(self, node) :
def append_to_front(self, node):
pass
def remove_from_tail(self) :
def remove_from_tail(self):
pass
class Cache(object) :
class Cache(object):
def __init__(self, MAX_SIZE) :
def __init__(self, MAX_SIZE):
self.MAX_SIZE = MAX_SIZE
self.size = 0
self.lookup = {} # key: query, value: node
self.linked_list = LinkedList()
self.linked_list = LinkedList()
def get(self, query) :
def get(self, query):
"""Get the stored query result from the cache.
Accessing a node updates its position to the front of the LRU list.
"""
node = self.lookup.get(query)
node = self.lookup.get(query)
if node is None:
return None
self.linked_list.move_to_front(node)
self.linked_list.move_to_front(node)
return node.results
def set(self, results, query) :
def set(self, results, query):
"""Set the result for the given query key in the cache.
When updating an entry, updates its position to the front of the LRU list.
If the entry is new and the cache is at capacity, removes the oldest entry
before the new entry is added.
"""
node = self.lookup.get(query)
node = self.lookup.get(query)
if node is not None:
# Key exists in cache, update the value
node.results = results
self.linked_list.move_to_front(node)
self.linked_list.move_to_front(node)
else:
# Key does not exist in cache
if self.size == self.MAX_SIZE:
# Remove the oldest entry from the linked list and lookup
self.lookup.pop(self.linked_list.tail.query, None)
self.linked_list.remove_from_tail()
self.lookup.pop(self.linked_list.tail.query, None)
self.linked_list.remove_from_tail()
else:
self.size += 1
# Add the new key and value
new_node = Node(results)
self.linked_list.append_to_front(new_node)
new_node = Node(results)
self.linked_list.append_to_front(new_node)
self.lookup[query] = new_node