-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRU.py
79 lines (74 loc) · 2.08 KB
/
LRU.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class CacheNode:
key = None
value = None
pre = None
next = None
def __init__(self,key,value):
self.key = key
self.value = value
class LRUCache:
mapping = {}
head = None
end = None
def __init__(self,capacity):
self.capacity = capacity
def set(self,key,value):
if key in self.mapping:
oldNode = self.mapping[key]
if value is None:
self.mapping.pop(key)
self.remove(oldNode)
#self.getallkeys()
#print('erase key', key)
else:
oldNode.value = value
self.remove(oldNode)
self.sethead(oldNode)
else:
node = CacheNode(key,value)
if len(self.mapping) >= self.capacity:
self.mapping.pop(self.end.key)
self.remove(self.end)
self.sethead(node)
else:
self.sethead(node)
self.mapping[key] = node
def sethead(self,node):
node.next = self.head
node.pre = None
if self.head:
self.head.pre = node
self.head = node
if not self.end:
self.end = self.head
def remove(self,node):
if node.pre:
node.pre.next = node.next
else:
self.head = node.next
if node.next:
node.next.pre = node.pre
else:
self.end = node.pre
def getallkeys(self):
tmp = None
if self.head:
tmp = self.head
pair = "<{0},{1}>"
res = ""
while tmp:
tmp_p = pair.format(tmp.key,tmp.value)
res += tmp_p
res += "->"
tmp = tmp.next
tmp_p = pair.format(None,None)
res += tmp_p
print('current cache is ',res)
def get(self,key):
if key in self.mapping:
node = self.mapping[key]
self.remove(node)
self.sethead(node)
return node.value
else:
return None