-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRUcache.py
56 lines (49 loc) · 1.86 KB
/
LRUcache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from doubly_linked_list import Node
class LRUcache(object):
def __init__(self,size=None):
self.cache_list = Node()
self.cache_table = {}
self.size = size
def put(self,value):
if value in self.cache_table:
new_node = self.cache_table[value]
if new_node == self.cache_list.tail:
return
elif new_node == self.cache_list.head:
self.cache_list.delete_with_data(new_node)
self.cache_list.insert_at_end(new_node)
else:
self.cache_list.tail.prev.next = new_node
new_node.prev.next = self.cache_list.tail #1
new_node.next.prev = self.cache_list.tail #2
self.cache_list.tail.next = new_node.next #3
self.cache_list.tail.prev.next = new_node #4
# import pdb; pdb.set_trace()
new_node.prev, self.cache_list.tail.prev = self.cache_list.tail.prev, new_node.prev # 5 and 6
new_node.next = None # 7
self.cache_list.tail = new_node #8
else:
new_node = Node(value,None,None)
if self.cache_list.length < self.size:
self.cache_list.insert_at_end(value)
self.cache_table[value] = self.cache_list.tail
else:
del self.cache_table[self.cache_list.head.data]
self.cache_list.delete_from_beginning()
self.cache_list.insert_at_end(value)
self.cache_table[value] = self.cache_list.tail
def get(self,value):
if value in self.cache_table:
node = self.cache_table[value]
print node.data
else:
return -1
x = LRUcache(size=4)
x.put(1)
x.put(2)
x.put(3)
x.put(4)
x.put(2)
x.get(2)
import pdb; pdb.set_trace()
print True