-
Notifications
You must be signed in to change notification settings - Fork 0
/
question31.go
117 lines (97 loc) · 1.8 KB
/
question31.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package chapter05
type LRUCache[K comparable, V any] interface {
get(key K) V
put(key K, value V)
}
type entry[K comparable, V any] struct {
prev *entry[K, V]
next *entry[K, V]
key K
value V
}
type list[K comparable, V any] struct {
head *entry[K, V]
end *entry[K, V]
}
func (l *list[K, V]) delete(e *entry[K, V]) {
prev := e.prev
next := e.next
if prev == nil {
next.prev = nil
l.head = next
return
}
if next == nil {
prev.next = nil
l.end = prev
return
}
prev.next = next
next.prev = prev
}
func (l *list[K, V]) append(k K, v V) *entry[K, V] {
n := &entry[K, V]{key: k, value: v}
if l.head == nil {
l.head = n
l.end = n
return n
}
l.end.next = n
n.prev = l.end
l.end = n
return n
}
func (l *list[K, V]) renew(e *entry[K, V]) {
l.delete(e)
l.end.next = e
e.next = nil
e.prev = l.end
l.end = e
}
func (l *list[K, V]) expire() *entry[K, V] {
head := l.head
next := head.next
if next != nil {
l.head = next
next.prev = nil
} else {
l.head = nil
l.end = nil
}
return head
}
func newLRUCache[K comparable, V any](capacity int, defaults V) LRUCache[K, V] {
return &lruCache[K, V]{
capacity: capacity,
defaults: defaults,
nodes: map[K]*entry[K, V]{},
list: &list[K, V]{},
}
}
type lruCache[K comparable, V any] struct {
capacity int
defaults V
nodes map[K]*entry[K, V]
list *list[K, V]
}
func (l *lruCache[K, V]) get(k K) V {
if e, ok := l.nodes[k]; ok {
return e.value
} else {
return l.defaults
}
}
func (l *lruCache[K, V]) put(k K, v V) {
// Remove the oldest key if it exceeds the cache capacity.
if l.capacity <= len(l.nodes) {
e := l.list.expire()
delete(l.nodes, e.key)
}
// Insert or update the existing entry.
if e, ok := l.nodes[k]; ok {
e.value = v
} else {
e = l.list.append(k, v)
l.nodes[k] = e
}
}