-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo.go
More file actions
88 lines (76 loc) · 1.37 KB
/
fifo.go
File metadata and controls
88 lines (76 loc) · 1.37 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"container/list"
"fmt"
)
// T is for key, value types
type T interface{}
// Map uses map to hold cache items
type Map map[T]T
// FIFO cache
type FIFO struct {
cache Map
q *list.List
size int
}
// New returns new FIFO cache of the given size
func NewFIFO(size int) *FIFO {
return &FIFO{
cache: make(Map, size),
q: list.New(),
size: size,
}
}
// Set sets the given key,value pair in the cache.
func (c *FIFO) Set(key, val T) {
// defer c.dump()
// if it already exists
if val, ok := c.cache[key]; ok {
c.cache[key] = val
return
}
// when cache is not full
if len(c.cache) < c.size {
c.cache[key] = val
c.q.PushBack(val)
return
}
e := c.q.Front()
delete(c.cache, e.Value)
c.q.Remove(e)
c.cache[key] = val
c.q.PushBack(val)
return
}
// Get gets a value from the cache
func (c *FIFO) Get(key T) (T, bool) {
// defer c.dump()
val, ok := c.cache[key]
return val, ok
}
func (c *FIFO) Avg() float64 {
var sum float64 = 0.0
e := c.q.Front()
for i := 0; i < c.size; i++ {
if e != nil {
sum += e.Value.(float64)
e = e.Next()
}
}
return sum / float64(c.size)
}
// dump dumps cache content for debugging
func (c *FIFO) dump() {
e := c.q.Front()
fmt.Printf("|")
for i := 0; i < c.size; i++ {
var val T
val = " "
if e != nil {
val = e.Value
e = e.Next()
}
fmt.Printf(" %v |", val)
}
fmt.Println()
}