-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (93 loc) · 1.76 KB
/
main.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
package main
import (
"errors"
)
var ErrNotFound = errors.New("not found")
type Key comparable
type Value any
type LRUCache[K Key, T Value] struct {
length int
capacity int
// list
head *Node[K, T]
tail *Node[K, T]
// map
cache map[K]*Node[K, T]
}
type Node[K Key, T Value] struct {
key K
value T
prev *Node[K, T]
next *Node[K, T]
}
func Constructor[K Key, T Value](capacity int) LRUCache[K, T] {
head := &Node[K, T]{}
tail := &Node[K, T]{}
head.next = tail
tail.prev = head
return LRUCache[K, T]{
cache: make(map[K]*Node[K, T], capacity),
length: 0,
capacity: capacity,
head: head,
tail: tail,
}
}
func (l *LRUCache[K, T]) moveToHead(node *Node[K, T]) {
l.remove(node)
l.addToFront(node)
}
func (l *LRUCache[K, T]) remove(node *Node[K, T]) {
// remove node
prev := node.prev
next := node.next
prev.next = next
next.prev = prev
node.next = nil
node.prev = nil
}
func (l *LRUCache[K, T]) addToFront(node *Node[K, T]) {
next := l.head.next
l.head.next = node
next.prev = node
node.next = next
node.prev = l.head
}
func (l *LRUCache[K, T]) removeLast() *Node[K, T] {
x := l.tail.prev
l.remove(x)
return x
}
func (l *LRUCache[K, T]) Get(key K) (T, error) {
node, found := l.cache[key]
if !found {
var zeroValue T
return zeroValue, ErrNotFound
}
l.moveToHead(node)
return node.value, nil
}
func (l *LRUCache[K, T]) Put(key K, value T) {
node, found := l.cache[key]
if found {
// update
node.value = value
l.moveToHead(node)
return
}
// not found
// add to map & add to DLL
n := &Node[K, T]{
value: value,
key: key,
}
l.cache[key] = n
l.addToFront(n)
// evict DLL tail & remove from map
if l.length < l.capacity {
l.length++
} else {
last := l.removeLast()
delete(l.cache, last.key)
}
}