- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCache.py
More file actions
Latest commit
55 lines (51 loc) · 1.33 KB
/
Copy pathCache.py
File metadata and controls
55 lines (51 loc) · 1.33 KB
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
classNode(object):
'''
implement LRU cache
using doubly linked list
null <- 1 <-> 2 <-> 3 <-> 4 ->null
'''
def__init__(self,pageNumbe,prev=None,next=None):
self.pageNumber=pageNumber
self.prev=prev
self.next=next
classCache(object):
'''
using double linked list to create cache
'''
def__init__(self,n):
self.limit=n
self.head=None
self.tail=None
defenqueue(self, pageNum):
newNode=Node(pageNum)
ifself.headisNone:
self.head=newNode
self.tail=newNode
else:
# push the node on the first node
newNode.next=self.head
self.head.prev=newNode
self.head=newNode
# pop the least recent used page
defLRU(self,pageNum):
# check whether the page is in the memory
p=Node(next=self.head)
# if it already in the memory, move to the end
# if not, move to the end of the queue directly
whilep.nextandp.next.pageNumber!=pageNum:
p=p.next
node=p.next
#remove the node
ifnode.prev=null:
self.head=node.next
else:
node.prev.next=node.next
ifnode.next=null:
self.tail=node.prev
else:
node.next.prev=node.prev
# put in the end of the queue
self.tail.next=node
node.prev=self.tail
self.tail=node
returnself.pop(0)