-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathlru_cache.py
More file actions
73 lines (57 loc) · 1.69 KB
/
lru_cache.py
File metadata and controls
73 lines (57 loc) · 1.69 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.next = None
self.previous = None
class LruCache:
def __init__(self, limit: int):
if limit <= 0:
raise ValueError("limit must be > 0")
self.head = None
self.tail = None
self.limit = limit
self.dict = {}
def set(self, key, value):
node = Node(key, value)
self.dict[key] = node
self.add_to_head(node)
if len(self.dict) > self.limit:
self.evict_tail()
def get(self, key):
node = self.dict.get(key)
if node is None:
return None
else:
self.move_to_head(node)
return node.value
def add_to_head(self, node):
node.previous = None
node.next = self.head
if self.head is None:
self.head = self.tail = node
else:
self.head.previous = node
self.head = node
def remove(self, node):
if node.previous is None:
self.head = node.next
else:
node.previous.next = node.next
if node.next is None:
self.tail = node.previous
else:
node.next.previous = node.previous
node.previous = None
node.next = None
def move_to_head(self, node):
if node is self.head:
return
self.remove(node)
self.add_to_head(node)
def evict_tail(self):
if self.tail is None:
return
old_tail = self.tail
self.remove(old_tail)
del self.dict[old_tail.key]