Created
July 9, 2020 05:37
-
-
Save kumarsuraj9450/f3ae763b55d3b64fc41f88ada35c55aa to your computer and use it in GitHub Desktop.
This file contains 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
from DoublyLinkedList import DoublyLinkedList,node_ll | |
class CustomDLL(DoublyLinkedList): | |
def add_node(self, key, value): | |
n = self.headNode | |
if n.value == None: | |
self.headNode.value = value | |
self.headNode.key = key | |
else: | |
n.prev = node_ll(key, value) | |
n.prev.next = n | |
self.headNode = n.prev | |
return self.headNode | |
def pop(self): | |
n = self.headNode | |
while n.next != None: n = n.next | |
# n.prev.value = n.value | |
n.prev.next = None | |
return n | |
class LRUChache: | |
def __init__(self, cache_size = 10 ): | |
self.__ll = CustomDLL() | |
self.__hash = {} | |
self.c_size = cache_size | |
def __add_opr(self, key, value): | |
if not self.__cache_info()[1]: self.pop() | |
node = self.__ll.add_node(key,value) | |
self.__hash[key] = node | |
def add_opr(self, key, value, replace = False): | |
if self.__access(key): | |
if not replace : self.__update_head(key) | |
else: | |
self.__popItem(key) | |
self.__add_opr(key,value) | |
return | |
self.__add_opr(key, value) | |
def get_tail(self): | |
return self.__ll.last() | |
def __popItem(self,key, remove = True): | |
evict = self.__hash.get(key) | |
evict.prev.next = evict.next | |
evict.next.prev = evict.prev | |
self.__hash.pop(key) | |
if remove: del(evict) | |
def __update_head(self, key): | |
item = self.__hash.get(key) | |
if item: | |
self.__popItem(key) | |
self.add_opr(item.key, item.value) | |
del(item) | |
def pop(self): | |
if self.__hash: | |
last = self.__ll.pop() | |
self.__hash.pop(last.key) | |
del(last) | |
def show(self): | |
self.__ll.show() | |
def access(self,key): | |
# self.__update_head(key) | |
return self.__access(key) | |
def __access(self, key): | |
return self.__hash.get(key) | |
def get_head(self): | |
return self.__ll.headNode | |
def values(self): | |
return self.__hash | |
def __cache_info(self): | |
s = len(self.__hash) | |
r = self.c_size - s | |
return [s, r] | |
def cache_info(self): | |
info = self.__cache_info() | |
return f'Used :- {info[0]}\nAvilable :- {info[1]}' | |
def __repr__(self): | |
return " ".join([x.__repr__() for x in self.values().values()]) | |
obj = LRUChache(cache_size = 10) | |
for i,s in enumerate('abcdefghijkl'): | |
obj.add_opr(i+1,s) | |
# print(obj.values()) | |
# print('-'*10) | |
# obj.pop() | |
# print(obj.values()) | |
# print('-'*10) | |
# obj.show() | |
# print('-'*10) | |
# print('tail ->', obj.get_tail()) | |
# print('head ->', obj.get_head()) | |
# print('-'*10) | |
# print(obj.cache_info()) | |
print(obj) | |
obj.add_opr(5,'ad', replace = True) | |
print(obj) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment