-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsieve.go
102 lines (91 loc) · 1.61 KB
/
sieve.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
package sieve
import "fmt"
type Node[T comparable] struct {
Value T
visited bool
prev, next *Node[T]
}
type Cache[T comparable] struct {
capacity int
cache map[T]*Node[T]
head, tail, hand *Node[T]
size int
}
func New[T comparable](capacity int) *Cache[T] {
return &Cache[T]{
capacity: capacity,
cache: make(map[T]*Node[T]),
}
}
func (c *Cache[T]) addToHead(node *Node[T]) {
node.next = c.head
node.prev = nil
if c.head != nil {
c.head.prev = node
}
c.head = node
if c.tail == nil {
c.tail = node
}
}
func (c *Cache[T]) removeNode(node *Node[T]) {
if node.prev != nil {
node.prev.next = node.next
} else {
c.head = node.next
}
if node.next != nil {
node.next.prev = node.prev
} else {
c.tail = node.prev
}
}
func (c *Cache[T]) evict() {
obj := c.tail
if c.hand != nil {
obj = c.hand
}
for obj != nil && obj.visited {
obj.visited = false
obj = c.tail
if obj.prev != nil {
obj = obj.prev
}
}
c.hand = nil
if obj.prev != nil {
c.hand = obj.prev
}
delete(c.cache, obj.Value)
c.removeNode(obj)
c.size--
}
func (c *Cache[T]) Access(x T) {
if _, ok := c.cache[x]; ok {
node := c.cache[x]
node.visited = true
} else {
if c.size == c.capacity {
c.evict()
}
newNode := &Node[T]{
Value: x,
}
c.addToHead(newNode)
c.cache[x] = newNode
c.size++
newNode.visited = false
}
}
func (self *Cache[T]) Show() {
current := self.head
for current != nil {
fmt.Printf("%v (Visited: %v)", current.Value, current.visited)
if current.next != nil {
fmt.Printf(" -> ")
} else {
fmt.Printf("\n")
}
current = current.next
}
}