-
Notifications
You must be signed in to change notification settings - Fork 12
/
eviction_index.go
62 lines (47 loc) · 1011 Bytes
/
eviction_index.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
package model
import (
iotago "github.com/iotaledger/iota.go/v4"
)
type EvictionIndex[K iotago.SlotIndex | iotago.EpochIndex] struct {
index *K
}
func NewEvictionIndex[K iotago.SlotIndex | iotago.EpochIndex]() *EvictionIndex[K] {
return &EvictionIndex[K]{}
}
func (e *EvictionIndex[K]) ShouldEvict(newIndex K) bool {
if e.index == nil {
return true
}
return newIndex > *e.index
}
func (e *EvictionIndex[K]) MarkEvicted(index K) (previous K, hadPrevious bool) {
var prev K
var hadPrev bool
if e.index == nil {
e.index = new(K)
hadPrev = false
} else {
prev = *e.index
hadPrev = true
}
*e.index = index
return prev, hadPrev
}
func (e *EvictionIndex[K]) Index() (current K, valid bool) {
if e.index == nil {
return 0, false
}
return *e.index, true
}
func (e *EvictionIndex[K]) NextIndex() K {
if e.index == nil {
return 0
}
return *e.index + 1
}
func (e *EvictionIndex[K]) IsEvicted(index K) bool {
if e.index == nil {
return false
}
return index <= *e.index
}