-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru.go
201 lines (182 loc) · 3.04 KB
/
lru.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"fmt"
"strconv"
)
type Node struct {
Prev *Node
Next *Node
Data *string
}
type List struct {
N int
First *Node
Last *Node
}
func NewList() *List {
list := new(List)
return list
}
type LRUData struct {
Node *Node
Value *string
}
type LRU struct {
N int
Link *List
Hash map[string]LRUData
}
func (l *List) Push(d *string) *Node {
node := new(Node)
node.Data = d
if l.Last == nil {
l.First = node
l.Last = node
} else {
l.Last.Next = node
node.Prev = l.Last
l.Last = node
}
l.N++
return node
}
func (l *List) Unshift(d *string) *Node {
node := new(Node)
node.Data = d
if l.First == nil {
l.First = node
l.Last = node
l.N++
} else {
l.First.Prev = node
node.Next = l.First
l.First = node
}
return node
}
func (l *List) Remove(node *Node) *string {
if node == nil {
return nil
}
if node == l.First && node == l.Last {
l.First = nil
l.Last = nil
} else if node == l.First {
l.First = node.Next
l.First.Prev = nil
} else if node == l.Last {
l.Last = node.Prev
l.Last.Next = nil
} else {
after := node.Next
before := node.Prev
after.Prev = before
before.Next = after
}
l.N--
return node.Data
}
func (l *List) Shift() *string {
if l.First == nil {
return nil
}
data := l.First.Data
l.Remove(l.First)
return data
}
func (l *List) Pop() *string {
if l.Last == nil {
return nil
}
data := l.Last.Data
l.Remove(l.Last)
return data
}
func (l *List) Dump() {
if l.First == nil {
fmt.Println("<empty>")
}
p := l.First
i := 1
for p != nil {
fmt.Printf("%02d:%#v[%v]\n", i, p, *p.Data)
p = p.Next
i++
}
}
func NewLRU(n int) *LRU {
lru := new(LRU)
lru.N = n
lru.Link = NewList()
lru.Hash = make(map[string]LRUData)
return lru
}
func (lru *LRU) Delete(k string) {
data, ok := lru.Hash[k]
if ok {
lru.Link.Remove(data.Node)
delete(lru.Hash, *data.Value)
}
}
func (lru *LRU) Add(k, v string) {
if lru.Link.N == lru.N {
id := lru.Link.Shift()
fmt.Printf("removing %s from linked list (limit reached)\n", *id)
delete(lru.Hash, *id)
}
lru.Delete(k)
node := lru.Link.Push(&k)
lru.Hash[k] = LRUData{Node: node, Value: &v}
}
func (lru *LRU) Dump() {
fmt.Printf("LRU(List):%d\n", lru.Link.N)
lru.Link.Dump()
fmt.Println("LRU(Map):")
if len(lru.Hash) > 0 {
for k, v := range lru.Hash {
fmt.Printf("%#v: %#v\n", k, *v.Value)
}
} else {
fmt.Println("<empty>")
}
}
func testList() {
var m *Node
d := NewList()
max := 10
for i := 1; i <= max; i++ {
c := strconv.Itoa(i)
n := d.Push(&c)
if i == max/2 {
m = n
}
}
d.Dump()
fmt.Printf("Pop: %#v\n", *d.Pop())
d.Dump()
fmt.Printf("Shift: %#v\n", *d.Shift())
d.Dump()
fmt.Printf("Remove: %#v\n", *d.Remove(m))
d.Dump()
}
func testLRU() {
max := 5
lru := NewLRU(max)
for i := 1; i <= max; i++ {
c := strconv.Itoa(i)
lru.Add(c, "Data"+c)
}
lru.Dump()
fmt.Println("adding 6 to LRU")
lru.Add("6", "Data6")
lru.Dump()
fmt.Println("adding 2 to LRU")
lru.Add("2", "Data2")
lru.Dump()
fmt.Println("adding 7 to LRU")
lru.Add("7", "Data7")
lru.Dump()
}
func main() {
testLRU()
}