-
Notifications
You must be signed in to change notification settings - Fork 2
/
146_LRU_cache.go
186 lines (143 loc) · 3.74 KB
/
146_LRU_cache.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package linked_list
import "fmt"
type Cache struct {
key int
value int
previous *Cache
next *Cache
}
type LRUCache struct {
datastore map[int]int
capacity int
currently_holding int
history *Cache
}
func NewLRU(capacity int) LRUCache {
ds := make(map[int]int)
return LRUCache{
datastore: ds,
capacity: capacity,
currently_holding: 0,
history: nil,
}
}
func (this *LRUCache) Get(key int) int {
if val, ok := this.datastore[key]; ok {
// this.SwapLastHistory()
return val
}
c := this.history.next
c = c.next
this.history = c
// this.
return -1
}
func (this *LRUCache) SwapLastHistory() {
// 1 -> 2 -> 3
nextKey, nextVal := this.history.next.key, this.history.next.value
this.history.next.key = this.history.key
this.history.next.value = this.history.value
this.history.key = nextKey
this.history.value = nextVal
}
// working
func (this *LRUCache) EvictLastHistory() {
// 1-> 2-> 3->4
c := this.history
// x -> 2 -> 3 -> 4
c = c.next // one step foreward
c.previous = nil
this.history = c
}
func (this *LRUCache) UpdateLastHistory(key, value int) {
this.history.key = key
this.history.value = value
}
func (this *LRUCache) UpdateCacheWhere(key, value int) {
cache := this.history
for cache != nil { // traverse the whole history to find the value
if cache.key == key {
// found the cache
// swap it with head
// swapping won't work: [we are swaping the head by swaping the keys and values]
// cache.key = this.history.key
// cache.value = this.history.value
// take the previous key
// take the next key
prev := cache.previous
next := cache.next
prev.next = next
next.previous = prev
break
}
cache = cache.next
}
// finally add a new head head
c := Cache{
key: key,
value: value,
previous: nil,
next: this.history,
}
this.history = &c
}
func (this *LRUCache) Put(key int, value int) {
// firstly, check if it exists already
// if it does, we need to update the key and value
if _, ok := this.datastore[key]; ok {
// update of key-value is being done at the very end
// need to update the history nodes
// secondly, we need to take the node, bring it all way front,
// sew the remaining parts
// 1 -> 2 -> 3 -> 4
// 3 -> 1 -> 2 -> 4
this.UpdateCacheWhere(key, value)
} else {
// check if current holding + 1 > capacity
// if yes,
// 1. delete the last node, // we dont need to delete the node
// 2. create a new node
// 3. set this new node as head
// 4. attach the rest of history
// 5. this.datastore[key] = value (maybe defer this?)
// 6. NOPE, because we are deleting 1 node, so current_holding++ and -- [this.currently_holding++]
if this.currently_holding+1 > this.capacity {
// this.UpdateLastHistory(key, value)
fmt.Printf("%d should be deleted from map\n", this.history.key)
delete(this.datastore, this.history.key)
this.EvictLastHistory()
this.addToCache(key, value, this.history)
// delete from map
} else {
// if no, 2- > 3 -> 4 -> 5 -> 6
this.addToCache(key, value, this.history)
this.currently_holding++
}
}
this.datastore[key] = value
}
func (this *LRUCache) addToCache(key, value int, next *Cache) {
// 1 -> 2 -> 3
h := this.history
for h != nil {
h = h.next
}
fmt.Println("h", this.history)
}
func (this *LRUCache) PrintLRU() {
fmt.Println("printing lru history")
node := this.history
for node != nil {
fmt.Println(node.key, node.value)
node = node.next
}
}
func (this *LRUCache) PrintDatastore() {
fmt.Println("printing datastore")
for k, v := range this.datastore {
fmt.Printf("%d = %d\n", k, v)
}
}
func (this *LRUCache) PrintLURKey() {
fmt.Println("lru key", this.history.key)
}